gooya-asr / app.py
navidved's picture
Update app.py
f118118 verified
raw
history blame
3.41 kB
import os, time, requests, gradio as gr
print("Gradio version:", gr.version)
# ---------- Environment Variables ----------
ASR_API_URL = os.getenv("ASR_API_URL")
AUTH_TOKEN = os.getenv("AUTH_TOKEN")
if not ASR_API_URL or not AUTH_TOKEN:
print("⚠️ ASR_API_URL or AUTH_TOKEN is not set; API calls will fail.")
# ---------- Core Transcription Function ----------
def transcribe_audio(file_path: str):
if not ASR_API_URL or not AUTH_TOKEN:
return "❌ Error: ASR_API_URL or AUTH_TOKEN is not set.", ""
headers = {
"accept": "application/json",
"Authorization": f"Bearer {AUTH_TOKEN}",
}
start = time.time()
try:
with open(file_path, "rb") as f:
files = {"file": (os.path.basename(file_path), f, "audio/mpeg")}
resp = requests.post(ASR_API_URL, headers=headers, files=files, timeout=120)
except Exception as e:
return f"❌ Error while calling ASR API: {e}", ""
elapsed = time.time() - start
if resp.status_code == 200:
data = resp.json()
text = data.get("transcription", "No transcription returned.")
return text, f"{data.get('time', elapsed):.2f} s"
return f"❌ Error: {resp.status_code}, {resp.text}", ""
# ---------- Styling ----------
VIOLET_MAIN = "#7F3FBF"
VIOLET_LIGHT = "#C3A6FF"
custom_css = f"""
#gooya-title {{
color:#fff;
background:linear-gradient(90deg,{VIOLET_MAIN} 0%,{VIOLET_LIGHT} 100%);
border-radius:12px;padding:20px 10px;margin-bottom:12px;
}}
.gooya-badge {{
display:inline-block;background:{VIOLET_MAIN};color:#fff;
border-radius:16px;padding:6px 16px;font-size:.97rem;margin-top:4px;
}}
"""
# ---------- UI ----------
with gr.Blocks(css=custom_css, title="Gooya ASR v1.4") as demo:
with gr.Row():
with gr.Column():
audio_input = gr.Audio(
label="Audio Input (upload or record, up to 30 s)",
type="filepath",
sources=["upload", "microphone"],
)
with gr.Column():
processing_time_tb = gr.Textbox(
label="⏱️ Processing Time",
interactive=False,
elem_classes="gooya-badge",
)
transcription_tb = gr.Textbox(
label="πŸ“ Transcription",
lines=5,
show_copy_button=True,
placeholder="The transcription will appear here...",
elem_id="gooya-textbox",
)
with gr.Row():
btn_transcribe = gr.Button("Transcribe", variant="primary")
btn_clear = gr.Button("Clear", variant="secondary")
gr.Markdown(
"""
Guidelines
- Maximum audio length: 30 seconds
- Audio content should be in Persian.
- Both transcription and processing time are displayed immediately.
See the [Persian ASR Leaderboard](https://huggingface.co/spaces/navidved/open_persian_asr_leaderboard) for benchmarks.
"""
)
# ---------- Callbacks ----------
btn_transcribe.click(
fn=transcribe_audio,
inputs=[audio_input],
outputs=[transcription_tb, processing_time_tb],
)
btn_clear.click(
lambda: ("", "", None),
inputs=None,
outputs=[transcription_tb, processing_time_tb, audio_input],
)
# ---------- Launch ----------
if name == "main":
demo.queue().launch(debug=True, share=False)