TruthLens commited on
Commit
48d87a8
Β·
verified Β·
1 Parent(s): 314672c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -58
app.py CHANGED
@@ -1,9 +1,7 @@
1
  import streamlit as st
2
  import requests
3
- import numpy as np
4
- import sounddevice as sd
5
- import wave
6
  import io
 
7
 
8
  # βœ… Set Streamlit Page Config
9
  st.set_page_config(page_title="Sai Vahini AI Assistant", layout="centered")
@@ -14,70 +12,81 @@ RENDER_API_URL = "https://saivahini.onrender.com/process_audio"
14
  # βœ… UI Header
15
  st.markdown("<h1 style='text-align: center; color: #ff5733;'>Sai Vahini AI Voice Assistant πŸ•‰οΈ</h1>", unsafe_allow_html=True)
16
 
17
- # βœ… Audio recording parameters
18
- DURATION = 5 # Recording duration in seconds
19
- SAMPLE_RATE = 16000
 
 
20
 
21
- # βœ… Function to record audio
22
- def record_audio():
23
- """Records live audio and saves it as a WAV file"""
24
- st.info("🎀 Recording... Speak now!")
25
- audio = sd.rec(int(DURATION * SAMPLE_RATE), samplerate=SAMPLE_RATE, channels=1, dtype=np.int16)
26
- sd.wait() # Wait until recording is finished
27
- st.success("βœ… Recording completed!")
28
 
29
- # βœ… Save the audio as a WAV file
30
- audio_bytes = io.BytesIO()
31
- with wave.open(audio_bytes, "wb") as wf:
32
- wf.setnchannels(1)
33
- wf.setsampwidth(2)
34
- wf.setframerate(SAMPLE_RATE)
35
- wf.writeframes(audio.tobytes())
36
 
37
- audio_bytes.seek(0)
38
- return audio_bytes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- # βœ… Record button
41
- if st.button("🎀 Record Live Audio"):
42
- audio_file = record_audio()
43
- st.session_state["audio_data"] = audio_file
 
 
 
 
44
 
45
- # βœ… Play recorded audio before sending
46
- if "audio_data" in st.session_state:
47
- st.audio(st.session_state["audio_data"], format="audio/wav")
48
 
49
  # βœ… Process Button
50
  if st.button("βœ… Process Recorded Audio"):
51
- if "audio_data" in st.session_state:
52
- with st.spinner("πŸ”„ Sending audio to AI model..."):
53
- try:
54
- # βœ… Send recorded audio to Render API
55
- response = requests.post(RENDER_API_URL, files={"file": ("audio.wav", st.session_state["audio_data"], "audio/wav")})
 
56
 
57
- # βœ… Handle API response
58
- if response.status_code == 200:
59
- result = response.json()
60
- st.success("βœ… AI Response:")
61
- st.write("πŸ“ **Transcription:**", result.get("transcription", "No transcription"))
62
- st.write("πŸ€– **Answer:**", result.get("response", "No response found."))
63
 
64
- # βœ… Fetch and play AI-generated voice response
65
- audio_response_url = result.get("audio")
66
- if audio_response_url:
67
- st.write("πŸ”Š **AI-generated voice response:**")
68
- audio_response = requests.get(audio_response_url)
69
- if audio_response.status_code == 200:
70
- st.audio(audio_response.content, format="audio/wav")
71
- else:
72
- st.error(f"❌ Failed to load AI audio ({audio_response.status_code})")
73
- else:
74
- st.warning("⚠️ No audio response received from API.")
75
 
 
 
 
 
 
 
 
 
 
76
  else:
77
- st.error(f"❌ API Error: {response.status_code} - {response.text}")
78
-
79
- except requests.exceptions.RequestException as e:
80
- st.error(f"❌ Failed to connect to API: {str(e)}")
81
-
82
- else:
83
- st.error("⚠️ No audio recorded. Click 'Record Live Audio' first!")
 
1
  import streamlit as st
2
  import requests
 
 
 
3
  import io
4
+ import base64
5
 
6
  # βœ… Set Streamlit Page Config
7
  st.set_page_config(page_title="Sai Vahini AI Assistant", layout="centered")
 
12
  # βœ… UI Header
13
  st.markdown("<h1 style='text-align: center; color: #ff5733;'>Sai Vahini AI Voice Assistant πŸ•‰οΈ</h1>", unsafe_allow_html=True)
14
 
15
+ # βœ… HTML5 Audio Recorder (JavaScript + Streamlit)
16
+ audio_recorder_html = """
17
+ <script>
18
+ let mediaRecorder;
19
+ let audioChunks = [];
20
 
21
+ function startRecording() {
22
+ navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
23
+ mediaRecorder = new MediaRecorder(stream);
24
+ mediaRecorder.start();
 
 
 
25
 
26
+ mediaRecorder.ondataavailable = event => {
27
+ audioChunks.push(event.data);
28
+ };
 
 
 
 
29
 
30
+ mediaRecorder.onstop = () => {
31
+ const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
32
+ const reader = new FileReader();
33
+ reader.readAsDataURL(audioBlob);
34
+ reader.onloadend = () => {
35
+ fetch("/upload_audio", {
36
+ method: "POST",
37
+ headers: { "Content-Type": "application/json" },
38
+ body: JSON.stringify({ audio: reader.result })
39
+ }).then(response => response.json()).then(data => {
40
+ document.getElementById("audio_url").value = data.audio_url;
41
+ });
42
+ };
43
+ };
44
+ });
45
+ }
46
 
47
+ function stopRecording() {
48
+ mediaRecorder.stop();
49
+ }
50
+ </script>
51
+ <button onclick="startRecording()">🎀 Start Recording</button>
52
+ <button onclick="stopRecording()">⏹ Stop Recording</button>
53
+ <input type="hidden" id="audio_url">
54
+ """
55
 
56
+ # βœ… Display HTML5 Recorder
57
+ st.components.v1.html(audio_recorder_html, height=150)
 
58
 
59
  # βœ… Process Button
60
  if st.button("βœ… Process Recorded Audio"):
61
+ with st.spinner("πŸ”„ Sending audio to AI model..."):
62
+ audio_url = st.session_state.get("audio_url", None)
63
+ if audio_url:
64
+ # Convert Base64 audio to WAV format
65
+ audio_data = base64.b64decode(audio_url.split(",")[1])
66
+ audio_bytes = io.BytesIO(audio_data)
67
 
68
+ # βœ… Send recorded audio to Render API
69
+ response = requests.post(RENDER_API_URL, files={"file": ("audio.wav", audio_bytes, "audio/wav")})
 
 
 
 
70
 
71
+ # βœ… Handle API response
72
+ if response.status_code == 200:
73
+ result = response.json()
74
+ st.success("βœ… AI Response:")
75
+ st.write("πŸ“ **Transcription:**", result.get("transcription", "No transcription"))
76
+ st.write("πŸ€– **Answer:**", result.get("response", "No response found."))
 
 
 
 
 
77
 
78
+ # βœ… Fetch and play AI-generated voice response
79
+ audio_response_url = result.get("audio")
80
+ if audio_response_url:
81
+ st.write("πŸ”Š **AI-generated voice response:**")
82
+ audio_response = requests.get(audio_response_url)
83
+ if audio_response.status_code == 200:
84
+ st.audio(audio_response.content, format="audio/wav")
85
+ else:
86
+ st.error(f"❌ Failed to load AI audio ({audio_response.status_code})")
87
  else:
88
+ st.warning("⚠️ No audio response received from API.")
89
+ else:
90
+ st.error(f"❌ API Error: {response.status_code} - {response.text}")
91
+ else:
92
+ st.error("⚠️ No audio recorded. Please record first!")