ziyadsuper2017 commited on
Commit
fb00ecf
·
1 Parent(s): 326cdbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -10
app.py CHANGED
@@ -52,21 +52,25 @@ def send_message():
52
  if uploaded_files:
53
  st.session_state['use_vision_model'] = True
54
 
55
- # Prepare the prompt with chat history for the vision model
56
- chat_history_str = "\n".join(
57
- part['text'] for entry in st.session_state['chat_history'] for part in entry['parts'] if 'text' in part
58
- )
 
 
 
 
59
 
60
- # If there is text input, add it to the chat history string
61
  if user_input:
62
- chat_history_str += f"\n{user_input}"
63
  st.session_state['chat_history'].append({"role": "user", "parts": [{"text": user_input}]})
64
 
65
- # Add uploaded images to chat history string and session state
66
  if uploaded_files:
67
  for uploaded_file in uploaded_files:
68
  base64_image = get_image_base64(Image.open(uploaded_file))
69
- chat_history_str += f"\n[Image]"
70
  st.session_state['chat_history'].append({
71
  "role": "user",
72
  "parts": [{"mime_type": uploaded_file.type, "data": base64_image}]
@@ -82,8 +86,21 @@ def send_message():
82
  safety_settings=safety_settings
83
  )
84
 
 
 
 
85
  # Generate content from the chat history or the latest prompt
86
- response = model.generate_content([{"role": "user", "parts": [{"text": chat_history_str}]}])
 
 
 
 
 
 
 
 
 
 
87
  response_text = response.text if hasattr(response, "text") else "No response text found."
88
 
89
  # Display the model response
@@ -127,4 +144,4 @@ send_button = st.button("Send", on_click=send_message)
127
  clear_button = st.button("Clear Conversation", on_click=clear_conversation)
128
 
129
  # Ensure the file_uploader widget state is tied to the randomly generated key
130
- st.session_state.uploaded_files = uploaded_files
 
52
  if uploaded_files:
53
  st.session_state['use_vision_model'] = True
54
 
55
+ # Prepare the prompt for the vision model
56
+ prompts = []
57
+ for entry in st.session_state['chat_history']:
58
+ for part in entry['parts']:
59
+ if 'text' in part:
60
+ prompts.append(part['text'])
61
+ elif 'data' in part:
62
+ prompts.append("[Image]")
63
 
64
+ # If there is text input, add it to the list of prompts
65
  if user_input:
66
+ prompts.append(user_input)
67
  st.session_state['chat_history'].append({"role": "user", "parts": [{"text": user_input}]})
68
 
69
+ # Add uploaded images to the list of prompts and session state
70
  if uploaded_files:
71
  for uploaded_file in uploaded_files:
72
  base64_image = get_image_base64(Image.open(uploaded_file))
73
+ prompts.append("[Image]")
74
  st.session_state['chat_history'].append({
75
  "role": "user",
76
  "parts": [{"mime_type": uploaded_file.type, "data": base64_image}]
 
86
  safety_settings=safety_settings
87
  )
88
 
89
+ # Create a single prompt string from the list of prompts
90
+ chat_history_str = "\n".join(prompts)
91
+
92
  # Generate content from the chat history or the latest prompt
93
+ if st.session_state['use_vision_model']:
94
+ # When using vision model, include images as base64 strings
95
+ prompt_parts = [{"text": chat_history_str}] + [
96
+ {"data": part['data'], "mime_type": "image/jpeg"}
97
+ for entry in st.session_state['chat_history'] for part in entry['parts']
98
+ if 'data' in part
99
+ ]
100
+ else:
101
+ prompt_parts = [{"text": chat_history_str}]
102
+
103
+ response = model.generate_content([{"role": "user", "parts": prompt_parts}])
104
  response_text = response.text if hasattr(response, "text") else "No response text found."
105
 
106
  # Display the model response
 
144
  clear_button = st.button("Clear Conversation", on_click=clear_conversation)
145
 
146
  # Ensure the file_uploader widget state is tied to the randomly generated key
147
+ st.session_state.uploaded_files = uploaded_files