Sayandip commited on
Commit
5c52538
·
verified ·
1 Parent(s): e8aad03

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -19
app.py CHANGED
@@ -170,16 +170,17 @@ def capture_video():
170
  """
171
  html(video_recorder_html, height=400)
172
 
 
173
  # --------- Main App ---------
174
  def main():
175
  st.set_page_config(page_title="Gemini 2.0 Flash Chat Q&A", layout="wide")
176
  st.title("\U0001F4C4 Team 18 Comprehensive Prototype Using Gemini 2.0 Flash")
177
 
178
- # Ensure that session state variables are correctly initialized
179
  if "conversation" not in st.session_state:
180
  st.session_state.conversation = []
181
  if "documents_text" not in st.session_state:
182
- st.session_state.documents_text = "" # Initialize as an empty string, not a list
183
  if "chat_active" not in st.session_state:
184
  st.session_state.chat_active = True
185
  if "chat_history" not in st.session_state:
@@ -195,6 +196,7 @@ def main():
195
  accept_multiple_files=True
196
  )
197
 
 
198
  if uploaded_files:
199
  all_text_content = ""
200
  for uploaded_file in uploaded_files:
@@ -283,28 +285,35 @@ def main():
283
  if st.session_state.documents_text.strip():
284
  content_blocks.append({"text": f"Context:\n{st.session_state.documents_text}"})
285
 
286
- if "image_data" in st.session_state:
287
- content_blocks.append(st.session_state.image_data)
 
288
 
289
- if "video_data" in st.session_state:
290
- content_blocks.append(st.session_state.video_data)
 
291
 
292
  content_blocks.append({"text": f"Question: {user_input}"})
293
 
294
- response = client.models.generate_content(
295
- model="gemini-2.0-flash",
296
- contents=content_blocks,
297
- config={"tools": [{"google_search": {}}]}
298
- )
 
 
299
 
300
- st.session_state.conversation.append((user_input, response.text))
301
- st.success("\U0001F4A1 Answer:")
302
- st.write(response.text)
303
 
304
- st.session_state.chat_history.append({
305
- "question": user_input,
306
- "answer": response.text
307
- })
 
 
 
308
 
309
  if st.session_state.conversation:
310
  pdf_path = export_conversation_to_pdf(st.session_state.conversation)
@@ -317,4 +326,6 @@ def main():
317
  )
318
 
319
  if __name__ == "__main__":
320
- main()
 
 
 
170
  """
171
  html(video_recorder_html, height=400)
172
 
173
+ # --------- Main App ---------
174
  # --------- Main App ---------
175
  def main():
176
  st.set_page_config(page_title="Gemini 2.0 Flash Chat Q&A", layout="wide")
177
  st.title("\U0001F4C4 Team 18 Comprehensive Prototype Using Gemini 2.0 Flash")
178
 
179
+ # Initialize session state variables
180
  if "conversation" not in st.session_state:
181
  st.session_state.conversation = []
182
  if "documents_text" not in st.session_state:
183
+ st.session_state.documents_text = "" # Initialize as an empty string
184
  if "chat_active" not in st.session_state:
185
  st.session_state.chat_active = True
186
  if "chat_history" not in st.session_state:
 
196
  accept_multiple_files=True
197
  )
198
 
199
+ # Handle the files uploaded
200
  if uploaded_files:
201
  all_text_content = ""
202
  for uploaded_file in uploaded_files:
 
285
  if st.session_state.documents_text.strip():
286
  content_blocks.append({"text": f"Context:\n{st.session_state.documents_text}"})
287
 
288
+ # Send image data if available
289
+ if "image_data" in st.session_state and st.session_state.image_data:
290
+ content_blocks.append({"image": st.session_state.image_data})
291
 
292
+ # Send video data if available
293
+ if "video_data" in st.session_state and st.session_state.video_data:
294
+ content_blocks.append({"video": st.session_state.video_data})
295
 
296
  content_blocks.append({"text": f"Question: {user_input}"})
297
 
298
+ try:
299
+ # Ensure we are sending the data in the correct format
300
+ response = client.models.generate_content(
301
+ model="gemini-2.0-flash",
302
+ contents=content_blocks,
303
+ config={"tools": [{"google_search": {}}]}
304
+ )
305
 
306
+ st.session_state.conversation.append((user_input, response.text))
307
+ st.success("\U0001F4A1 Answer:")
308
+ st.write(response.text)
309
 
310
+ st.session_state.chat_history.append({
311
+ "question": user_input,
312
+ "answer": response.text
313
+ })
314
+
315
+ except Exception as e:
316
+ st.error(f"Error with Gemini API request: {e}")
317
 
318
  if st.session_state.conversation:
319
  pdf_path = export_conversation_to_pdf(st.session_state.conversation)
 
326
  )
327
 
328
  if __name__ == "__main__":
329
+ main()
330
+
331
+