Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ def save_binary_file(file_name, data):
|
|
12 |
f.write(data)
|
13 |
f.close()
|
14 |
|
15 |
-
def generate_image(prompt, image=None):
|
16 |
# Initialize client with the API key
|
17 |
client = genai.Client(
|
18 |
api_key="AIzaSyAQcy3LfrkMy6DqS_8MqftAXu1Bx_ov_E8",
|
@@ -89,21 +89,17 @@ def generate_image(prompt, image=None): # Removed output_filename parameter
|
|
89 |
|
90 |
|
91 |
# Function to handle chat interaction
|
92 |
-
def chat_handler(prompt, user_image, chat_history):
|
93 |
# Add the user prompt to the chat history - ONLY TEXT PROMPT for user message
|
94 |
if prompt:
|
95 |
chat_history.append({"role": "user", "content": prompt})
|
96 |
if user_image is not None:
|
97 |
-
# If there's a user image, add a separate message for the
|
98 |
-
user_thumbnail_size = (100, 100)
|
99 |
-
user_thumbnail = user_image.copy()
|
100 |
-
user_thumbnail.thumbnail(user_thumbnail_size)
|
101 |
buffered = io.BytesIO()
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
chat_history.append({"role": "user", "content": gr.HTML(f'<img src="{
|
106 |
-
|
107 |
|
108 |
# If no input, return early
|
109 |
if not prompt and not user_image:
|
@@ -111,27 +107,21 @@ def chat_handler(prompt, user_image, chat_history): # Removed output_filename pa
|
|
111 |
return chat_history, user_image, None, ""
|
112 |
|
113 |
# Generate image based on user input
|
114 |
-
img, status = generate_image(prompt or "Generate an image", user_image)
|
115 |
|
116 |
thumbnail_data_uri = None # Initialize to None
|
117 |
if img:
|
118 |
-
#
|
119 |
-
|
120 |
-
thumbnail = img.copy()
|
121 |
-
thumbnail = thumbnail.convert("RGB") # Force RGB mode for thumbnail
|
122 |
-
thumbnail.thumbnail(thumbnail_size)
|
123 |
-
|
124 |
-
# Convert thumbnail to base64 for chatbot display
|
125 |
buffered = io.BytesIO()
|
126 |
-
|
127 |
thumbnail_base64 = base64.b64encode(buffered.getvalue()).decode()
|
128 |
thumbnail_data_uri = f"data:image/png;base64,{thumbnail_base64}"
|
129 |
-
print("
|
130 |
-
assistant_message_content = gr.HTML(f'<img src="{thumbnail_data_uri}" alt="Generated Image
|
131 |
else:
|
132 |
assistant_message_content = status # If no image, send text status
|
133 |
|
134 |
-
|
135 |
# Update chat history - Assistant message is now EITHER gr.HTML or text
|
136 |
chat_history.append({"role": "assistant", "content": assistant_message_content})
|
137 |
|
@@ -169,12 +159,6 @@ with gr.Blocks(title="Image Editing Chatbot") as demo:
|
|
169 |
placeholder="Enter your image description here...",
|
170 |
lines=3
|
171 |
)
|
172 |
-
# Removed filename_input
|
173 |
-
# filename_input = gr.Textbox(
|
174 |
-
# label="Output Filename",
|
175 |
-
# value="generated_image",
|
176 |
-
# placeholder="Enter desired filename (without extension)"
|
177 |
-
# )
|
178 |
generate_btn = gr.Button("Generate Image")
|
179 |
|
180 |
# State to maintain chat history
|
@@ -183,14 +167,14 @@ with gr.Blocks(title="Image Editing Chatbot") as demo:
|
|
183 |
# Connect the button to the chat handler
|
184 |
generate_btn.click(
|
185 |
fn=chat_handler,
|
186 |
-
inputs=[prompt_input, image_input, chat_state],
|
187 |
outputs=[chatbot, uploaded_image_output, generated_image_output, prompt_input]
|
188 |
)
|
189 |
|
190 |
# Also allow Enter key to submit
|
191 |
prompt_input.submit(
|
192 |
fn=chat_handler,
|
193 |
-
inputs=[prompt_input, image_input, chat_state],
|
194 |
outputs=[chatbot, uploaded_image_output, generated_image_output, prompt_input]
|
195 |
)
|
196 |
|
|
|
12 |
f.write(data)
|
13 |
f.close()
|
14 |
|
15 |
+
def generate_image(prompt, image=None):
|
16 |
# Initialize client with the API key
|
17 |
client = genai.Client(
|
18 |
api_key="AIzaSyAQcy3LfrkMy6DqS_8MqftAXu1Bx_ov_E8",
|
|
|
89 |
|
90 |
|
91 |
# Function to handle chat interaction
|
92 |
+
def chat_handler(prompt, user_image, chat_history):
|
93 |
# Add the user prompt to the chat history - ONLY TEXT PROMPT for user message
|
94 |
if prompt:
|
95 |
chat_history.append({"role": "user", "content": prompt})
|
96 |
if user_image is not None:
|
97 |
+
# If there's a user image, add a separate message for the high-quality image in a smaller container
|
|
|
|
|
|
|
98 |
buffered = io.BytesIO()
|
99 |
+
user_image.save(buffered, format="PNG")
|
100 |
+
user_image_base64 = base64.b64encode(buffered.getvalue()).decode()
|
101 |
+
user_image_data_uri = f"data:image/png;base64,{user_image_base64}"
|
102 |
+
chat_history.append({"role": "user", "content": gr.HTML(f'<img src="{user_image_data_uri}" alt="Uploaded Image" style="width:100px; height:100px; object-fit:contain;">')})
|
|
|
103 |
|
104 |
# If no input, return early
|
105 |
if not prompt and not user_image:
|
|
|
107 |
return chat_history, user_image, None, ""
|
108 |
|
109 |
# Generate image based on user input
|
110 |
+
img, status = generate_image(prompt or "Generate an image", user_image)
|
111 |
|
112 |
thumbnail_data_uri = None # Initialize to None
|
113 |
if img:
|
114 |
+
# Use full-resolution image in a smaller container
|
115 |
+
img = img.convert("RGB") # Force RGB mode for consistency
|
|
|
|
|
|
|
|
|
|
|
116 |
buffered = io.BytesIO()
|
117 |
+
img.save(buffered, format="PNG")
|
118 |
thumbnail_base64 = base64.b64encode(buffered.getvalue()).decode()
|
119 |
thumbnail_data_uri = f"data:image/png;base64,{thumbnail_base64}"
|
120 |
+
print("Image Data URI:", thumbnail_data_uri) # Print to console
|
121 |
+
assistant_message_content = gr.HTML(f'<img src="{thumbnail_data_uri}" alt="Generated Image" style="width:100px; height:100px; object-fit:contain;">') # Use gr.HTML with CSS
|
122 |
else:
|
123 |
assistant_message_content = status # If no image, send text status
|
124 |
|
|
|
125 |
# Update chat history - Assistant message is now EITHER gr.HTML or text
|
126 |
chat_history.append({"role": "assistant", "content": assistant_message_content})
|
127 |
|
|
|
159 |
placeholder="Enter your image description here...",
|
160 |
lines=3
|
161 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
generate_btn = gr.Button("Generate Image")
|
163 |
|
164 |
# State to maintain chat history
|
|
|
167 |
# Connect the button to the chat handler
|
168 |
generate_btn.click(
|
169 |
fn=chat_handler,
|
170 |
+
inputs=[prompt_input, image_input, chat_state],
|
171 |
outputs=[chatbot, uploaded_image_output, generated_image_output, prompt_input]
|
172 |
)
|
173 |
|
174 |
# Also allow Enter key to submit
|
175 |
prompt_input.submit(
|
176 |
fn=chat_handler,
|
177 |
+
inputs=[prompt_input, image_input, chat_state],
|
178 |
outputs=[chatbot, uploaded_image_output, generated_image_output, prompt_input]
|
179 |
)
|
180 |
|