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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -59
app.py CHANGED
@@ -85,15 +85,19 @@ def generate_image(prompt, image=None, output_filename="generated_image"):
85
  return None, "No image generated"
86
 
87
  # Function to handle chat interaction
88
- def chat_handler(prompt, user_image, chat_history, output_filename="generated_image"):
89
  # Add the prompt to the chat history
90
  if prompt:
91
  chat_history.append({"role": "user", "content": prompt})
92
 
 
 
 
 
93
  # If no input, return early
94
  if not prompt and not user_image:
95
  chat_history.append({"role": "assistant", "content": "Please provide a prompt or an image."})
96
- return chat_history, user_image, None, ""
97
 
98
  # Generate image based on user input
99
  img, status = generate_image(prompt or "Generate an image", user_image, output_filename)
@@ -101,31 +105,18 @@ def chat_handler(prompt, user_image, chat_history, output_filename="generated_im
101
  # Add the status message to the chat history
102
  chat_history.append({"role": "assistant", "content": status})
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,
@@ -133,6 +124,15 @@ with gr.Blocks(title="Image Editing Chatbot") as demo:
133
  avatar_images=(None, None)
134
  )
135
 
 
 
 
 
 
 
 
 
 
136
  # Separate image outputs
137
  with gr.Row():
138
  uploaded_image_output = gr.Image(label="Uploaded Image")
@@ -159,55 +159,22 @@ 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'); // 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,
202
- inputs=[prompt_input, image_input, chat_state, filename_input],
203
- outputs=[chatbot, uploaded_image_output, generated_image_output, prompt_input]
204
  )
205
 
206
  # Also allow Enter key to submit
207
  prompt_input.submit(
208
  fn=chat_handler,
209
- inputs=[prompt_input, image_input, chat_state, filename_input],
210
- outputs=[chatbot, uploaded_image_output, generated_image_output, prompt_input]
211
  )
212
 
213
  if __name__ == "__main__":
 
85
  return None, "No image generated"
86
 
87
  # Function to handle chat interaction
88
+ def chat_handler(prompt, user_image, chat_history, image_history, output_filename="generated_image"):
89
  # Add the prompt to the chat history
90
  if prompt:
91
  chat_history.append({"role": "user", "content": prompt})
92
 
93
+ # Add the uploaded image to the image history
94
+ if user_image:
95
+ image_history.append(user_image)
96
+
97
  # If no input, return early
98
  if not prompt and not user_image:
99
  chat_history.append({"role": "assistant", "content": "Please provide a prompt or an image."})
100
+ return chat_history, user_image, None, image_history, ""
101
 
102
  # Generate image based on user input
103
  img, status = generate_image(prompt or "Generate an image", user_image, output_filename)
 
105
  # Add the status message to the chat history
106
  chat_history.append({"role": "assistant", "content": status})
107
 
108
+ # Add the generated image to the image history
109
+ if img:
110
+ image_history.append(img)
 
 
 
 
 
 
 
 
111
 
112
+ return chat_history, user_image, img, image_history, ""
 
 
 
 
 
113
 
114
  # Create Gradio interface
115
  with gr.Blocks(title="Image Editing Chatbot") as demo:
116
  gr.Markdown("# Image Editing Chatbot")
117
  gr.Markdown("Upload an image and/or type a prompt to generate or edit an image using Google's Gemini model")
118
 
119
+ # Chatbot display area for text messages
120
  chatbot = gr.Chatbot(
121
  label="Chat",
122
  height=300,
 
124
  avatar_images=(None, None)
125
  )
126
 
127
+ # Gallery to display image history as thumbnails
128
+ image_history_display = gr.Gallery(
129
+ label="Image History",
130
+ height=200,
131
+ columns=4,
132
+ object_fit="contain",
133
+ preview=True
134
+ )
135
+
136
  # Separate image outputs
137
  with gr.Row():
138
  uploaded_image_output = gr.Image(label="Uploaded Image")
 
159
  )
160
  generate_btn = gr.Button("Generate Image")
161
 
162
+ # State to maintain chat history and image history
 
 
 
 
163
  chat_state = gr.State([])
164
+ image_history_state = gr.State([])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
 
166
  # Connect the button to the chat handler
167
  generate_btn.click(
168
  fn=chat_handler,
169
+ inputs=[prompt_input, image_input, chat_state, image_history_state, filename_input],
170
+ outputs=[chatbot, uploaded_image_output, generated_image_output, image_history_display, prompt_input]
171
  )
172
 
173
  # Also allow Enter key to submit
174
  prompt_input.submit(
175
  fn=chat_handler,
176
+ inputs=[prompt_input, image_input, chat_state, image_history_state, filename_input],
177
+ outputs=[chatbot, uploaded_image_output, generated_image_output, image_history_display, prompt_input]
178
  )
179
 
180
  if __name__ == "__main__":