TruthLens commited on
Commit
88ba4c2
Β·
verified Β·
1 Parent(s): f170e39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -22
app.py CHANGED
@@ -4,23 +4,28 @@ import av
4
  import wave
5
  import requests
6
  import io
 
7
 
8
- st.title("Sai Vahini AI Voice Assistant πŸ•‰οΈ")
9
 
10
- # βœ… Render API URL (Make sure this matches your deployed API on Render)
11
- RENDER_API_URL = "https://saivahini.onrender.com/process_audio" # Update if needed
 
 
 
12
 
13
  # βœ… WebRTC audio processing
14
  def audio_frame_callback(frame):
 
15
  audio = frame.to_ndarray(format="s16le")
16
  audio_bytes = audio.tobytes()
 
 
17
  st.session_state.frames.append(audio_bytes)
18
  return av.AudioFrame.from_ndarray(audio, format="s16", layout="mono")
19
 
20
- if "frames" not in st.session_state:
21
- st.session_state.frames = []
22
-
23
- # βœ… WebRTC streamer for automatic audio capture
24
  webrtc_streamer(
25
  key="audio-recorder",
26
  mode=WebRtcMode.SENDRECV,
@@ -28,34 +33,39 @@ webrtc_streamer(
28
  media_stream_constraints={"audio": True, "video": False},
29
  )
30
 
 
 
 
 
 
31
  if st.button("βœ… Process Recorded Audio"):
32
- if st.session_state.frames:
33
  with st.spinner("πŸ”„ Processing your voice..."):
34
- # βœ… Convert recorded audio frames into WAV format
35
- audio_bytes = io.BytesIO()
36
- with wave.open(audio_bytes, "wb") as wf:
37
- wf.setnchannels(1)
38
- wf.setsampwidth(2)
39
- wf.setframerate(16000)
40
- wf.writeframes(b''.join(st.session_state.frames))
 
41
 
42
- audio_bytes.seek(0)
43
 
44
- # βœ… Send recorded audio to Render API
45
- try:
46
  response = requests.post(RENDER_API_URL, files={"file": ("audio.wav", audio_bytes, "audio/wav")})
47
 
48
  # βœ… Handle API response
49
  if response.status_code == 200:
50
  result = response.json()
51
  st.success("βœ… AI Response:")
52
- st.write("**Transcription:**", result.get("transcription", "No transcription"))
53
- st.write("**Answer:**", result.get("response", "No response found."))
54
 
55
- # βœ… Fetch and play AI-generated audio response
56
  audio_response_url = result.get("audio")
57
  if audio_response_url:
58
- st.write(f"πŸ”Š Fetching AI-generated voice from: {audio_response_url}")
59
  audio_response = requests.get(audio_response_url)
60
  if audio_response.status_code == 200:
61
  st.audio(audio_response.content, format="audio/wav")
 
4
  import wave
5
  import requests
6
  import io
7
+ import numpy as np
8
 
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
  # βœ… WebRTC audio processing
18
  def audio_frame_callback(frame):
19
+ """Handles incoming audio frames from WebRTC"""
20
  audio = frame.to_ndarray(format="s16le")
21
  audio_bytes = audio.tobytes()
22
+ if "frames" not in st.session_state:
23
+ st.session_state.frames = []
24
  st.session_state.frames.append(audio_bytes)
25
  return av.AudioFrame.from_ndarray(audio, format="s16", layout="mono")
26
 
27
+ # βœ… WebRTC Streamer for recording
28
+ st.write("🎀 **Click below to start speaking...**")
 
 
29
  webrtc_streamer(
30
  key="audio-recorder",
31
  mode=WebRtcMode.SENDRECV,
 
33
  media_stream_constraints={"audio": True, "video": False},
34
  )
35
 
36
+ # βœ… Check if audio frames exist
37
+ if "frames" not in st.session_state or not st.session_state.frames:
38
+ st.warning("⚠️ No audio recorded. Click the button above to start recording.")
39
+
40
+ # βœ… Process Button
41
  if st.button("βœ… Process Recorded Audio"):
42
+ if "frames" in st.session_state and st.session_state.frames:
43
  with st.spinner("πŸ”„ Processing your voice..."):
44
+ try:
45
+ # βœ… Convert recorded audio frames into WAV format
46
+ audio_bytes = io.BytesIO()
47
+ with wave.open(audio_bytes, "wb") as wf:
48
+ wf.setnchannels(1)
49
+ wf.setsampwidth(2)
50
+ wf.setframerate(16000)
51
+ wf.writeframes(b''.join(st.session_state.frames))
52
 
53
+ audio_bytes.seek(0) # Reset buffer pointer
54
 
55
+ # βœ… Send recorded audio to Render API
 
56
  response = requests.post(RENDER_API_URL, files={"file": ("audio.wav", audio_bytes, "audio/wav")})
57
 
58
  # βœ… Handle API response
59
  if response.status_code == 200:
60
  result = response.json()
61
  st.success("βœ… AI Response:")
62
+ st.write("πŸ“ **Transcription:**", result.get("transcription", "No transcription"))
63
+ st.write("πŸ€– **Answer:**", result.get("response", "No response found."))
64
 
65
+ # βœ… Fetch and play AI-generated voice response
66
  audio_response_url = result.get("audio")
67
  if audio_response_url:
68
+ st.write(f"πŸ”Š **AI-generated voice response:**")
69
  audio_response = requests.get(audio_response_url)
70
  if audio_response.status_code == 200:
71
  st.audio(audio_response.content, format="audio/wav")