TruthLens commited on
Commit
7f4ca76
Β·
verified Β·
1 Parent(s): b3f65d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -41
app.py CHANGED
@@ -1,57 +1,27 @@
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 page title and layout
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 # Seconds
19
- SAMPLE_RATE = 16000
20
-
21
- # βœ… Function to record audio
22
- def record_audio():
23
- st.info("🎀 Recording... Speak now!")
24
- audio = sd.rec(int(DURATION * SAMPLE_RATE), samplerate=SAMPLE_RATE, channels=1, dtype=np.int16)
25
- sd.wait() # Wait until recording is finished
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:
@@ -63,7 +33,7 @@ if st.button("βœ… Process Recorded Audio"):
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")
@@ -79,4 +49,4 @@ if st.button("βœ… Process Recorded Audio"):
79
  st.error(f"❌ Failed to connect to API: {str(e)}")
80
 
81
  else:
82
- st.error("⚠️ No audio recorded. Click 'Record Audio' first!")
 
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:
 
33
  # βœ… Fetch and play AI-generated voice response
34
  audio_response_url = result.get("audio")
35
  if audio_response_url:
36
+ st.write("πŸ”Š **AI-generated voice response:**")
37
  audio_response = requests.get(audio_response_url)
38
  if audio_response.status_code == 200:
39
  st.audio(audio_response.content, format="audio/wav")
 
49
  st.error(f"❌ Failed to connect to API: {str(e)}")
50
 
51
  else:
52
+ st.error("⚠️ Please upload an audio file first!")