Spaces:
Runtime error
Runtime error
Commit
·
00bfc2f
1
Parent(s):
dfdbfa8
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ from PIL import Image
|
|
3 |
import io
|
4 |
import base64
|
5 |
|
|
|
6 |
import google.generativeai as genai
|
7 |
|
8 |
# Configure the API key (should be set as an environment variable or secure storage in production)
|
@@ -24,30 +25,12 @@ if 'chat_history' not in st.session_state:
|
|
24 |
st.title("Gemini Chatbot")
|
25 |
|
26 |
# Display the chat history
|
27 |
-
for
|
28 |
-
role, text = message
|
29 |
st.markdown(f"**{role.title()}:** {text}")
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
-
|
34 |
-
# File uploader for images
|
35 |
-
uploaded_files = st.file_uploader(
|
36 |
-
"Upload images:",
|
37 |
-
type=["png", "jpg", "jpeg"],
|
38 |
-
accept_multiple_files=True,
|
39 |
-
key="file_uploader"
|
40 |
-
)
|
41 |
-
|
42 |
-
# Function to convert image to base64
|
43 |
-
def get_image_base64(image):
|
44 |
-
buffered = io.BytesIO()
|
45 |
-
image.save(buffered, format="JPEG")
|
46 |
-
img_str = base64.b64encode(buffered.getvalue()).decode()
|
47 |
-
return img_str
|
48 |
-
|
49 |
-
# When the 'Send' button is clicked, process the input and generate a response
|
50 |
-
if st.button("Send", key="send_button"):
|
51 |
if user_input:
|
52 |
# Save user input to the chat history
|
53 |
st.session_state['chat_history'].append(("User", user_input))
|
@@ -87,9 +70,26 @@ if st.button("Send", key="send_button"):
|
|
87 |
st.session_state['chat_history'].append(("Gemini", response_text))
|
88 |
|
89 |
# Clear the input box after sending the message
|
90 |
-
st.session_state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
-
#
|
93 |
-
|
94 |
-
|
95 |
-
st.markdown(f"**{role.title()}:** {text}")
|
|
|
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
|
8 |
|
9 |
# Configure the API key (should be set as an environment variable or secure storage in production)
|
|
|
25 |
st.title("Gemini Chatbot")
|
26 |
|
27 |
# Display the chat history
|
28 |
+
for idx, (role, text) in enumerate(st.session_state['chat_history']):
|
|
|
29 |
st.markdown(f"**{role.title()}:** {text}")
|
30 |
|
31 |
+
# Function to handle sending messages
|
32 |
+
def send_message():
|
33 |
+
user_input = st.session_state.user_input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
if user_input:
|
35 |
# Save user input to the chat history
|
36 |
st.session_state['chat_history'].append(("User", user_input))
|
|
|
70 |
st.session_state['chat_history'].append(("Gemini", response_text))
|
71 |
|
72 |
# Clear the input box after sending the message
|
73 |
+
st.session_state.user_input = ""
|
74 |
+
|
75 |
+
# Text input for the user to send messages
|
76 |
+
user_input = st.text_input("Enter your message here:", key="user_input", on_change=send_message)
|
77 |
+
|
78 |
+
# File uploader for images
|
79 |
+
uploaded_files = st.file_uploader(
|
80 |
+
"Upload images:",
|
81 |
+
type=["png", "jpg", "jpeg"],
|
82 |
+
accept_multiple_files=True,
|
83 |
+
key="file_uploader"
|
84 |
+
)
|
85 |
+
|
86 |
+
# Function to convert image to base64
|
87 |
+
def get_image_base64(image):
|
88 |
+
buffered = io.BytesIO()
|
89 |
+
image.save(buffered, format="JPEG")
|
90 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
91 |
+
return img_str
|
92 |
|
93 |
+
# Button to send the message
|
94 |
+
# The button now doesn't need to do anything since the text input's on_change callback handles the logic
|
95 |
+
st.button("Send", key="send_button", on_click=send_message)
|
|