Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,15 +4,16 @@ 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 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
"""
|
| 17 |
if audio is None:
|
| 18 |
return "No audio provided. Please record something."
|
|
@@ -26,11 +27,10 @@ def process_audio(audio):
|
|
| 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 |
-
#
|
| 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,21 +40,22 @@ def process_audio(audio):
|
|
| 40 |
result = f"Exception during processing: {e}"
|
| 41 |
finally:
|
| 42 |
os.remove(wav_path)
|
|
|
|
| 43 |
return result
|
| 44 |
|
| 45 |
-
# Create a Gradio interface
|
|
|
|
| 46 |
iface = gr.Interface(
|
| 47 |
fn=process_audio,
|
| 48 |
-
inputs=gr.Audio(
|
| 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 |
-
"
|
| 54 |
-
"an AI-generated response."
|
| 55 |
)
|
| 56 |
)
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
-
# Launch the app
|
| 60 |
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 (adjust port/path if needed)
|
|
|
|
| 8 |
SERVER_URL = "http://204.12.245.139:5000/process_audio"
|
| 9 |
|
| 10 |
def process_audio(audio):
|
| 11 |
"""
|
| 12 |
+
Process the input audio:
|
| 13 |
+
- Expects 'audio' as a tuple: (sample_rate, numpy_array)
|
| 14 |
+
- Writes the audio data to a temporary WAV file
|
| 15 |
+
- Sends the file via POST to your server endpoint
|
| 16 |
+
- Returns the transcription (or server response) as text
|
| 17 |
"""
|
| 18 |
if audio is None:
|
| 19 |
return "No audio provided. Please record something."
|
|
|
|
| 27 |
try:
|
| 28 |
with open(wav_path, "rb") as f:
|
| 29 |
files = {"file": f}
|
|
|
|
| 30 |
response = requests.post(SERVER_URL, files=files, timeout=30)
|
| 31 |
if response.status_code == 200:
|
| 32 |
json_data = response.json()
|
| 33 |
+
# Expect server to return a key 'transcription' or '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 |
+
|
| 44 |
return result
|
| 45 |
|
| 46 |
+
# Create a Gradio interface.
|
| 47 |
+
# Note: We removed "source" from gr.Audio to ensure compatibility with your Gradio version.
|
| 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 server "
|
| 55 |
+
"for processing with GPU acceleration. Your server should return a transcription or generated response."
|
|
|
|
| 56 |
)
|
| 57 |
)
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
+
# Launch the Gradio app to listen on all interfaces. Hugging Face Spaces will assign a public URL.
|
| 61 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|