Update app.py
Browse files
app.py
CHANGED
@@ -27,6 +27,8 @@ def load_flux_model():
|
|
27 |
# Global variable to store the model (loaded once)
|
28 |
flux_pipe = None
|
29 |
|
|
|
|
|
30 |
def fuzzy_match_object(user_input, detected_labels):
|
31 |
"""
|
32 |
Advanced matching function that handles synonyms, plurals, and fuzzy matching
|
@@ -81,6 +83,7 @@ def fuzzy_match_object(user_input, detected_labels):
|
|
81 |
|
82 |
return matches
|
83 |
|
|
|
84 |
@spaces.GPU
|
85 |
def flux_inpainting(image, object_name, guidance_scale=2.5, steps=28):
|
86 |
"""
|
@@ -117,6 +120,7 @@ def flux_inpainting(image, object_name, guidance_scale=2.5, steps=28):
|
|
117 |
print(f"FLUX inpainting error: {str(e)}")
|
118 |
return None, False
|
119 |
|
|
|
120 |
@spaces.GPU
|
121 |
def remove_objects(image, object_name, guidance_scale, steps):
|
122 |
"""
|
@@ -134,7 +138,7 @@ def remove_objects(image, object_name, guidance_scale, steps):
|
|
134 |
if not token:
|
135 |
raise gr.Error("Please provide your Hugging Face token or set HF_TOKEN in Space secrets")
|
136 |
|
137 |
-
# Use FLUX.1 Kontext for intelligent object removal
|
138 |
print("Using FLUX.1 Kontext for advanced object removal...")
|
139 |
result_image, flux_success = flux_inpainting(image, object_name, guidance_scale, steps)
|
140 |
|
@@ -149,191 +153,52 @@ def remove_objects(image, object_name, guidance_scale, steps):
|
|
149 |
return result_image, status_msg
|
150 |
|
151 |
except Exception as e:
|
152 |
-
return image, f"❌ Error: {str(e)}"
|
153 |
|
154 |
-
#
|
155 |
-
custom_css = """
|
156 |
-
/* Global Styles */
|
157 |
-
.gradio-container {
|
158 |
-
background: linear-gradient(135deg, #0a0a0a 0%, #1a1a2e 50%, #16213e 100%) !important;
|
159 |
-
color: white !important;
|
160 |
-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif !important;
|
161 |
-
}
|
162 |
-
/* Header Styling */
|
163 |
-
.header-text h1 {
|
164 |
-
background: linear-gradient(45deg, #00ff88, #00ccff) !important;
|
165 |
-
-webkit-background-clip: text !important;
|
166 |
-
-webkit-text-fill-color: transparent !important;
|
167 |
-
font-size: 2.5rem !important;
|
168 |
-
text-align: center !important;
|
169 |
-
margin-bottom: 1rem !important;
|
170 |
-
}
|
171 |
-
.header-text p {
|
172 |
-
text-align: center !important;
|
173 |
-
color: rgba(255, 255, 255, 0.8) !important;
|
174 |
-
font-size: 1.1rem !important;
|
175 |
-
margin-bottom: 2rem !important;
|
176 |
-
}
|
177 |
-
/* Chat Panel Styling */
|
178 |
-
.chat-panel {
|
179 |
-
background: rgba(20, 20, 30, 0.8) !important;
|
180 |
-
backdrop-filter: blur(20px) !important;
|
181 |
-
border-radius: 16px !important;
|
182 |
-
border: 1px solid rgba(255, 255, 255, 0.1) !important;
|
183 |
-
padding: 1.5rem !important;
|
184 |
-
margin-bottom: 1rem !important;
|
185 |
-
}
|
186 |
-
.chat-panel .gr-form {
|
187 |
-
background: transparent !important;
|
188 |
-
}
|
189 |
-
/* Input Styling */
|
190 |
-
.gr-textbox input, .gr-slider input {
|
191 |
-
background: rgba(255, 255, 255, 0.1) !important;
|
192 |
-
border: 1px solid rgba(255, 255, 255, 0.3) !important;
|
193 |
-
border-radius: 25px !important;
|
194 |
-
color: white !important;
|
195 |
-
padding: 12px 16px !important;
|
196 |
-
}
|
197 |
-
.gr-textbox input:focus, .gr-slider input:focus {
|
198 |
-
border-color: #00ff88 !important;
|
199 |
-
box-shadow: 0 0 0 2px rgba(0, 255, 136, 0.2) !important;
|
200 |
-
}
|
201 |
-
.gr-textbox input::placeholder {
|
202 |
-
color: rgba(255, 255, 255, 0.5) !important;
|
203 |
-
}
|
204 |
-
/* Button Styling */
|
205 |
-
.gr-button {
|
206 |
-
background: linear-gradient(135deg, #00ff88, #00ccff) !important;
|
207 |
-
border: none !important;
|
208 |
-
border-radius: 25px !important;
|
209 |
-
color: #000 !important;
|
210 |
-
font-weight: 600 !important;
|
211 |
-
padding: 12px 24px !important;
|
212 |
-
transition: all 0.3s ease !important;
|
213 |
-
}
|
214 |
-
.gr-button:hover {
|
215 |
-
transform: translateY(-2px) !important;
|
216 |
-
box-shadow: 0 8px 25px rgba(0, 255, 136, 0.3) !important;
|
217 |
-
}
|
218 |
-
/* Output Panel Styling */
|
219 |
-
.output-panel {
|
220 |
-
background: rgba(10, 10, 20, 0.8) !important;
|
221 |
-
backdrop-filter: blur(20px) !important;
|
222 |
-
border-radius: 16px !important;
|
223 |
-
border: 1px solid rgba(255, 255, 255, 0.1) !important;
|
224 |
-
padding: 1.5rem !important;
|
225 |
-
text-align: center !important;
|
226 |
-
}
|
227 |
-
.gr-image {
|
228 |
-
border-radius: 16px !important;
|
229 |
-
border: 2px solid rgba(255, 255, 255, 0.1) !important;
|
230 |
-
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4) !important;
|
231 |
-
}
|
232 |
-
/* Status Text Styling */
|
233 |
-
.gr-textbox textarea {
|
234 |
-
background: rgba(255, 193, 7, 0.1) !important;
|
235 |
-
border: 1px solid rgba(255, 193, 7, 0.3) !important;
|
236 |
-
color: #ffc107 !important;
|
237 |
-
border-radius: 12px !important;
|
238 |
-
}
|
239 |
-
/* File Upload Styling */
|
240 |
-
.gr-file-upload {
|
241 |
-
background: rgba(255, 255, 255, 0.1) !important;
|
242 |
-
border: 2px dashed rgba(255, 255, 255, 0.3) !important;
|
243 |
-
border-radius: 16px !important;
|
244 |
-
color: white !important;
|
245 |
-
}
|
246 |
-
/* Examples Styling */
|
247 |
-
.gr-examples {
|
248 |
-
background: rgba(255, 255, 255, 0.05) !important;
|
249 |
-
border-radius: 12px !important;
|
250 |
-
padding: 1rem !important;
|
251 |
-
}
|
252 |
-
.gr-examples .gr-button {
|
253 |
-
background: rgba(255, 255, 255, 0.1) !important;
|
254 |
-
color: white !important;
|
255 |
-
border: 1px solid rgba(255, 255, 255, 0.3) !important;
|
256 |
-
font-size: 0.9rem !important;
|
257 |
-
}
|
258 |
-
/* Accordion Styling */
|
259 |
-
.gr-accordion {
|
260 |
-
background: rgba(255, 255, 255, 0.05) !important;
|
261 |
-
border: 1px solid rgba(255, 255, 255, 0.1) !important;
|
262 |
-
border-radius: 12px !important;
|
263 |
-
}
|
264 |
-
.gr-accordion summary {
|
265 |
-
color: white !important;
|
266 |
-
font-weight: 500 !important;
|
267 |
-
}
|
268 |
-
/* Responsive adjustments */
|
269 |
-
@media (max-width: 768px) {
|
270 |
-
.gr-row {
|
271 |
-
flex-direction: column !important;
|
272 |
-
}
|
273 |
-
|
274 |
-
.header-text h1 {
|
275 |
-
font-size: 2rem !important;
|
276 |
-
}
|
277 |
-
}
|
278 |
-
"""
|
279 |
-
|
280 |
-
# Create the Gradio interface with modern styling
|
281 |
with gr.Blocks(
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
secondary_hue="blue",
|
286 |
-
neutral_hue="slate",
|
287 |
-
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"]
|
288 |
-
),
|
289 |
-
css=custom_css,
|
290 |
-
fill_height=True
|
291 |
) as demo:
|
292 |
|
293 |
-
|
294 |
-
|
295 |
-
with gr.Column():
|
296 |
-
gr.HTML("""
|
297 |
-
<div class="header-text">
|
298 |
-
<h1>🚀 AI Object Removal Chat</h1>
|
299 |
-
<p>Upload an image and tell me what object you want to remove with professional results!</p>
|
300 |
-
</div>
|
301 |
-
""")
|
302 |
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
|
|
|
|
308 |
|
309 |
-
# Image Upload
|
310 |
input_image = gr.Image(
|
311 |
-
label="
|
312 |
type="pil",
|
313 |
-
height=
|
314 |
-
container=True
|
315 |
)
|
316 |
|
317 |
-
# Object Input
|
318 |
object_name = gr.Textbox(
|
319 |
-
label="🎯
|
320 |
-
placeholder="
|
321 |
value="person",
|
322 |
-
|
323 |
-
info="💡 I understand synonyms and variations!"
|
324 |
)
|
325 |
|
326 |
-
#
|
327 |
with gr.Row():
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
|
|
|
|
334 |
|
335 |
-
# Advanced Settings
|
336 |
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
|
|
337 |
guidance_scale = gr.Slider(
|
338 |
minimum=1.0,
|
339 |
maximum=10.0,
|
@@ -348,51 +213,43 @@ with gr.Blocks(
|
|
348 |
maximum=50,
|
349 |
value=28,
|
350 |
step=2,
|
351 |
-
label="🔄
|
352 |
info="More steps = higher quality but slower processing"
|
353 |
)
|
354 |
|
355 |
-
|
356 |
-
|
357 |
-
"🚀 Remove Object",
|
358 |
-
variant="primary",
|
359 |
-
size="lg",
|
360 |
-
elem_id="process-btn"
|
361 |
-
)
|
362 |
|
363 |
-
|
364 |
-
|
365 |
-
gr.Markdown("
|
366 |
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
)
|
374 |
|
375 |
status_text = gr.Textbox(
|
376 |
-
label="📊 Status &
|
377 |
-
max_lines=4,
|
378 |
interactive=False,
|
379 |
-
|
380 |
)
|
381 |
-
|
382 |
|
383 |
-
# Event
|
384 |
-
|
385 |
fn=remove_objects,
|
386 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
387 |
outputs=[output_image, status_text]
|
388 |
)
|
389 |
|
390 |
-
|
391 |
-
object_name.submit(
|
392 |
-
fn=remove_objects,
|
393 |
-
inputs=[input_image, object_name, guidance_scale, steps],
|
394 |
-
outputs=[output_image, status_text]
|
395 |
-
)
|
396 |
|
397 |
if __name__ == "__main__":
|
398 |
demo.launch()
|
|
|
27 |
# Global variable to store the model (loaded once)
|
28 |
flux_pipe = None
|
29 |
|
30 |
+
# Initialize object detection using proven working models
|
31 |
+
|
32 |
def fuzzy_match_object(user_input, detected_labels):
|
33 |
"""
|
34 |
Advanced matching function that handles synonyms, plurals, and fuzzy matching
|
|
|
83 |
|
84 |
return matches
|
85 |
|
86 |
+
|
87 |
@spaces.GPU
|
88 |
def flux_inpainting(image, object_name, guidance_scale=2.5, steps=28):
|
89 |
"""
|
|
|
120 |
print(f"FLUX inpainting error: {str(e)}")
|
121 |
return None, False
|
122 |
|
123 |
+
|
124 |
@spaces.GPU
|
125 |
def remove_objects(image, object_name, guidance_scale, steps):
|
126 |
"""
|
|
|
138 |
if not token:
|
139 |
raise gr.Error("Please provide your Hugging Face token or set HF_TOKEN in Space secrets")
|
140 |
|
141 |
+
# Step 3: Use FLUX.1 Kontext for intelligent object removal
|
142 |
print("Using FLUX.1 Kontext for advanced object removal...")
|
143 |
result_image, flux_success = flux_inpainting(image, object_name, guidance_scale, steps)
|
144 |
|
|
|
153 |
return result_image, status_msg
|
154 |
|
155 |
except Exception as e:
|
156 |
+
return image, None, f"❌ Error: {str(e)}"
|
157 |
|
158 |
+
# Create Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
with gr.Blocks(
|
160 |
+
fill_height=True,
|
161 |
+
title="Professional Object Removal",
|
162 |
+
theme=gr.themes.Soft()
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
) as demo:
|
164 |
|
165 |
+
gr.Markdown("""
|
166 |
+
# 🚀 Professional Object Removal using Advanced AI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
+
Upload an image and specify **ANY object** you want to remove with professional results!
|
169 |
+
""")
|
170 |
+
|
171 |
+
with gr.Row():
|
172 |
+
with gr.Column(scale=1):
|
173 |
+
# Input section
|
174 |
+
gr.Markdown("## 📤 Input")
|
175 |
|
|
|
176 |
input_image = gr.Image(
|
177 |
+
label="Upload Image",
|
178 |
type="pil",
|
179 |
+
height=300
|
|
|
180 |
)
|
181 |
|
|
|
182 |
object_name = gr.Textbox(
|
183 |
+
label="🎯 Object to Remove",
|
184 |
+
placeholder="Enter any object name (e.g., person, car, dog, bottle, tree, sign...)",
|
185 |
value="person",
|
186 |
+
info="Type ANY object name - supports synonyms and variations!"
|
|
|
187 |
)
|
188 |
|
189 |
+
# Add suggestions
|
190 |
with gr.Row():
|
191 |
+
gr.Examples(
|
192 |
+
examples=[
|
193 |
+
["person"], ["car"], ["dog"], ["cat"], ["bottle"],
|
194 |
+
["chair"], ["tree"], ["sign"], ["bag"], ["phone"]
|
195 |
+
],
|
196 |
+
inputs=[object_name],
|
197 |
+
label="💡 Quick Examples"
|
198 |
+
)
|
199 |
|
|
|
200 |
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
201 |
+
|
202 |
guidance_scale = gr.Slider(
|
203 |
minimum=1.0,
|
204 |
maximum=10.0,
|
|
|
213 |
maximum=50,
|
214 |
value=28,
|
215 |
step=2,
|
216 |
+
label="🔄 Steps",
|
217 |
info="More steps = higher quality but slower processing"
|
218 |
)
|
219 |
|
220 |
+
|
221 |
+
remove_btn = gr.Button("🚀 Remove Objects", variant="primary", size="lg")
|
|
|
|
|
|
|
|
|
|
|
222 |
|
223 |
+
with gr.Column(scale=2):
|
224 |
+
# Output section
|
225 |
+
gr.Markdown("## 📋 Results")
|
226 |
|
227 |
+
with gr.Row():
|
228 |
+
output_image = gr.Image(
|
229 |
+
label="🖼️ Result",
|
230 |
+
type="pil",
|
231 |
+
height=300
|
232 |
+
)
|
|
|
233 |
|
234 |
status_text = gr.Textbox(
|
235 |
+
label="📊 Status & Detection Info",
|
|
|
236 |
interactive=False,
|
237 |
+
max_lines=5
|
238 |
)
|
|
|
239 |
|
240 |
+
# Event handlers
|
241 |
+
remove_btn.click(
|
242 |
fn=remove_objects,
|
243 |
+
inputs=[
|
244 |
+
input_image,
|
245 |
+
object_name,
|
246 |
+
guidance_scale,
|
247 |
+
steps,
|
248 |
+
],
|
249 |
outputs=[output_image, status_text]
|
250 |
)
|
251 |
|
252 |
+
|
|
|
|
|
|
|
|
|
|
|
253 |
|
254 |
if __name__ == "__main__":
|
255 |
demo.launch()
|