Spaces:
Runtime error
Runtime error
Switch to alternative google translate library
Browse files- app.py +14 -11
- requirements.txt +1 -3
app.py
CHANGED
@@ -5,11 +5,13 @@ from pytube import YouTube
|
|
5 |
from datasets import Dataset, Audio
|
6 |
from moviepy.editor import AudioFileClip
|
7 |
|
8 |
-
import googletrans
|
9 |
-
from googletrans import Translator
|
|
|
10 |
|
11 |
pipe = pipeline(model="Neprox/model")
|
12 |
-
translator =
|
|
|
13 |
|
14 |
# Get languages available for translation
|
15 |
languages = []
|
@@ -75,16 +77,17 @@ def divide_into_30s_segments(audio_fpath, seconds_max):
|
|
75 |
|
76 |
return segment_paths, segment_start_times
|
77 |
|
78 |
-
def get_translation(text,
|
79 |
"""
|
80 |
Translates the given Swedish text to the language specified.
|
81 |
"""
|
82 |
-
lang_code =
|
83 |
-
|
84 |
-
|
|
|
85 |
|
86 |
|
87 |
-
def translate(audio, url, seconds_max,
|
88 |
"""
|
89 |
Translates a YouTube video if a url is specified and returns the transcription.
|
90 |
If not url is specified, it translates the audio file as passed by Gradio.
|
@@ -103,8 +106,8 @@ def translate(audio, url, seconds_max, dest_language):
|
|
103 |
for i, (seconds, output) in enumerate(zip(segment_start_times, pred)):
|
104 |
text += f"[Segment {i+1}/{n_segments}, start time {get_timestamp(seconds)}]\n"
|
105 |
text += f"{output['text']}\n"
|
106 |
-
text += f"[Translation ({
|
107 |
-
text += f"{get_translation(output['text'],
|
108 |
return text
|
109 |
|
110 |
else:
|
@@ -117,7 +120,7 @@ iface = gr.Interface(
|
|
117 |
gr.Audio(source="microphone", type="filepath", label="Translate from Microphone"),
|
118 |
gr.Text(max_lines=1, placeholder="Enter YouTube Link with Swedish speech to be translated", label="Translate from YouTube URL"),
|
119 |
gr.Slider(minimum=30, maximum=300, value=30, step=30, label="Number of seconds to translate from YouTube URL"),
|
120 |
-
gr.Dropdown(languages, label="
|
121 |
],
|
122 |
outputs="text",
|
123 |
title="Whisper Small Swedish",
|
|
|
5 |
from datasets import Dataset, Audio
|
6 |
from moviepy.editor import AudioFileClip
|
7 |
|
8 |
+
# import googletrans # googletrans api
|
9 |
+
# from googletrans import Translator # googletrans api
|
10 |
+
from google_trans_new import google_translator
|
11 |
|
12 |
pipe = pipeline(model="Neprox/model")
|
13 |
+
translator = google_translator()
|
14 |
+
# translator = Translator() # googletrans api
|
15 |
|
16 |
# Get languages available for translation
|
17 |
languages = []
|
|
|
77 |
|
78 |
return segment_paths, segment_start_times
|
79 |
|
80 |
+
def get_translation(text, target_lang="English (en)"):
|
81 |
"""
|
82 |
Translates the given Swedish text to the language specified.
|
83 |
"""
|
84 |
+
lang_code = target_lang.split(" ")[-1][1:-1]
|
85 |
+
return translator.translate(text, lang_tgt=lang_code)
|
86 |
+
# result = translator.translate(text, lang_code, 'sv') # googletrans api
|
87 |
+
# return result.text # googletrans api
|
88 |
|
89 |
|
90 |
+
def translate(audio, url, seconds_max, target_lang):
|
91 |
"""
|
92 |
Translates a YouTube video if a url is specified and returns the transcription.
|
93 |
If not url is specified, it translates the audio file as passed by Gradio.
|
|
|
106 |
for i, (seconds, output) in enumerate(zip(segment_start_times, pred)):
|
107 |
text += f"[Segment {i+1}/{n_segments}, start time {get_timestamp(seconds)}]\n"
|
108 |
text += f"{output['text']}\n"
|
109 |
+
text += f"[Translation ({target_lang})]\n"
|
110 |
+
text += f"{get_translation(output['text'], target_lang)}\n\n"
|
111 |
return text
|
112 |
|
113 |
else:
|
|
|
120 |
gr.Audio(source="microphone", type="filepath", label="Translate from Microphone"),
|
121 |
gr.Text(max_lines=1, placeholder="Enter YouTube Link with Swedish speech to be translated", label="Translate from YouTube URL"),
|
122 |
gr.Slider(minimum=30, maximum=300, value=30, step=30, label="Number of seconds to translate from YouTube URL"),
|
123 |
+
gr.Dropdown(languages, label="Target language")
|
124 |
],
|
125 |
outputs="text",
|
126 |
title="Whisper Small Swedish",
|
requirements.txt
CHANGED
@@ -7,6 +7,4 @@ torch
|
|
7 |
torchaudio
|
8 |
moviepy
|
9 |
git+https://github.com/pytube/pytube
|
10 |
-
|
11 |
-
httpcore==0.16.2
|
12 |
-
googletrans==4.0.0-rc1
|
|
|
7 |
torchaudio
|
8 |
moviepy
|
9 |
git+https://github.com/pytube/pytube
|
10 |
+
google-trans-new
|
|
|
|