ziyadsuper2017 commited on
Commit
489223c
·
verified ·
1 Parent(s): a28faaa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -15
app.py CHANGED
@@ -8,6 +8,7 @@ import google.generativeai as genai
8
  from io import BytesIO
9
  import PyPDF2
10
  from streamlit_webrtc import webrtc_streamer, WebRtcMode, RTCConfiguration
 
11
 
12
  # Set your API key
13
  api_key = "AIzaSyAHD0FwX-Ds6Y3eI-i5Oz7IdbJqR6rN7pg" # Replace with your actual API key
@@ -171,22 +172,23 @@ uploaded_files = st.file_uploader(
171
  # --- WebRTC Audio Recording ---
172
  RTC_CONFIGURATION = RTCConfiguration({"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]})
173
 
174
- webrtc_ctx = webrtc_streamer(
175
- key="audio-recorder",
176
- mode=WebRtcMode.SENDONLY,
177
- rtc_configuration=RTC_CONFIGURATION,
178
- audio_receiver_size=256,
179
- media_stream_constraints={"video": False, "audio": True},
180
- )
 
 
181
 
182
- if webrtc_ctx.audio_receiver:
183
- st.write("Recording audio...")
184
- audio_frames = webrtc_ctx.audio_receiver.get_frames(timeout=None)
185
- audio_data = b"".join([frame for frame in audio_frames])
186
 
187
- # Send the recorded audio when the "Send" button is clicked
188
- if st.button("Send Recording"):
189
- send_message(audio_data=audio_data)
190
 
191
  # --- Other Buttons ---
192
  st.button("Clear Conversation", on_click=clear_conversation)
@@ -211,5 +213,6 @@ st.markdown(
211
  unsafe_allow_html=True
212
  )
213
 
214
- # --- Display Chat History ---
 
215
  display_chat_history()
 
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
 
172
  # --- WebRTC Audio Recording ---
173
  RTC_CONFIGURATION = RTCConfiguration({"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]})
174
 
175
+ # Run webrtc_streamer in a separate async function to avoid conflicts
176
+ async def run_webrtc():
177
+ webrtc_ctx = webrtc_streamer(
178
+ key="audio-recorder",
179
+ mode=WebRtcMode.SENDONLY,
180
+ rtc_configuration=RTC_CONFIGURATION,
181
+ audio_receiver_size=256,
182
+ media_stream_constraints={"video": False, "audio": True},
183
+ )
184
 
185
+ if webrtc_ctx.audio_receiver:
186
+ st.write("Recording audio...")
187
+ audio_frames = webrtc_ctx.audio_receiver.get_frames(timeout=None)
188
+ audio_data = b"".join([frame for frame in audio_frames])
189
 
190
+ if st.button("Send Recording"):
191
+ send_message(audio_data=audio_data)
 
192
 
193
  # --- Other Buttons ---
194
  st.button("Clear Conversation", on_click=clear_conversation)
 
213
  unsafe_allow_html=True
214
  )
215
 
216
+ # --- Run WebRTC and display chat history ---
217
+ asyncio.new_event_loop().run_until_complete(run_webrtc())
218
  display_chat_history()