import gradio as gr import requests import os import time ASR_API_URL = os.getenv('ASR_API_URL') AUTH_TOKEN = os.getenv('AUTH_TOKEN') def transcribe_audio(file_path): # بررسی اینکه فایل وجود دارد if file_path is None: return "❌ Error: Please upload or record an audio file.", "" 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.time() try: # استفاده از with برای مدیریت بهتر فایل with open(file_path, 'rb') as audio_file: files = { 'file': (os.path.basename(file_path), audio_file, 'audio/mpeg'), } response = requests.post(ASR_API_URL, headers=headers, files=files, timeout=60) except FileNotFoundError: return f"❌ Error: File not found: {file_path}", "" except requests.exceptions.Timeout: return "❌ Error: Request timeout. Please try again.", "" except requests.exceptions.ConnectionError: return "❌ Error: Connection failed. Please check your internet connection.", "" except Exception as e: return f"❌ Error: {str(e)}", "" inference_time = time.time() - start_time if response.status_code == 200: try: res = response.json() transcription = res.get("transcription", "No transcription returned.") inference_time_str = f"{res.get('time', inference_time):.2f} seconds" return transcription, inference_time_str except ValueError: return "❌ Error: Invalid response format from server.", "" else: error_msg = f"❌ Error: {response.status_code}" try: error_detail = response.json().get('detail', response.text) error_msg += f", {error_detail}" except: error_msg += f", {response.text[:200]}" # محدود کردن طول پیام خطا return error_msg, "" with gr.Blocks(css=""" #gooya-title {color:white; 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:0.97rem; margin-top:4px;} #gooya-box {background:#F7FAFF; border:1px solid #e7e9ef; border-radius:14px; padding:22px 18px; margin-top:12px;} """) as demo: gr.HTML("""
High-performance Persian Speech-to-Text
Upload or record a Persian audio file (max 30s) and instantly receive the transcription.