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