Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,58 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
|
|
|
|
|
|
|
|
|
| 3 |
import io
|
| 4 |
|
| 5 |
# β
Set Streamlit Page Config
|
| 6 |
st.set_page_config(page_title="Sai Vahini AI Assistant", layout="centered")
|
| 7 |
|
| 8 |
-
# β
Render API URL (
|
| 9 |
RENDER_API_URL = "https://saivahini.onrender.com/process_audio"
|
| 10 |
|
| 11 |
# β
UI Header
|
| 12 |
st.markdown("<h1 style='text-align: center; color: #ff5733;'>Sai Vahini AI Voice Assistant ποΈ</h1>", unsafe_allow_html=True)
|
| 13 |
|
| 14 |
-
# β
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# β
Process Button
|
| 18 |
-
if st.button("β
Process Audio"):
|
| 19 |
-
if
|
| 20 |
with st.spinner("π Sending audio to AI model..."):
|
| 21 |
try:
|
| 22 |
# β
Send recorded audio to Render API
|
| 23 |
-
|
| 24 |
-
response = requests.post(RENDER_API_URL, files=files)
|
| 25 |
|
| 26 |
# β
Handle API response
|
| 27 |
if response.status_code == 200:
|
|
@@ -49,4 +80,4 @@ if st.button("β
Process Audio"):
|
|
| 49 |
st.error(f"β Failed to connect to API: {str(e)}")
|
| 50 |
|
| 51 |
else:
|
| 52 |
-
st.error("β οΈ
|
|
|
|
| 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")
|
| 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 |
+
# β
Audio recording parameters
|
| 18 |
+
DURATION = 5 # Recording duration in seconds
|
| 19 |
+
SAMPLE_RATE = 16000
|
| 20 |
+
|
| 21 |
+
# β
Function to record audio
|
| 22 |
+
def record_audio():
|
| 23 |
+
"""Records live audio and saves it as a WAV file"""
|
| 24 |
+
st.info("π€ Recording... Speak now!")
|
| 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 |
+
# β
Save the audio as a WAV file
|
| 30 |
+
audio_bytes = io.BytesIO()
|
| 31 |
+
with wave.open(audio_bytes, "wb") as wf:
|
| 32 |
+
wf.setnchannels(1)
|
| 33 |
+
wf.setsampwidth(2)
|
| 34 |
+
wf.setframerate(SAMPLE_RATE)
|
| 35 |
+
wf.writeframes(audio.tobytes())
|
| 36 |
+
|
| 37 |
+
audio_bytes.seek(0)
|
| 38 |
+
return audio_bytes
|
| 39 |
+
|
| 40 |
+
# β
Record button
|
| 41 |
+
if st.button("π€ Record Live Audio"):
|
| 42 |
+
audio_file = record_audio()
|
| 43 |
+
st.session_state["audio_data"] = audio_file
|
| 44 |
+
|
| 45 |
+
# β
Play recorded audio before sending
|
| 46 |
+
if "audio_data" in st.session_state:
|
| 47 |
+
st.audio(st.session_state["audio_data"], format="audio/wav")
|
| 48 |
|
| 49 |
# β
Process Button
|
| 50 |
+
if st.button("β
Process Recorded Audio"):
|
| 51 |
+
if "audio_data" in st.session_state:
|
| 52 |
with st.spinner("π Sending audio to AI model..."):
|
| 53 |
try:
|
| 54 |
# β
Send recorded audio to Render API
|
| 55 |
+
response = requests.post(RENDER_API_URL, files={"file": ("audio.wav", st.session_state["audio_data"], "audio/wav")})
|
|
|
|
| 56 |
|
| 57 |
# β
Handle API response
|
| 58 |
if response.status_code == 200:
|
|
|
|
| 80 |
st.error(f"β Failed to connect to API: {str(e)}")
|
| 81 |
|
| 82 |
else:
|
| 83 |
+
st.error("β οΈ No audio recorded. Click 'Record Live Audio' first!")
|