Update app.py
Browse files
app.py
CHANGED
@@ -21,7 +21,15 @@ def fetch_youtube_id(youtube_url: str) -> str:
|
|
21 |
return youtube_url.split("/")[-1]
|
22 |
else:
|
23 |
raise Exception("Unsupported URL format")
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
def download_youtube_audio(youtube_url: str, output_dir: Optional[str] = None) -> str:
|
26 |
video_id = fetch_youtube_id(youtube_url)
|
27 |
|
@@ -76,7 +84,7 @@ def run_asr(audio_file, youtube_url):
|
|
76 |
temp_file = None
|
77 |
with_timestamp=False
|
78 |
model_choice = "local_whisper"
|
79 |
-
|
80 |
try:
|
81 |
if last_modified == 2 and youtube_url:
|
82 |
# It's a YouTube URL
|
@@ -107,15 +115,34 @@ def run_asr(audio_file, youtube_url):
|
|
107 |
if temp_file and os.path.exists(temp_file):
|
108 |
os.remove(temp_file)
|
109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
# Create the Gradio interface with improved aesthetics
|
112 |
-
with gr.Blocks() as demo:
|
113 |
-
gr.Markdown("Audio Transcription Service")
|
114 |
gr.Markdown("Upload an audio file, record your voice, or paste a YouTube URL to get an English transcription.")
|
115 |
|
116 |
with gr.Row():
|
117 |
with gr.Column(scale=2):
|
118 |
-
audio_input = gr.Audio(sources=['microphone', 'upload'], type
|
119 |
youtube_input = gr.Textbox(label="YouTube URL", placeholder="Or paste a YouTube URL here...")
|
120 |
video_player = gr.HTML(visible=False)
|
121 |
#timestamp_toggle = gr.Checkbox(label="Include Timestamps", value=False)
|
@@ -127,49 +154,29 @@ with gr.Blocks() as demo:
|
|
127 |
lines=10
|
128 |
)
|
129 |
|
130 |
-
run_button = gr.Button("Transcribe Audio", variant="primary", interactive=False)
|
131 |
-
run_button.click(run_asr, inputs=[audio_input, youtube_input], outputs=[result])
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
def update_button_state_youtube(audio, youtube_url):
|
142 |
-
global last_modified
|
143 |
-
print(" youtube: ",audio, " youtube_url: ", youtube_url)
|
144 |
-
last_modified=2
|
145 |
-
# Button is interactive if there's input and progress is 0 or 1 (not in progress)
|
146 |
-
return gr.update(interactive=bool(audio) or bool(youtube_url !=""))
|
147 |
-
|
148 |
-
audio_input.change(
|
149 |
-
fn=update_button_state_audio,
|
150 |
-
inputs=[audio_input, youtube_input],
|
151 |
-
outputs=run_button
|
152 |
)
|
|
|
153 |
youtube_input.change(
|
154 |
-
fn=
|
155 |
-
inputs=[
|
156 |
-
outputs=run_button
|
157 |
)
|
158 |
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
return embed_html
|
165 |
-
except Exception as e:
|
166 |
-
print(f"Error embedding video: {e}")
|
167 |
-
return ""
|
168 |
-
|
169 |
-
youtube_input.change(
|
170 |
-
fn=update_video_embed,
|
171 |
-
inputs=[youtube_input],
|
172 |
-
outputs=[video_player]
|
173 |
)
|
174 |
|
175 |
gr.Markdown("### How to use:")
|
|
|
21 |
return youtube_url.split("/")[-1]
|
22 |
else:
|
23 |
raise Exception("Unsupported URL format")
|
24 |
+
"""
|
25 |
+
def fetch_youtube_id(youtube_url: str) -> str:
|
26 |
+
if 'v=' in youtube_url:
|
27 |
+
return youtube_url.split("v=")[1]
|
28 |
+
elif 'shorts' in youtube_url:
|
29 |
+
return youtube_url.split("/")[-1]
|
30 |
+
else:
|
31 |
+
raise Exception("Unsupported URL format")
|
32 |
+
"""
|
33 |
def download_youtube_audio(youtube_url: str, output_dir: Optional[str] = None) -> str:
|
34 |
video_id = fetch_youtube_id(youtube_url)
|
35 |
|
|
|
84 |
temp_file = None
|
85 |
with_timestamp=False
|
86 |
model_choice = "local_whisper"
|
87 |
+
|
88 |
try:
|
89 |
if last_modified == 2 and youtube_url:
|
90 |
# It's a YouTube URL
|
|
|
115 |
if temp_file and os.path.exists(temp_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 |
|
138 |
# Create the Gradio interface with improved aesthetics
|
139 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
140 |
+
gr.Markdown("# 🎙️ Audio Transcription Service")
|
141 |
gr.Markdown("Upload an audio file, record your voice, or paste a YouTube URL to get an English transcription.")
|
142 |
|
143 |
with gr.Row():
|
144 |
with gr.Column(scale=2):
|
145 |
+
audio_input = gr.Audio(sources=['microphone', 'upload'], type="filepath", label="Audio Input")
|
146 |
youtube_input = gr.Textbox(label="YouTube URL", placeholder="Or paste a YouTube URL here...")
|
147 |
video_player = gr.HTML(visible=False)
|
148 |
#timestamp_toggle = gr.Checkbox(label="Include Timestamps", value=False)
|
|
|
154 |
lines=10
|
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))),
|
171 |
+
inputs=[youtube_input],
|
172 |
+
outputs=[video_player, result, audio_input, run_button]
|
173 |
)
|
174 |
|
175 |
+
# Clear transcription, YouTube input, video player, and update run button when audio is input
|
176 |
+
audio_input.change(
|
177 |
+
fn=clear_on_audio_input,
|
178 |
+
inputs=[audio_input],
|
179 |
+
outputs=[result, video_player, youtube_input, run_button]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
)
|
181 |
|
182 |
gr.Markdown("### How to use:")
|