Update app.py
Browse files
app.py
CHANGED
@@ -9,56 +9,49 @@ transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-sma
|
|
9 |
# Za艂aduj model do t艂umaczenia na angielski
|
10 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-pl-en")
|
11 |
|
12 |
-
# Funkcja konwersji
|
13 |
def convert_audio(audio):
|
14 |
try:
|
15 |
-
# Sprawd藕 format pliku na podstawie rozszerzenia
|
16 |
extension = os.path.splitext(audio)[1].lower()
|
17 |
-
|
18 |
-
# Wczytaj plik w zale偶no艣ci od formatu
|
19 |
if extension in [".mov", ".mp4", ".m4a"]:
|
20 |
sound = AudioSegment.from_file(audio, format="mov" if extension == ".mov" else "mp4")
|
21 |
else:
|
22 |
sound = AudioSegment.from_file(audio)
|
23 |
-
|
24 |
-
# Konwersja do
|
25 |
sound = sound.set_frame_rate(16000).set_channels(1)
|
26 |
temp_file = "converted.wav"
|
27 |
sound.export(temp_file, format="wav")
|
28 |
return temp_file
|
29 |
except Exception as e:
|
30 |
-
print(f"B艂膮d konwersji
|
31 |
return None
|
32 |
|
33 |
# Funkcja transkrypcji i t艂umaczenia
|
34 |
def transcribe_and_translate(audio):
|
35 |
try:
|
36 |
-
|
37 |
-
converted_audio = convert_audio(audio)
|
38 |
if not converted_audio:
|
39 |
-
return "Nie uda艂o si臋 przetworzy膰 pliku
|
40 |
-
|
41 |
-
# Transkrypcja pliku audio
|
42 |
result = transcriber(converted_audio)
|
43 |
transcription = result['text']
|
44 |
-
|
45 |
-
# T艂umaczenie na angielski
|
46 |
translation = translator(transcription)[0]['translation_text']
|
47 |
|
48 |
return transcription, translation
|
49 |
except Exception as e:
|
50 |
return f"B艂膮d: {e}", ""
|
51 |
|
52 |
-
# Interfejs Gradio
|
53 |
iface = gr.Interface(
|
54 |
fn=transcribe_and_translate,
|
55 |
-
inputs=gr.
|
56 |
outputs=[
|
57 |
-
gr.Textbox(label="Transkrypcja tekstowa"),
|
58 |
-
gr.Textbox(label="T艂umaczenie na angielski")
|
59 |
],
|
60 |
title="Whisper Small - Transkrypcja i T艂umaczenie",
|
61 |
-
description="Aplikacja obs艂uguj膮ca pliki MOV, MP4 i
|
62 |
)
|
63 |
|
64 |
iface.launch()
|
|
|
9 |
# Za艂aduj model do t艂umaczenia na angielski
|
10 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-pl-en")
|
11 |
|
12 |
+
# Funkcja konwersji plik贸w MOV, MP4 i innych format贸w do audio 16 kHz
|
13 |
def convert_audio(audio):
|
14 |
try:
|
|
|
15 |
extension = os.path.splitext(audio)[1].lower()
|
|
|
|
|
16 |
if extension in [".mov", ".mp4", ".m4a"]:
|
17 |
sound = AudioSegment.from_file(audio, format="mov" if extension == ".mov" else "mp4")
|
18 |
else:
|
19 |
sound = AudioSegment.from_file(audio)
|
20 |
+
|
21 |
+
# Konwersja do WAV 16 kHz mono
|
22 |
sound = sound.set_frame_rate(16000).set_channels(1)
|
23 |
temp_file = "converted.wav"
|
24 |
sound.export(temp_file, format="wav")
|
25 |
return temp_file
|
26 |
except Exception as e:
|
27 |
+
print(f"B艂膮d konwersji: {e}")
|
28 |
return None
|
29 |
|
30 |
# Funkcja transkrypcji i t艂umaczenia
|
31 |
def transcribe_and_translate(audio):
|
32 |
try:
|
33 |
+
converted_audio = convert_audio(audio.name) # 艢cie偶ka do pliku
|
|
|
34 |
if not converted_audio:
|
35 |
+
return "Nie uda艂o si臋 przetworzy膰 pliku.", ""
|
36 |
+
|
|
|
37 |
result = transcriber(converted_audio)
|
38 |
transcription = result['text']
|
|
|
|
|
39 |
translation = translator(transcription)[0]['translation_text']
|
40 |
|
41 |
return transcription, translation
|
42 |
except Exception as e:
|
43 |
return f"B艂膮d: {e}", ""
|
44 |
|
45 |
+
# Interfejs Gradio z gr.File do przesy艂ania plik贸w
|
46 |
iface = gr.Interface(
|
47 |
fn=transcribe_and_translate,
|
48 |
+
inputs=gr.File(label="Prze艣lij plik audio lub wideo (MOV, MP4, WAV, MP3)"),
|
49 |
outputs=[
|
50 |
+
gr.Textbox(label="Transkrypcja tekstowa"),
|
51 |
+
gr.Textbox(label="T艂umaczenie na angielski")
|
52 |
],
|
53 |
title="Whisper Small - Transkrypcja i T艂umaczenie",
|
54 |
+
description="Aplikacja obs艂uguj膮ca pliki MOV, MP4 i audio. Konwertuje plik na WAV i przetwarza tre艣膰."
|
55 |
)
|
56 |
|
57 |
iface.launch()
|