Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import streamlit as st
|
2 |
-
from streamlit_webrtc import webrtc_streamer
|
3 |
import requests
|
4 |
import av
|
5 |
import numpy as np
|
@@ -8,9 +8,12 @@ import io
|
|
8 |
|
9 |
st.title("Sai Vahini AI Voice Assistant ποΈ")
|
10 |
|
11 |
-
|
|
|
12 |
|
13 |
-
|
|
|
|
|
14 |
def save_audio(frames, sample_rate=16000):
|
15 |
audio_bytes = io.BytesIO()
|
16 |
wf = wave.open(audio_bytes, 'wb')
|
@@ -22,38 +25,42 @@ def save_audio(frames, sample_rate=16000):
|
|
22 |
audio_bytes.seek(0)
|
23 |
return audio_bytes
|
24 |
|
25 |
-
#
|
26 |
def audio_frame_callback(frame):
|
27 |
audio = frame.to_ndarray(format="s16le")
|
28 |
frames.append(audio.tobytes())
|
29 |
return av.AudioFrame.from_ndarray(audio, format="s16", layout="mono")
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
# UI to record voice input
|
34 |
if st.button("ποΈ Record your voice"):
|
35 |
frames.clear()
|
36 |
-
webrtc_streamer(
|
|
|
|
|
|
|
|
|
|
|
37 |
|
|
|
38 |
if st.button("β
Stop & Process Audio"):
|
39 |
if frames:
|
40 |
-
st.
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
else:
|
59 |
-
st.error("β οΈ No audio captured. Please record again.")
|
|
|
1 |
import streamlit as st
|
2 |
+
from streamlit_webrtc import webrtc_streamer, WebRtcMode
|
3 |
import requests
|
4 |
import av
|
5 |
import numpy as np
|
|
|
8 |
|
9 |
st.title("Sai Vahini AI Voice Assistant ποΈ")
|
10 |
|
11 |
+
# Replace with your actual Render API endpoint
|
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')
|
|
|
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 |
frames.append(audio.tobytes())
|
32 |
return av.AudioFrame.from_ndarray(audio, format="s16", layout="mono")
|
33 |
|
34 |
+
# Button to start recording audio
|
|
|
|
|
35 |
if st.button("ποΈ Record your voice"):
|
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 |
+
# Button to stop recording and process audio
|
45 |
if st.button("β
Stop & Process Audio"):
|
46 |
if frames:
|
47 |
+
with st.spinner("π Processing your voice..."):
|
48 |
+
audio_file = save_audio(frames)
|
49 |
+
|
50 |
+
response = requests.post(api_url, files={"file": ("audio.wav", audio_file, "audio/wav")})
|
51 |
+
|
52 |
+
if response.status_code == 200:
|
53 |
+
result = response.json()
|
54 |
+
st.success("β
AI Response:")
|
55 |
+
st.write("**Transcription:**", result["transcription"])
|
56 |
+
st.write("**Answer:**", result["response"])
|
57 |
+
|
58 |
+
audio_response_url = result.get("audio")
|
59 |
+
|
60 |
+
# If the API returns a URL, fetch and play the audio directly
|
61 |
+
audio_content = requests.get(audio_response_url).content
|
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 again.")
|