Update app.py
Browse files
app.py
CHANGED
|
@@ -151,265 +151,63 @@ def remove_objects(image, object_name, guidance_scale, steps):
|
|
| 151 |
except Exception as e:
|
| 152 |
return image, f"❌ Error: {str(e)}"
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
/* Chat Panel Styling */
|
| 181 |
-
.chat-panel {
|
| 182 |
-
background: rgba(20, 20, 30, 0.8) !important;
|
| 183 |
-
backdrop-filter: blur(20px) !important;
|
| 184 |
-
border-radius: 16px !important;
|
| 185 |
-
border: 1px solid rgba(255, 255, 255, 0.1) !important;
|
| 186 |
-
padding: 1.5rem !important;
|
| 187 |
-
margin-bottom: 1rem !important;
|
| 188 |
-
}
|
| 189 |
-
|
| 190 |
-
.chat-panel .gr-form {
|
| 191 |
-
background: transparent !important;
|
| 192 |
-
}
|
| 193 |
-
|
| 194 |
-
/* Input Styling */
|
| 195 |
-
.gr-textbox input, .gr-slider input {
|
| 196 |
-
background: rgba(255, 255, 255, 0.1) !important;
|
| 197 |
-
border: 1px solid rgba(255, 255, 255, 0.3) !important;
|
| 198 |
-
border-radius: 25px !important;
|
| 199 |
-
color: white !important;
|
| 200 |
-
padding: 12px 16px !important;
|
| 201 |
-
}
|
| 202 |
-
|
| 203 |
-
.gr-textbox input:focus, .gr-slider input:focus {
|
| 204 |
-
border-color: #00ff88 !important;
|
| 205 |
-
box-shadow: 0 0 0 2px rgba(0, 255, 136, 0.2) !important;
|
| 206 |
-
}
|
| 207 |
-
|
| 208 |
-
.gr-textbox input::placeholder {
|
| 209 |
-
color: rgba(255, 255, 255, 0.5) !important;
|
| 210 |
-
}
|
| 211 |
-
|
| 212 |
-
/* Button Styling */
|
| 213 |
-
.gr-button {
|
| 214 |
-
background: linear-gradient(135deg, #00ff88, #00ccff) !important;
|
| 215 |
-
border: none !important;
|
| 216 |
-
border-radius: 25px !important;
|
| 217 |
-
color: #000 !important;
|
| 218 |
-
font-weight: 600 !important;
|
| 219 |
-
padding: 12px 24px !important;
|
| 220 |
-
transition: all 0.3s ease !important;
|
| 221 |
-
}
|
| 222 |
-
|
| 223 |
-
.gr-button:hover {
|
| 224 |
-
transform: translateY(-2px) !important;
|
| 225 |
-
box-shadow: 0 8px 25px rgba(0, 255, 136, 0.3) !important;
|
| 226 |
-
}
|
| 227 |
-
|
| 228 |
-
/* Output Panel Styling */
|
| 229 |
-
.output-panel {
|
| 230 |
-
background: rgba(10, 10, 20, 0.8) !important;
|
| 231 |
-
backdrop-filter: blur(20px) !important;
|
| 232 |
-
border-radius: 16px !important;
|
| 233 |
-
border: 1px solid rgba(255, 255, 255, 0.1) !important;
|
| 234 |
-
padding: 1.5rem !important;
|
| 235 |
-
text-align: center !important;
|
| 236 |
-
}
|
| 237 |
-
|
| 238 |
-
.gr-image {
|
| 239 |
-
border-radius: 16px !important;
|
| 240 |
-
border: 2px solid rgba(255, 255, 255, 0.1) !important;
|
| 241 |
-
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4) !important;
|
| 242 |
-
}
|
| 243 |
-
|
| 244 |
-
/* Status Text Styling */
|
| 245 |
-
.gr-textbox textarea {
|
| 246 |
-
background: rgba(255, 193, 7, 0.1) !important;
|
| 247 |
-
border: 1px solid rgba(255, 193, 7, 0.3) !important;
|
| 248 |
-
color: #ffc107 !important;
|
| 249 |
-
border-radius: 12px !important;
|
| 250 |
-
}
|
| 251 |
-
|
| 252 |
-
/* File Upload Styling */
|
| 253 |
-
.gr-file-upload {
|
| 254 |
-
background: rgba(255, 255, 255, 0.1) !important;
|
| 255 |
-
border: 2px dashed rgba(255, 255, 255, 0.3) !important;
|
| 256 |
-
border-radius: 16px !important;
|
| 257 |
-
color: white !important;
|
| 258 |
-
}
|
| 259 |
-
|
| 260 |
-
/* Examples Styling */
|
| 261 |
-
.gr-examples {
|
| 262 |
-
background: rgba(255, 255, 255, 0.05) !important;
|
| 263 |
-
border-radius: 12px !important;
|
| 264 |
-
padding: 1rem !important;
|
| 265 |
-
}
|
| 266 |
-
|
| 267 |
-
.gr-examples .gr-button {
|
| 268 |
-
background: rgba(255, 255, 255, 0.1) !important;
|
| 269 |
-
color: white !important;
|
| 270 |
-
border: 1px solid rgba(255, 255, 255, 0.3) !important;
|
| 271 |
-
font-size: 0.9rem !important;
|
| 272 |
-
}
|
| 273 |
-
|
| 274 |
-
/* Accordion Styling */
|
| 275 |
-
.gr-accordion {
|
| 276 |
-
background: rgba(255, 255, 255, 0.05) !important;
|
| 277 |
-
border: 1px solid rgba(255, 255, 255, 0.1) !important;
|
| 278 |
-
border-radius: 12px !important;
|
| 279 |
-
}
|
| 280 |
-
|
| 281 |
-
.gr-accordion summary {
|
| 282 |
-
color: white !important;
|
| 283 |
-
font-weight: 500 !important;
|
| 284 |
-
}
|
| 285 |
-
|
| 286 |
-
/* Responsive adjustments */
|
| 287 |
-
@media (max-width: 768px) {
|
| 288 |
-
.gr-row {
|
| 289 |
-
flex-direction: column !important;
|
| 290 |
-
}
|
| 291 |
-
|
| 292 |
-
.header-text h1 {
|
| 293 |
-
font-size: 2rem !important;
|
| 294 |
-
}
|
| 295 |
-
}
|
| 296 |
-
"""
|
| 297 |
-
|
| 298 |
-
# Create the Gradio interface with modern styling
|
| 299 |
-
with gr.Blocks(
|
| 300 |
-
title="🚀 AI Object Removal Chat",
|
| 301 |
-
theme=gr.themes.Base(
|
| 302 |
-
primary_hue="emerald",
|
| 303 |
-
secondary_hue="blue",
|
| 304 |
-
neutral_hue="slate",
|
| 305 |
-
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"]
|
| 306 |
-
),
|
| 307 |
-
css=custom_css,
|
| 308 |
-
fill_height=True
|
| 309 |
-
) as demo:
|
| 310 |
-
|
| 311 |
-
# Header Section
|
| 312 |
with gr.Row():
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
</div>
|
| 319 |
-
""")
|
| 320 |
-
|
| 321 |
-
# Main Content Layout
|
| 322 |
-
with gr.Row(equal_height=True):
|
| 323 |
-
# Left Panel - Chat-like Interface
|
| 324 |
-
with gr.Column(scale=2, elem_classes=["chat-panel"]):
|
| 325 |
-
gr.Markdown("### 💬 Chat Interface")
|
| 326 |
-
|
| 327 |
-
# Image Upload
|
| 328 |
-
input_image = gr.Image(
|
| 329 |
-
label="📷 Upload Your Image",
|
| 330 |
-
type="pil",
|
| 331 |
-
height=250,
|
| 332 |
-
container=True
|
| 333 |
-
)
|
| 334 |
-
|
| 335 |
-
# Object Input
|
| 336 |
-
object_name = gr.Textbox(
|
| 337 |
-
label="🎯 What should I remove?",
|
| 338 |
-
placeholder="Type any object name (e.g., person, car, dog, bottle, tree, sign...)",
|
| 339 |
-
value="person",
|
| 340 |
-
container=True,
|
| 341 |
-
info="💡 I understand synonyms and variations!"
|
| 342 |
-
)
|
| 343 |
-
|
| 344 |
-
# Quick Examples
|
| 345 |
with gr.Row():
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
# Advanced Settings
|
| 354 |
-
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 355 |
-
guidance_scale = gr.Slider(
|
| 356 |
-
minimum=1.0,
|
| 357 |
-
maximum=10.0,
|
| 358 |
-
value=2.5,
|
| 359 |
-
step=0.1,
|
| 360 |
-
label="🎯 Guidance Scale",
|
| 361 |
-
info="Higher = more faithful to prompt, lower = more creative"
|
| 362 |
-
)
|
| 363 |
-
|
| 364 |
-
steps = gr.Slider(
|
| 365 |
-
minimum=10,
|
| 366 |
-
maximum=50,
|
| 367 |
-
value=28,
|
| 368 |
-
step=2,
|
| 369 |
-
label="🔄 Processing Steps",
|
| 370 |
-
info="More steps = higher quality but slower processing"
|
| 371 |
-
)
|
| 372 |
-
|
| 373 |
-
# Process Button
|
| 374 |
-
process_btn = gr.Button(
|
| 375 |
-
"🚀 Remove Object",
|
| 376 |
-
variant="primary",
|
| 377 |
-
size="lg",
|
| 378 |
-
elem_id="process-btn"
|
| 379 |
-
)
|
| 380 |
-
|
| 381 |
-
# Right Panel - Output
|
| 382 |
-
with gr.Column(scale=3, elem_classes=["output-panel"]):
|
| 383 |
-
gr.Markdown("### 🖼️ Result")
|
| 384 |
-
|
| 385 |
-
output_image = gr.Image(
|
| 386 |
-
label="Processed Image",
|
| 387 |
-
type="pil",
|
| 388 |
-
height=400,
|
| 389 |
-
container=True,
|
| 390 |
-
interactive=False
|
| 391 |
-
)
|
| 392 |
-
|
| 393 |
-
status_text = gr.Textbox(
|
| 394 |
-
label="📊 Status & Information",
|
| 395 |
-
max_lines=4,
|
| 396 |
-
interactive=False,
|
| 397 |
-
container=True
|
| 398 |
-
)
|
| 399 |
|
| 400 |
-
|
| 401 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
process_btn.click(
|
| 403 |
-
fn=
|
| 404 |
-
inputs=[input_image, object_name,
|
| 405 |
-
outputs=[output_image, status_text]
|
| 406 |
)
|
| 407 |
-
|
| 408 |
-
#
|
| 409 |
object_name.submit(
|
| 410 |
-
fn=
|
| 411 |
-
inputs=[input_image, object_name,
|
| 412 |
-
outputs=[output_image, status_text]
|
| 413 |
)
|
| 414 |
|
| 415 |
if __name__ == "__main__":
|
|
|
|
| 151 |
except Exception as e:
|
| 152 |
return image, f"❌ Error: {str(e)}"
|
| 153 |
|
| 154 |
+
def _remove(image, object_name, guidance, steps):
|
| 155 |
+
img, ok = flux_inpainting(image, object_name, guidance, steps)
|
| 156 |
+
if ok:
|
| 157 |
+
status = f"✅ Removed **{object_name}** | Guidance={guidance}, Steps={steps}"
|
| 158 |
+
else:
|
| 159 |
+
status = f"⚠️ Failed to remove **{object_name}** – try adjusting settings."
|
| 160 |
+
return img, status
|
| 161 |
+
|
| 162 |
+
def chat_pipeline(image, object_name, guidance, steps, history):
|
| 163 |
+
"""
|
| 164 |
+
Triggered on each “Remove” click.
|
| 165 |
+
- history is a list of [user_msg, bot_msg] pairs
|
| 166 |
+
"""
|
| 167 |
+
# 1) record the user turn
|
| 168 |
+
user_msg = f"🖼️ Uploaded image & remove **{object_name}**"
|
| 169 |
+
# 2) run your model
|
| 170 |
+
result_img, status = _remove(image, object_name, guidance, steps)
|
| 171 |
+
# 3) record the assistant turn
|
| 172 |
+
bot_msg = status
|
| 173 |
+
# 4) append to history
|
| 174 |
+
history = history + [[user_msg, bot_msg]]
|
| 175 |
+
# 5) return updated history + preview
|
| 176 |
+
return history, history, result_img, status
|
| 177 |
+
|
| 178 |
+
with gr.Blocks(css=custom_css, title="🚀 AI Object Removal Chat") as demo:
|
| 179 |
+
# two‑column layout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
with gr.Row():
|
| 181 |
+
# ─── LEFT: chat + controls ─────────────────────────────
|
| 182 |
+
with gr.Column(scale=2):
|
| 183 |
+
chat = gr.Chatbot(label="💬 Conversation").style(height=450)
|
| 184 |
+
state = gr.State([]) # to store history
|
| 185 |
+
# controls below chat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
with gr.Row():
|
| 187 |
+
input_image = gr.Image(type="pil", label="📷 Upload")
|
| 188 |
+
object_name = gr.Textbox(label="🎯 What to remove?", value="person")
|
| 189 |
+
with gr.Row():
|
| 190 |
+
guidance = gr.Slider(1.0, 10.0, value=2.5, label="Guidance")
|
| 191 |
+
steps = gr.Slider(10, 50, value=28, step=2, label="Steps")
|
| 192 |
+
process_btn = gr.Button("🚀 Remove Object", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
+
# ─── RIGHT: preview ───────────────────────────────────
|
| 195 |
+
with gr.Column(scale=3):
|
| 196 |
+
output_image = gr.Image(label="🖼 Result", interactive=False)
|
| 197 |
+
status_text = gr.Markdown("", label="📊 Info")
|
| 198 |
+
|
| 199 |
+
# wire up the chat pipeline
|
| 200 |
process_btn.click(
|
| 201 |
+
fn=chat_pipeline,
|
| 202 |
+
inputs=[input_image, object_name, guidance, steps, state],
|
| 203 |
+
outputs=[chat, state, output_image, status_text]
|
| 204 |
)
|
| 205 |
+
|
| 206 |
+
# also send on Enter in the textbox
|
| 207 |
object_name.submit(
|
| 208 |
+
fn=chat_pipeline,
|
| 209 |
+
inputs=[input_image, object_name, guidance, steps, state],
|
| 210 |
+
outputs=[chat, state, output_image, status_text]
|
| 211 |
)
|
| 212 |
|
| 213 |
if __name__ == "__main__":
|