Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,36 @@
|
|
| 1 |
-
# app.py
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
from diffusers import AutoPipelineForInpainting
|
| 6 |
from PIL import Image
|
|
|
|
| 7 |
|
| 8 |
-
# --- Model Loading ---
|
| 9 |
-
#
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
variant="fp16"
|
| 17 |
-
).to("cuda")
|
| 18 |
-
except Exception as e:
|
| 19 |
-
print(f"Could not load model on GPU: {e}. Falling back to CPU.")
|
| 20 |
-
pipe = AutoPipelineForInpainting.from_pretrained(
|
| 21 |
-
"stabilityai/stable-diffusion-2-inpainting"
|
| 22 |
-
)
|
| 23 |
|
| 24 |
# --- The Inpainting Function ---
|
| 25 |
-
|
| 26 |
-
def inpaint_image(input_dict, prompt, negative_prompt, guidance_scale, num_steps):
|
| 27 |
"""
|
| 28 |
Performs inpainting on an image based on a mask and a prompt.
|
| 29 |
-
|
| 30 |
-
Args:
|
| 31 |
-
input_dict (dict): A dictionary from Gradio's Image component containing 'image' and 'mask'.
|
| 32 |
-
prompt (str): The text prompt describing what to generate in the masked area.
|
| 33 |
-
negative_prompt (str): The text prompt describing what to avoid.
|
| 34 |
-
guidance_scale (float): A value to control how much the generation follows the prompt.
|
| 35 |
-
num_steps (int): The number of inference steps.
|
| 36 |
-
|
| 37 |
-
Returns:
|
| 38 |
-
PIL.Image: The resulting image after inpainting.
|
| 39 |
"""
|
| 40 |
-
# Separate the image and the mask from the input dictionary
|
| 41 |
image = input_dict["image"].convert("RGB")
|
| 42 |
mask_image = input_dict["mask"].convert("RGB")
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
# However, it's good practice to inform the user that square images work best.
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
result_image = pipe(
|
| 52 |
prompt=prompt,
|
| 53 |
image=image,
|
|
@@ -55,7 +38,12 @@ def inpaint_image(input_dict, prompt, negative_prompt, guidance_scale, num_steps
|
|
| 55 |
negative_prompt=negative_prompt,
|
| 56 |
guidance_scale=guidance_scale,
|
| 57 |
num_inference_steps=int(num_steps),
|
|
|
|
|
|
|
| 58 |
).images[0]
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
return result_image
|
| 61 |
|
|
@@ -64,57 +52,40 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 64 |
gr.Markdown(
|
| 65 |
"""
|
| 66 |
# 🎨 AI Image Fixer (Inpainting)
|
| 67 |
-
|
| 68 |
-
Have an AI-generated image with weird hands, faces, or artifacts? Fix it here!
|
| 69 |
-
|
| 70 |
-
**How to use:**
|
| 71 |
-
1. Upload your image.
|
| 72 |
-
2. Use the brush tool to "paint" over the parts you want to replace. This is your mask.
|
| 73 |
-
3. Write a prompt describing what you want in the painted-over area.
|
| 74 |
-
4. Adjust the advanced settings if you want more control.
|
| 75 |
-
5. Click "Fix It!" and see the magic happen.
|
| 76 |
"""
|
| 77 |
)
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
with gr.Row():
|
| 80 |
-
# Input column
|
| 81 |
with gr.Column():
|
| 82 |
-
gr.Markdown("### 1. Upload & Mask Your Image")
|
| 83 |
-
# The Image component with a drawing tool for masking
|
| 84 |
input_image = gr.Image(
|
| 85 |
-
label="Upload Image & Draw Mask",
|
| 86 |
-
source="upload",
|
| 87 |
-
tool="brush",
|
| 88 |
-
type="pil" # We want to work with PIL images in our function
|
| 89 |
)
|
| 90 |
-
|
| 91 |
-
gr.Markdown("### 2. Describe Your Fix")
|
| 92 |
-
prompt = gr.Textbox(label="Prompt", placeholder="e.g., 'A beautiful, realistic human hand, detailed fingers'")
|
| 93 |
-
|
| 94 |
-
# Accordion for advanced settings to keep the UI clean
|
| 95 |
with gr.Accordion("Advanced Settings", open=False):
|
| 96 |
-
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="e.g., 'blurry, distorted, extra fingers
|
| 97 |
-
guidance_scale = gr.Slider(minimum=0, maximum=20, value=
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
-
# Output column
|
| 101 |
with gr.Column():
|
| 102 |
-
gr.
|
| 103 |
-
output_image = gr.Image(
|
| 104 |
-
label="Resulting Image",
|
| 105 |
-
type="pil"
|
| 106 |
-
)
|
| 107 |
|
| 108 |
-
# The button to trigger the process
|
| 109 |
submit_button = gr.Button("Fix It!", variant="primary")
|
| 110 |
|
| 111 |
-
#
|
| 112 |
submit_button.click(
|
| 113 |
fn=inpaint_image,
|
| 114 |
inputs=[input_image, prompt, negative_prompt, guidance_scale, num_steps],
|
| 115 |
outputs=output_image
|
| 116 |
)
|
| 117 |
|
| 118 |
-
# Launch the Gradio app
|
| 119 |
if __name__ == "__main__":
|
| 120 |
demo.launch()
|
|
|
|
| 1 |
+
# app.py (Modified for CPU)
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
from diffusers import AutoPipelineForInpainting
|
| 6 |
from PIL import Image
|
| 7 |
+
import time # Import time to measure execution
|
| 8 |
|
| 9 |
+
# --- Model Loading (CPU Version) ---
|
| 10 |
+
# We load the model without GPU-specific options.
|
| 11 |
+
# This will run on the CPU.
|
| 12 |
+
print("Loading model on CPU... This may take a moment.")
|
| 13 |
+
pipe = AutoPipelineForInpainting.from_pretrained(
|
| 14 |
+
"stabilityai/stable-diffusion-2-inpainting"
|
| 15 |
+
)
|
| 16 |
+
print("Model loaded successfully.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# --- The Inpainting Function ---
|
| 19 |
+
def inpaint_image(input_dict, prompt, negative_prompt, guidance_scale, num_steps, progress=gr.Progress()):
|
|
|
|
| 20 |
"""
|
| 21 |
Performs inpainting on an image based on a mask and a prompt.
|
| 22 |
+
Includes progress tracking for the slow CPU process.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"""
|
|
|
|
| 24 |
image = input_dict["image"].convert("RGB")
|
| 25 |
mask_image = input_dict["mask"].convert("RGB")
|
| 26 |
|
| 27 |
+
print(f"Starting inpainting with prompt: '{prompt}' on CPU.")
|
| 28 |
+
start_time = time.time()
|
|
|
|
| 29 |
|
| 30 |
+
# Callback to update the progress bar in the UI
|
| 31 |
+
def progress_callback(step, timestep, latents):
|
| 32 |
+
progress(step / int(num_steps), desc=f"Running step {step}/{int(num_steps)}")
|
| 33 |
+
|
| 34 |
result_image = pipe(
|
| 35 |
prompt=prompt,
|
| 36 |
image=image,
|
|
|
|
| 38 |
negative_prompt=negative_prompt,
|
| 39 |
guidance_scale=guidance_scale,
|
| 40 |
num_inference_steps=int(num_steps),
|
| 41 |
+
callback_steps=1,
|
| 42 |
+
callback=progress_callback,
|
| 43 |
).images[0]
|
| 44 |
+
|
| 45 |
+
end_time = time.time()
|
| 46 |
+
print(f"Inpainting finished in {end_time - start_time:.2f} seconds.")
|
| 47 |
|
| 48 |
return result_image
|
| 49 |
|
|
|
|
| 52 |
gr.Markdown(
|
| 53 |
"""
|
| 54 |
# 🎨 AI Image Fixer (Inpainting)
|
| 55 |
+
Upload an image, mask the area to fix, and describe the change.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
"""
|
| 57 |
)
|
| 58 |
|
| 59 |
+
# *CRUCIAL* Warning for CPU users
|
| 60 |
+
gr.Warning(
|
| 61 |
+
"⚠️ This Space is running on a free CPU. "
|
| 62 |
+
"Image generation will be VERY SLOW (expect 5-15 minutes per image). "
|
| 63 |
+
"Please be patient! A progress bar will appear below the 'Fix It!' button."
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
with gr.Row():
|
|
|
|
| 67 |
with gr.Column():
|
|
|
|
|
|
|
| 68 |
input_image = gr.Image(
|
| 69 |
+
label="Upload Image & Draw Mask", source="upload", tool="brush", type="pil"
|
|
|
|
|
|
|
|
|
|
| 70 |
)
|
| 71 |
+
prompt = gr.Textbox(label="Prompt", placeholder="e.g., 'A beautiful, realistic human hand'")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
with gr.Accordion("Advanced Settings", open=False):
|
| 73 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="e.g., 'blurry, distorted, extra fingers'")
|
| 74 |
+
guidance_scale = gr.Slider(minimum=0, maximum=20, value=7.5, label="Guidance Scale")
|
| 75 |
+
# Lower the default steps for faster (but lower quality) generation on CPU
|
| 76 |
+
num_steps = gr.Slider(minimum=5, maximum=50, step=1, value=20, label="Inference Steps")
|
| 77 |
|
|
|
|
| 78 |
with gr.Column():
|
| 79 |
+
output_image = gr.Image(label="Resulting Image", type="pil")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
|
|
|
| 81 |
submit_button = gr.Button("Fix It!", variant="primary")
|
| 82 |
|
| 83 |
+
# We add a progress component to be updated
|
| 84 |
submit_button.click(
|
| 85 |
fn=inpaint_image,
|
| 86 |
inputs=[input_image, prompt, negative_prompt, guidance_scale, num_steps],
|
| 87 |
outputs=output_image
|
| 88 |
)
|
| 89 |
|
|
|
|
| 90 |
if __name__ == "__main__":
|
| 91 |
demo.launch()
|