Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
-
import numpy as np
|
4 |
-
import sounddevice as sd
|
5 |
-
import wave
|
6 |
import io
|
|
|
7 |
|
8 |
# β
Set Streamlit Page Config
|
9 |
st.set_page_config(page_title="Sai Vahini AI Assistant", layout="centered")
|
@@ -14,70 +12,81 @@ RENDER_API_URL = "https://saivahini.onrender.com/process_audio"
|
|
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 |
-
# β
Audio
|
18 |
-
|
19 |
-
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
audio = sd.rec(int(DURATION * SAMPLE_RATE), samplerate=SAMPLE_RATE, channels=1, dtype=np.int16)
|
26 |
-
sd.wait() # Wait until recording is finished
|
27 |
-
st.success("β
Recording completed!")
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
wf.setnchannels(1)
|
33 |
-
wf.setsampwidth(2)
|
34 |
-
wf.setframerate(SAMPLE_RATE)
|
35 |
-
wf.writeframes(audio.tobytes())
|
36 |
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
# β
|
46 |
-
|
47 |
-
st.audio(st.session_state["audio_data"], format="audio/wav")
|
48 |
|
49 |
# β
Process Button
|
50 |
if st.button("β
Process Recorded Audio"):
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
result = response.json()
|
60 |
-
st.success("β
AI Response:")
|
61 |
-
st.write("π **Transcription:**", result.get("transcription", "No transcription"))
|
62 |
-
st.write("π€ **Answer:**", result.get("response", "No response found."))
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
st.audio(audio_response.content, format="audio/wav")
|
71 |
-
else:
|
72 |
-
st.error(f"β Failed to load AI audio ({audio_response.status_code})")
|
73 |
-
else:
|
74 |
-
st.warning("β οΈ No audio response received from API.")
|
75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
else:
|
77 |
-
st.
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
else:
|
83 |
-
st.error("β οΈ No audio recorded. Click 'Record Live Audio' first!")
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
|
|
|
|
|
|
3 |
import io
|
4 |
+
import base64
|
5 |
|
6 |
# β
Set Streamlit Page Config
|
7 |
st.set_page_config(page_title="Sai Vahini AI Assistant", layout="centered")
|
|
|
12 |
# β
UI Header
|
13 |
st.markdown("<h1 style='text-align: center; color: #ff5733;'>Sai Vahini AI Voice Assistant ποΈ</h1>", unsafe_allow_html=True)
|
14 |
|
15 |
+
# β
HTML5 Audio Recorder (JavaScript + Streamlit)
|
16 |
+
audio_recorder_html = """
|
17 |
+
<script>
|
18 |
+
let mediaRecorder;
|
19 |
+
let audioChunks = [];
|
20 |
|
21 |
+
function startRecording() {
|
22 |
+
navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
|
23 |
+
mediaRecorder = new MediaRecorder(stream);
|
24 |
+
mediaRecorder.start();
|
|
|
|
|
|
|
25 |
|
26 |
+
mediaRecorder.ondataavailable = event => {
|
27 |
+
audioChunks.push(event.data);
|
28 |
+
};
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
mediaRecorder.onstop = () => {
|
31 |
+
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
|
32 |
+
const reader = new FileReader();
|
33 |
+
reader.readAsDataURL(audioBlob);
|
34 |
+
reader.onloadend = () => {
|
35 |
+
fetch("/upload_audio", {
|
36 |
+
method: "POST",
|
37 |
+
headers: { "Content-Type": "application/json" },
|
38 |
+
body: JSON.stringify({ audio: reader.result })
|
39 |
+
}).then(response => response.json()).then(data => {
|
40 |
+
document.getElementById("audio_url").value = data.audio_url;
|
41 |
+
});
|
42 |
+
};
|
43 |
+
};
|
44 |
+
});
|
45 |
+
}
|
46 |
|
47 |
+
function stopRecording() {
|
48 |
+
mediaRecorder.stop();
|
49 |
+
}
|
50 |
+
</script>
|
51 |
+
<button onclick="startRecording()">π€ Start Recording</button>
|
52 |
+
<button onclick="stopRecording()">βΉ Stop Recording</button>
|
53 |
+
<input type="hidden" id="audio_url">
|
54 |
+
"""
|
55 |
|
56 |
+
# β
Display HTML5 Recorder
|
57 |
+
st.components.v1.html(audio_recorder_html, height=150)
|
|
|
58 |
|
59 |
# β
Process Button
|
60 |
if st.button("β
Process Recorded Audio"):
|
61 |
+
with st.spinner("π Sending audio to AI model..."):
|
62 |
+
audio_url = st.session_state.get("audio_url", None)
|
63 |
+
if audio_url:
|
64 |
+
# Convert Base64 audio to WAV format
|
65 |
+
audio_data = base64.b64decode(audio_url.split(",")[1])
|
66 |
+
audio_bytes = io.BytesIO(audio_data)
|
67 |
|
68 |
+
# β
Send recorded audio to Render API
|
69 |
+
response = requests.post(RENDER_API_URL, files={"file": ("audio.wav", audio_bytes, "audio/wav")})
|
|
|
|
|
|
|
|
|
70 |
|
71 |
+
# β
Handle API response
|
72 |
+
if response.status_code == 200:
|
73 |
+
result = response.json()
|
74 |
+
st.success("β
AI Response:")
|
75 |
+
st.write("π **Transcription:**", result.get("transcription", "No transcription"))
|
76 |
+
st.write("π€ **Answer:**", result.get("response", "No response found."))
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
+
# β
Fetch and play AI-generated voice response
|
79 |
+
audio_response_url = result.get("audio")
|
80 |
+
if audio_response_url:
|
81 |
+
st.write("π **AI-generated voice response:**")
|
82 |
+
audio_response = requests.get(audio_response_url)
|
83 |
+
if audio_response.status_code == 200:
|
84 |
+
st.audio(audio_response.content, format="audio/wav")
|
85 |
+
else:
|
86 |
+
st.error(f"β Failed to load AI audio ({audio_response.status_code})")
|
87 |
else:
|
88 |
+
st.warning("β οΈ No audio response received from API.")
|
89 |
+
else:
|
90 |
+
st.error(f"β API Error: {response.status_code} - {response.text}")
|
91 |
+
else:
|
92 |
+
st.error("β οΈ No audio recorded. Please record first!")
|
|
|
|