TruthLens commited on
Commit
480b0b2
Β·
verified Β·
1 Parent(s): fb9f773

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -27
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import streamlit as st
2
- from streamlit_webrtc import webrtc_streamer
3
  import requests
4
  import av
5
  import numpy as np
@@ -8,9 +8,12 @@ import io
8
 
9
  st.title("Sai Vahini AI Voice Assistant πŸ•‰οΈ")
10
 
11
- api_url = "https://saivahini.onrender.com/" # Replace with your Render API clearly!
 
12
 
13
- # Function to save audio as WAV clearly
 
 
14
  def save_audio(frames, sample_rate=16000):
15
  audio_bytes = io.BytesIO()
16
  wf = wave.open(audio_bytes, 'wb')
@@ -22,38 +25,42 @@ def save_audio(frames, sample_rate=16000):
22
  audio_bytes.seek(0)
23
  return audio_bytes
24
 
25
- # WebRTC Configuration (audio-only clearly)
26
  def audio_frame_callback(frame):
27
  audio = frame.to_ndarray(format="s16le")
28
  frames.append(audio.tobytes())
29
  return av.AudioFrame.from_ndarray(audio, format="s16", layout="mono")
30
 
31
- frames = []
32
-
33
- # UI to record voice input
34
  if st.button("πŸŽ™οΈ Record your voice"):
35
  frames.clear()
36
- webrtc_streamer(key="audio-recorder", mode="sendrecv", audio_frame_callback=audio_frame_callback, media_stream_constraints={"audio": True, "video": False})
 
 
 
 
 
37
 
 
38
  if st.button("βœ… Stop & Process Audio"):
39
  if frames:
40
- st.write("πŸ”„ Processing your voice...")
41
- audio_file = save_audio(frames)
42
-
43
- response = requests.post(api_url, files={"file": ("audio.wav", audio_file, "audio/wav")})
44
-
45
- if response.status_code == 200:
46
- result = response.json()
47
- st.success("βœ… AI Response:")
48
- st.write("**Transcription:**", result["transcription"])
49
- st.write("**Answer:**", result["response"])
50
-
51
- audio_response = result["audio"]
52
-
53
- # Display response audio
54
- audio_data = open(audio_response, 'rb').read()
55
- st.audio(audio_data, format='audio/wav')
56
- else:
57
- st.error(f"❌ API Error: {response.status_code}")
58
  else:
59
- st.error("⚠️ No audio captured. Please record again.")
 
1
  import streamlit as st
2
+ from streamlit_webrtc import webrtc_streamer, WebRtcMode
3
  import requests
4
  import av
5
  import numpy as np
 
8
 
9
  st.title("Sai Vahini AI Voice Assistant πŸ•‰οΈ")
10
 
11
+ # Replace with your actual Render API endpoint
12
+ api_url = "https://saivahini.onrender.com/process_audio"
13
 
14
+ frames = []
15
+
16
+ # Function to save audio as WAV file in memory
17
  def save_audio(frames, sample_rate=16000):
18
  audio_bytes = io.BytesIO()
19
  wf = wave.open(audio_bytes, 'wb')
 
25
  audio_bytes.seek(0)
26
  return audio_bytes
27
 
28
+ # Callback to handle audio frames
29
  def audio_frame_callback(frame):
30
  audio = frame.to_ndarray(format="s16le")
31
  frames.append(audio.tobytes())
32
  return av.AudioFrame.from_ndarray(audio, format="s16", layout="mono")
33
 
34
+ # Button to start recording audio
 
 
35
  if st.button("πŸŽ™οΈ Record your voice"):
36
  frames.clear()
37
+ webrtc_streamer(
38
+ key="audio-recorder",
39
+ mode=WebRtcMode.SENDRECV,
40
+ audio_frame_callback=audio_frame_callback,
41
+ media_stream_constraints={"audio": True, "video": False}
42
+ )
43
 
44
+ # Button to stop recording and process audio
45
  if st.button("βœ… Stop & Process Audio"):
46
  if frames:
47
+ with st.spinner("πŸ”„ Processing your voice..."):
48
+ audio_file = save_audio(frames)
49
+
50
+ response = requests.post(api_url, files={"file": ("audio.wav", audio_file, "audio/wav")})
51
+
52
+ if response.status_code == 200:
53
+ result = response.json()
54
+ st.success("βœ… AI Response:")
55
+ st.write("**Transcription:**", result["transcription"])
56
+ st.write("**Answer:**", result["response"])
57
+
58
+ audio_response_url = result.get("audio")
59
+
60
+ # If the API returns a URL, fetch and play the audio directly
61
+ audio_content = requests.get(audio_response_url).content
62
+ st.audio(audio_content, format='audio/wav')
63
+ else:
64
+ st.error(f"❌ API Error: {response.status_code}")
65
  else:
66
+ st.error("⚠️ No audio captured. Please record again.")