tomekstor9 commited on
Commit
933bac2
·
verified ·
1 Parent(s): aa713df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -22
app.py CHANGED
@@ -9,34 +9,42 @@ 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 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
 
@@ -49,7 +57,7 @@ iface = gr.Interface(
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 dzielone na segmenty 30-sekundowe."
53
  )
54
 
55
  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 pliku do mniejszego audio WAV (16 kHz, mono)
13
+ def preprocess_and_reduce_audio(input_path):
14
+ try:
15
+ # Wczytaj plik za pomocą Pydub
16
+ audio = AudioSegment.from_file(input_path)
17
+
18
+ # Zmniejsz jakość dźwięku: 16 kHz, mono, mniejsza głośność (opcjonalnie)
19
+ audio = audio.set_frame_rate(16000).set_channels(1).set_sample_width(2)
20
+
21
+ # Zapisz zmniejszoną ścieżkę audio
22
+ output_path = "reduced_audio.wav"
23
+ audio.export(output_path, format="wav", bitrate="64k") # Zmniejszona jakość
24
+ return output_path
25
+ except Exception as e:
26
+ print(f"Błąd konwersji audio: {e}")
27
+ return None
28
 
29
  # Funkcja transkrypcji i tłumaczenia
30
+ def transcribe_and_translate(file):
31
  try:
32
+ # Konwersja pliku wejściowego na zoptymalizowaną ścieżkę audio
33
+ reduced_audio = preprocess_and_reduce_audio(file.name)
34
+ if not reduced_audio:
35
+ return "Nie udało się przetworzyć pliku.", ""
36
 
37
+ # Transkrypcja audio
38
+ result = transcriber(reduced_audio)
39
+ transcription = result['text']
 
 
40
 
41
  # Tłumaczenie na angielski
42
+ translation = translator(transcription)[0]['translation_text']
43
+
44
+ # Usuń tymczasowy plik po zakończeniu
45
+ os.remove(reduced_audio)
46
 
47
+ return transcription, translation
48
  except Exception as e:
49
  return f"Błąd: {e}", ""
50
 
 
57
  gr.Textbox(label="Tłumaczenie na angielski")
58
  ],
59
  title="Whisper Small - Transkrypcja i Tłumaczenie",
60
+ description="Aplikacja konwertuje pliki MOV/MP4 do mniejszej ścieżki audio i wykonuje transkrypcję oraz tłumaczenie."
61
  )
62
 
63
  iface.launch()