Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,243 +1,129 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from diffusers import
|
| 4 |
from PIL import Image
|
| 5 |
-
import numpy as np
|
| 6 |
-
from transformers import pipeline
|
| 7 |
import spaces
|
| 8 |
|
| 9 |
-
# Initialize models
|
| 10 |
-
@spaces.GPU
|
| 11 |
-
def load_models():
|
| 12 |
-
"""Load the image editing models"""
|
| 13 |
-
try:
|
| 14 |
-
# Using Stable Diffusion XL for image editing
|
| 15 |
-
pipe = AutoPipelineForImage2Image.from_pretrained(
|
| 16 |
-
"stabilityai/stable-diffusion-xl-refiner-1.0",
|
| 17 |
-
torch_dtype=torch.float16,
|
| 18 |
-
variant="fp16",
|
| 19 |
-
use_safetensors=True
|
| 20 |
-
)
|
| 21 |
-
pipe.to("cuda")
|
| 22 |
-
pipe.enable_model_cpu_offload()
|
| 23 |
-
return pipe
|
| 24 |
-
except Exception as e:
|
| 25 |
-
print(f"Error loading model: {e}")
|
| 26 |
-
return None
|
| 27 |
-
|
| 28 |
# Global model variable
|
| 29 |
pipe = None
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
"""
|
| 34 |
-
Edit an image based on text prompt
|
| 35 |
-
|
| 36 |
-
Args:
|
| 37 |
-
input_image: Input PIL Image
|
| 38 |
-
edit_prompt: What to change/add in the image
|
| 39 |
-
negative_prompt: What to avoid in the output
|
| 40 |
-
strength: How much to change (0-1)
|
| 41 |
-
guidance_scale: How closely to follow prompt
|
| 42 |
-
steps: Number of inference steps
|
| 43 |
-
"""
|
| 44 |
global pipe
|
| 45 |
-
|
| 46 |
if pipe is None:
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
if input_image is None:
|
| 50 |
-
return None, "Please upload an image first!"
|
| 51 |
|
| 52 |
if not edit_prompt or edit_prompt.strip() == "":
|
| 53 |
-
return None, "Please provide
|
| 54 |
|
| 55 |
try:
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
if max(input_image.size) > max_size:
|
| 59 |
ratio = max_size / max(input_image.size)
|
| 60 |
new_size = tuple(int(dim * ratio) for dim in input_image.size)
|
| 61 |
input_image = input_image.resize(new_size, Image.Resampling.LANCZOS)
|
| 62 |
|
| 63 |
-
# Ensure
|
| 64 |
width = (input_image.width // 8) * 8
|
| 65 |
height = (input_image.height // 8) * 8
|
| 66 |
input_image = input_image.resize((width, height))
|
| 67 |
|
|
|
|
|
|
|
| 68 |
# Generate edited image
|
| 69 |
-
result =
|
| 70 |
-
|
| 71 |
image=input_image,
|
| 72 |
-
|
| 73 |
-
strength=strength,
|
| 74 |
guidance_scale=guidance_scale,
|
| 75 |
-
|
| 76 |
).images[0]
|
| 77 |
|
|
|
|
| 78 |
return result, "✅ Image edited successfully!"
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
error_msg = f"❌ Error: {str(e)}"
|
| 82 |
print(error_msg)
|
|
|
|
|
|
|
| 83 |
return None, error_msg
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
examples = [
|
| 87 |
-
["examples/sample1.jpg", "change the text on the sign to say 'OPEN 24/7'", "blurry, low quality", 0.7, 7.5, 30],
|
| 88 |
-
["examples/sample2.jpg", "remove the person in the background", "distorted, artifacts", 0.6, 7.5, 30],
|
| 89 |
-
["examples/sample3.jpg", "add sunset lighting and warm tones", "dark, cold colors", 0.5, 7.5, 30],
|
| 90 |
-
["examples/sample4.jpg", "make it look like a watercolor painting", "realistic, photographic", 0.8, 8.0, 35],
|
| 91 |
-
]
|
| 92 |
-
|
| 93 |
-
# Custom CSS for better UI
|
| 94 |
custom_css = """
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
-webkit-background-clip: text;
|
| 99 |
-
-webkit-text-fill-color: transparent;
|
| 100 |
-
font-size: 2.5em;
|
| 101 |
-
font-weight: bold;
|
| 102 |
-
margin-bottom: 0.5em;
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
-
#subtitle {
|
| 106 |
-
text-align: center;
|
| 107 |
-
color: #666;
|
| 108 |
-
font-size: 1.2em;
|
| 109 |
-
margin-bottom: 2em;
|
| 110 |
-
}
|
| 111 |
-
|
| 112 |
-
.gradio-container {
|
| 113 |
-
max-width: 1200px !important;
|
| 114 |
-
margin: auto !important;
|
| 115 |
-
}
|
| 116 |
-
|
| 117 |
-
#edit_btn {
|
| 118 |
-
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
|
| 119 |
-
border: none;
|
| 120 |
-
font-size: 1.1em;
|
| 121 |
-
font-weight: bold;
|
| 122 |
-
}
|
| 123 |
"""
|
| 124 |
|
| 125 |
-
# Build
|
| 126 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 127 |
|
| 128 |
-
gr.HTML("<h1 id='
|
| 129 |
-
gr.
|
| 130 |
|
| 131 |
with gr.Row():
|
| 132 |
-
with gr.Column(
|
| 133 |
-
gr.
|
| 134 |
-
|
| 135 |
-
input_image = gr.Image(
|
| 136 |
-
label="Upload Image",
|
| 137 |
-
type="pil",
|
| 138 |
-
height=400
|
| 139 |
-
)
|
| 140 |
|
| 141 |
edit_prompt = gr.Textbox(
|
| 142 |
-
label="✏️
|
| 143 |
-
placeholder="
|
| 144 |
lines=3
|
| 145 |
)
|
| 146 |
|
| 147 |
-
negative_prompt = gr.Textbox(
|
| 148 |
-
label="🚫 What to avoid?",
|
| 149 |
-
placeholder="Example: blurry, low quality, distorted, artifacts...",
|
| 150 |
-
value="blurry, low quality, distorted, ugly, bad anatomy",
|
| 151 |
-
lines=2
|
| 152 |
-
)
|
| 153 |
-
|
| 154 |
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
maximum=1.0,
|
| 159 |
-
value=0.7,
|
| 160 |
-
step=0.05,
|
| 161 |
-
info="Lower = subtle changes, Higher = dramatic changes"
|
| 162 |
-
)
|
| 163 |
-
|
| 164 |
-
guidance_scale = gr.Slider(
|
| 165 |
-
label="Guidance Scale (prompt adherence)",
|
| 166 |
-
minimum=1.0,
|
| 167 |
-
maximum=15.0,
|
| 168 |
-
value=7.5,
|
| 169 |
-
step=0.5,
|
| 170 |
-
info="How closely to follow your prompt"
|
| 171 |
-
)
|
| 172 |
-
|
| 173 |
-
steps = gr.Slider(
|
| 174 |
-
label="Inference Steps (quality)",
|
| 175 |
-
minimum=10,
|
| 176 |
-
maximum=50,
|
| 177 |
-
value=30,
|
| 178 |
-
step=5,
|
| 179 |
-
info="More steps = better quality but slower"
|
| 180 |
-
)
|
| 181 |
-
|
| 182 |
-
edit_btn = gr.Button("✨ Edit Image", variant="primary", elem_id="edit_btn", size="lg")
|
| 183 |
|
| 184 |
-
|
| 185 |
|
| 186 |
-
with gr.Column(
|
| 187 |
-
gr.
|
| 188 |
-
|
| 189 |
-
output_image = gr.Image(
|
| 190 |
-
label="Edited Image",
|
| 191 |
-
type="pil",
|
| 192 |
-
height=400
|
| 193 |
-
)
|
| 194 |
-
|
| 195 |
-
gr.Markdown("""
|
| 196 |
-
### 💡 Tips for Best Results:
|
| 197 |
-
- Be specific in your edit description
|
| 198 |
-
- For text editing: mention exact words you want
|
| 199 |
-
- For removing objects: describe what to replace them with
|
| 200 |
-
- Adjust strength: 0.5-0.7 for subtle edits, 0.7-0.9 for major changes
|
| 201 |
-
- Use negative prompts to avoid unwanted artifacts
|
| 202 |
-
""")
|
| 203 |
|
| 204 |
-
# Examples section
|
| 205 |
-
gr.Markdown("### 🎯 Example Edits (Click to load)")
|
| 206 |
-
gr.Examples(
|
| 207 |
-
examples=examples,
|
| 208 |
-
inputs=[input_image, edit_prompt, negative_prompt, strength, guidance_scale, steps],
|
| 209 |
-
outputs=[output_image, status_text],
|
| 210 |
-
fn=edit_image,
|
| 211 |
-
cache_examples=False
|
| 212 |
-
)
|
| 213 |
-
|
| 214 |
-
# Footer
|
| 215 |
gr.Markdown("""
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
-
|
| 219 |
-
-
|
| 220 |
-
-
|
| 221 |
-
-
|
| 222 |
-
|
| 223 |
-
### 📝 Popular Use Cases:
|
| 224 |
-
- Edit storefront signs and banners
|
| 225 |
-
- Remove unwanted objects from photos
|
| 226 |
-
- Change product colors and styles
|
| 227 |
-
- Create artistic variations of photos
|
| 228 |
-
- Fix and enhance images
|
| 229 |
-
|
| 230 |
-
⚡ Powered by Stable Diffusion XL | Made with ❤️ for the AI community
|
| 231 |
""")
|
| 232 |
|
| 233 |
-
|
|
|
|
|
|
|
| 234 |
edit_btn.click(
|
| 235 |
fn=edit_image,
|
| 236 |
-
inputs=[input_image, edit_prompt,
|
| 237 |
-
outputs=[output_image,
|
| 238 |
)
|
| 239 |
|
| 240 |
-
# Launch the app
|
| 241 |
if __name__ == "__main__":
|
| 242 |
-
demo.queue(max_size=
|
| 243 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from diffusers import StableDiffusionInstructPix2PixPipeline
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
| 5 |
import spaces
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Global model variable
|
| 8 |
pipe = None
|
| 9 |
|
| 10 |
+
def load_model():
|
| 11 |
+
"""Load the image editing model"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
global pipe
|
|
|
|
| 13 |
if pipe is None:
|
| 14 |
+
print("Loading model...")
|
| 15 |
+
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(
|
| 16 |
+
"timbrooks/instruct-pix2pix",
|
| 17 |
+
torch_dtype=torch.float16,
|
| 18 |
+
safety_checker=None
|
| 19 |
+
)
|
| 20 |
+
pipe.to("cuda")
|
| 21 |
+
pipe.enable_attention_slicing()
|
| 22 |
+
print("Model loaded successfully!")
|
| 23 |
+
return pipe
|
| 24 |
+
|
| 25 |
+
@spaces.GPU
|
| 26 |
+
def edit_image(input_image, edit_prompt, num_steps, guidance_scale, image_guidance_scale):
|
| 27 |
+
"""Edit an image based on text instructions"""
|
| 28 |
|
| 29 |
if input_image is None:
|
| 30 |
+
return None, "❌ Please upload an image first!"
|
| 31 |
|
| 32 |
if not edit_prompt or edit_prompt.strip() == "":
|
| 33 |
+
return None, "❌ Please provide edit instructions!"
|
| 34 |
|
| 35 |
try:
|
| 36 |
+
print(f"Starting edit with prompt: {edit_prompt}")
|
| 37 |
+
|
| 38 |
+
# Load model
|
| 39 |
+
model = load_model()
|
| 40 |
+
|
| 41 |
+
# Resize if too large
|
| 42 |
+
max_size = 512
|
| 43 |
if max(input_image.size) > max_size:
|
| 44 |
ratio = max_size / max(input_image.size)
|
| 45 |
new_size = tuple(int(dim * ratio) for dim in input_image.size)
|
| 46 |
input_image = input_image.resize(new_size, Image.Resampling.LANCZOS)
|
| 47 |
|
| 48 |
+
# Ensure dimensions are multiples of 8
|
| 49 |
width = (input_image.width // 8) * 8
|
| 50 |
height = (input_image.height // 8) * 8
|
| 51 |
input_image = input_image.resize((width, height))
|
| 52 |
|
| 53 |
+
print(f"Processing image: {width}x{height}")
|
| 54 |
+
|
| 55 |
# Generate edited image
|
| 56 |
+
result = model(
|
| 57 |
+
edit_prompt,
|
| 58 |
image=input_image,
|
| 59 |
+
num_inference_steps=num_steps,
|
|
|
|
| 60 |
guidance_scale=guidance_scale,
|
| 61 |
+
image_guidance_scale=image_guidance_scale,
|
| 62 |
).images[0]
|
| 63 |
|
| 64 |
+
print("Edit completed successfully!")
|
| 65 |
return result, "✅ Image edited successfully!"
|
| 66 |
|
| 67 |
except Exception as e:
|
| 68 |
error_msg = f"❌ Error: {str(e)}"
|
| 69 |
print(error_msg)
|
| 70 |
+
import traceback
|
| 71 |
+
traceback.print_exc()
|
| 72 |
return None, error_msg
|
| 73 |
|
| 74 |
+
# Custom CSS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
custom_css = """
|
| 76 |
+
.gradio-container {max-width: 1200px !important; margin: auto !important;}
|
| 77 |
+
#title {text-align: center; background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
|
| 78 |
+
-webkit-background-clip: text; -webkit-text-fill-color: transparent; font-size: 2.5em; font-weight: bold;}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
"""
|
| 80 |
|
| 81 |
+
# Build interface
|
| 82 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 83 |
|
| 84 |
+
gr.HTML("<h1 id='title'>🎨 AI Image Editor</h1>")
|
| 85 |
+
gr.Markdown("<p style='text-align: center; font-size: 1.2em;'>Edit images with simple text instructions</p>")
|
| 86 |
|
| 87 |
with gr.Row():
|
| 88 |
+
with gr.Column():
|
| 89 |
+
input_image = gr.Image(label="Upload Image", type="pil", height=400)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
edit_prompt = gr.Textbox(
|
| 92 |
+
label="✏️ Edit Instructions",
|
| 93 |
+
placeholder="Examples:\n- make it a cartoon\n- turn the sky to sunset\n- make it black and white\n- add snow on the ground",
|
| 94 |
lines=3
|
| 95 |
)
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 98 |
+
num_steps = gr.Slider(10, 100, value=20, step=5, label="Steps (quality)")
|
| 99 |
+
guidance_scale = gr.Slider(1, 10, value=7.5, step=0.5, label="Text Guidance")
|
| 100 |
+
image_guidance_scale = gr.Slider(1, 2, value=1.5, step=0.1, label="Image Guidance")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
edit_btn = gr.Button("✨ Edit Image", variant="primary", size="lg")
|
| 103 |
|
| 104 |
+
with gr.Column():
|
| 105 |
+
output_image = gr.Image(label="Edited Image", type="pil", height=400)
|
| 106 |
+
status = gr.Textbox(label="Status", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
gr.Markdown("""
|
| 109 |
+
### 💡 Example Prompts:
|
| 110 |
+
- "make it look like a painting"
|
| 111 |
+
- "turn day into night"
|
| 112 |
+
- "make it winter"
|
| 113 |
+
- "add a smile"
|
| 114 |
+
- "make it black and white"
|
| 115 |
+
- "turn it into a cartoon"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
""")
|
| 117 |
|
| 118 |
+
gr.Markdown("---\n⚡ Powered by InstructPix2Pix")
|
| 119 |
+
|
| 120 |
+
# Connect button
|
| 121 |
edit_btn.click(
|
| 122 |
fn=edit_image,
|
| 123 |
+
inputs=[input_image, edit_prompt, num_steps, guidance_scale, image_guidance_scale],
|
| 124 |
+
outputs=[output_image, status]
|
| 125 |
)
|
| 126 |
|
|
|
|
| 127 |
if __name__ == "__main__":
|
| 128 |
+
demo.queue(max_size=10)
|
| 129 |
demo.launch()
|