Callbot / app.py
eBlessings's picture
Update app.py
427e9b7 verified
raw
history blame
2.37 kB
import gradio as gr
import requests
import soundfile as sf
import tempfile
import os
# URL of your dedicated processing server (adjust port/path if needed)
SERVER_URL = "http://204.12.245.139:5000/process_audio"
def process_audio(audio):
"""
Process the input audio:
- Expects 'audio' as a tuple: (sample_rate, numpy_array)
- Writes the audio data to a temporary WAV file
- Sends the file via POST to your server endpoint
- Returns the transcription (or server response) as text
"""
if audio is None:
return "No audio provided. Please record something."
sample_rate, audio_data = audio
# Write the audio data to a temporary WAV file.
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
wav_path = tmp_file.name
sf.write(wav_path, audio_data, sample_rate)
try:
with open(wav_path, "rb") as f:
files = {"file": f}
response = requests.post(SERVER_URL, files=files, timeout=30)
if response.status_code == 200:
json_data = response.json()
# Expect server to return a key 'transcription' or 'response'
result = json_data.get("transcription") or json_data.get("response")
if not result:
result = "Server processed the audio, but did not return a result."
else:
result = f"Server error {response.status_code}: {response.text}"
except Exception as e:
result = f"Exception during processing: {e}"
finally:
os.remove(wav_path)
return result
# Create a Gradio interface.
# Note: We removed "source" from gr.Audio to ensure compatibility with your Gradio version.
iface = gr.Interface(
fn=process_audio,
inputs=gr.Audio(type="numpy", label="Record Your Voice"),
outputs=gr.Textbox(label="Server Response"),
title="Live AI Call Agent – Browser Mic Frontend",
description=(
"Record audio using your browser microphone. The audio will be sent to our dedicated server "
"for processing with GPU acceleration. Your server should return a transcription or generated response."
)
)
if __name__ == "__main__":
# Launch the Gradio app to listen on all interfaces. Hugging Face Spaces will assign a public URL.
iface.launch(server_name="0.0.0.0", server_port=7860)