import os import time from pathlib import Path import gradio as gr import requests import spaces ASSEMBLYAI_API = "https://api.assemblyai.com/v2" MAX_FILE_MB = 500 def _headers(api_key: str) -> dict: return { "authorization": api_key.strip(), "content-type": "application/json", } def transcribe_audio(api_key: str, media_file: str): """Upload an audio/video file to AssemblyAI and poll for its transcript.""" if not api_key or not api_key.strip(): raise gr.Error("Please paste your AssemblyAI API key first.") if not media_file: raise gr.Error("Please upload an MP4, MP3, WAV, M4A, or WEBM file.") file_path = Path(media_file) if not file_path.exists(): raise gr.Error("The uploaded file could not be found. Please upload it again.") if file_path.stat().st_size > MAX_FILE_MB * 1024 * 1024: raise gr.Error(f"Please keep files under {MAX_FILE_MB} MB for this demo.") try: with file_path.open("rb") as audio: upload = requests.post( f"{ASSEMBLYAI_API}/upload", headers={"authorization": api_key.strip()}, data=audio, timeout=300, ) upload.raise_for_status() audio_url = upload.json()["upload_url"] payload = { "audio_url": audio_url, "language_detection": True, "punctuate": True, "format_text": True, } created = requests.post( f"{ASSEMBLYAI_API}/transcript", headers=_headers(api_key), json=payload, timeout=60, ) created.raise_for_status() transcript_id = created.json()["id"] while True: result = requests.get( f"{ASSEMBLYAI_API}/transcript/{transcript_id}", headers=_headers(api_key), timeout=60, ) result.raise_for_status() data = result.json() status = data.get("status") if status == "completed": text = data.get("text") or "No speech was detected in this file." language = data.get("language_code") or "auto-detected" return text, f"Completed • detected language: {language}", text if status == "error": raise gr.Error(data.get("error") or "AssemblyAI could not transcribe this file.") time.sleep(3) except requests.HTTPError as exc: detail = exc.response.text[:500] if exc.response is not None else str(exc) raise gr.Error(f"AssemblyAI request failed: {detail}") from exc except requests.RequestException as exc: raise gr.Error(f"Network error while contacting AssemblyAI: {exc}") from exc def clear_all(): return "", None, "Ready when you are.", "", None custom_css = """ :root { --bg: #080b16; --panel: #121a29; --panel-2: #1b2738; --muted: #aab3c5; --purple: #7558ff; --purple-2: #5444d7; } body, .gradio-container { background: radial-gradient(circle at 50% -10%, #24315b 0%, var(--bg) 42%); color: #f7f8ff; } .gradio-container { max-width: 980px !important; padding: 28px 18px 40px !important; } .hero { text-align: center; padding: 10px 0 22px; } .hero h1 { font-size: 2.2rem; margin: 0 0 8px; letter-spacing: -0.03em; } .hero p { color: var(--muted); margin: 0; } .app-card { background: rgba(18, 26, 41, .94); border: 1px solid #26344a; border-radius: 22px; padding: 24px; box-shadow: 0 20px 70px rgba(0,0,0,.35); } .section-label { display: inline-block; background: linear-gradient(135deg,var(--purple),var(--purple-2)); border-radius: 8px; padding: 7px 11px; font-weight: 700; margin: 5px 0 10px; } .note { color: var(--muted); font-size: .9rem; margin: 0 0 14px; } textarea, input { background: #263346 !important; color: #fff !important; border: 1px solid #3a4a63 !important; } .upload-box { background: #1b2a3b; border: 1px dashed #60708a; border-radius: 14px; min-height: 150px; } button.primary { background: linear-gradient(135deg,var(--purple),#5a45d4) !important; border: none !important; color: white !important; font-weight: 800 !important; } button.secondary { background: #293548 !important; color: #eef !important; border: 1px solid #3b4a60 !important; } .status { color: #bfc8da; } footer { text-align:center; color:#76839a; font-size:.8rem; margin-top:16px; } """ with gr.Blocks(title="Transcript Studio", theme=gr.themes.Base(), css=custom_css) as demo: gr.HTML( """
Turn your MP4 or MP3 into clean, readable text in seconds.