Spaces:
Runtime error
Runtime error
Commit
·
389cdce
1
Parent(s):
ba4c612
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)
|
|
@@ -23,24 +24,9 @@ if 'chat_history' not in st.session_state:
|
|
| 23 |
# UI layout
|
| 24 |
st.title("Gemini Chatbot")
|
| 25 |
|
| 26 |
-
# Display the chat history
|
| 27 |
-
for message in st.session_state['chat_history']:
|
| 28 |
-
role, text = message
|
| 29 |
-
st.markdown(f"**{role.title()}**: {text}")
|
| 30 |
-
|
| 31 |
-
# Multiline text input for the user to send messages
|
| 32 |
-
user_input = st.text_area("Enter your message here:", key="user_input")
|
| 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()
|
|
@@ -48,6 +34,9 @@ def get_image_base64(image):
|
|
| 48 |
|
| 49 |
# Callback function to send message and clear input
|
| 50 |
def send_message():
|
|
|
|
|
|
|
|
|
|
| 51 |
if user_input or uploaded_files:
|
| 52 |
# Save user input to the chat history
|
| 53 |
if user_input:
|
|
@@ -68,8 +57,7 @@ def send_message():
|
|
| 68 |
# Process and save uploaded images to the chat history
|
| 69 |
if uploaded_files:
|
| 70 |
for uploaded_file in uploaded_files:
|
| 71 |
-
|
| 72 |
-
image = Image.open(io.BytesIO(bytes_data))
|
| 73 |
image_base64 = get_image_base64(image)
|
| 74 |
st.session_state['chat_history'].append(("User", f"Uploaded image: {uploaded_file.name}"))
|
| 75 |
|
|
@@ -86,10 +74,21 @@ def send_message():
|
|
| 86 |
# Clear the input box after sending the message
|
| 87 |
st.session_state.user_input = ""
|
| 88 |
|
| 89 |
-
#
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
#
|
| 93 |
for message in st.session_state['chat_history']:
|
| 94 |
role, text = message
|
| 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)
|
|
|
|
| 24 |
# UI layout
|
| 25 |
st.title("Gemini Chatbot")
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# Function to convert image to base64
|
| 28 |
def get_image_base64(image):
|
| 29 |
+
image = image.convert("RGB") # Convert to RGB to remove alpha channel if present
|
| 30 |
buffered = io.BytesIO()
|
| 31 |
image.save(buffered, format="JPEG")
|
| 32 |
img_str = base64.b64encode(buffered.getvalue()).decode()
|
|
|
|
| 34 |
|
| 35 |
# Callback 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
|
| 42 |
if user_input:
|
|
|
|
| 57 |
# Process and save uploaded images to the chat history
|
| 58 |
if uploaded_files:
|
| 59 |
for uploaded_file in uploaded_files:
|
| 60 |
+
image = Image.open(uploaded_file).convert("RGB") # Ensure image is in RGB
|
|
|
|
| 61 |
image_base64 = get_image_base64(image)
|
| 62 |
st.session_state['chat_history'].append(("User", f"Uploaded image: {uploaded_file.name}"))
|
| 63 |
|
|
|
|
| 74 |
# Clear the input box after sending the message
|
| 75 |
st.session_state.user_input = ""
|
| 76 |
|
| 77 |
+
# Multiline text input for the user to send messages
|
| 78 |
+
user_input = st.text_area("Enter your message here:", key="user_input", value="")
|
| 79 |
+
|
| 80 |
+
# File uploader for images
|
| 81 |
+
uploaded_files = st.file_uploader(
|
| 82 |
+
"Upload images:",
|
| 83 |
+
type=["png", "jpg", "jpeg"],
|
| 84 |
+
accept_multiple_files=True,
|
| 85 |
+
key="file_uploader"
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
# Button to send the message
|
| 89 |
+
send_button = st.button("Send", on_click=send_message)
|
| 90 |
|
| 91 |
+
# Display the chat history
|
| 92 |
for message in st.session_state['chat_history']:
|
| 93 |
role, text = message
|
| 94 |
st.markdown(f"**{role.title()}**: {text}")
|