Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,28 @@
|
|
1 |
import os, time, requests, gradio as gr
|
2 |
|
3 |
-
# ---------- Hot-patch
|
4 |
import gradio.blocks as _blocks
|
5 |
-
if not hasattr(_blocks.Blocks, "_api_info_patched"):
|
6 |
_blocks.Blocks._api_info_patched = True
|
7 |
-
_blocks.Blocks.get_api_info = lambda self: {}
|
8 |
|
9 |
-
print("Gradio version:", gr.__version__)
|
10 |
|
11 |
-
# ----------
|
12 |
ASR_API_URL = os.getenv("ASR_API_URL")
|
13 |
AUTH_TOKEN = os.getenv("AUTH_TOKEN")
|
14 |
if not ASR_API_URL or not AUTH_TOKEN:
|
15 |
-
print("⚠️
|
16 |
|
17 |
-
# ----------
|
18 |
def transcribe_audio(file_path: str):
|
19 |
if not ASR_API_URL or not AUTH_TOKEN:
|
20 |
return "❌ Error: ASR_API_URL or AUTH_TOKEN is not set.", ""
|
21 |
|
22 |
-
headers = {
|
23 |
-
|
|
|
|
|
24 |
|
25 |
start = time.time()
|
26 |
try:
|
@@ -33,29 +35,39 @@ def transcribe_audio(file_path: str):
|
|
33 |
elapsed = time.time() - start
|
34 |
if resp.status_code == 200:
|
35 |
data = resp.json()
|
36 |
-
|
37 |
-
|
38 |
return f"❌ Error: {resp.status_code}, {resp.text}", ""
|
39 |
|
40 |
-
# ----------
|
41 |
-
|
42 |
-
#
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
"""
|
47 |
|
48 |
-
# ----------
|
49 |
with gr.Blocks(css=custom_css, title="Gooya ASR v1.4") as demo:
|
50 |
gr.HTML(
|
51 |
-
"""
|
52 |
<div id="gooya-title">
|
53 |
-
<h1 style='margin-bottom:10px;font-weight:800;font-size:2rem;'>
|
54 |
-
<span style="font-size:1.1rem;font-weight:400;opacity:.8;">v1.4</span>
|
55 |
</h1>
|
56 |
-
<p style='font-size:1.12rem;margin-bottom:2px;'>
|
57 |
-
|
58 |
-
|
|
|
|
|
59 |
</p>
|
60 |
</div>
|
61 |
"""
|
@@ -88,17 +100,16 @@ with gr.Blocks(css=custom_css, title="Gooya ASR v1.4") as demo:
|
|
88 |
|
89 |
gr.Markdown(
|
90 |
"""
|
91 |
-
|
92 |
-
-
|
93 |
-
-
|
94 |
-
-
|
95 |
-
|
96 |
-
|
97 |
-
[Persian ASR Leaderboard](https://huggingface.co/spaces/navidved/open_persian_asr_leaderboard)
|
98 |
-
مراجعه کنید.
|
99 |
"""
|
100 |
)
|
101 |
|
|
|
102 |
btn_transcribe.click(
|
103 |
transcribe_audio,
|
104 |
inputs=audio_input,
|
@@ -111,8 +122,7 @@ with gr.Blocks(css=custom_css, title="Gooya ASR v1.4") as demo:
|
|
111 |
outputs=[transcription_tb, processing_time_tb, audio_input],
|
112 |
)
|
113 |
|
114 |
-
# ----------
|
115 |
if __name__ == "__main__":
|
116 |
-
# show_api=False
|
117 |
-
|
118 |
-
demo.queue().launch(show_api=False, debug=True, share=False)
|
|
|
1 |
import os, time, requests, gradio as gr
|
2 |
|
3 |
+
# ---------- Hot-patch to bypass Gradio 4.44.0 JSON-schema bug ----------
|
4 |
import gradio.blocks as _blocks
|
5 |
+
if not hasattr(_blocks.Blocks, "_api_info_patched"):
|
6 |
_blocks.Blocks._api_info_patched = True
|
7 |
+
_blocks.Blocks.get_api_info = lambda self: {}
|
8 |
|
9 |
+
print("Gradio version:", gr.__version__) # should be 4.44.0
|
10 |
|
11 |
+
# ---------- Environment Variables ----------
|
12 |
ASR_API_URL = os.getenv("ASR_API_URL")
|
13 |
AUTH_TOKEN = os.getenv("AUTH_TOKEN")
|
14 |
if not ASR_API_URL or not AUTH_TOKEN:
|
15 |
+
print("⚠️ ASR_API_URL or AUTH_TOKEN is not set; API calls will fail.")
|
16 |
|
17 |
+
# ---------- Core Transcription Function ----------
|
18 |
def transcribe_audio(file_path: str):
|
19 |
if not ASR_API_URL or not AUTH_TOKEN:
|
20 |
return "❌ Error: ASR_API_URL or AUTH_TOKEN is not set.", ""
|
21 |
|
22 |
+
headers = {
|
23 |
+
"accept": "application/json",
|
24 |
+
"Authorization": f"Bearer {AUTH_TOKEN}",
|
25 |
+
}
|
26 |
|
27 |
start = time.time()
|
28 |
try:
|
|
|
35 |
elapsed = time.time() - start
|
36 |
if resp.status_code == 200:
|
37 |
data = resp.json()
|
38 |
+
text = data.get("transcription", "No transcription returned.")
|
39 |
+
return text, f"{data.get('time', elapsed):.2f} s"
|
40 |
return f"❌ Error: {resp.status_code}, {resp.text}", ""
|
41 |
|
42 |
+
# ---------- Styling ----------
|
43 |
+
VIOLET_MAIN = "#7F3FBF" # primary violet
|
44 |
+
VIOLET_LIGHT = "#C3A6FF" # lighter violet for gradient/badge
|
45 |
+
|
46 |
+
custom_css = f"""
|
47 |
+
#gooya-title {{
|
48 |
+
color:#fff;
|
49 |
+
background:linear-gradient(90deg,{VIOLET_MAIN} 0%,{VIOLET_LIGHT} 100%);
|
50 |
+
border-radius:12px;padding:20px 10px;margin-bottom:12px;
|
51 |
+
}}
|
52 |
+
.gooya-badge {{
|
53 |
+
display:inline-block;background:{VIOLET_MAIN};color:#fff;
|
54 |
+
border-radius:16px;padding:6px 16px;font-size:.97rem;margin-top:4px;
|
55 |
+
}}
|
56 |
"""
|
57 |
|
58 |
+
# ---------- UI ----------
|
59 |
with gr.Blocks(css=custom_css, title="Gooya ASR v1.4") as demo:
|
60 |
gr.HTML(
|
61 |
+
f"""
|
62 |
<div id="gooya-title">
|
63 |
+
<h1 style='margin-bottom:10px;font-weight:800;font-size:2rem;'>
|
64 |
+
Gooya ASR <span style="font-size:1.1rem;font-weight:400;opacity:.8;">v1.4</span>
|
65 |
</h1>
|
66 |
+
<p style='font-size:1.12rem;margin-bottom:2px;'>
|
67 |
+
High-performance Persian Speech-to-Text
|
68 |
+
</p>
|
69 |
+
<p style='font-size:.98rem;color:#e9dbff'>
|
70 |
+
Upload or record a Persian audio file (max 30 s) and instantly get the transcription.
|
71 |
</p>
|
72 |
</div>
|
73 |
"""
|
|
|
100 |
|
101 |
gr.Markdown(
|
102 |
"""
|
103 |
+
**Guidelines**
|
104 |
+
- Maximum audio length: **30 seconds**
|
105 |
+
- Audio content should be in Persian.
|
106 |
+
- Both transcription and processing time are displayed immediately.
|
107 |
+
|
108 |
+
See the [Persian ASR Leaderboard](https://huggingface.co/spaces/navidved/open_persian_asr_leaderboard) for benchmarks.
|
|
|
|
|
109 |
"""
|
110 |
)
|
111 |
|
112 |
+
# ---------- Callbacks ----------
|
113 |
btn_transcribe.click(
|
114 |
transcribe_audio,
|
115 |
inputs=audio_input,
|
|
|
122 |
outputs=[transcription_tb, processing_time_tb, audio_input],
|
123 |
)
|
124 |
|
125 |
+
# ---------- Launch ----------
|
126 |
if __name__ == "__main__":
|
127 |
+
# show_api=False prevents /api-info creation; share=True avoids localhost accessibility errors
|
128 |
+
demo.queue().launch(show_api=False, share=True)
|
|