Deadmon commited on
Commit
e8d1c65
·
verified ·
1 Parent(s): 1a2d550

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -63
app.py CHANGED
@@ -3,7 +3,7 @@ import os
3
  import mimetypes
4
  from google import genai
5
  from google.genai import types
6
- import gradio as gr # Correct import for Gradio
7
  import io
8
  from PIL import Image
9
 
@@ -103,29 +103,12 @@ def chat_handler(prompt, user_image, chat_history, output_filename="generated_im
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 and uploaded_image_url.strip():
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 and generated_image_url.strip():
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,53 +142,9 @@ with gr.Blocks(title="Image Editing Chatbot") as demo:
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'); // Uploaded Image component
175
- const generatedImage = document.querySelector('#component-4 img'); // Generated Image component
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; // Chat state component
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 () => { // Generate Image button
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; // Chatbot component
194
- }, 1000); // Delay to ensure images are loaded
195
- });
196
-
197
- // Also run the function when the Enter key is pressed in the prompt input
198
- document.querySelector('#component-6').addEventListener('keypress', async (e) => { // Prompt input
199
- if (e.key === 'Enter') {
200
- setTimeout(async () => {
201
- const updatedChat = await updateImageURLs();
202
- document.querySelector('#component-1').value = updatedChat;
203
- }, 1000);
204
- }
205
- });
206
- </script>
207
- """)
208
-
209
  # Connect the button to the chat handler
210
  generate_btn.click(
211
  fn=chat_handler,
 
3
  import mimetypes
4
  from google import genai
5
  from google.genai import types
6
+ import gradio as gr
7
  import io
8
  from PIL import Image
9
 
 
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
  )
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,