Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,45 @@
|
|
1 |
import streamlit as st
|
2 |
from streamlit_webrtc import webrtc_streamer, WebRtcMode
|
3 |
-
import requests
|
4 |
import av
|
5 |
-
import numpy as np
|
6 |
import wave
|
|
|
7 |
import io
|
8 |
|
9 |
st.title("Sai Vahini AI Voice Assistant ποΈ")
|
10 |
|
11 |
-
# Replace
|
12 |
-
api_url = "https://saivahini.onrender.com/process_audio"
|
13 |
-
|
14 |
-
frames = []
|
15 |
-
|
16 |
-
# Function to save audio as WAV file in memory
|
17 |
-
def save_audio(frames, sample_rate=16000):
|
18 |
-
audio_bytes = io.BytesIO()
|
19 |
-
wf = wave.open(audio_bytes, 'wb')
|
20 |
-
wf.setnchannels(1)
|
21 |
-
wf.setsampwidth(2)
|
22 |
-
wf.setframerate(sample_rate)
|
23 |
-
wf.writeframes(b''.join(frames))
|
24 |
-
wf.close()
|
25 |
-
audio_bytes.seek(0)
|
26 |
-
return audio_bytes
|
27 |
|
28 |
-
# Callback to handle audio frames
|
29 |
def audio_frame_callback(frame):
|
30 |
audio = frame.to_ndarray(format="s16le")
|
31 |
-
|
|
|
32 |
return av.AudioFrame.from_ndarray(audio, format="s16", layout="mono")
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
frames.clear()
|
37 |
-
webrtc_streamer(
|
38 |
-
key="audio-recorder",
|
39 |
-
mode=WebRtcMode.SENDRECV,
|
40 |
-
audio_frame_callback=audio_frame_callback,
|
41 |
-
media_stream_constraints={"audio": True, "video": False}
|
42 |
-
)
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
with st.spinner("π Processing your voice..."):
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
|
|
|
51 |
|
52 |
if response.status_code == 200:
|
53 |
result = response.json()
|
@@ -55,12 +47,15 @@ if st.button("β
Stop & Process Audio"):
|
|
55 |
st.write("**Transcription:**", result["transcription"])
|
56 |
st.write("**Answer:**", result["response"])
|
57 |
|
58 |
-
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
#
|
61 |
-
|
62 |
-
st.audio(audio_content, format='audio/wav')
|
63 |
else:
|
64 |
st.error(f"β API Error: {response.status_code}")
|
65 |
else:
|
66 |
-
st.error("β οΈ No audio captured. Please record
|
|
|
1 |
import streamlit as st
|
2 |
from streamlit_webrtc import webrtc_streamer, WebRtcMode
|
|
|
3 |
import av
|
|
|
4 |
import wave
|
5 |
+
import requests
|
6 |
import io
|
7 |
|
8 |
st.title("Sai Vahini AI Voice Assistant ποΈ")
|
9 |
|
10 |
+
api_url = "https://saivahini.onrender.com/process_audio" # Replace if needed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
|
|
12 |
def audio_frame_callback(frame):
|
13 |
audio = frame.to_ndarray(format="s16le")
|
14 |
+
audio_bytes = audio.tobytes()
|
15 |
+
st.session_state.frames.append(audio_bytes)
|
16 |
return av.AudioFrame.from_ndarray(audio, format="s16", layout="mono")
|
17 |
|
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,
|
25 |
+
audio_frame_callback=audio_frame_callback,
|
26 |
+
media_stream_constraints={"audio": True, "video": False},
|
27 |
+
)
|
28 |
+
|
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)
|
35 |
+
wf.setsampwidth(2)
|
36 |
+
wf.setframerate(16000)
|
37 |
+
wf.writeframes(b''.join(st.session_state.frames))
|
38 |
+
|
39 |
+
audio_bytes.seek(0)
|
40 |
|
41 |
+
# Send to your API
|
42 |
+
response = requests.post(api_url, files={"file": ("audio.wav", audio_bytes, "audio/wav")})
|
43 |
|
44 |
if response.status_code == 200:
|
45 |
result = response.json()
|
|
|
47 |
st.write("**Transcription:**", result["transcription"])
|
48 |
st.write("**Answer:**", result["response"])
|
49 |
|
50 |
+
audio_response = result["audio"]
|
51 |
+
|
52 |
+
# Fetch and play audio response
|
53 |
+
audio_content = requests.get(audio_response).content
|
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!")
|