Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -113,7 +113,7 @@ def process_audio_chunk(audio_tuple):
|
|
| 113 |
text = ""
|
| 114 |
try:
|
| 115 |
text = recognizer.recognize_google(audio, language='fa-IR') # Persian first
|
| 116 |
-
# اگر key داری: recognize_google(audio, language='fa-IR', key="YOUR_GOOGLE_API_KEY")
|
| 117 |
except sr.UnknownValueError:
|
| 118 |
try:
|
| 119 |
text = recognizer.recognize_google(audio, language='en-US')
|
|
@@ -153,10 +153,13 @@ def clear_transcript():
|
|
| 153 |
current_transcript = ""
|
| 154 |
# Clear queue
|
| 155 |
while not audio_queue.empty():
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
| 157 |
return ""
|
| 158 |
|
| 159 |
-
# File transcription (unchanged
|
| 160 |
def transcribe_file(audio_file, chunk_duration=30):
|
| 161 |
if audio_file is None:
|
| 162 |
yield "لطفاً فایل آپلود کنید", ""
|
|
@@ -210,7 +213,7 @@ def save_text(text):
|
|
| 210 |
temp_file.close()
|
| 211 |
return temp_file.name
|
| 212 |
|
| 213 |
-
# Gradio UI
|
| 214 |
with gr.Blocks(
|
| 215 |
title="تبدیل گفتار به متن - Real-time Fixed",
|
| 216 |
theme=gr.themes.Soft(),
|
|
@@ -229,7 +232,7 @@ with gr.Blocks(
|
|
| 229 |
""")
|
| 230 |
|
| 231 |
with gr.Tabs():
|
| 232 |
-
with gr.TabItem("🎙️ ضبط مستقیم"):
|
| 233 |
gr.Markdown("### فعال کنید و 5+ ثانیه واضح صحبت کنید (متن هر 3s آپدیت میشه)", elem_classes="rtl")
|
| 234 |
|
| 235 |
with gr.Row():
|
|
@@ -239,9 +242,6 @@ with gr.Blocks(
|
|
| 239 |
label="میکروفون (ضبط رو شروع کن)",
|
| 240 |
elem_classes="rtl"
|
| 241 |
)
|
| 242 |
-
|
| 243 |
-
start_btn = gr.Button("▶️ شروع real-time", variant="primary")
|
| 244 |
-
stop_btn = gr.Button("⏹️ توقف", variant="secondary")
|
| 245 |
|
| 246 |
realtime_output = gr.Textbox(
|
| 247 |
label="متن live",
|
|
@@ -255,23 +255,23 @@ with gr.Blocks(
|
|
| 255 |
|
| 256 |
clear_btn = gr.Button("🗑️ پاک کردن", variant="secondary")
|
| 257 |
|
| 258 |
-
# Events
|
| 259 |
-
audio_input.change(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
-
# Timer for live update (هر 2s transcript رو pull کن)
|
| 262 |
-
timer = gr.Timer(2.0) # Start after tab open
|
| 263 |
timer.tick(get_current_transcript, outputs=[realtime_output])
|
| 264 |
|
| 265 |
clear_btn.click(clear_transcript, outputs=[realtime_output])
|
| 266 |
-
|
| 267 |
-
# Start/stop recording (toggle microphone)
|
| 268 |
-
def toggle_recording(active):
|
| 269 |
-
return gr.update(value=active) # Simple toggle, but Gradio handles start/stop
|
| 270 |
-
|
| 271 |
-
start_btn.click(lambda: gr.update(visible=True), outputs=[stop_btn]).click(
|
| 272 |
-
toggle_recording, inputs=[audio_input], outputs=[audio_input]
|
| 273 |
-
)
|
| 274 |
-
stop_btn.click(lambda: gr.update(visible=False), outputs=[start_btn])
|
| 275 |
|
| 276 |
with gr.TabItem("📁 فایل صوتی"):
|
| 277 |
gr.Markdown("### فایل آپلود کن و تبدیل کن", elem_classes="rtl")
|
|
@@ -290,13 +290,13 @@ with gr.Blocks(
|
|
| 290 |
download_file = gr.File(label="دانلود TXT", visible=False, elem_classes="rtl")
|
| 291 |
|
| 292 |
process_btn.click(transcribe_file, [file_input, chunk_slider], [file_output, progress_label])
|
| 293 |
-
save_btn.click(save_text, file_output, download_file).then(lambda: gr.update(visible=True), download_file)
|
| 294 |
clear_file_btn.click(lambda: ("", ""), [file_output, progress_label])
|
| 295 |
|
| 296 |
with gr.Accordion("📖 راهنما", open=False, elem_classes="rtl"):
|
| 297 |
gr.Markdown("""
|
| 298 |
### استفاده:
|
| 299 |
-
- **Real-time**:
|
| 300 |
- **فایل**: آپلود و دکمه بزن.
|
| 301 |
### نکات:
|
| 302 |
- 🗣️ واضح صحبت کن، نویز کم.
|
|
@@ -306,7 +306,7 @@ with gr.Blocks(
|
|
| 306 |
- توزیع: share لینک رو share کن، همه browserها ساپورت.
|
| 307 |
""", elem_classes="rtl")
|
| 308 |
|
| 309 |
-
gr.HTML('<div style="text-align: center; margin-top: 2em; padding: 1em; background: #f8f9fa;"><p style="color: #666;">نسخه 2.
|
| 310 |
|
| 311 |
if __name__ == "__main__":
|
| 312 |
demo.queue().launch(share=True, show_error=True, server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 113 |
text = ""
|
| 114 |
try:
|
| 115 |
text = recognizer.recognize_google(audio, language='fa-IR') # Persian first
|
| 116 |
+
# اگر key داری: text = recognizer.recognize_google(audio, language='fa-IR', key="YOUR_GOOGLE_API_KEY")
|
| 117 |
except sr.UnknownValueError:
|
| 118 |
try:
|
| 119 |
text = recognizer.recognize_google(audio, language='en-US')
|
|
|
|
| 153 |
current_transcript = ""
|
| 154 |
# Clear queue
|
| 155 |
while not audio_queue.empty():
|
| 156 |
+
try:
|
| 157 |
+
audio_queue.get_nowait()
|
| 158 |
+
except queue.Empty:
|
| 159 |
+
break
|
| 160 |
return ""
|
| 161 |
|
| 162 |
+
# File transcription (unchanged)
|
| 163 |
def transcribe_file(audio_file, chunk_duration=30):
|
| 164 |
if audio_file is None:
|
| 165 |
yield "لطفاً فایل آپلود کنید", ""
|
|
|
|
| 213 |
temp_file.close()
|
| 214 |
return temp_file.name
|
| 215 |
|
| 216 |
+
# Gradio UI (Fixed: Separate clicks, Timer with load event)
|
| 217 |
with gr.Blocks(
|
| 218 |
title="تبدیل گفتار به متن - Real-time Fixed",
|
| 219 |
theme=gr.themes.Soft(),
|
|
|
|
| 232 |
""")
|
| 233 |
|
| 234 |
with gr.Tabs():
|
| 235 |
+
with gr.TabItem("🎙️ ضبط مستقیم", id="realtime_tab") as realtime_tab:
|
| 236 |
gr.Markdown("### فعال کنید و 5+ ثانیه واضح صحبت کنید (متن هر 3s آپدیت میشه)", elem_classes="rtl")
|
| 237 |
|
| 238 |
with gr.Row():
|
|
|
|
| 242 |
label="میکروفون (ضبط رو شروع کن)",
|
| 243 |
elem_classes="rtl"
|
| 244 |
)
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
realtime_output = gr.Textbox(
|
| 247 |
label="متن live",
|
|
|
|
| 255 |
|
| 256 |
clear_btn = gr.Button("🗑️ پاک کردن", variant="secondary")
|
| 257 |
|
| 258 |
+
# Events (Fixed: Separate clicks)
|
| 259 |
+
audio_input.change(
|
| 260 |
+
handle_realtime_audio,
|
| 261 |
+
inputs=[audio_input],
|
| 262 |
+
outputs=[realtime_output] # Update output on change
|
| 263 |
+
)
|
| 264 |
+
|
| 265 |
+
# Timer for live update: Start when tab loads
|
| 266 |
+
timer = gr.Timer(value=2.0, active=False)
|
| 267 |
+
def start_timer():
|
| 268 |
+
timer.change(active=True)
|
| 269 |
+
return get_current_transcript()
|
| 270 |
+
realtime_tab.select(start_timer, outputs=[realtime_output])
|
| 271 |
|
|
|
|
|
|
|
| 272 |
timer.tick(get_current_transcript, outputs=[realtime_output])
|
| 273 |
|
| 274 |
clear_btn.click(clear_transcript, outputs=[realtime_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
with gr.TabItem("📁 فایل صوتی"):
|
| 277 |
gr.Markdown("### فایل آپلود کن و تبدیل کن", elem_classes="rtl")
|
|
|
|
| 290 |
download_file = gr.File(label="دانلود TXT", visible=False, elem_classes="rtl")
|
| 291 |
|
| 292 |
process_btn.click(transcribe_file, [file_input, chunk_slider], [file_output, progress_label])
|
| 293 |
+
save_btn.click(save_text, file_output, download_file).then(lambda: gr.update(visible=True), outputs=[download_file])
|
| 294 |
clear_file_btn.click(lambda: ("", ""), [file_output, progress_label])
|
| 295 |
|
| 296 |
with gr.Accordion("📖 راهنما", open=False, elem_classes="rtl"):
|
| 297 |
gr.Markdown("""
|
| 298 |
### استفاده:
|
| 299 |
+
- **Real-time**: تب رو باز کن، میکروفون فعال کن، 5s+ صحبت کن. هر 3s متن آپدیت میشه (background).
|
| 300 |
- **فایل**: آپلود و دکمه بزن.
|
| 301 |
### نکات:
|
| 302 |
- 🗣️ واضح صحبت کن، نویز کم.
|
|
|
|
| 306 |
- توزیع: share لینک رو share کن، همه browserها ساپورت.
|
| 307 |
""", elem_classes="rtl")
|
| 308 |
|
| 309 |
+
gr.HTML('<div style="text-align: center; margin-top: 2em; padding: 1em; background: #f8f9fa;"><p style="color: #666;">نسخه 2.3 - Fixed Timer & Clicks | Google Backend</p></div>')
|
| 310 |
|
| 311 |
if __name__ == "__main__":
|
| 312 |
demo.queue().launch(share=True, show_error=True, server_name="0.0.0.0", server_port=7860)
|