Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,60 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import soundfile as sf
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# URL of your dedicated processing server.
|
| 8 |
+
# Adjust the port or endpoint path if needed.
|
| 9 |
+
SERVER_URL = "http://204.12.245.139:5000/process_audio"
|
| 10 |
+
|
| 11 |
+
def process_audio(audio):
|
| 12 |
+
"""
|
| 13 |
+
Receives audio input from the browser (as a tuple: (sample_rate, numpy_array)),
|
| 14 |
+
writes it to a temporary WAV file, then sends the file to your dedicated server.
|
| 15 |
+
Expects a JSON response with a key 'transcription' or 'response' that is returned.
|
| 16 |
+
"""
|
| 17 |
+
if audio is None:
|
| 18 |
+
return "No audio provided. Please record something."
|
| 19 |
+
|
| 20 |
+
sample_rate, audio_data = audio
|
| 21 |
+
# Write the audio data to a temporary WAV file.
|
| 22 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
|
| 23 |
+
wav_path = tmp_file.name
|
| 24 |
+
sf.write(wav_path, audio_data, sample_rate)
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
with open(wav_path, "rb") as f:
|
| 28 |
+
files = {"file": f}
|
| 29 |
+
# Timeout can be adjusted as needed.
|
| 30 |
+
response = requests.post(SERVER_URL, files=files, timeout=30)
|
| 31 |
+
if response.status_code == 200:
|
| 32 |
+
json_data = response.json()
|
| 33 |
+
# Try to get a 'transcription' key first, then 'response'
|
| 34 |
+
result = json_data.get("transcription") or json_data.get("response")
|
| 35 |
+
if not result:
|
| 36 |
+
result = "Server processed the audio, but did not return a result."
|
| 37 |
+
else:
|
| 38 |
+
result = f"Server error {response.status_code}: {response.text}"
|
| 39 |
+
except Exception as e:
|
| 40 |
+
result = f"Exception during processing: {e}"
|
| 41 |
+
finally:
|
| 42 |
+
os.remove(wav_path)
|
| 43 |
+
return result
|
| 44 |
+
|
| 45 |
+
# Create a Gradio interface that captures audio via the browser microphone.
|
| 46 |
+
iface = gr.Interface(
|
| 47 |
+
fn=process_audio,
|
| 48 |
+
inputs=gr.Audio(source="microphone", type="numpy", label="Record Your Voice"),
|
| 49 |
+
outputs=gr.Textbox(label="Server Response"),
|
| 50 |
+
title="Live AI Call Agent – Browser Mic Frontend",
|
| 51 |
+
description=(
|
| 52 |
+
"Record audio using your browser microphone. The audio will be sent to our dedicated "
|
| 53 |
+
"server for processing with GPU acceleration. Your server should return a transcription or "
|
| 54 |
+
"an AI-generated response."
|
| 55 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
|
|
|
| 58 |
if __name__ == "__main__":
|
| 59 |
+
# Launch the app so that it listens on all interfaces. Hugging Face Spaces uses these settings.
|
| 60 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|