Spaces:
Runtime error
Runtime error
| #pip install gradio==4.43.0 | |
| #pip install gradio-client==1.3.0 | |
| #pip install huggingface_hub>=0.24.0 | |
| import os | |
| import gradio as gr | |
| from gradio_client import Client, handle_file | |
| import zipfile | |
| import time | |
| BACKEND_URL = os.getenv("BACKEND_URL") | |
| HF_TOKEN = os.environ["HF_TOKEN"] # set in Space Secrets | |
| # Client is created on the server; token never goes to the browser. | |
| client = Client(BACKEND_URL, hf_token=HF_TOKEN) | |
| def proxy_transcribe(audio_path): | |
| if not audio_path: | |
| return "No audio provided.", None, None | |
| try: | |
| # Send file server-side to the private backend | |
| out = client.predict(audio_path=handle_file(audio_path), api_name="/transcribe") | |
| transcript = out[0] if isinstance(out, (list, tuple)) else out | |
| # Create zip | |
| timestamp = int(time.time()) | |
| zip_path = f"/tmp/results_{timestamp}.zip" | |
| with zipfile.ZipFile(zip_path, "w") as zf: | |
| zf.write(audio_path, arcname=os.path.basename(audio_path)) | |
| transcript_path = audio_path.rsplit(".", 1)[0] + ".txt" | |
| with open(transcript_path, "w", encoding="utf-8") as f: | |
| f.write(transcript) | |
| zf.write(transcript_path, arcname=os.path.basename(transcript_path)) | |
| return transcript, audio_path, zip_path | |
| except Exception as e: | |
| # Never echo secrets; keep errors generic | |
| return f"Backend error: {type(e).__name__}",None ,audio_path | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## ASR Demo (frontend proxy)") | |
| a = gr.Audio(sources=["upload", "microphone"], type="filepath", label="Audio") | |
| t = gr.Textbox(label="Transcription",lines=7, interactive=False) #,elem_classes="auto-height") | |
| audio_file_output = gr.File(label="Download Audio") | |
| zip_output = gr.File(label="Download ZIP") | |
| a.change(proxy_transcribe, inputs=a, outputs=[t,audio_file_output, zip_output]) | |
| # demo.css = """ | |
| # .auto-height textarea { | |
| # height: auto !important; | |
| # min-height: 40px; | |
| # max-height: 150px; | |
| # overflow-y: auto; | |
| # } | |
| # """ | |
| # demo.queue(concurrency_count=2, max_size=8) | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860, share=True) | |