TruthLens commited on
Commit
b3f65d4
Β·
verified Β·
1 Parent(s): 9235ff8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -1
app.py CHANGED
@@ -26,4 +26,57 @@ def record_audio():
26
  st.success("βœ… Recording completed!")
27
 
28
  # βœ… Save the audio as a WAV file
29
- audio_bytes = io.BytesIO
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  st.success("βœ… Recording completed!")
27
 
28
  # βœ… Save the audio as a WAV file
29
+ audio_bytes = io.BytesIO()
30
+ with wave.open(audio_bytes, "wb") as wf:
31
+ wf.setnchannels(1)
32
+ wf.setsampwidth(2)
33
+ wf.setframerate(SAMPLE_RATE)
34
+ wf.writeframes(audio.tobytes())
35
+
36
+ audio_bytes.seek(0)
37
+ return audio_bytes
38
+
39
+ # βœ… Record button
40
+ if st.button("🎀 Record Audio"):
41
+ audio_file = record_audio()
42
+ st.session_state["audio_data"] = audio_file
43
+
44
+ # βœ… Check if audio was recorded
45
+ if "audio_data" in st.session_state:
46
+ st.audio(st.session_state["audio_data"], format="audio/wav")
47
+
48
+ # βœ… Process Button
49
+ if st.button("βœ… Process Recorded Audio"):
50
+ if "audio_data" in st.session_state:
51
+ with st.spinner("πŸ”„ Processing your voice..."):
52
+ try:
53
+ # βœ… Send recorded audio to Render API
54
+ response = requests.post(RENDER_API_URL, files={"file": ("audio.wav", st.session_state["audio_data"], "audio/wav")})
55
+
56
+ # βœ… Handle API response
57
+ if response.status_code == 200:
58
+ result = response.json()
59
+ st.success("βœ… AI Response:")
60
+ st.write("πŸ“ **Transcription:**", result.get("transcription", "No transcription"))
61
+ st.write("πŸ€– **Answer:**", result.get("response", "No response found."))
62
+
63
+ # βœ… Fetch and play AI-generated voice response
64
+ audio_response_url = result.get("audio")
65
+ if audio_response_url:
66
+ st.write(f"πŸ”Š **AI-generated voice response:**")
67
+ audio_response = requests.get(audio_response_url)
68
+ if audio_response.status_code == 200:
69
+ st.audio(audio_response.content, format="audio/wav")
70
+ else:
71
+ st.error(f"❌ Failed to load AI audio ({audio_response.status_code})")
72
+ else:
73
+ st.warning("⚠️ No audio response received from API.")
74
+
75
+ else:
76
+ st.error(f"❌ API Error: {response.status_code} - {response.text}")
77
+
78
+ except requests.exceptions.RequestException as e:
79
+ st.error(f"❌ Failed to connect to API: {str(e)}")
80
+
81
+ else:
82
+ st.error("⚠️ No audio recorded. Click 'Record Audio' first!")