sAIvahini / app.py
TruthLens's picture
Update app.py
480b0b2 verified
raw
history blame
2.18 kB
import streamlit as st
from streamlit_webrtc import webrtc_streamer, WebRtcMode
import requests
import av
import numpy as np
import wave
import io
st.title("Sai Vahini AI Voice Assistant πŸ•‰οΈ")
# Replace with your actual Render API endpoint
api_url = "https://saivahini.onrender.com/process_audio"
frames = []
# Function to save audio as WAV file in memory
def save_audio(frames, sample_rate=16000):
audio_bytes = io.BytesIO()
wf = wave.open(audio_bytes, 'wb')
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(sample_rate)
wf.writeframes(b''.join(frames))
wf.close()
audio_bytes.seek(0)
return audio_bytes
# Callback to handle audio frames
def audio_frame_callback(frame):
audio = frame.to_ndarray(format="s16le")
frames.append(audio.tobytes())
return av.AudioFrame.from_ndarray(audio, format="s16", layout="mono")
# Button to start recording audio
if st.button("πŸŽ™οΈ Record your voice"):
frames.clear()
webrtc_streamer(
key="audio-recorder",
mode=WebRtcMode.SENDRECV,
audio_frame_callback=audio_frame_callback,
media_stream_constraints={"audio": True, "video": False}
)
# Button to stop recording and process audio
if st.button("βœ… Stop & Process Audio"):
if frames:
with st.spinner("πŸ”„ Processing your voice..."):
audio_file = save_audio(frames)
response = requests.post(api_url, files={"file": ("audio.wav", audio_file, "audio/wav")})
if response.status_code == 200:
result = response.json()
st.success("βœ… AI Response:")
st.write("**Transcription:**", result["transcription"])
st.write("**Answer:**", result["response"])
audio_response_url = result.get("audio")
# If the API returns a URL, fetch and play the audio directly
audio_content = requests.get(audio_response_url).content
st.audio(audio_content, format='audio/wav')
else:
st.error(f"❌ API Error: {response.status_code}")
else:
st.error("⚠️ No audio captured. Please record again.")