ziyadsuper2017 commited on
Commit
eabca2c
1 Parent(s): fd4809b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -8
app.py CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
2
  from PIL import Image
3
  import io
4
  import base64
 
5
 
6
  # Assuming google.generativeai as genai is the correct import based on your description
7
  import google.generativeai as genai
@@ -17,9 +18,11 @@ generation_config = genai.GenerationConfig(
17
 
18
  safety_settings = []
19
 
20
- # Initialize session state for chat history
21
  if 'chat_history' not in st.session_state:
22
  st.session_state['chat_history'] = []
 
 
23
 
24
  # UI layout
25
  st.title("Gemini Chatbot")
@@ -34,8 +37,8 @@ def get_image_base64(image):
34
 
35
  # Function to send message and clear input
36
  def send_message():
37
- user_input = st.session_state['user_input']
38
- uploaded_files = st.session_state['file_uploader']
39
 
40
  if user_input or uploaded_files:
41
  # Save user input to the chat history
@@ -65,9 +68,9 @@ def send_message():
65
  response_text = response.text if hasattr(response, "text") else "No response text found."
66
  st.session_state['chat_history'].append({"role": "model", "parts": [{"text": response_text}]})
67
 
68
- # Clear the input box after sending the message
69
- st.session_state.user_input = ""
70
- st.session_state.file_uploader = None # Clear uploaded files
71
 
72
  # Multiline text input for the user to send messages
73
  user_input = st.text_area("Enter your message here:", key="user_input", value="")
@@ -77,7 +80,7 @@ uploaded_files = st.file_uploader(
77
  "Upload images:",
78
  type=["png", "jpg", "jpeg"],
79
  accept_multiple_files=True,
80
- key="file_uploader"
81
  )
82
 
83
  # Button to send the message
@@ -90,4 +93,7 @@ for entry in st.session_state['chat_history']:
90
  if 'text' in parts:
91
  st.markdown(f"**{role.title()}**: {parts['text']}")
92
  elif 'data' in parts:
93
- st.markdown(f"**{role.title()}**: (Image)")
 
 
 
 
2
  from PIL import Image
3
  import io
4
  import base64
5
+ import uuid
6
 
7
  # Assuming google.generativeai as genai is the correct import based on your description
8
  import google.generativeai as genai
 
18
 
19
  safety_settings = []
20
 
21
+ # Initialize session state for chat history and file uploader key
22
  if 'chat_history' not in st.session_state:
23
  st.session_state['chat_history'] = []
24
+ if 'file_uploader_key' not in st.session_state:
25
+ st.session_state['file_uploader_key'] = str(uuid.uuid4())
26
 
27
  # UI layout
28
  st.title("Gemini Chatbot")
 
37
 
38
  # Function to send message and clear input
39
  def send_message():
40
+ user_input = st.session_state.user_input
41
+ uploaded_files = st.session_state.uploaded_files
42
 
43
  if user_input or uploaded_files:
44
  # Save user input to the chat history
 
68
  response_text = response.text if hasattr(response, "text") else "No response text found."
69
  st.session_state['chat_history'].append({"role": "model", "parts": [{"text": response_text}]})
70
 
71
+ # Clear the user input and generate a new key for the file uploader widget to reset it
72
+ st.session_state.user_input = ''
73
+ st.session_state.file_uploader_key = str(uuid.uuid4())
74
 
75
  # Multiline text input for the user to send messages
76
  user_input = st.text_area("Enter your message here:", key="user_input", value="")
 
80
  "Upload images:",
81
  type=["png", "jpg", "jpeg"],
82
  accept_multiple_files=True,
83
+ key=st.session_state.file_uploader_key
84
  )
85
 
86
  # Button to send the message
 
93
  if 'text' in parts:
94
  st.markdown(f"**{role.title()}**: {parts['text']}")
95
  elif 'data' in parts:
96
+ st.markdown(f"**{role.title()}**: (Image)")
97
+
98
+ # Ensure the file_uploader widget state is tied to the randomly generated key
99
+ st.session_state.uploaded_files = uploaded_files