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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -39
app.py CHANGED
@@ -1,53 +1,45 @@
1
  import streamlit as st
2
  from streamlit_webrtc import webrtc_streamer, WebRtcMode
3
- import requests
4
  import av
5
- import numpy as np
6
  import wave
 
7
  import io
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')
20
- wf.setnchannels(1)
21
- wf.setsampwidth(2)
22
- wf.setframerate(sample_rate)
23
- wf.writeframes(b''.join(frames))
24
- wf.close()
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()
@@ -55,12 +47,15 @@ if st.button("βœ… Stop & Process Audio"):
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.")
 
1
  import streamlit as st
2
  from streamlit_webrtc import webrtc_streamer, WebRtcMode
 
3
  import av
 
4
  import wave
5
+ import requests
6
  import io
7
 
8
  st.title("Sai Vahini AI Voice Assistant πŸ•‰οΈ")
9
 
10
+ api_url = "https://saivahini.onrender.com/process_audio" # Replace if needed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
 
12
  def audio_frame_callback(frame):
13
  audio = frame.to_ndarray(format="s16le")
14
+ audio_bytes = audio.tobytes()
15
+ st.session_state.frames.append(audio_bytes)
16
  return av.AudioFrame.from_ndarray(audio, format="s16", layout="mono")
17
 
18
+ if "frames" not in st.session_state:
19
+ st.session_state.frames = []
 
 
 
 
 
 
 
20
 
21
+ # WebRTC streamer for automatic audio capture
22
+ webrtc_streamer(
23
+ key="audio-recorder",
24
+ mode=WebRtcMode.SENDRECV,
25
+ audio_frame_callback=audio_frame_callback,
26
+ media_stream_constraints={"audio": True, "video": False},
27
+ )
28
+
29
+ if st.button("βœ… Process Recorded Audio"):
30
+ if st.session_state.frames:
31
  with st.spinner("πŸ”„ Processing your voice..."):
32
+ audio_bytes = io.BytesIO()
33
+ with wave.open(audio_bytes, "wb") as wf:
34
+ wf.setnchannels(1)
35
+ wf.setsampwidth(2)
36
+ wf.setframerate(16000)
37
+ wf.writeframes(b''.join(st.session_state.frames))
38
+
39
+ audio_bytes.seek(0)
40
 
41
+ # Send to your API
42
+ response = requests.post(api_url, files={"file": ("audio.wav", audio_bytes, "audio/wav")})
43
 
44
  if response.status_code == 200:
45
  result = response.json()
 
47
  st.write("**Transcription:**", result["transcription"])
48
  st.write("**Answer:**", result["response"])
49
 
50
+ audio_response = result["audio"]
51
+
52
+ # Fetch and play audio response
53
+ audio_content = requests.get(audio_response).content
54
+ st.audio(audio_content, format="audio/wav")
55
 
56
+ # Clear session state for new recording
57
+ st.session_state.frames = []
 
58
  else:
59
  st.error(f"❌ API Error: {response.status_code}")
60
  else:
61
+ st.error("⚠️ No audio captured. Please record first!")