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