Update app.py
Browse files
app.py
CHANGED
@@ -9,40 +9,38 @@ 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
|
13 |
-
def
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
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 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
translation = translator(transcription)[0]['translation_text']
|
40 |
|
41 |
-
return
|
42 |
except Exception as e:
|
43 |
return f"B艂膮d: {e}", ""
|
44 |
|
45 |
-
# Interfejs Gradio
|
46 |
iface = gr.Interface(
|
47 |
fn=transcribe_and_translate,
|
48 |
inputs=gr.File(label="Prze艣lij plik audio lub wideo (MOV, MP4, WAV, MP3)"),
|
@@ -51,7 +49,7 @@ iface = gr.Interface(
|
|
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.
|
55 |
)
|
56 |
|
57 |
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 podzia艂u pliku audio na segmenty 30-sekundowe
|
13 |
+
def split_audio(audio_path, segment_length=30):
|
14 |
+
audio = AudioSegment.from_file(audio_path)
|
15 |
+
segments = []
|
16 |
+
for i in range(0, len(audio), segment_length * 1000): # segment_length w milisekundach
|
17 |
+
segment = audio[i:i + segment_length * 1000]
|
18 |
+
temp_file = f"segment_{i // 1000}.wav"
|
19 |
+
segment.export(temp_file, format="wav")
|
20 |
+
segments.append(temp_file)
|
21 |
+
return segments
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# Funkcja transkrypcji i t艂umaczenia
|
24 |
def transcribe_and_translate(audio):
|
25 |
try:
|
26 |
+
# Podziel plik na 30-sekundowe segmenty
|
27 |
+
segments = split_audio(audio.name)
|
28 |
+
full_transcription = ""
|
29 |
+
|
30 |
+
# Przetwarzanie ka偶dego segmentu
|
31 |
+
for segment in segments:
|
32 |
+
result = transcriber(segment)
|
33 |
+
full_transcription += result['text'] + " "
|
34 |
+
os.remove(segment) # Usu艅 segment po przetworzeniu
|
35 |
|
36 |
+
# T艂umaczenie na angielski
|
37 |
+
translation = translator(full_transcription)[0]['translation_text']
|
|
|
38 |
|
39 |
+
return full_transcription.strip(), translation.strip()
|
40 |
except Exception as e:
|
41 |
return f"B艂膮d: {e}", ""
|
42 |
|
43 |
+
# Interfejs Gradio
|
44 |
iface = gr.Interface(
|
45 |
fn=transcribe_and_translate,
|
46 |
inputs=gr.File(label="Prze艣lij plik audio lub wideo (MOV, MP4, WAV, MP3)"),
|
|
|
49 |
gr.Textbox(label="T艂umaczenie na angielski")
|
50 |
],
|
51 |
title="Whisper Small - Transkrypcja i T艂umaczenie",
|
52 |
+
description="Aplikacja obs艂uguj膮ca d艂ugie pliki MOV, MP4 i audio. Pliki s膮 dzielone na segmenty 30-sekundowe."
|
53 |
)
|
54 |
|
55 |
iface.launch()
|