Deadmon commited on
Commit
47c4309
·
verified ·
1 Parent(s): ff6bd0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -1
app.py CHANGED
@@ -103,12 +103,29 @@ def chat_handler(prompt, user_image, chat_history, output_filename="generated_im
103
 
104
  return chat_history, user_image, img, ""
105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  # Create Gradio interface
107
  with gr.Blocks(title="Image Editing Chatbot") as demo:
108
  gr.Markdown("# Image Editing Chatbot")
109
  gr.Markdown("Upload an image and/or type a prompt to generate or edit an image using Google's Gemini model")
110
 
111
- # Chatbot display area for text messages
112
  chatbot = gr.Chatbot(
113
  label="Chat",
114
  height=300,
@@ -142,9 +159,43 @@ with gr.Blocks(title="Image Editing Chatbot") as demo:
142
  )
143
  generate_btn = gr.Button("Generate Image")
144
 
 
 
 
 
145
  # State to maintain chat history
146
  chat_state = gr.State([])
147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  # Connect the button to the chat handler
149
  generate_btn.click(
150
  fn=chat_handler,
 
103
 
104
  return chat_history, user_image, img, ""
105
 
106
+ # Function to update chat history with thumbnails
107
+ def update_chat_with_thumbnails(chat_history, uploaded_image_url, generated_image_url):
108
+ # Create a copy of the chat history to avoid modifying the input directly
109
+ updated_history = chat_history.copy()
110
+
111
+ # If there's an uploaded image, add its thumbnail to the chat history
112
+ if uploaded_image_url:
113
+ thumbnail_html = f'<img src="{uploaded_image_url}" width="100px" style="margin: 5px;" />'
114
+ updated_history.append({"role": "user", "content": thumbnail_html})
115
+
116
+ # If there's a generated image, add its thumbnail to the chat history
117
+ if generated_image_url:
118
+ thumbnail_html = f'<img src="{generated_image_url}" width="100px" style="margin: 5px;" />'
119
+ updated_history.append({"role": "assistant", "content": thumbnail_html})
120
+
121
+ return updated_history
122
+
123
  # Create Gradio interface
124
  with gr.Blocks(title="Image Editing Chatbot") as demo:
125
  gr.Markdown("# Image Editing Chatbot")
126
  gr.Markdown("Upload an image and/or type a prompt to generate or edit an image using Google's Gemini model")
127
 
128
+ # Chatbot display area for text messages and thumbnails
129
  chatbot = gr.Chatbot(
130
  label="Chat",
131
  height=300,
 
159
  )
160
  generate_btn = gr.Button("Generate Image")
161
 
162
+ # Hidden components to store image URLs
163
+ uploaded_image_url = gr.State("")
164
+ generated_image_url = gr.State("")
165
+
166
  # State to maintain chat history
167
  chat_state = gr.State([])
168
 
169
+ # JavaScript to extract image URLs and update the chat history
170
+ gr.HTML("""
171
+ <script>
172
+ async function updateImageURLs() {
173
+ // Get the image elements from the gr.Image components
174
+ const uploadedImage = document.querySelector('#component-3 img'); // Adjust component ID based on Gradio's generated IDs
175
+ const generatedImage = document.querySelector('#component-4 img'); // Adjust component ID based on Gradio's generated IDs
176
+
177
+ // Extract the src attributes (URLs)
178
+ const uploadedImageURL = uploadedImage && uploadedImage.src ? uploadedImage.src : "";
179
+ const generatedImageURL = generatedImage && generatedImage.src ? generatedImage.src : "";
180
+
181
+ // Call the Python function to update the chat history with the URLs
182
+ const chatHistory = await gradioApp().querySelector('#component-2').value; // Adjust component ID for chat_state
183
+ const result = await gradioApp().querySelector('#component-0').callFunction('update_chat_with_thumbnails', [chatHistory, uploadedImageURL, generatedImageURL]);
184
+ return result;
185
+ }
186
+
187
+ // Run the function when the Generate Image button is clicked
188
+ document.querySelector('#component-8').addEventListener('click', async () => { // Adjust component ID for generate_btn
189
+ // Wait for the images to update
190
+ setTimeout(async () => {
191
+ const updatedChat = await updateImageURLs();
192
+ // Update the chatbot component with the new history
193
+ document.querySelector('#component-1').value = updatedChat; // Adjust component ID for chatbot
194
+ }, 1000); // Delay to ensure images are loaded
195
+ });
196
+ </script>
197
+ """)
198
+
199
  # Connect the button to the chat handler
200
  generate_btn.click(
201
  fn=chat_handler,