Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,15 @@
|
|
1 |
-
import os
|
2 |
-
|
3 |
-
|
4 |
-
import streamlit as st
|
5 |
|
6 |
# ---------- Environment Variables ----------
|
7 |
ASR_API_URL = os.getenv("ASR_API_URL")
|
8 |
-
AUTH_TOKEN
|
9 |
-
|
10 |
if not ASR_API_URL or not AUTH_TOKEN:
|
11 |
-
|
12 |
|
13 |
# ---------- Core Transcription Function ----------
|
14 |
-
def transcribe_audio(
|
15 |
if not ASR_API_URL or not AUTH_TOKEN:
|
16 |
return "β Error: ASR_API_URL or AUTH_TOKEN is not set.", ""
|
17 |
|
@@ -22,8 +20,9 @@ def transcribe_audio(file_obj):
|
|
22 |
|
23 |
start = time.time()
|
24 |
try:
|
25 |
-
|
26 |
-
|
|
|
27 |
except Exception as e:
|
28 |
return f"β Error while calling ASR API: {e}", ""
|
29 |
|
@@ -34,55 +33,73 @@ def transcribe_audio(file_obj):
|
|
34 |
return text, f"{data.get('time', elapsed):.2f} s"
|
35 |
return f"β Error: {resp.status_code}, {resp.text}", ""
|
36 |
|
37 |
-
# ----------
|
38 |
-
|
39 |
-
|
40 |
-
VIOLET_MAIN = "#7F3FBF"
|
41 |
VIOLET_LIGHT = "#C3A6FF"
|
42 |
-
st.markdown(
|
43 |
-
f"""
|
44 |
-
<h1 style="background: linear-gradient(90deg, {VIOLET_MAIN}, {VIOLET_LIGHT}); color: white; padding: 20px; border-radius: 12px; text-align: center;">
|
45 |
-
Gooya ASR v1.4
|
46 |
-
</h1>
|
47 |
-
""",
|
48 |
-
unsafe_allow_html=True
|
49 |
-
)
|
50 |
-
|
51 |
-
tab1, tab2 = st.tabs(["π€ Record from Microphone", "π Upload Audio File"])
|
52 |
-
|
53 |
-
with tab1:
|
54 |
-
audio_file = st.audio_input("ποΈ Record audio from microphone")
|
55 |
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
with
|
61 |
-
|
62 |
-
with
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
- Maximum audio length: 30 seconds
|
84 |
-
- Audio content should be in Persian
|
85 |
-
- Both transcription and processing time
|
86 |
-
|
87 |
-
|
88 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os, time, requests, gradio as gr
|
2 |
+
|
3 |
+
print("Gradio version:", gr.version)
|
|
|
4 |
|
5 |
# ---------- Environment Variables ----------
|
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 |
|
|
|
20 |
|
21 |
start = time.time()
|
22 |
try:
|
23 |
+
with open(file_path, "rb") as f:
|
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 while calling ASR API: {e}", ""
|
28 |
|
|
|
33 |
return text, f"{data.get('time', elapsed):.2f} s"
|
34 |
return f"β Error: {resp.status_code}, {resp.text}", ""
|
35 |
|
36 |
+
# ---------- Styling ----------
|
37 |
+
VIOLET_MAIN = "#7F3FBF"
|
|
|
|
|
38 |
VIOLET_LIGHT = "#C3A6FF"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
audio_input = gr.Audio(
|
57 |
+
label="Audio Input (upload or record, up to 30 s)",
|
58 |
+
type="filepath",
|
59 |
+
sources=["upload", "microphone"],
|
60 |
+
)
|
61 |
+
with gr.Column():
|
62 |
+
processing_time_tb = gr.Textbox(
|
63 |
+
label="β±οΈ Processing Time",
|
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 |
+
btn_transcribe = gr.Button("Transcribe", variant="primary")
|
77 |
+
btn_clear = gr.Button("Clear", variant="secondary")
|
78 |
+
|
79 |
+
gr.Markdown(
|
80 |
+
"""
|
81 |
+
Guidelines
|
82 |
- Maximum audio length: 30 seconds
|
83 |
+
- Audio content should be in Persian.
|
84 |
+
- Both transcription and processing time are displayed immediately.
|
85 |
+
|
86 |
+
See the [Persian ASR Leaderboard](https://huggingface.co/spaces/navidved/open_persian_asr_leaderboard) for benchmarks.
|
87 |
+
"""
|
88 |
+
)
|
89 |
+
|
90 |
+
# ---------- Callbacks ----------
|
91 |
+
btn_transcribe.click(
|
92 |
+
fn=transcribe_audio,
|
93 |
+
inputs=[audio_input],
|
94 |
+
outputs=[transcription_tb, processing_time_tb],
|
95 |
+
)
|
96 |
+
|
97 |
+
btn_clear.click(
|
98 |
+
lambda: ("", "", None),
|
99 |
+
inputs=None,
|
100 |
+
outputs=[transcription_tb, processing_time_tb, audio_input],
|
101 |
+
)
|
102 |
+
|
103 |
+
# ---------- Launch ----------
|
104 |
+
if name == "main":
|
105 |
+
demo.queue().launch(debug=True, share=False)
|