Spaces:
Runtime error
Runtime error
ziyadsuper2017
commited on
Commit
•
fd4809b
1
Parent(s):
389cdce
Update app.py
Browse files
app.py
CHANGED
@@ -32,7 +32,7 @@ def get_image_base64(image):
|
|
32 |
img_str = base64.b64encode(buffered.getvalue()).decode()
|
33 |
return img_str
|
34 |
|
35 |
-
#
|
36 |
def send_message():
|
37 |
user_input = st.session_state['user_input']
|
38 |
uploaded_files = st.session_state['file_uploader']
|
@@ -40,39 +40,34 @@ def send_message():
|
|
40 |
if user_input or uploaded_files:
|
41 |
# Save user input to the chat history
|
42 |
if user_input:
|
43 |
-
st.session_state['chat_history'].append(
|
44 |
-
|
45 |
-
# Process
|
46 |
-
|
47 |
-
prompts = [{"role": "user", "parts": [{"text": user_input}]}]
|
48 |
-
model = genai.GenerativeModel(
|
49 |
-
model_name='gemini-pro',
|
50 |
-
generation_config=generation_config,
|
51 |
-
safety_settings=safety_settings
|
52 |
-
)
|
53 |
-
response = model.generate_content(prompts)
|
54 |
-
response_text = response.text if hasattr(response, "text") else "No response text found."
|
55 |
-
st.session_state['chat_history'].append(("Gemini", response_text))
|
56 |
-
|
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 |
-
|
64 |
image_prompt = {"role": "user", "parts": [{"mime_type": uploaded_file.type, "data": image_base64}]}
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
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="")
|
@@ -89,6 +84,10 @@ uploaded_files = st.file_uploader(
|
|
89 |
send_button = st.button("Send", on_click=send_message)
|
90 |
|
91 |
# Display the chat history
|
92 |
-
for
|
93 |
-
role
|
94 |
-
|
|
|
|
|
|
|
|
|
|
32 |
img_str = base64.b64encode(buffered.getvalue()).decode()
|
33 |
return img_str
|
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']
|
|
|
40 |
if user_input or uploaded_files:
|
41 |
# Save user input to the chat history
|
42 |
if user_input:
|
43 |
+
st.session_state['chat_history'].append({"role": "user", "parts": [{"text": user_input}]})
|
44 |
+
|
45 |
+
# Process uploaded images
|
46 |
+
image_prompts = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
if uploaded_files:
|
48 |
for uploaded_file in uploaded_files:
|
49 |
image = Image.open(uploaded_file).convert("RGB") # Ensure image is in RGB
|
50 |
image_base64 = get_image_base64(image)
|
|
|
|
|
51 |
image_prompt = {"role": "user", "parts": [{"mime_type": uploaded_file.type, "data": image_base64}]}
|
52 |
+
image_prompts.append(image_prompt)
|
53 |
+
st.session_state['chat_history'].extend(image_prompts)
|
54 |
+
|
55 |
+
# Choose the appropriate model based on the input type
|
56 |
+
model_name = 'gemini-pro-vision' if uploaded_files else 'gemini-pro'
|
57 |
+
model = genai.GenerativeModel(
|
58 |
+
model_name=model_name,
|
59 |
+
generation_config=generation_config,
|
60 |
+
safety_settings=safety_settings
|
61 |
+
)
|
62 |
+
|
63 |
+
# Generate the response
|
64 |
+
response = model.generate_content(st.session_state['chat_history'])
|
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="")
|
|
|
84 |
send_button = st.button("Send", on_click=send_message)
|
85 |
|
86 |
# Display the chat history
|
87 |
+
for entry in st.session_state['chat_history']:
|
88 |
+
role = entry["role"]
|
89 |
+
parts = entry["parts"][0]
|
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)")
|