Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,22 +4,22 @@ import soundfile as sf
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
# URL of your dedicated processing server
|
|
|
|
| 8 |
SERVER_URL = "http://204.12.245.139:5000/process_audio"
|
| 9 |
|
| 10 |
def process_audio(audio):
|
| 11 |
"""
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
- Returns the transcription (or server response) as text
|
| 17 |
"""
|
| 18 |
if audio is None:
|
| 19 |
return "No audio provided. Please record something."
|
| 20 |
|
| 21 |
sample_rate, audio_data = audio
|
| 22 |
-
# Write
|
| 23 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
|
| 24 |
wav_path = tmp_file.name
|
| 25 |
sf.write(wav_path, audio_data, sample_rate)
|
|
@@ -30,7 +30,7 @@ def process_audio(audio):
|
|
| 30 |
response = requests.post(SERVER_URL, files=files, timeout=30)
|
| 31 |
if response.status_code == 200:
|
| 32 |
json_data = response.json()
|
| 33 |
-
#
|
| 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."
|
|
@@ -40,22 +40,22 @@ def process_audio(audio):
|
|
| 40 |
result = f"Exception during processing: {e}"
|
| 41 |
finally:
|
| 42 |
os.remove(wav_path)
|
| 43 |
-
|
| 44 |
return result
|
| 45 |
|
| 46 |
# Create a Gradio interface.
|
| 47 |
-
# Note:
|
| 48 |
iface = gr.Interface(
|
| 49 |
fn=process_audio,
|
| 50 |
inputs=gr.Audio(type="numpy", label="Record Your Voice"),
|
| 51 |
outputs=gr.Textbox(label="Server Response"),
|
| 52 |
title="Live AI Call Agent – Browser Mic Frontend",
|
| 53 |
description=(
|
| 54 |
-
"Record audio using your browser microphone. The audio will be sent to our dedicated
|
| 55 |
-
"for processing with GPU acceleration. Your server should return a transcription or
|
|
|
|
| 56 |
)
|
| 57 |
)
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
-
# Launch the
|
| 61 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# URL of your dedicated processing server.
|
| 8 |
+
# Adjust the SERVER_URL (and port/endpoint) as needed.
|
| 9 |
SERVER_URL = "http://204.12.245.139:5000/process_audio"
|
| 10 |
|
| 11 |
def process_audio(audio):
|
| 12 |
"""
|
| 13 |
+
Receives audio from the browser (a tuple: (sample_rate, numpy_array)),
|
| 14 |
+
writes it to a temporary WAV file, then sends it in a POST request
|
| 15 |
+
to your GPU-accelerated server endpoint. Expects a JSON response with either
|
| 16 |
+
a 'transcription' or 'response' key.
|
|
|
|
| 17 |
"""
|
| 18 |
if audio is None:
|
| 19 |
return "No audio provided. Please record something."
|
| 20 |
|
| 21 |
sample_rate, audio_data = audio
|
| 22 |
+
# Write audio to a temporary WAV file
|
| 23 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
|
| 24 |
wav_path = tmp_file.name
|
| 25 |
sf.write(wav_path, audio_data, sample_rate)
|
|
|
|
| 30 |
response = requests.post(SERVER_URL, files=files, timeout=30)
|
| 31 |
if response.status_code == 200:
|
| 32 |
json_data = response.json()
|
| 33 |
+
# Try to retrieve 'transcription' then fallback to '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."
|
|
|
|
| 40 |
result = f"Exception during processing: {e}"
|
| 41 |
finally:
|
| 42 |
os.remove(wav_path)
|
|
|
|
| 43 |
return result
|
| 44 |
|
| 45 |
# Create a Gradio interface.
|
| 46 |
+
# Note: The "source" keyword is omitted because your installed Gradio version does not accept it.
|
| 47 |
iface = gr.Interface(
|
| 48 |
fn=process_audio,
|
| 49 |
inputs=gr.Audio(type="numpy", label="Record Your Voice"),
|
| 50 |
outputs=gr.Textbox(label="Server Response"),
|
| 51 |
title="Live AI Call Agent – Browser Mic Frontend",
|
| 52 |
description=(
|
| 53 |
+
"Record audio using your browser microphone. The audio will be sent to our dedicated "
|
| 54 |
+
"server for processing with GPU acceleration. Your server should return a transcription or "
|
| 55 |
+
"an AI-generated response."
|
| 56 |
)
|
| 57 |
)
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
+
# Launch the app so that it listens on all interfaces. Adjust the port if needed.
|
| 61 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|