Spaces:
Runtime error
Runtime error
Commit
·
327d4af
1
Parent(s):
40e8df5
Update app.py
Browse files
app.py
CHANGED
@@ -23,6 +23,8 @@ 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")
|
@@ -38,6 +40,7 @@ def get_image_base64(image):
|
|
38 |
# Function to clear conversation
|
39 |
def clear_conversation():
|
40 |
st.session_state['chat_history'] = []
|
|
|
41 |
st.session_state['file_uploader_key'] = str(uuid.uuid4())
|
42 |
|
43 |
# Function to send message and clear input
|
@@ -45,40 +48,35 @@ def send_message():
|
|
45 |
user_input = st.session_state.user_input
|
46 |
uploaded_files = st.session_state.uploaded_files
|
47 |
|
48 |
-
#
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
#
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
# Use Gemini Pro Vision model for image-based interaction
|
61 |
vision_model = genai.GenerativeModel(
|
62 |
model_name='gemini-pro-vision',
|
63 |
generation_config=generation_config,
|
64 |
safety_settings=safety_settings
|
65 |
)
|
66 |
-
response = vision_model.generate_content(
|
67 |
-
response_text = response.text if hasattr(response, "text") else "No response text found."
|
68 |
-
|
69 |
-
# Append images and response to chat history
|
70 |
-
for prompt in image_prompts:
|
71 |
-
st.session_state['chat_history'].append(prompt)
|
72 |
-
st.session_state['chat_history'].append({"role": "model", "parts": [{"text": response_text}]})
|
73 |
-
|
74 |
-
# If no images are uploaded, use Gemini Pro model for text-based interaction
|
75 |
-
elif user_input:
|
76 |
-
text_model = genai.GenerativeModel(
|
77 |
-
model_name='gemini-pro',
|
78 |
-
generation_config=generation_config,
|
79 |
-
safety_settings=safety_settings
|
80 |
-
)
|
81 |
-
response = text_model.generate_content(st.session_state['chat_history'])
|
82 |
response_text = response.text if hasattr(response, "text") else "No response text found."
|
83 |
st.session_state['chat_history'].append({"role": "model", "parts": [{"text": response_text}]})
|
84 |
|
|
|
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 |
+
if 'using_vision_model' not in st.session_state:
|
27 |
+
st.session_state['using_vision_model'] = False
|
28 |
|
29 |
# UI layout
|
30 |
st.title("Gemini Chatbot")
|
|
|
40 |
# Function to clear conversation
|
41 |
def clear_conversation():
|
42 |
st.session_state['chat_history'] = []
|
43 |
+
st.session_state['using_vision_model'] = False
|
44 |
st.session_state['file_uploader_key'] = str(uuid.uuid4())
|
45 |
|
46 |
# Function to send message and clear input
|
|
|
48 |
user_input = st.session_state.user_input
|
49 |
uploaded_files = st.session_state.uploaded_files
|
50 |
|
51 |
+
# If images are uploaded or we have already used the vision model,
|
52 |
+
# bundle the entire conversation history into a single-turn interaction.
|
53 |
+
if uploaded_files or st.session_state['using_vision_model']:
|
54 |
+
st.session_state['using_vision_model'] = True
|
55 |
+
prompts = []
|
56 |
+
|
57 |
+
# Add all previous chat history to the prompt
|
58 |
+
for entry in st.session_state['chat_history']:
|
59 |
+
parts = entry["parts"][0]
|
60 |
+
prompts.append({
|
61 |
+
"role": entry["role"],
|
62 |
+
"parts": [{"text": parts.get("text"), "mime_type": parts.get("mime_type"), "data": parts.get("data")}]
|
63 |
+
})
|
64 |
+
|
65 |
+
# Add new user input or images to the prompt
|
66 |
+
if user_input:
|
67 |
+
prompts.append({"role": "user", "parts": [{"text": user_input}]})
|
68 |
+
if uploaded_files:
|
69 |
+
for uploaded_file in uploaded_files:
|
70 |
+
image_base64 = get_image_base64(Image.open(uploaded_file))
|
71 |
+
prompts.append({"role": "user", "parts": [{"mime_type": uploaded_file.type, "data": image_base64}]})
|
72 |
+
|
73 |
# Use Gemini Pro Vision model for image-based interaction
|
74 |
vision_model = genai.GenerativeModel(
|
75 |
model_name='gemini-pro-vision',
|
76 |
generation_config=generation_config,
|
77 |
safety_settings=safety_settings
|
78 |
)
|
79 |
+
response = vision_model.generate_content(prompts)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
response_text = response.text if hasattr(response, "text") else "No response text found."
|
81 |
st.session_state['chat_history'].append({"role": "model", "parts": [{"text": response_text}]})
|
82 |
|