Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,7 @@ from pydub import AudioSegment # Import AudioSegment
|
|
10 |
# Define the FastAPI URL
|
11 |
API_URL = "http://astarwiz.com:9998"
|
12 |
rapid_key = os.environ.get("RAPID_API_KEY")
|
13 |
-
|
14 |
def fetch_youtube_id(youtube_url: str) -> str:
|
15 |
if 'v=' in youtube_url:
|
16 |
return youtube_url.split("v=")[1].split("&")[0]
|
@@ -86,7 +86,7 @@ def run_asr(audio_file, youtube_url):
|
|
86 |
model_choice = "local_whisper"
|
87 |
|
88 |
try:
|
89 |
-
if youtube_url:
|
90 |
# It's a YouTube URL
|
91 |
audio_file = download_youtube_audio(youtube_url)
|
92 |
temp_file = audio_file
|
@@ -116,17 +116,22 @@ def run_asr(audio_file, youtube_url):
|
|
116 |
os.remove(temp_file)
|
117 |
|
118 |
def embed_youtube(youtube_url):
|
|
|
119 |
if youtube_url:
|
120 |
try:
|
121 |
video_id = fetch_youtube_id(youtube_url)
|
122 |
embed_html = f'<iframe width="560" height="315" src="https://www.youtube.com/embed/{video_id}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>'
|
|
|
123 |
return gr.update(value=embed_html, visible=True), "", None
|
124 |
except Exception as e:
|
125 |
return gr.update(value="", visible=False), f"Invalid YouTube URL: {str(e)}", None
|
126 |
return gr.update(value="", visible=False), "", None
|
127 |
|
128 |
def clear_on_audio_input(audio):
|
|
|
|
|
129 |
if audio is not None:
|
|
|
130 |
return "", gr.update(value="", visible=False), "", gr.update(interactive=True)
|
131 |
return gr.update(), gr.update(), gr.update(), gr.update(interactive=False)
|
132 |
|
@@ -150,8 +155,16 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
150 |
)
|
151 |
|
152 |
run_button = gr.Button("π Transcribe Audio", variant="primary", interactive=False)
|
153 |
-
run_button.click(run_asr, inputs=[audio_input, youtube_input], outputs=[result])
|
154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
# Update video player, clear transcription and audio input, and enable run button when YouTube URL is entered
|
156 |
youtube_input.change(
|
157 |
fn=lambda url: (*embed_youtube(url), gr.update(interactive=bool(url))),
|
|
|
10 |
# Define the FastAPI URL
|
11 |
API_URL = "http://astarwiz.com:9998"
|
12 |
rapid_key = os.environ.get("RAPID_API_KEY")
|
13 |
+
last_modified = 1
|
14 |
def fetch_youtube_id(youtube_url: str) -> str:
|
15 |
if 'v=' in youtube_url:
|
16 |
return youtube_url.split("v=")[1].split("&")[0]
|
|
|
86 |
model_choice = "local_whisper"
|
87 |
|
88 |
try:
|
89 |
+
if last_modified == 2 and youtube_url:
|
90 |
# It's a YouTube URL
|
91 |
audio_file = download_youtube_audio(youtube_url)
|
92 |
temp_file = audio_file
|
|
|
116 |
os.remove(temp_file)
|
117 |
|
118 |
def embed_youtube(youtube_url):
|
119 |
+
globals last_modified
|
120 |
if youtube_url:
|
121 |
try:
|
122 |
video_id = fetch_youtube_id(youtube_url)
|
123 |
embed_html = f'<iframe width="560" height="315" src="https://www.youtube.com/embed/{video_id}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>'
|
124 |
+
last_modified = 2;
|
125 |
return gr.update(value=embed_html, visible=True), "", None
|
126 |
except Exception as e:
|
127 |
return gr.update(value="", visible=False), f"Invalid YouTube URL: {str(e)}", None
|
128 |
return gr.update(value="", visible=False), "", None
|
129 |
|
130 |
def clear_on_audio_input(audio):
|
131 |
+
globals last_modified
|
132 |
+
|
133 |
if audio is not None:
|
134 |
+
last_modified =1
|
135 |
return "", gr.update(value="", visible=False), "", gr.update(interactive=True)
|
136 |
return gr.update(), gr.update(), gr.update(), gr.update(interactive=False)
|
137 |
|
|
|
155 |
)
|
156 |
|
157 |
run_button = gr.Button("π Transcribe Audio", variant="primary", interactive=False)
|
158 |
+
#run_button.click(run_asr, inputs=[audio_input, youtube_input], outputs=[result])
|
159 |
+
run_button.click(
|
160 |
+
fn=lambda: '', # This lambda function sets the output to empty
|
161 |
+
inputs=None,
|
162 |
+
outputs=result
|
163 |
+
).then(
|
164 |
+
run_asr,
|
165 |
+
inputs=[audio_input, youtube_input],
|
166 |
+
outputs=result
|
167 |
+
)
|
168 |
# Update video player, clear transcription and audio input, and enable run button when YouTube URL is entered
|
169 |
youtube_input.change(
|
170 |
fn=lambda url: (*embed_youtube(url), gr.update(interactive=bool(url))),
|