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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -8
app.py CHANGED
@@ -1,27 +1,58 @@
1
  import streamlit as st
2
  import requests
 
 
 
3
  import io
4
 
5
  # βœ… Set Streamlit Page Config
6
  st.set_page_config(page_title="Sai Vahini AI Assistant", layout="centered")
7
 
8
- # βœ… Render API URL (Replace with your deployed API URL)
9
  RENDER_API_URL = "https://saivahini.onrender.com/process_audio"
10
 
11
  # βœ… UI Header
12
  st.markdown("<h1 style='text-align: center; color: #ff5733;'>Sai Vahini AI Voice Assistant πŸ•‰οΈ</h1>", unsafe_allow_html=True)
13
 
14
- # βœ… Upload Audio File
15
- uploaded_audio = st.file_uploader("🎀 Upload an audio file (WAV only)", type=["wav"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  # βœ… Process Button
18
- if st.button("βœ… Process Audio"):
19
- if uploaded_audio is not None:
20
  with st.spinner("πŸ”„ Sending audio to AI model..."):
21
  try:
22
  # βœ… Send recorded audio to Render API
23
- files = {"file": (uploaded_audio.name, uploaded_audio, "audio/wav")}
24
- response = requests.post(RENDER_API_URL, files=files)
25
 
26
  # βœ… Handle API response
27
  if response.status_code == 200:
@@ -49,4 +80,4 @@ if st.button("βœ… Process Audio"):
49
  st.error(f"❌ Failed to connect to API: {str(e)}")
50
 
51
  else:
52
- st.error("⚠️ Please upload an audio file first!")
 
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")
10
 
11
+ # βœ… Render API URL (Ensure this matches your deployed API on Render)
12
  RENDER_API_URL = "https://saivahini.onrender.com/process_audio"
13
 
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:
 
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!")