import gradio as gr import requests, os, time import gradio_client print("--------- Gradio version:", gr.__version__, "----------") print("gradio-client version:", gradio_client.__version__) ASR_API_URL = os.getenv("ASR_API_URL") AUTH_TOKEN = os.getenv("AUTH_TOKEN") def transcribe_audio(file_path: str): """ دریافت مسیر فایل صوتی (type='filepath') و برگرداندن رونوشت و مدت زمان پردازش. """ 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}", } files = {"file": (file_path, open(file_path, "rb"), "audio/mpeg")} start = time.time() try: resp = requests.post(ASR_API_URL, headers=headers, files=files) except Exception as e: return f"❌ Error: {e}", "" elapsed = time.time() - start if resp.status_code == 200: data = resp.json() transcript = data.get("transcription", "No transcription returned.") t = f"{data.get('time', elapsed):.2f} ثانیه" return transcript, t else: return f"❌ Error: {resp.status_code}, {resp.text}", "" custom_css = """ #gooya-title{color:#fff; background:linear-gradient(90deg,#224CA5 0%,#2CD8D5 100%); border-radius:12px;padding:20px 10px;margin-bottom:12px;} .gooya-badge{display:inline-block;background:#224CA5;color:#fff; border-radius:16px;padding:6px 16px;font-size:.97rem;margin-top:4px;} #gooya-box{background:#F7FAFF;border:1px solid #e7e9ef;border-radius:14px; padding:22px 18px;margin-top:12px;} """ with gr.Blocks(css=custom_css, title="Gooya ASR") as demo: gr.HTML( """

Gooya ASR v1.4

High-performance Persian Speech-to-Text

Upload or record a Persian audio file (max 30 s) and instantly receive the transcription.

""" ) with gr.Row(): with gr.Column(): audio = gr.Audio( label="Audio Input (upload or record, up to 30 s)", type="filepath", sources=["upload", "microphone"], ) with gr.Column(): inference_time = gr.Label( label="⏱️ Processing Time", elem_classes="gooya-badge" ) transcription = 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( """ **دستورالعمل‌ها** - حداکثر طول صدا: **۳۰ ثانیه** - صدا باید فارسی باشد. - نتیجه‌ی رونویسی و زمان پردازش بلافاصله نمایش داده می‌شود. برای مشاهده بنچمارک‌ها به [Persian ASR Leaderboard](https://huggingface.co/spaces/navidved/open_persian_asr_leaderboard) مراجعه کنید. """ ) btn_transcribe.click( transcribe_audio, inputs=audio, outputs=[transcription, inference_time], queue=True ) # خروجی سوم (audio) هم باید reset شود، تا همه چیز مطابق gradio خوب پاک شود btn_clear.click( lambda: ("", "", None), inputs=None, outputs=[transcription, inference_time, audio] ) if __name__ == "__main__": print("==== Gooya ASR Demo Launching... ====") demo.queue(max_size=128) \ .launch(debug=True, share=False)