Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,27 @@
|
|
| 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
|
| 9 |
st.set_page_config(page_title="Sai Vahini AI Assistant", layout="centered")
|
| 10 |
|
| 11 |
-
# β
Render API URL (
|
| 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
|
| 18 |
-
|
| 19 |
-
SAMPLE_RATE = 16000
|
| 20 |
-
|
| 21 |
-
# β
Function to record audio
|
| 22 |
-
def record_audio():
|
| 23 |
-
st.info("π€ Recording... Speak now!")
|
| 24 |
-
audio = sd.rec(int(DURATION * SAMPLE_RATE), samplerate=SAMPLE_RATE, channels=1, dtype=np.int16)
|
| 25 |
-
sd.wait() # Wait until recording is finished
|
| 26 |
-
st.success("β
Recording completed!")
|
| 27 |
-
|
| 28 |
-
# β
Save the audio as a WAV file
|
| 29 |
-
audio_bytes = io.BytesIO()
|
| 30 |
-
with wave.open(audio_bytes, "wb") as wf:
|
| 31 |
-
wf.setnchannels(1)
|
| 32 |
-
wf.setsampwidth(2)
|
| 33 |
-
wf.setframerate(SAMPLE_RATE)
|
| 34 |
-
wf.writeframes(audio.tobytes())
|
| 35 |
-
|
| 36 |
-
audio_bytes.seek(0)
|
| 37 |
-
return audio_bytes
|
| 38 |
-
|
| 39 |
-
# β
Record button
|
| 40 |
-
if st.button("π€ Record Audio"):
|
| 41 |
-
audio_file = record_audio()
|
| 42 |
-
st.session_state["audio_data"] = audio_file
|
| 43 |
-
|
| 44 |
-
# β
Check if audio was recorded
|
| 45 |
-
if "audio_data" in st.session_state:
|
| 46 |
-
st.audio(st.session_state["audio_data"], format="audio/wav")
|
| 47 |
|
| 48 |
# β
Process Button
|
| 49 |
-
if st.button("β
Process
|
| 50 |
-
if
|
| 51 |
-
with st.spinner("π
|
| 52 |
try:
|
| 53 |
# β
Send recorded audio to Render API
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
# β
Handle API response
|
| 57 |
if response.status_code == 200:
|
|
@@ -63,7 +33,7 @@ if st.button("β
Process Recorded Audio"):
|
|
| 63 |
# β
Fetch and play AI-generated voice response
|
| 64 |
audio_response_url = result.get("audio")
|
| 65 |
if audio_response_url:
|
| 66 |
-
st.write(
|
| 67 |
audio_response = requests.get(audio_response_url)
|
| 68 |
if audio_response.status_code == 200:
|
| 69 |
st.audio(audio_response.content, format="audio/wav")
|
|
@@ -79,4 +49,4 @@ if st.button("β
Process Recorded Audio"):
|
|
| 79 |
st.error(f"β Failed to connect to API: {str(e)}")
|
| 80 |
|
| 81 |
else:
|
| 82 |
-
st.error("β οΈ
|
|
|
|
| 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 (Replace with your deployed 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 |
+
# β
Upload Audio File
|
| 15 |
+
uploaded_audio = st.file_uploader("π€ Upload an audio file (WAV only)", type=["wav"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# β
Process Button
|
| 18 |
+
if st.button("β
Process Audio"):
|
| 19 |
+
if uploaded_audio is not None:
|
| 20 |
+
with st.spinner("π Sending audio to AI model..."):
|
| 21 |
try:
|
| 22 |
# β
Send recorded audio to Render API
|
| 23 |
+
files = {"file": (uploaded_audio.name, uploaded_audio, "audio/wav")}
|
| 24 |
+
response = requests.post(RENDER_API_URL, files=files)
|
| 25 |
|
| 26 |
# β
Handle API response
|
| 27 |
if response.status_code == 200:
|
|
|
|
| 33 |
# β
Fetch and play AI-generated voice response
|
| 34 |
audio_response_url = result.get("audio")
|
| 35 |
if audio_response_url:
|
| 36 |
+
st.write("π **AI-generated voice response:**")
|
| 37 |
audio_response = requests.get(audio_response_url)
|
| 38 |
if audio_response.status_code == 200:
|
| 39 |
st.audio(audio_response.content, format="audio/wav")
|
|
|
|
| 49 |
st.error(f"β Failed to connect to API: {str(e)}")
|
| 50 |
|
| 51 |
else:
|
| 52 |
+
st.error("β οΈ Please upload an audio file first!")
|