Update app.py
Browse files
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!")
|