Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,8 @@ from io import BytesIO
|
|
9 |
import PyPDF2
|
10 |
from streamlit_webrtc import webrtc_streamer, WebRtcMode, RTCConfiguration
|
11 |
import asyncio
|
|
|
|
|
12 |
|
13 |
# Set your API key
|
14 |
api_key = "AIzaSyAHD0FwX-Ds6Y3eI-i5Oz7IdbJqR6rN7pg" # Replace with your actual API key
|
@@ -176,13 +178,27 @@ with col2:
|
|
176 |
uploaded_files = st.file_uploader(
|
177 |
"Upload Files (Images, Videos, PDFs, MP3):",
|
178 |
type=["png", "jpg", "jpeg", "mp4", "pdf", "mp3"],
|
179 |
-
|
180 |
key=st.session_state.file_uploader_key
|
181 |
)
|
182 |
|
183 |
# --- WebRTC Audio Recording ---
|
184 |
RTC_CONFIGURATION = RTCConfiguration({"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]})
|
185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
async def run_webrtc():
|
187 |
webrtc_ctx = webrtc_streamer(
|
188 |
key="audio-recorder",
|
@@ -196,7 +212,8 @@ async def run_webrtc():
|
|
196 |
st.session_state.is_recording = True
|
197 |
st.write("Recording audio...")
|
198 |
audio_frames = webrtc_ctx.audio_receiver.get_frames(timeout=None)
|
199 |
-
|
|
|
200 |
|
201 |
if st.button("Send Recording"):
|
202 |
send_message(audio_data=st.session_state.audio_data)
|
|
|
9 |
import PyPDF2
|
10 |
from streamlit_webrtc import webrtc_streamer, WebRtcMode, RTCConfiguration
|
11 |
import asyncio
|
12 |
+
import numpy as np
|
13 |
+
import pydub
|
14 |
|
15 |
# Set your API key
|
16 |
api_key = "AIzaSyAHD0FwX-Ds6Y3eI-i5Oz7IdbJqR6rN7pg" # Replace with your actual API key
|
|
|
178 |
uploaded_files = st.file_uploader(
|
179 |
"Upload Files (Images, Videos, PDFs, MP3):",
|
180 |
type=["png", "jpg", "jpeg", "mp4", "pdf", "mp3"],
|
181 |
+
accept_multiple files=True,
|
182 |
key=st.session_state.file_uploader_key
|
183 |
)
|
184 |
|
185 |
# --- WebRTC Audio Recording ---
|
186 |
RTC_CONFIGURATION = RTCConfiguration({"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]})
|
187 |
|
188 |
+
def convert_frames_to_wav(audio_frames):
|
189 |
+
"""Convert audio frames to WAV format using PyDub."""
|
190 |
+
audio = np.concatenate(audio_frames)
|
191 |
+
audio_segment = pydub.AudioSegment(
|
192 |
+
audio.tobytes(),
|
193 |
+
frame_rate=16000,
|
194 |
+
sample_width=audio.dtype.itemsize,
|
195 |
+
channels=1
|
196 |
+
)
|
197 |
+
wav_io = BytesIO()
|
198 |
+
audio_segment.export(wav_io, format="wav")
|
199 |
+
wav_io.seek(0)
|
200 |
+
return wav_io.getvalue()
|
201 |
+
|
202 |
async def run_webrtc():
|
203 |
webrtc_ctx = webrtc_streamer(
|
204 |
key="audio-recorder",
|
|
|
212 |
st.session_state.is_recording = True
|
213 |
st.write("Recording audio...")
|
214 |
audio_frames = webrtc_ctx.audio_receiver.get_frames(timeout=None)
|
215 |
+
audio_data = [frame.to_ndarray() for frame in audio_frames]
|
216 |
+
st.session_state.audio_data = convert_frames_to_wav(audio_data)
|
217 |
|
218 |
if st.button("Send Recording"):
|
219 |
send_message(audio_data=st.session_state.audio_data)
|