tomekstor9 commited on
Commit
aa713df
verified
1 Parent(s): 2e3e70e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -26
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 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)"),
@@ -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. Konwertuje plik na WAV i przetwarza tre艣膰."
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()