import gradio as gr import requests import os def send_audio(audio): url = os.getenv("TRANSLATION_URL") if audio is None: return {"error": "No audio file provided"} try: if isinstance(audio, bytes): # If audio is a bytes object, save it to a temporary file with open("temp_audio.wav", "wb") as f: f.write(audio) audio_path = "temp_audio.wav" else: # If audio is already a file path audio_path = audio # Prepare form data for request files = { 'file': open(audio_path, 'rb'), } data = { 'lang': 'english-twi' } response = requests.post(url, files=files, data=data) response_json = response.json() return response_json # Extract the translation part translation = response_json.get('data', {}).get('translation', 'No translation available') return translation except Exception as e: return {"error": str(e)} # Gradio interface for recording speech iface = gr.Interface( fn=send_audio, inputs=gr.Audio(type="filepath"), # Expect a file path or file-like object outputs="text", title="English-Twi Speech Translation", description="Record speech and send for processing.", ) iface.launch()