Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,28 @@
|
|
1 |
import gradio as gr
|
2 |
-
import requests, os, time
|
3 |
|
4 |
ASR_API_URL = os.getenv("ASR_API_URL")
|
5 |
AUTH_TOKEN = os.getenv("AUTH_TOKEN")
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
if not file_path:
|
10 |
-
return "❌
|
|
|
11 |
if not ASR_API_URL or not AUTH_TOKEN:
|
12 |
return "❌ Error: ASR_API_URL or AUTH_TOKEN is not set.", ""
|
13 |
|
@@ -30,16 +45,16 @@ def transcribe_audio(file_path: str | None):
|
|
30 |
txt = data.get("transcription", "No transcription returned.")
|
31 |
elapsed = data.get("time", elapsed)
|
32 |
return txt, f"{elapsed:.2f} seconds"
|
|
|
33 |
return f"❌ Error {response.status_code}: {response.text}", ""
|
34 |
|
35 |
|
|
|
36 |
CUSTOM_CSS = """
|
37 |
#gooya-title {color:white; background:linear-gradient(90deg,#224CA5 0%,#2CD8D5 100%);
|
38 |
border-radius:12px;padding:20px 10px;margin-bottom:12px;}
|
39 |
.gooya-badge {display:inline-block;background:#224CA5;color:#fff;border-radius:16px;
|
40 |
padding:6px 16px;font-size:0.97rem;margin-top:4px;}
|
41 |
-
#gooya-box {background:#F7FAFF;border:1px solid #e7e9ef;border-radius:14px;
|
42 |
-
padding:22px 18px;margin-top:12px;}
|
43 |
"""
|
44 |
|
45 |
with gr.Blocks(css=CUSTOM_CSS) as demo:
|
@@ -75,33 +90,35 @@ with gr.Blocks(css=CUSTOM_CSS) as demo:
|
|
75 |
submit_btn = gr.Button("Transcribe", variant="primary", interactive=False)
|
76 |
clear_btn = gr.Button("Clear", variant="secondary")
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
- Input audio should be in Persian.
|
82 |
-
|
83 |
-
For benchmarks visit: [Persian ASR Leaderboard](https://huggingface.co/spaces/navidved/open_persian_asr_leaderboard)
|
84 |
-
""")
|
85 |
-
|
86 |
-
def toggle_btn(path):
|
87 |
-
return gr.Button.update(interactive=bool(path))
|
88 |
|
|
|
89 |
audio.change(toggle_btn, inputs=audio, outputs=submit_btn, queue=False)
|
|
|
90 |
audio.stop_recording(toggle_btn, inputs=audio, outputs=submit_btn, queue=False)
|
91 |
|
|
|
92 |
submit_btn.click(
|
93 |
transcribe_audio,
|
94 |
inputs=audio,
|
95 |
outputs=[transcription, inf_time_lbl],
|
96 |
-
).then(
|
97 |
lambda: gr.Button.update(interactive=False),
|
98 |
None,
|
99 |
submit_btn,
|
100 |
queue=False,
|
101 |
)
|
102 |
|
|
|
103 |
def clear_all():
|
104 |
-
return
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
clear_btn.click(
|
107 |
clear_all,
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests, os, time, pathlib
|
3 |
|
4 |
ASR_API_URL = os.getenv("ASR_API_URL")
|
5 |
AUTH_TOKEN = os.getenv("AUTH_TOKEN")
|
6 |
|
7 |
+
# ---------- کمکى: استخراج مسیر واقعی فایل ----------
|
8 |
+
def extract_path(audio_value):
|
9 |
+
"""
|
10 |
+
audio_value میتواند None، رشته یا دیکشنری باشد.
|
11 |
+
خروجی: مسیر فایل یا None
|
12 |
+
"""
|
13 |
+
if not audio_value:
|
14 |
+
return None
|
15 |
+
if isinstance(audio_value, dict):
|
16 |
+
return audio_value.get("path") # گرادیو ≥ 4
|
17 |
+
# برای سازگاری با نسخههای قدیمتر که str برمیگردانند
|
18 |
+
return audio_value if isinstance(audio_value, (str, pathlib.Path)) else None
|
19 |
+
|
20 |
+
# ---------- پردازش ----------
|
21 |
+
def transcribe_audio(audio_value):
|
22 |
+
file_path = extract_path(audio_value)
|
23 |
if not file_path:
|
24 |
+
return "❌ فایل صوتی هنوز آماده نیست.", ""
|
25 |
+
|
26 |
if not ASR_API_URL or not AUTH_TOKEN:
|
27 |
return "❌ Error: ASR_API_URL or AUTH_TOKEN is not set.", ""
|
28 |
|
|
|
45 |
txt = data.get("transcription", "No transcription returned.")
|
46 |
elapsed = data.get("time", elapsed)
|
47 |
return txt, f"{elapsed:.2f} seconds"
|
48 |
+
|
49 |
return f"❌ Error {response.status_code}: {response.text}", ""
|
50 |
|
51 |
|
52 |
+
# ---------- رابط کاربری ----------
|
53 |
CUSTOM_CSS = """
|
54 |
#gooya-title {color:white; background:linear-gradient(90deg,#224CA5 0%,#2CD8D5 100%);
|
55 |
border-radius:12px;padding:20px 10px;margin-bottom:12px;}
|
56 |
.gooya-badge {display:inline-block;background:#224CA5;color:#fff;border-radius:16px;
|
57 |
padding:6px 16px;font-size:0.97rem;margin-top:4px;}
|
|
|
|
|
58 |
"""
|
59 |
|
60 |
with gr.Blocks(css=CUSTOM_CSS) as demo:
|
|
|
90 |
submit_btn = gr.Button("Transcribe", variant="primary", interactive=False)
|
91 |
clear_btn = gr.Button("Clear", variant="secondary")
|
92 |
|
93 |
+
# ---------- فعال / غیرفعال کردن دکمه ----------
|
94 |
+
def toggle_btn(audio_value):
|
95 |
+
return gr.Button.update(interactive=bool(extract_path(audio_value)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
+
# بعد از آپلود فایل
|
98 |
audio.change(toggle_btn, inputs=audio, outputs=submit_btn, queue=False)
|
99 |
+
# بعد از پایان ضبط میکروفون
|
100 |
audio.stop_recording(toggle_btn, inputs=audio, outputs=submit_btn, queue=False)
|
101 |
|
102 |
+
# ---------- پردازش ----------
|
103 |
submit_btn.click(
|
104 |
transcribe_audio,
|
105 |
inputs=audio,
|
106 |
outputs=[transcription, inf_time_lbl],
|
107 |
+
).then(
|
108 |
lambda: gr.Button.update(interactive=False),
|
109 |
None,
|
110 |
submit_btn,
|
111 |
queue=False,
|
112 |
)
|
113 |
|
114 |
+
# ---------- پاککردن ----------
|
115 |
def clear_all():
|
116 |
+
return (
|
117 |
+
"", # متن
|
118 |
+
"", # زمان
|
119 |
+
gr.Audio.update(value=None), # پاک کردن کامپوننت Audio
|
120 |
+
gr.Button.update(interactive=False), # غیر فعال کردن دکمه
|
121 |
+
)
|
122 |
|
123 |
clear_btn.click(
|
124 |
clear_all,
|