Spaces:
Sleeping
Sleeping
Ubuntu
commited on
Commit
•
940d177
1
Parent(s):
1ad4435
embed youtube video
Browse files
app.py
CHANGED
@@ -26,7 +26,9 @@ RAPID_API_KEY = os.getenv("RAPID_API_KEY")
|
|
26 |
|
27 |
def fetch_youtube_id(youtube_url: str) -> str:
|
28 |
if 'v=' in youtube_url:
|
29 |
-
return youtube_url.split("v=")[1]
|
|
|
|
|
30 |
elif 'shorts' in youtube_url:
|
31 |
return youtube_url.split("/")[-1]
|
32 |
else:
|
@@ -156,7 +158,17 @@ def check_password(password):
|
|
156 |
|
157 |
def run_speech_translation(audio, source_lang, target_lang, youtube_url):
|
158 |
transcription, translated_text, audio_url = transcribe_and_speak(audio, source_lang, target_lang, youtube_url)
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
with gr.Blocks() as demo:
|
162 |
gr.Markdown("# Speech Translation")
|
@@ -178,6 +190,8 @@ with gr.Blocks() as demo:
|
|
178 |
user_translation_output = gr.Textbox(label="Translation")
|
179 |
user_audio_output = gr.Audio(label="Translated Speech")
|
180 |
|
|
|
|
|
181 |
def update_button_state(audio, youtube_url):
|
182 |
print(audio, youtube_url)
|
183 |
return gr.Button(interactive=bool(audio) or bool(youtube_url))
|
@@ -196,7 +210,7 @@ with gr.Blocks() as demo:
|
|
196 |
user_button.click(
|
197 |
fn=run_speech_translation,
|
198 |
inputs=[user_audio_input, user_source_lang, user_target_lang, user_youtube_url],
|
199 |
-
outputs=[user_transcription_output, user_translation_output, user_audio_output]
|
200 |
)
|
201 |
|
202 |
demo.launch(auth=(os.getenv("DEV_USER"), os.getenv("DEV_PWD")))
|
|
|
26 |
|
27 |
def fetch_youtube_id(youtube_url: str) -> str:
|
28 |
if 'v=' in youtube_url:
|
29 |
+
return youtube_url.split("v=")[1].split("&")[0]
|
30 |
+
elif 'youtu.be/' in youtube_url:
|
31 |
+
return youtube_url.split("youtu.be/")[1]
|
32 |
elif 'shorts' in youtube_url:
|
33 |
return youtube_url.split("/")[-1]
|
34 |
else:
|
|
|
158 |
|
159 |
def run_speech_translation(audio, source_lang, target_lang, youtube_url):
|
160 |
transcription, translated_text, audio_url = transcribe_and_speak(audio, source_lang, target_lang, youtube_url)
|
161 |
+
|
162 |
+
# Generate embedded video HTML if YouTube URL is provided
|
163 |
+
embedded_video = ""
|
164 |
+
if youtube_url:
|
165 |
+
try:
|
166 |
+
video_id = fetch_youtube_id(youtube_url)
|
167 |
+
embedded_video = f'<iframe width="560" height="315" src="https://www.youtube.com/embed/{video_id}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>'
|
168 |
+
except Exception as e:
|
169 |
+
print(f"Error embedding video: {e}")
|
170 |
+
|
171 |
+
return transcription, translated_text, audio_url, embedded_video
|
172 |
|
173 |
with gr.Blocks() as demo:
|
174 |
gr.Markdown("# Speech Translation")
|
|
|
190 |
user_translation_output = gr.Textbox(label="Translation")
|
191 |
user_audio_output = gr.Audio(label="Translated Speech")
|
192 |
|
193 |
+
user_video_output = gr.HTML(label="YouTube Video")
|
194 |
+
|
195 |
def update_button_state(audio, youtube_url):
|
196 |
print(audio, youtube_url)
|
197 |
return gr.Button(interactive=bool(audio) or bool(youtube_url))
|
|
|
210 |
user_button.click(
|
211 |
fn=run_speech_translation,
|
212 |
inputs=[user_audio_input, user_source_lang, user_target_lang, user_youtube_url],
|
213 |
+
outputs=[user_transcription_output, user_translation_output, user_audio_output, user_video_output]
|
214 |
)
|
215 |
|
216 |
demo.launch(auth=(os.getenv("DEV_USER"), os.getenv("DEV_PWD")))
|