Spaces:
Runtime error
Runtime error
Commit
·
f7d3fbb
1
Parent(s):
327d4af
Update app.py
Browse files
app.py
CHANGED
@@ -19,8 +19,6 @@ generation_config = genai.GenerationConfig(
|
|
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 |
if 'using_vision_model' not in st.session_state:
|
@@ -39,7 +37,6 @@ def get_image_base64(image):
|
|
39 |
|
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 |
|
@@ -48,27 +45,25 @@ def send_message():
|
|
48 |
user_input = st.session_state.user_input
|
49 |
uploaded_files = st.session_state.uploaded_files
|
50 |
|
51 |
-
#
|
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 |
-
|
56 |
-
|
57 |
-
#
|
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 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
# Use Gemini Pro Vision model for image-based interaction
|
74 |
vision_model = genai.GenerativeModel(
|
@@ -76,14 +71,31 @@ def send_message():
|
|
76 |
generation_config=generation_config,
|
77 |
safety_settings=safety_settings
|
78 |
)
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
response_text = response.text if hasattr(response, "text") else "No response text found."
|
81 |
-
st.
|
82 |
|
83 |
-
# Clear the user input and
|
84 |
st.session_state.user_input = ''
|
85 |
st.session_state.uploaded_files = []
|
86 |
-
st.session_state.file_uploader_key = str(uuid.uuid4())
|
87 |
|
88 |
# Multiline text input for the user to send messages
|
89 |
user_input = st.text_area("Enter your message here:", key="user_input")
|
@@ -92,7 +104,7 @@ user_input = st.text_area("Enter your message here:", key="user_input")
|
|
92 |
uploaded_files = st.file_uploader(
|
93 |
"Upload images:",
|
94 |
type=["png", "jpg", "jpeg"],
|
95 |
-
accept_multiple_files=
|
96 |
key=st.session_state.file_uploader_key
|
97 |
)
|
98 |
|
@@ -100,16 +112,4 @@ uploaded_files = st.file_uploader(
|
|
100 |
send_button = st.button("Send", on_click=send_message)
|
101 |
|
102 |
# Button to clear the conversation
|
103 |
-
clear_button = st.button("Clear Conversation", on_click=clear_conversation)
|
104 |
-
|
105 |
-
# Display the chat history
|
106 |
-
for entry in st.session_state['chat_history']:
|
107 |
-
role = entry["role"]
|
108 |
-
parts = entry["parts"][0]
|
109 |
-
if 'text' in parts:
|
110 |
-
st.markdown(f"{role.title()}: {parts['text']}")
|
111 |
-
elif 'data' in parts:
|
112 |
-
st.markdown(f"{role.title()}: (Image)")
|
113 |
-
|
114 |
-
# Ensure the file_uploader widget state is tied to the randomly generated key
|
115 |
-
st.session_state.uploaded_files = uploaded_files
|
|
|
19 |
safety_settings = []
|
20 |
|
21 |
# Initialize session state for chat history and file uploader key
|
|
|
|
|
22 |
if 'file_uploader_key' not in st.session_state:
|
23 |
st.session_state['file_uploader_key'] = str(uuid.uuid4())
|
24 |
if 'using_vision_model' not in st.session_state:
|
|
|
37 |
|
38 |
# Function to clear conversation
|
39 |
def clear_conversation():
|
|
|
40 |
st.session_state['using_vision_model'] = False
|
41 |
st.session_state['file_uploader_key'] = str(uuid.uuid4())
|
42 |
|
|
|
45 |
user_input = st.session_state.user_input
|
46 |
uploaded_files = st.session_state.uploaded_files
|
47 |
|
48 |
+
# Check if an image has been uploaded or if we are continuing with the vision model
|
|
|
49 |
if uploaded_files or st.session_state['using_vision_model']:
|
50 |
st.session_state['using_vision_model'] = True
|
51 |
+
image_prompt = None
|
52 |
+
|
53 |
+
# Create the prompt for the vision model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
if uploaded_files:
|
55 |
+
image = Image.open(uploaded_files[0]) # Only take the first image for simplicity
|
56 |
+
image_base64 = get_image_base64(image)
|
57 |
+
image_prompt = {
|
58 |
+
"role": "user",
|
59 |
+
"parts": [{"mime_type": uploaded_files[0].type, "data": image_base64}]
|
60 |
+
}
|
61 |
+
elif user_input:
|
62 |
+
# Text input after using vision model
|
63 |
+
image_prompt = {
|
64 |
+
"role": "user",
|
65 |
+
"parts": [{"text": user_input}]
|
66 |
+
}
|
67 |
|
68 |
# Use Gemini Pro Vision model for image-based interaction
|
69 |
vision_model = genai.GenerativeModel(
|
|
|
71 |
generation_config=generation_config,
|
72 |
safety_settings=safety_settings
|
73 |
)
|
74 |
+
|
75 |
+
response = vision_model.generate_content([image_prompt])
|
76 |
+
response_text = response.text if hasattr(response, "text") else "No response text found."
|
77 |
+
st.write("AI: " + response_text)
|
78 |
+
|
79 |
+
# If no images are uploaded and we haven't used the vision model yet, use Gemini Pro model
|
80 |
+
elif user_input and not st.session_state['using_vision_model']:
|
81 |
+
text_prompt = {
|
82 |
+
"role": "user",
|
83 |
+
"parts": [{"text": user_input}]
|
84 |
+
}
|
85 |
+
|
86 |
+
text_model = genai.GenerativeModel(
|
87 |
+
model_name='gemini-pro',
|
88 |
+
generation_config=generation_config,
|
89 |
+
safety_settings=safety_settings
|
90 |
+
)
|
91 |
+
|
92 |
+
response = text_model.generate_content([text_prompt])
|
93 |
response_text = response.text if hasattr(response, "text") else "No response text found."
|
94 |
+
st.write("AI: " + response_text)
|
95 |
|
96 |
+
# Clear the user input and uploaded files
|
97 |
st.session_state.user_input = ''
|
98 |
st.session_state.uploaded_files = []
|
|
|
99 |
|
100 |
# Multiline text input for the user to send messages
|
101 |
user_input = st.text_area("Enter your message here:", key="user_input")
|
|
|
104 |
uploaded_files = st.file_uploader(
|
105 |
"Upload images:",
|
106 |
type=["png", "jpg", "jpeg"],
|
107 |
+
accept_multiple_files=False, # For simplicity, we're only accepting one image at a time
|
108 |
key=st.session_state.file_uploader_key
|
109 |
)
|
110 |
|
|
|
112 |
send_button = st.button("Send", on_click=send_message)
|
113 |
|
114 |
# Button to clear the conversation
|
115 |
+
clear_button = st.button("Clear Conversation", on_click=clear_conversation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|