tomekstor9 commited on
Commit
647ae53
verified
1 Parent(s): 69610ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -33
app.py CHANGED
@@ -5,7 +5,7 @@ import os
5
  import re
6
 
7
  # Za艂aduj mniejszy model Whisper do transkrypcji
8
- transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
9
 
10
  # Za艂aduj model do t艂umaczenia na angielski
11
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-pl-en")
@@ -42,49 +42,64 @@ def clean_text(text):
42
  text = text.strip() # Usu艅 bia艂e znaki na pocz膮tku i ko艅cu
43
  return text
44
 
45
- # Funkcja przetwarzania pliku z u偶yciem Gradio Streaming
46
- def transcribe_and_translate_stream(file):
47
  try:
48
- # Zmniejszenie jako艣ci audio
49
  reduced_audio = reduce_audio_quality(file.name)
50
  if not reduced_audio:
51
- yield "Nie uda艂o si臋 zmniejszy膰 rozmiaru pliku.", ""
52
- return
53
 
54
- # Podziel plik na segmenty 30-sekundowe
55
  segments = split_audio_to_segments(reduced_audio, segment_length=30)
56
  full_transcription = ""
57
 
58
- # Przetwarzanie ka偶dego segmentu
59
  for segment in segments:
60
- result = transcriber(segment, return_timestamps=True)
61
  full_transcription += result['text'] + " "
62
  os.remove(segment) # Usu艅 segment po przetworzeniu
63
-
64
- # Wy艣wietl cz臋艣ciowy wynik na bie偶膮co
65
- cleaned_transcription = clean_text(full_transcription.strip())
66
- yield cleaned_transcription, ""
67
 
68
- os.remove(reduced_audio) # Usu艅 zmniejszony plik
69
-
70
- # Przet艂umacz ca艂膮 oczyszczon膮 transkrypcj臋
71
- cleaned_transcription = clean_text(full_transcription.strip())
72
- translation = translator(cleaned_transcription)[0]['translation_text']
73
-
74
- yield cleaned_transcription, translation.strip()
75
  except Exception as e:
76
- yield f"B艂膮d: {e}", ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
- # Interfejs Gradio ze streamingiem
79
- iface = gr.Interface(
80
- fn=transcribe_and_translate_stream,
81
- inputs=gr.File(label="Prze艣lij plik audio lub wideo (MOV, MP4, WAV, MP3)"),
82
- outputs=[
83
- gr.Textbox(label="Transkrypcja tekstowa"),
84
- gr.Textbox(label="T艂umaczenie na angielski")
85
- ],
86
- title="Whisper Tiny - Transkrypcja i T艂umaczenie",
87
- description="Aplikacja konwertuje pliki MOV/MP4 do mniejszej jako艣ci audio, dzieli je na segmenty 60-sekundowe i wykonuje transkrypcj臋 oraz t艂umaczenie na angielski."
88
- )
89
 
90
- iface.launch()
 
5
  import re
6
 
7
  # Za艂aduj mniejszy model Whisper do transkrypcji
8
+ transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-small")
9
 
10
  # Za艂aduj model do t艂umaczenia na angielski
11
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-pl-en")
 
42
  text = text.strip() # Usu艅 bia艂e znaki na pocz膮tku i ko艅cu
43
  return text
44
 
45
+ # Funkcja transkrypcji pliku audio
46
+ def transcribe_audio(file):
47
  try:
 
48
  reduced_audio = reduce_audio_quality(file.name)
49
  if not reduced_audio:
50
+ return "Nie uda艂o si臋 zmniejszy膰 rozmiaru pliku."
 
51
 
 
52
  segments = split_audio_to_segments(reduced_audio, segment_length=30)
53
  full_transcription = ""
54
 
 
55
  for segment in segments:
56
+ result = transcriber(segment)
57
  full_transcription += result['text'] + " "
58
  os.remove(segment) # Usu艅 segment po przetworzeniu
 
 
 
 
59
 
60
+ os.remove(reduced_audio)
61
+ return clean_text(full_transcription.strip())
 
 
 
 
 
62
  except Exception as e:
63
+ return f"B艂膮d: {e}"
64
+
65
+ # Funkcja t艂umaczenia tekstu
66
+ def translate_text(text):
67
+ try:
68
+ translation = translator(text)[0]['translation_text']
69
+ return translation.strip()
70
+ except Exception as e:
71
+ return f"B艂膮d podczas t艂umaczenia: {e}"
72
+
73
+ # Interfejs Gradio
74
+ with gr.Blocks() as app:
75
+ gr.Markdown("## Whisper Small - Transkrypcja i T艂umaczenie")
76
+ gr.Markdown(
77
+ "Aplikacja wykonuje transkrypcj臋 plik贸w audio/wideo za pomoc膮 Whisper Small. "
78
+ "U偶ytkownik mo偶e poprawi膰 wygenerowany tekst przed jego przet艂umaczeniem na j臋zyk angielski."
79
+ )
80
+
81
+ # Przesy艂anie pliku i transkrypcja
82
+ with gr.Row():
83
+ file_input = gr.File(label="Prze艣lij plik audio lub wideo (MOV, MP4, WAV, MP3)")
84
+ transcribe_button = gr.Button("Wykonaj transkrypcj臋")
85
+
86
+ # Pole do edycji transkrypcji i przycisk t艂umaczenia
87
+ transcription_output = gr.Textbox(label="Transkrypcja tekstowa (edytowalna)", lines=10)
88
+ translate_button = gr.Button("Przet艂umacz na angielski")
89
+ translation_output = gr.Textbox(label="T艂umaczenie na angielski", lines=10)
90
+
91
+ # Logika transkrypcji
92
+ transcribe_button.click(
93
+ transcribe_audio,
94
+ inputs=file_input,
95
+ outputs=transcription_output
96
+ )
97
 
98
+ # Logika t艂umaczenia
99
+ translate_button.click(
100
+ translate_text,
101
+ inputs=transcription_output,
102
+ outputs=translation_output
103
+ )
 
 
 
 
 
104
 
105
+ app.launch()