tomekstor9 commited on
Commit
b26b92f
verified
1 Parent(s): 5e09f88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -30
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
  from transformers import pipeline
3
  from pydub import AudioSegment
4
  import os
5
- import language_tool_python
6
 
7
  # Za艂aduj mniejszy model Whisper do transkrypcji
8
  transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-small")
@@ -10,9 +9,6 @@ transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-sma
10
  # Za艂aduj model do t艂umaczenia na angielski
11
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-pl-en")
12
 
13
- # Konfiguracja LanguageTool (u偶ywaj膮c zewn臋trznego serwera)
14
- tool = language_tool_python.LanguageToolPublicAPI('pl')
15
-
16
  # Funkcja zmniejszenia jako艣ci audio i konwersji do WAV
17
  def reduce_audio_quality(input_path):
18
  try:
@@ -35,36 +31,28 @@ def split_audio_to_segments(input_path, segment_length=30):
35
  segments.append(segment_path)
36
  return segments
37
 
38
- # Funkcja poprawy tekstu za pomoc膮 LanguageTool
39
- def correct_text_with_languagetool(text):
40
- matches = tool.check(text)
41
- corrected_text = language_tool_python.utils.correct(text, matches)
42
- return corrected_text
43
-
44
- # Funkcja transkrypcji pliku audio z poprawkami
45
- def transcribe_audio(file):
46
  try:
47
  reduced_audio = reduce_audio_quality(file.name)
48
  if not reduced_audio:
49
- return "Nie uda艂o si臋 zmniejszy膰 rozmiaru pliku."
50
-
 
51
  segments = split_audio_to_segments(reduced_audio, segment_length=30)
52
  full_transcription = ""
53
-
54
  for segment in segments:
55
  result = transcriber(segment)
56
  full_transcription += result['text'] + " "
57
  os.remove(segment)
 
58
 
59
  os.remove(reduced_audio)
60
-
61
- # Poprawienie tekstu za pomoc膮 LanguageTool
62
- corrected_transcription = correct_text_with_languagetool(full_transcription.strip())
63
- return corrected_transcription
64
  except Exception as e:
65
- return f"B艂膮d: {e}"
66
 
67
- # Funkcja t艂umaczenia tekstu
68
  def translate_text(text):
69
  try:
70
  translation = translator(text)[0]['translation_text']
@@ -75,27 +63,24 @@ def translate_text(text):
75
  # Interfejs Gradio
76
  with gr.Blocks() as app:
77
  gr.Markdown("## Whisper Small - Transkrypcja i T艂umaczenie")
78
- gr.Markdown(
79
- "Aplikacja wykonuje transkrypcj臋 plik贸w audio/wideo za pomoc膮 Whisper Small i automatycznie poprawia b艂臋dy literowe. "
80
- "U偶ytkownik mo偶e poprawi膰 wygenerowany tekst przed jego t艂umaczeniem na angielski."
81
- )
82
-
83
  with gr.Row():
84
  file_input = gr.File(label="Prze艣lij plik audio lub wideo (MOV, MP4, WAV, MP3)")
85
  transcribe_button = gr.Button("Wykonaj transkrypcj臋")
86
-
87
  transcription_output = gr.Textbox(label="Transkrypcja tekstowa (edytowalna)", lines=10)
88
  translate_button = gr.Button("Przet艂umacz na angielski")
89
  translation_output = gr.Textbox(label="T艂umaczenie na angielski", lines=10)
90
 
91
- # Logika transkrypcji
92
  transcribe_button.click(
93
- transcribe_audio,
94
  inputs=file_input,
95
  outputs=transcription_output
96
  )
97
 
98
- # Logika t艂umaczenia
99
  translate_button.click(
100
  translate_text,
101
  inputs=transcription_output,
 
2
  from transformers import pipeline
3
  from pydub import AudioSegment
4
  import os
 
5
 
6
  # Za艂aduj mniejszy model Whisper do transkrypcji
7
  transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-small")
 
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:
 
31
  segments.append(segment_path)
32
  return segments
33
 
34
+ # Funkcja przetwarzania pliku z u偶yciem streaming
35
+ def transcribe_audio_stream(file):
 
 
 
 
 
 
36
  try:
37
  reduced_audio = reduce_audio_quality(file.name)
38
  if not reduced_audio:
39
+ yield "Nie uda艂o si臋 zmniejszy膰 rozmiaru pliku."
40
+ return
41
+
42
  segments = split_audio_to_segments(reduced_audio, segment_length=30)
43
  full_transcription = ""
44
+
45
  for segment in segments:
46
  result = transcriber(segment)
47
  full_transcription += result['text'] + " "
48
  os.remove(segment)
49
+ yield full_transcription.strip() # Stream cz臋艣ciowej transkrypcji
50
 
51
  os.remove(reduced_audio)
 
 
 
 
52
  except Exception as e:
53
+ yield f"B艂膮d: {e}"
54
 
55
+ # Funkcja t艂umaczenia poprawionego tekstu
56
  def translate_text(text):
57
  try:
58
  translation = translator(text)[0]['translation_text']
 
63
  # Interfejs Gradio
64
  with gr.Blocks() as app:
65
  gr.Markdown("## Whisper Small - Transkrypcja i T艂umaczenie")
66
+ gr.Markdown("Prze艣lij plik audio/wideo, wygeneruj transkrypcj臋, popraw j膮 r臋cznie i przet艂umacz na angielski.")
67
+
 
 
 
68
  with gr.Row():
69
  file_input = gr.File(label="Prze艣lij plik audio lub wideo (MOV, MP4, WAV, MP3)")
70
  transcribe_button = gr.Button("Wykonaj transkrypcj臋")
71
+
72
  transcription_output = gr.Textbox(label="Transkrypcja tekstowa (edytowalna)", lines=10)
73
  translate_button = gr.Button("Przet艂umacz na angielski")
74
  translation_output = gr.Textbox(label="T艂umaczenie na angielski", lines=10)
75
 
76
+ # Streaming transkrypcji
77
  transcribe_button.click(
78
+ transcribe_audio_stream,
79
  inputs=file_input,
80
  outputs=transcription_output
81
  )
82
 
83
+ # T艂umaczenie tekstu po poprawkach
84
  translate_button.click(
85
  translate_text,
86
  inputs=transcription_output,