Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,87 +1,95 @@
|
|
1 |
import gradio as gr
|
2 |
-
import requests
|
3 |
-
import os
|
4 |
-
import time
|
5 |
|
6 |
-
ASR_API_URL = os.getenv(
|
7 |
-
AUTH_TOKEN
|
8 |
|
9 |
def transcribe_audio(file_path):
|
|
|
|
|
10 |
if not ASR_API_URL or not AUTH_TOKEN:
|
11 |
return "β Error: ASR_API_URL or AUTH_TOKEN is not set.", ""
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
}
|
16 |
-
|
17 |
-
'file': (file_path, open(file_path, 'rb'), 'audio/mpeg'),
|
18 |
-
}
|
19 |
-
start_time = time.time()
|
20 |
try:
|
21 |
response = requests.post(ASR_API_URL, headers=headers, files=files)
|
22 |
except Exception as e:
|
23 |
-
return f"β Error: {
|
24 |
-
|
25 |
-
|
26 |
if response.status_code == 200:
|
27 |
res = response.json()
|
28 |
-
|
29 |
-
|
30 |
-
return
|
31 |
else:
|
32 |
-
return f"β Error
|
33 |
|
34 |
with gr.Blocks(css="""
|
35 |
-
#gooya-title {color:white; background:
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
""") as demo:
|
|
|
39 |
gr.HTML("""<div id="gooya-title">
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
</div>""")
|
44 |
|
45 |
with gr.Row():
|
46 |
with gr.Column():
|
47 |
-
audio = gr.Audio(
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
sources=["upload", "microphone"]
|
52 |
-
)
|
53 |
with gr.Column():
|
54 |
-
|
55 |
-
transcription = gr.Textbox(
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
elem_id="gooya-textbox"
|
61 |
-
)
|
62 |
|
63 |
with gr.Row():
|
64 |
-
submit_btn = gr.Button("Transcribe", variant="primary")
|
65 |
-
clear_btn
|
66 |
|
67 |
gr.Markdown("""
|
68 |
**Instructions:**
|
69 |
-
- Maximum audio length: **30 seconds**
|
70 |
-
- Input audio should be in Persian.
|
71 |
-
- The transcription and processing time will be displayed instantly.
|
72 |
|
73 |
-
For
|
74 |
""")
|
75 |
|
|
|
|
|
|
|
|
|
|
|
76 |
submit_btn.click(
|
77 |
transcribe_audio,
|
78 |
inputs=audio,
|
79 |
-
outputs=[transcription,
|
|
|
|
|
|
|
|
|
80 |
)
|
|
|
81 |
clear_btn.click(
|
82 |
-
lambda: ("", ""),
|
83 |
None,
|
84 |
-
[transcription,
|
85 |
)
|
86 |
|
87 |
-
demo.launch(
|
|
|
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 |
def transcribe_audio(file_path):
|
8 |
+
if not file_path:
|
9 |
+
return "β Audio Loading ...", ""
|
10 |
if not ASR_API_URL or not AUTH_TOKEN:
|
11 |
return "β Error: ASR_API_URL or AUTH_TOKEN is not set.", ""
|
12 |
+
|
13 |
+
headers = {"accept": "application/json",
|
14 |
+
"Authorization": f"Bearer {AUTH_TOKEN}"}
|
15 |
+
files = {"file": (file_path, open(file_path, "rb"), "audio/mpeg")}
|
16 |
+
start = time.time()
|
|
|
|
|
|
|
17 |
try:
|
18 |
response = requests.post(ASR_API_URL, headers=headers, files=files)
|
19 |
except Exception as e:
|
20 |
+
return f"β Error: {e}", ""
|
21 |
+
|
22 |
+
inf_time = time.time() - start
|
23 |
if response.status_code == 200:
|
24 |
res = response.json()
|
25 |
+
text = res.get("transcription", "No transcription returned.")
|
26 |
+
inf_time = f"{res.get('time', inf_time):.2f} seconds"
|
27 |
+
return text, inf_time
|
28 |
else:
|
29 |
+
return f"β Error {response.status_code}: {response.text}", ""
|
30 |
|
31 |
with gr.Blocks(css="""
|
32 |
+
#gooya-title {color:white; background:linear-gradient(90deg,#224CA5 0%,#2CD8D5 100%);
|
33 |
+
border-radius:12px;padding:20px 10px;margin-bottom:12px;}
|
34 |
+
.gooya-badge {display:inline-block;background:#224CA5;color:#fff;border-radius:16px;
|
35 |
+
padding:6px 16px;font-size:0.97rem;margin-top:4px;}
|
36 |
+
#gooya-box {background:#F7FAFF;border:1px solid #e7e9ef;border-radius:14px;
|
37 |
+
padding:22px 18px;margin-top:12px;}
|
38 |
""") as demo:
|
39 |
+
|
40 |
gr.HTML("""<div id="gooya-title">
|
41 |
+
<h1 style='margin-bottom:10px;font-weight:800;font-size:2rem;'>Gooya ASR
|
42 |
+
<span style="font-size:1.1rem; font-weight:400; opacity:0.8;">v1.4</span></h1>
|
43 |
+
<p style='font-size:1.12rem;margin-bottom:2px;'>High-performance Persian Speech-to-Text</p>
|
44 |
+
<p style='font-size:0.98rem;color:#c6e8fa'>
|
45 |
+
Upload or record a Persian audio file (max 30s) and instantly receive the transcription.</p>
|
46 |
</div>""")
|
47 |
|
48 |
with gr.Row():
|
49 |
with gr.Column():
|
50 |
+
audio = gr.Audio(label="Audio Input (Upload or record, up to 30s)",
|
51 |
+
type="filepath",
|
52 |
+
sources=["upload", "microphone"],
|
53 |
+
show_label=True)
|
|
|
|
|
54 |
with gr.Column():
|
55 |
+
inf_time_lbl = gr.Label(label="β±οΈ Processing Time", elem_classes="gooya-badge")
|
56 |
+
transcription = gr.Textbox(label="π Transcription",
|
57 |
+
lines=5,
|
58 |
+
show_copy_button=True,
|
59 |
+
placeholder="The transcription will appear here...",
|
60 |
+
elem_id="gooya-textbox")
|
|
|
|
|
61 |
|
62 |
with gr.Row():
|
63 |
+
submit_btn = gr.Button("Transcribe", variant="primary", interactive=False)
|
64 |
+
clear_btn = gr.Button("Clear", variant="secondary")
|
65 |
|
66 |
gr.Markdown("""
|
67 |
**Instructions:**
|
68 |
+
- Maximum audio length: **30 seconds**
|
69 |
+
- Input audio should be in Persian.
|
|
|
70 |
|
71 |
+
For benchmarks visit: [Persian ASR Leaderboard](https://huggingface.co/spaces/navidved/open_persian_asr_leaderboard)
|
72 |
""")
|
73 |
|
74 |
+
def toggle_btn(path):
|
75 |
+
return gr.Button.update(interactive=bool(path))
|
76 |
+
|
77 |
+
audio.change(toggle_btn, inputs=audio, outputs=submit_btn)
|
78 |
+
|
79 |
submit_btn.click(
|
80 |
transcribe_audio,
|
81 |
inputs=audio,
|
82 |
+
outputs=[transcription, inf_time_lbl]
|
83 |
+
).then(
|
84 |
+
lambda: gr.Button.update(interactive=False),
|
85 |
+
None,
|
86 |
+
submit_btn
|
87 |
)
|
88 |
+
|
89 |
clear_btn.click(
|
90 |
+
lambda: ("", "", None, False),
|
91 |
None,
|
92 |
+
[transcription, inf_time_lbl, audio, submit_btn],
|
93 |
)
|
94 |
|
95 |
+
demo.launch()
|