Update app.py
Browse files
app.py
CHANGED
@@ -7,8 +7,10 @@ import io
|
|
7 |
|
8 |
st.title("Sai Vahini AI Voice Assistant ποΈ")
|
9 |
|
10 |
-
|
|
|
11 |
|
|
|
12 |
def audio_frame_callback(frame):
|
13 |
audio = frame.to_ndarray(format="s16le")
|
14 |
audio_bytes = audio.tobytes()
|
@@ -18,7 +20,7 @@ def audio_frame_callback(frame):
|
|
18 |
if "frames" not in st.session_state:
|
19 |
st.session_state.frames = []
|
20 |
|
21 |
-
# WebRTC streamer for automatic audio capture
|
22 |
webrtc_streamer(
|
23 |
key="audio-recorder",
|
24 |
mode=WebRtcMode.SENDRECV,
|
@@ -29,6 +31,7 @@ webrtc_streamer(
|
|
29 |
if st.button("β
Process Recorded Audio"):
|
30 |
if st.session_state.frames:
|
31 |
with st.spinner("π Processing your voice..."):
|
|
|
32 |
audio_bytes = io.BytesIO()
|
33 |
with wave.open(audio_bytes, "wb") as wf:
|
34 |
wf.setnchannels(1)
|
@@ -38,24 +41,37 @@ if st.button("β
Process Recorded Audio"):
|
|
38 |
|
39 |
audio_bytes.seek(0)
|
40 |
|
41 |
-
# Send to
|
42 |
-
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
49 |
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
st.audio(audio_content, format="audio/wav")
|
55 |
|
56 |
-
# Clear session state for new recording
|
57 |
-
st.session_state.frames = []
|
58 |
-
else:
|
59 |
-
st.error(f"β API Error: {response.status_code}")
|
60 |
else:
|
61 |
st.error("β οΈ No audio captured. Please record first!")
|
|
|
7 |
|
8 |
st.title("Sai Vahini AI Voice Assistant ποΈ")
|
9 |
|
10 |
+
# β
Render API URL (Make sure this matches your deployed API on Render)
|
11 |
+
RENDER_API_URL = "https://saivahini.onrender.com/process_audio" # Update if needed
|
12 |
|
13 |
+
# β
WebRTC audio processing
|
14 |
def audio_frame_callback(frame):
|
15 |
audio = frame.to_ndarray(format="s16le")
|
16 |
audio_bytes = audio.tobytes()
|
|
|
20 |
if "frames" not in st.session_state:
|
21 |
st.session_state.frames = []
|
22 |
|
23 |
+
# β
WebRTC streamer for automatic audio capture
|
24 |
webrtc_streamer(
|
25 |
key="audio-recorder",
|
26 |
mode=WebRtcMode.SENDRECV,
|
|
|
31 |
if st.button("β
Process Recorded Audio"):
|
32 |
if st.session_state.frames:
|
33 |
with st.spinner("π Processing your voice..."):
|
34 |
+
# β
Convert recorded audio frames into WAV format
|
35 |
audio_bytes = io.BytesIO()
|
36 |
with wave.open(audio_bytes, "wb") as wf:
|
37 |
wf.setnchannels(1)
|
|
|
41 |
|
42 |
audio_bytes.seek(0)
|
43 |
|
44 |
+
# β
Send recorded audio to Render API
|
45 |
+
try:
|
46 |
+
response = requests.post(RENDER_API_URL, files={"file": ("audio.wav", audio_bytes, "audio/wav")})
|
47 |
|
48 |
+
# β
Handle API response
|
49 |
+
if response.status_code == 200:
|
50 |
+
result = response.json()
|
51 |
+
st.success("β
AI Response:")
|
52 |
+
st.write("**Transcription:**", result.get("transcription", "No transcription"))
|
53 |
+
st.write("**Answer:**", result.get("response", "No response found."))
|
54 |
|
55 |
+
# β
Fetch and play AI-generated audio response
|
56 |
+
audio_response_url = result.get("audio")
|
57 |
+
if audio_response_url:
|
58 |
+
st.write(f"π Fetching AI-generated voice from: {audio_response_url}")
|
59 |
+
audio_response = requests.get(audio_response_url)
|
60 |
+
if audio_response.status_code == 200:
|
61 |
+
st.audio(audio_response.content, format="audio/wav")
|
62 |
+
else:
|
63 |
+
st.error(f"β Failed to load AI audio ({audio_response.status_code})")
|
64 |
+
else:
|
65 |
+
st.warning("β οΈ No audio response received from API.")
|
66 |
+
|
67 |
+
# β
Clear session state for new recording
|
68 |
+
st.session_state.frames = []
|
69 |
+
|
70 |
+
else:
|
71 |
+
st.error(f"β API Error: {response.status_code} - {response.text}")
|
72 |
|
73 |
+
except requests.exceptions.RequestException as e:
|
74 |
+
st.error(f"β Failed to connect to API: {str(e)}")
|
|
|
75 |
|
|
|
|
|
|
|
|
|
76 |
else:
|
77 |
st.error("β οΈ No audio captured. Please record first!")
|