yvankob commited on
Commit
33dbb9e
·
1 Parent(s): adc457e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +154 -22
app.py CHANGED
@@ -8,6 +8,8 @@ import tempfile
8
  from transformers.pipelines.audio_utils import ffmpeg_read
9
  from gradio.components import Audio, Dropdown, Radio, Textbox
10
  import os
 
 
11
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
12
 
13
 
@@ -17,7 +19,6 @@ YT_LENGTH_LIMIT_S = 3600 # Limite de 1 heure pour les vidéos YouTube
17
 
18
  # Charger les codes de langue
19
  from flores200_codes import flores_codes
20
- lang_codes = list(flores_codes.keys())
21
 
22
  # Fonction pour déterminer le device
23
  def set_device():
@@ -46,54 +47,185 @@ def load_models():
46
 
47
  load_models()
48
 
 
 
 
49
  # Fonction pour la transcription
50
  def transcribe_audio(audio_file):
51
- model_size = "large-v2"
52
- model = WhisperModel(model_size)
53
  # model = WhisperModel(model_size, device=device, compute_type="int8")
 
54
  segments, _ = model.transcribe(audio_file, beam_size=1)
55
  transcriptions = [("[%.2fs -> %.2fs]" % (seg.start, seg.end), seg.text) for seg in segments]
56
  return transcriptions
57
 
 
58
  # Fonction pour la traduction
59
  def traduction(text, source_lang, target_lang):
 
 
 
 
 
 
 
 
60
  model_name = "nllb-distilled-600M"
61
  model = model_dict[model_name + "_model"]
62
  tokenizer = model_dict[model_name + "_tokenizer"]
63
  translator = pipeline("translation", model=model, tokenizer=tokenizer)
64
- return translator(text, src_lang=flores_codes[source_lang], tgt_lang=flores_codes[target_lang])[0]["translation_text"]
 
 
65
 
66
  # Fonction principale
67
- def full_transcription_and_translation(audio_file, source_lang, target_lang):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  transcriptions = transcribe_audio(audio_file)
69
  translations = [(timestamp, traduction(text, source_lang, target_lang)) for timestamp, text in transcriptions]
 
 
 
 
 
70
  return transcriptions, translations
71
 
72
  # Téléchargement audio YouTube
73
- def download_yt_audio(yt_url, filename):
74
- with youtube_dl.YoutubeDL({'format': 'bestaudio'}) as ydl:
75
- ydl.download([yt_url])
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  # Interface Gradio
78
  def gradio_interface(audio_file, source_lang, target_lang):
 
 
79
  transcriptions, translations = full_transcription_and_translation(audio_file, source_lang, target_lang)
80
  transcribed_text = '\n'.join([f"{timestamp}: {text}" for timestamp, text in transcriptions])
81
  translated_text = '\n'.join([f"{timestamp}: {text}" for timestamp, text in translations])
82
  return transcribed_text, translated_text
83
 
84
 
85
- iface = gr.Interface(
86
- fn=gradio_interface,
87
- inputs=[
88
- gr.Audio(type="filepath"),
89
- gr.Dropdown(lang_codes, value='French', label='Source Language'),
90
- gr.Dropdown(lang_codes, value='English', label='Target Language'),
91
- ],
92
- outputs=[
93
- gr.Textbox(label="Transcribed Text"),
94
- gr.Textbox(label="Translated Text")
95
- ]
96
- )
97
-
98
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
 
8
  from transformers.pipelines.audio_utils import ffmpeg_read
9
  from gradio.components import Audio, Dropdown, Radio, Textbox
10
  import os
11
+ import numpy as np
12
+ import soundfile as sf
13
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
14
 
15
 
 
19
 
20
  # Charger les codes de langue
21
  from flores200_codes import flores_codes
 
22
 
23
  # Fonction pour déterminer le device
24
  def set_device():
 
47
 
48
  load_models()
49
 
50
+ model_size = "large-v2"
51
+ model = WhisperModel(model_size)
52
+
53
  # Fonction pour la transcription
54
  def transcribe_audio(audio_file):
55
+ # model_size = "large-v2"
56
+ # model = WhisperModel(model_size)
57
  # model = WhisperModel(model_size, device=device, compute_type="int8")
58
+ global model
59
  segments, _ = model.transcribe(audio_file, beam_size=1)
60
  transcriptions = [("[%.2fs -> %.2fs]" % (seg.start, seg.end), seg.text) for seg in segments]
61
  return transcriptions
62
 
63
+ # Fonction pour la traduction
64
  # Fonction pour la traduction
65
  def traduction(text, source_lang, target_lang):
66
+ # Vérifier si les codes de langue sont dans flores_codes
67
+ if source_lang not in flores_codes or target_lang not in flores_codes:
68
+ print(f"Code de langue non trouvé : {source_lang} ou {target_lang}")
69
+ return ""
70
+
71
+ src_code = flores_codes[source_lang]
72
+ tgt_code = flores_codes[target_lang]
73
+
74
  model_name = "nllb-distilled-600M"
75
  model = model_dict[model_name + "_model"]
76
  tokenizer = model_dict[model_name + "_tokenizer"]
77
  translator = pipeline("translation", model=model, tokenizer=tokenizer)
78
+
79
+ return translator(text, src_lang=src_code, tgt_lang=tgt_code)[0]["translation_text"]
80
+
81
 
82
  # Fonction principale
83
+ def full_transcription_and_translation(audio_input, source_lang, target_lang):
84
+ # Si audio_input est une URL
85
+ if isinstance(audio_input, str) and audio_input.startswith("http"):
86
+ audio_file = download_yt_audio(audio_input)
87
+ # Si audio_input est un dictionnaire contenant des données audio
88
+ elif isinstance(audio_input, dict) and "array" in audio_input and "sampling_rate" in audio_input:
89
+ audio_array = audio_input["array"]
90
+ sampling_rate = audio_input["sampling_rate"]
91
+ # Écrivez le tableau NumPy dans un fichier temporaire WAV
92
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as f:
93
+ sf.write(f, audio_array, sampling_rate)
94
+ audio_file = f.name
95
+ else:
96
+ # Supposons que c'est un chemin de fichier
97
+ audio_file = audio_input
98
+
99
  transcriptions = transcribe_audio(audio_file)
100
  translations = [(timestamp, traduction(text, source_lang, target_lang)) for timestamp, text in transcriptions]
101
+
102
+ # Supprimez le fichier temporaire s'il a été créé
103
+ if isinstance(audio_input, dict):
104
+ os.remove(audio_file)
105
+
106
  return transcriptions, translations
107
 
108
  # Téléchargement audio YouTube
109
+ """def download_yt_audio(yt_url):
110
+ with tempfile.NamedTemporaryFile(suffix='.mp3') as f:
111
+ ydl_opts = {
112
+ 'format': 'bestaudio/best',
113
+ 'outtmpl': f.name,
114
+ 'postprocessors': [{
115
+ 'key': 'FFmpegExtractAudio',
116
+ 'preferredcodec': 'mp3',
117
+ 'preferredquality': '192',
118
+ }],
119
+ }
120
+ with youtube_dl.YoutubeDL(ydl_opts) as ydl:
121
+ ydl.download([yt_url])
122
+ return f.name"""
123
+
124
+ lang_codes = list(flores_codes.keys())
125
 
126
  # Interface Gradio
127
  def gradio_interface(audio_file, source_lang, target_lang):
128
+ if audio_file.startswith("http"):
129
+ audio_file = download_yt_audio(audio_file)
130
  transcriptions, translations = full_transcription_and_translation(audio_file, source_lang, target_lang)
131
  transcribed_text = '\n'.join([f"{timestamp}: {text}" for timestamp, text in transcriptions])
132
  translated_text = '\n'.join([f"{timestamp}: {text}" for timestamp, text in translations])
133
  return transcribed_text, translated_text
134
 
135
 
136
+ def _return_yt_html_embed(yt_url):
137
+ video_id = yt_url.split("?v=")[-1]
138
+ HTML_str = (
139
+ f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
140
+ " </center>"
141
+ )
142
+ return HTML_str
143
+
144
+ def download_yt_audio(yt_url, filename):
145
+ info_loader = youtube_dl.YoutubeDL()
146
+
147
+ try:
148
+ info = info_loader.extract_info(yt_url, download=False)
149
+ except youtube_dl.utils.DownloadError as err:
150
+ raise gr.Error(str(err))
151
+
152
+ file_length = info["duration_string"]
153
+ file_h_m_s = file_length.split(":")
154
+ file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
155
+
156
+ if len(file_h_m_s) == 1:
157
+ file_h_m_s.insert(0, 0)
158
+ if len(file_h_m_s) == 2:
159
+ file_h_m_s.insert(0, 0)
160
+ file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
161
+
162
+ if file_length_s > YT_LENGTH_LIMIT_S:
163
+ yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
164
+ file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
165
+ raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
166
+
167
+ ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
168
+
169
+ with youtube_dl.YoutubeDL(ydl_opts) as ydl:
170
+ try:
171
+ ydl.download([yt_url])
172
+ except youtube_dl.utils.ExtractorError as err:
173
+ raise gr.Error(str(err))
174
+
175
+
176
+ def yt_transcribe(yt_url, task, max_filesize=75.0):
177
+ html_embed_str = _return_yt_html_embed(yt_url)
178
+ global model # Assurez-vous que model est accessibl
179
+
180
+ with tempfile.TemporaryDirectory() as tmpdirname:
181
+ filepath = os.path.join(tmpdirname, "video.mp4")
182
+ download_yt_audio(yt_url, filepath)
183
+ with open(filepath, "rb") as f:
184
+ inputs = f.read()
185
+
186
+ inputs = ffmpeg_read(inputs, model.feature_extractor.sampling_rate)
187
+ inputs = {"array": inputs, "sampling_rate": model.feature_extractor.sampling_rate}
188
+
189
+ transcriptions, translations = full_transcription_and_translation(inputs, source_lang, target_lang)
190
+ transcribed_text = '\n'.join([f"{timestamp}: {text}" for timestamp, text in transcriptions])
191
+ translated_text = '\n'.join([f"{timestamp}: {text}" for timestamp, text in translations])
192
+ return html_embed_str, transcribed_text, translated_text
193
+
194
+
195
+ # Interfaces
196
+ demo = gr.Blocks()
197
+
198
+ with demo:
199
+ with gr.Tab("Microphone"):
200
+ gr.Interface(
201
+ fn=gradio_interface,
202
+ inputs=[
203
+ gr.Audio(sources=["microphone"], type="filepath"),
204
+ gr.Dropdown(lang_codes, value='French', label='Source Language'),
205
+ gr.Dropdown(lang_codes, value='English', label='Target Language')],
206
+ outputs=[gr.Textbox(label="Transcribed Text"), gr.Textbox(label="Translated Text")]
207
+ )
208
+
209
+ with gr.Tab("Audio file"):
210
+ gr.Interface(
211
+ fn=gradio_interface,
212
+ inputs=[
213
+ gr.Audio(type="filepath", label="Audio file"),
214
+ gr.Dropdown(lang_codes, value='French', label='Source Language'),
215
+ gr.Dropdown(lang_codes, value='English', label='Target Language')],
216
+ outputs=[gr.Textbox(label="Transcribed Text"), gr.Textbox(label="Translated Text")]
217
+ )
218
+
219
+ with gr.Tab("YouTube"):
220
+ gr.Interface(
221
+ fn=yt_transcribe,
222
+ inputs=[
223
+ gr.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
224
+ gr.Dropdown(lang_codes, value='French', label='Source Language'),
225
+ gr.Dropdown(lang_codes, value='English', label='Target Language')
226
+ ],
227
+ outputs=["html", gr.Textbox(label="Transcribed Text"), gr.Textbox(label="Translated Text")]
228
+ )
229
+
230
+ demo.launch()
231