Update app.py
Browse files
app.py
CHANGED
@@ -4,23 +4,28 @@ import av
|
|
4 |
import wave
|
5 |
import requests
|
6 |
import io
|
|
|
7 |
|
8 |
-
st.
|
9 |
|
10 |
-
# β
Render API URL (
|
11 |
-
RENDER_API_URL = "https://saivahini.onrender.com/process_audio"
|
|
|
|
|
|
|
12 |
|
13 |
# β
WebRTC audio processing
|
14 |
def audio_frame_callback(frame):
|
|
|
15 |
audio = frame.to_ndarray(format="s16le")
|
16 |
audio_bytes = audio.tobytes()
|
|
|
|
|
17 |
st.session_state.frames.append(audio_bytes)
|
18 |
return av.AudioFrame.from_ndarray(audio, format="s16", layout="mono")
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
# β
WebRTC streamer for automatic audio capture
|
24 |
webrtc_streamer(
|
25 |
key="audio-recorder",
|
26 |
mode=WebRtcMode.SENDRECV,
|
@@ -28,34 +33,39 @@ webrtc_streamer(
|
|
28 |
media_stream_constraints={"audio": True, "video": False},
|
29 |
)
|
30 |
|
|
|
|
|
|
|
|
|
|
|
31 |
if st.button("β
Process Recorded Audio"):
|
32 |
-
if st.session_state.frames:
|
33 |
with st.spinner("π Processing your voice..."):
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
41 |
|
42 |
-
|
43 |
|
44 |
-
|
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
|
56 |
audio_response_url = result.get("audio")
|
57 |
if audio_response_url:
|
58 |
-
st.write(f"π
|
59 |
audio_response = requests.get(audio_response_url)
|
60 |
if audio_response.status_code == 200:
|
61 |
st.audio(audio_response.content, format="audio/wav")
|
|
|
4 |
import wave
|
5 |
import requests
|
6 |
import io
|
7 |
+
import numpy as np
|
8 |
|
9 |
+
st.set_page_config(page_title="Sai Vahini AI Assistant", layout="centered")
|
10 |
|
11 |
+
# β
Render API URL (Ensure this matches your deployed API on Render)
|
12 |
+
RENDER_API_URL = "https://saivahini.onrender.com/process_audio"
|
13 |
+
|
14 |
+
# β
UI Header
|
15 |
+
st.markdown("<h1 style='text-align: center; color: #ff5733;'>Sai Vahini AI Voice Assistant ποΈ</h1>", unsafe_allow_html=True)
|
16 |
|
17 |
# β
WebRTC audio processing
|
18 |
def audio_frame_callback(frame):
|
19 |
+
"""Handles incoming audio frames from WebRTC"""
|
20 |
audio = frame.to_ndarray(format="s16le")
|
21 |
audio_bytes = audio.tobytes()
|
22 |
+
if "frames" not in st.session_state:
|
23 |
+
st.session_state.frames = []
|
24 |
st.session_state.frames.append(audio_bytes)
|
25 |
return av.AudioFrame.from_ndarray(audio, format="s16", layout="mono")
|
26 |
|
27 |
+
# β
WebRTC Streamer for recording
|
28 |
+
st.write("π€ **Click below to start speaking...**")
|
|
|
|
|
29 |
webrtc_streamer(
|
30 |
key="audio-recorder",
|
31 |
mode=WebRtcMode.SENDRECV,
|
|
|
33 |
media_stream_constraints={"audio": True, "video": False},
|
34 |
)
|
35 |
|
36 |
+
# β
Check if audio frames exist
|
37 |
+
if "frames" not in st.session_state or not st.session_state.frames:
|
38 |
+
st.warning("β οΈ No audio recorded. Click the button above to start recording.")
|
39 |
+
|
40 |
+
# β
Process Button
|
41 |
if st.button("β
Process Recorded Audio"):
|
42 |
+
if "frames" in st.session_state and st.session_state.frames:
|
43 |
with st.spinner("π Processing your voice..."):
|
44 |
+
try:
|
45 |
+
# β
Convert recorded audio frames into WAV format
|
46 |
+
audio_bytes = io.BytesIO()
|
47 |
+
with wave.open(audio_bytes, "wb") as wf:
|
48 |
+
wf.setnchannels(1)
|
49 |
+
wf.setsampwidth(2)
|
50 |
+
wf.setframerate(16000)
|
51 |
+
wf.writeframes(b''.join(st.session_state.frames))
|
52 |
|
53 |
+
audio_bytes.seek(0) # Reset buffer pointer
|
54 |
|
55 |
+
# β
Send recorded audio to Render API
|
|
|
56 |
response = requests.post(RENDER_API_URL, files={"file": ("audio.wav", audio_bytes, "audio/wav")})
|
57 |
|
58 |
# β
Handle API response
|
59 |
if response.status_code == 200:
|
60 |
result = response.json()
|
61 |
st.success("β
AI Response:")
|
62 |
+
st.write("π **Transcription:**", result.get("transcription", "No transcription"))
|
63 |
+
st.write("π€ **Answer:**", result.get("response", "No response found."))
|
64 |
|
65 |
+
# β
Fetch and play AI-generated voice response
|
66 |
audio_response_url = result.get("audio")
|
67 |
if audio_response_url:
|
68 |
+
st.write(f"π **AI-generated voice response:**")
|
69 |
audio_response = requests.get(audio_response_url)
|
70 |
if audio_response.status_code == 200:
|
71 |
st.audio(audio_response.content, format="audio/wav")
|