Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -8,9 +8,6 @@ import google.generativeai as genai
|
|
8 |
from io import BytesIO
|
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
|
@@ -47,14 +44,6 @@ if 'chat_history' not in st.session_state:
|
|
47 |
st.session_state['chat_history'] = []
|
48 |
if 'file_uploader_key' not in st.session_state:
|
49 |
st.session_state['file_uploader_key'] = str(uuid.uuid4())
|
50 |
-
if 'uploaded_files' not in st.session_state:
|
51 |
-
st.session_state['uploaded_files'] = []
|
52 |
-
if 'user_input' not in st.session_state:
|
53 |
-
st.session_state['user_input'] = ''
|
54 |
-
if 'audio_data' not in st.session_state:
|
55 |
-
st.session_state['audio_data'] = None
|
56 |
-
if 'is_recording' not in st.session_state:
|
57 |
-
st.session_state['is_recording'] = False
|
58 |
|
59 |
# --- Streamlit UI ---
|
60 |
st.title("Gemini Chatbot")
|
@@ -74,26 +63,22 @@ def get_file_base64(file_content, mime_type):
|
|
74 |
def clear_conversation():
|
75 |
st.session_state['chat_history'] = []
|
76 |
st.session_state['file_uploader_key'] = str(uuid.uuid4())
|
77 |
-
st.session_state['user_input'] = ''
|
78 |
-
st.session_state['uploaded_files'] = []
|
79 |
-
st.session_state['audio_data'] = None
|
80 |
|
81 |
def display_chat_history():
|
82 |
chat_container = st.empty()
|
83 |
with chat_container.container():
|
84 |
for entry in st.session_state['chat_history']:
|
85 |
role = entry["role"]
|
86 |
-
parts = entry["parts"]
|
87 |
-
|
88 |
if 'text' in parts:
|
89 |
-
st.markdown(f"{role.title()}
|
90 |
elif 'data' in parts:
|
91 |
mime_type = parts.get('mime_type', '')
|
92 |
if mime_type.startswith('image'):
|
93 |
st.image(Image.open(io.BytesIO(base64.b64decode(parts['data']))),
|
94 |
-
|
95 |
elif mime_type == 'application/pdf':
|
96 |
-
st.write("PDF Content
|
97 |
pdf_reader = PyPDF2.PdfReader(io.BytesIO(base64.b64decode(parts['data'])))
|
98 |
for page_num in range(len(pdf_reader.pages)):
|
99 |
page = pdf_reader.pages[page_num]
|
@@ -153,6 +138,7 @@ def send_message(audio_data=None):
|
|
153 |
except Exception as e:
|
154 |
st.error(f"An error occurred: {e}")
|
155 |
|
|
|
156 |
st.session_state.uploaded_files = []
|
157 |
st.session_state.file_uploader_key = str(uuid.uuid4())
|
158 |
|
@@ -165,8 +151,7 @@ with col1:
|
|
165 |
user_input = st.text_area(
|
166 |
"Enter your message:",
|
167 |
value="",
|
168 |
-
key="user_input"
|
169 |
-
on_change=lambda: setattr(st.session_state, 'user_input', '')
|
170 |
)
|
171 |
with col2:
|
172 |
send_button = st.button(
|
@@ -186,21 +171,10 @@ uploaded_files = st.file_uploader(
|
|
186 |
# --- WebRTC Audio Recording ---
|
187 |
RTC_CONFIGURATION = RTCConfiguration({"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]})
|
188 |
|
189 |
-
|
190 |
-
|
191 |
-
audio = np.concatenate(audio_frames)
|
192 |
-
audio_segment = pydub.AudioSegment(
|
193 |
-
audio.tobytes(),
|
194 |
-
frame_rate=16000,
|
195 |
-
sample_width=audio.dtype.itemsize,
|
196 |
-
channels=1
|
197 |
-
)
|
198 |
-
wav_io = BytesIO()
|
199 |
-
audio_segment.export(wav_io, format="wav")
|
200 |
-
wav_io.seek(0)
|
201 |
-
return wav_io.getvalue()
|
202 |
|
203 |
-
|
204 |
webrtc_ctx = webrtc_streamer(
|
205 |
key="audio-recorder",
|
206 |
mode=WebRtcMode.SENDONLY,
|
@@ -208,17 +182,15 @@ async def run_webrtc():
|
|
208 |
audio_receiver_size=256,
|
209 |
media_stream_constraints={"video": False, "audio": True},
|
210 |
)
|
211 |
-
|
212 |
-
|
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=
|
220 |
-
else:
|
221 |
-
st.session_state.is_recording = False
|
222 |
|
223 |
# --- Other Buttons ---
|
224 |
st.button("Clear Conversation", on_click=clear_conversation)
|
@@ -230,16 +202,28 @@ st.session_state.uploaded_files = uploaded_files
|
|
230 |
st.markdown(
|
231 |
"""
|
232 |
<script>
|
233 |
-
|
234 |
-
|
235 |
-
|
|
|
|
|
236 |
}
|
237 |
});
|
|
|
238 |
</script>
|
239 |
""",
|
240 |
unsafe_allow_html=True
|
241 |
)
|
242 |
|
243 |
-
# ---
|
244 |
-
|
245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
44 |
st.session_state['chat_history'] = []
|
45 |
if 'file_uploader_key' not in st.session_state:
|
46 |
st.session_state['file_uploader_key'] = str(uuid.uuid4())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
# --- Streamlit UI ---
|
49 |
st.title("Gemini Chatbot")
|
|
|
63 |
def clear_conversation():
|
64 |
st.session_state['chat_history'] = []
|
65 |
st.session_state['file_uploader_key'] = str(uuid.uuid4())
|
|
|
|
|
|
|
66 |
|
67 |
def display_chat_history():
|
68 |
chat_container = st.empty()
|
69 |
with chat_container.container():
|
70 |
for entry in st.session_state['chat_history']:
|
71 |
role = entry["role"]
|
72 |
+
parts = entry["parts"][0]
|
|
|
73 |
if 'text' in parts:
|
74 |
+
st.markdown(f"**{role.title()}:** {parts['text']}")
|
75 |
elif 'data' in parts:
|
76 |
mime_type = parts.get('mime_type', '')
|
77 |
if mime_type.startswith('image'):
|
78 |
st.image(Image.open(io.BytesIO(base64.b64decode(parts['data']))),
|
79 |
+
caption='Uploaded Image', use_column_width=True)
|
80 |
elif mime_type == 'application/pdf':
|
81 |
+
st.write("**PDF Content:**")
|
82 |
pdf_reader = PyPDF2.PdfReader(io.BytesIO(base64.b64decode(parts['data'])))
|
83 |
for page_num in range(len(pdf_reader.pages)):
|
84 |
page = pdf_reader.pages[page_num]
|
|
|
138 |
except Exception as e:
|
139 |
st.error(f"An error occurred: {e}")
|
140 |
|
141 |
+
st.session_state.user_input = ''
|
142 |
st.session_state.uploaded_files = []
|
143 |
st.session_state.file_uploader_key = str(uuid.uuid4())
|
144 |
|
|
|
151 |
user_input = st.text_area(
|
152 |
"Enter your message:",
|
153 |
value="",
|
154 |
+
key="user_input"
|
|
|
155 |
)
|
156 |
with col2:
|
157 |
send_button = st.button(
|
|
|
171 |
# --- WebRTC Audio Recording ---
|
172 |
RTC_CONFIGURATION = RTCConfiguration({"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]})
|
173 |
|
174 |
+
if "webrtc_initialized" not in st.session_state:
|
175 |
+
st.session_state.webrtc_initialized = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
+
if not st.session_state.webrtc_initialized:
|
178 |
webrtc_ctx = webrtc_streamer(
|
179 |
key="audio-recorder",
|
180 |
mode=WebRtcMode.SENDONLY,
|
|
|
182 |
audio_receiver_size=256,
|
183 |
media_stream_constraints={"video": False, "audio": True},
|
184 |
)
|
185 |
+
st.session_state.webrtc_initialized = True
|
186 |
+
st.experimental_rerun() # Force Streamlit to rerun
|
|
|
|
|
|
|
|
|
187 |
|
188 |
+
if webrtc_ctx.audio_receiver:
|
189 |
+
st.write("Recording audio...")
|
190 |
+
audio_frames = webrtc_ctx.audio_receiver.get_frames(timeout=None)
|
191 |
+
audio_data = b"".join([frame for frame in audio_frames])
|
192 |
if st.button("Send Recording"):
|
193 |
+
send_message(audio_data=audio_data)
|
|
|
|
|
194 |
|
195 |
# --- Other Buttons ---
|
196 |
st.button("Clear Conversation", on_click=clear_conversation)
|
|
|
202 |
st.markdown(
|
203 |
"""
|
204 |
<script>
|
205 |
+
document.addEventListener('DOMContentLoaded', (event) => {
|
206 |
+
document.querySelector('.stTextArea textarea').addEventListener('keydown', function(e) {
|
207 |
+
if (e.key === 'Enter' && e.ctrlKey) {
|
208 |
+
document.querySelector('.stButton > button').click();
|
209 |
+
e.preventDefault();
|
210 |
}
|
211 |
});
|
212 |
+
});
|
213 |
</script>
|
214 |
""",
|
215 |
unsafe_allow_html=True
|
216 |
)
|
217 |
|
218 |
+
# --- Display Chat History ---
|
219 |
+
display_chat_history()
|
220 |
+
content_copy
|
221 |
+
Use code with caution.
|
222 |
+
Python
|
223 |
+
|
224 |
+
requirements.txt:
|
225 |
+
|
226 |
+
streamlit
|
227 |
+
streamlit-webrt
|
228 |
+
content_copy
|
229 |
+
Use code with caution.
|