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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -24
app.py CHANGED
@@ -9,42 +9,53 @@ 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 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,7 +68,7 @@ iface = gr.Interface(
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()
 
9
  # Załaduj model do tłumaczenia na angielski
10
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-pl-en")
11
 
12
+ # Funkcja zmniejszenia jakości audio i konwersji do WAV
13
+ def reduce_audio_quality(input_path):
14
  try:
 
15
  audio = AudioSegment.from_file(input_path)
16
 
17
+ # Redukcja jakości: 16 kHz, mono, 64 kbps
18
+ reduced_audio = audio.set_frame_rate(16000).set_channels(1).set_sample_width(2)
19
+ reduced_path = "reduced_audio.wav"
20
+ reduced_audio.export(reduced_path, format="wav", bitrate="64k")
21
+ return reduced_path
 
 
22
  except Exception as e:
23
+ print(f"Błąd podczas zmniejszania jakości pliku: {e}")
24
  return None
25
 
26
+ # Funkcja podziału audio na segmenty 30-sekundowe
27
+ def split_audio_to_segments(input_path, segment_length=30):
28
+ audio = AudioSegment.from_file(input_path)
29
+ segments = []
30
+ for i in range(0, len(audio), segment_length * 1000): # segment_length w milisekundach
31
+ segment = audio[i:i + segment_length * 1000]
32
+ segment_path = f"segment_{i // 1000}.wav"
33
+ segment.export(segment_path, format="wav")
34
+ segments.append(segment_path)
35
+ return segments
36
+
37
+ # Funkcja przetwarzania pliku: zmniejszenie rozmiaru, dzielenie, transkrypcja, tłumaczenie
38
  def transcribe_and_translate(file):
39
  try:
40
+ # Zmniejszenie jakości audio
41
+ reduced_audio = reduce_audio_quality(file.name)
42
  if not reduced_audio:
43
+ return "Nie udało się zmniejszyć rozmiaru pliku.", ""
44
 
45
+ # Podziel plik na 30-sekundowe segmenty
46
+ segments = split_audio_to_segments(reduced_audio)
47
+ full_transcription = ""
48
 
49
+ # Przetwarzanie każdego segmentu
50
+ for segment in segments:
51
+ result = transcriber(segment)
52
+ full_transcription += result['text'] + " "
53
+ os.remove(segment) # Usuń segment po przetworzeniu
54
 
55
+ os.remove(reduced_audio) # Usuń zmniejszony plik
56
+ translation = translator(full_transcription)[0]['translation_text']
57
 
58
+ return full_transcription.strip(), translation.strip()
59
  except Exception as e:
60
  return f"Błąd: {e}", ""
61
 
 
68
  gr.Textbox(label="Tłumaczenie na angielski")
69
  ],
70
  title="Whisper Small - Transkrypcja i Tłumaczenie",
71
+ description="Aplikacja konwertuje pliki MOV/MP4 do mniejszej jakości audio, dzieli je na segmenty 30-sekundowe i wykonuje transkrypcję oraz tłumaczenie."
72
  )
73
 
74
  iface.launch()