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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -17
app.py CHANGED
@@ -7,8 +7,10 @@ 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()
@@ -18,7 +20,7 @@ def audio_frame_callback(frame):
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,
@@ -29,6 +31,7 @@ webrtc_streamer(
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)
@@ -38,24 +41,37 @@ if st.button("βœ… Process Recorded Audio"):
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()
46
- st.success("βœ… AI Response:")
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!")
 
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()
 
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,
 
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)
 
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")
62
+ else:
63
+ st.error(f"❌ Failed to load AI audio ({audio_response.status_code})")
64
+ else:
65
+ st.warning("⚠️ No audio response received from API.")
66
+
67
+ # βœ… Clear session state for new recording
68
+ st.session_state.frames = []
69
+
70
+ else:
71
+ st.error(f"❌ API Error: {response.status_code} - {response.text}")
72
 
73
+ except requests.exceptions.RequestException as e:
74
+ st.error(f"❌ Failed to connect to API: {str(e)}")
 
75
 
 
 
 
 
76
  else:
77
  st.error("⚠️ No audio captured. Please record first!")