Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,53 @@
|
|
| 1 |
-
# app.py
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
from diffusers import AutoPipelineForInpainting
|
| 6 |
from PIL import Image
|
| 7 |
-
import time
|
| 8 |
|
| 9 |
# --- Model Loading (CPU Version) ---
|
| 10 |
-
|
| 11 |
-
# This will run on the CPU.
|
| 12 |
-
print("Loading model on CPU... This may take a moment.")
|
| 13 |
pipe = AutoPipelineForInpainting.from_pretrained(
|
| 14 |
-
"
|
| 15 |
)
|
| 16 |
print("Model loaded successfully.")
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# --- The Inpainting Function ---
|
| 19 |
-
|
|
|
|
| 20 |
"""
|
| 21 |
-
Performs inpainting
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,
|
|
@@ -47,43 +64,57 @@ def inpaint_image(input_dict, prompt, negative_prompt, guidance_scale, num_steps
|
|
| 47 |
|
| 48 |
return result_image
|
| 49 |
|
|
|
|
| 50 |
# --- Gradio User Interface ---
|
| 51 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 52 |
gr.Markdown(
|
| 53 |
"""
|
| 54 |
-
# 🎨 AI Image Fixer
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 63 |
-
"
|
| 64 |
)
|
| 65 |
|
| 66 |
with gr.Row():
|
| 67 |
-
|
|
|
|
| 68 |
input_image = gr.Image(
|
| 69 |
-
label="Upload
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
)
|
| 71 |
-
|
| 72 |
with gr.Accordion("Advanced Settings", open=False):
|
| 73 |
-
|
| 74 |
-
|
| 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 |
-
|
| 79 |
-
|
|
|
|
| 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,
|
| 87 |
outputs=output_image
|
| 88 |
)
|
| 89 |
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
from diffusers import AutoPipelineForInpainting
|
| 6 |
from PIL import Image
|
| 7 |
+
import time
|
| 8 |
|
| 9 |
# --- Model Loading (CPU Version) ---
|
| 10 |
+
print("Loading model on CPU... This may take several minutes.")
|
|
|
|
|
|
|
| 11 |
pipe = AutoPipelineForInpainting.from_pretrained(
|
| 12 |
+
"runwayml/stable-diffusion-inpainting" # Using the slightly smaller 1.5 model for better CPU performance
|
| 13 |
)
|
| 14 |
print("Model loaded successfully.")
|
| 15 |
|
| 16 |
+
# --- Default "Magic" Prompts ---
|
| 17 |
+
# These will be used if the user doesn't provide their own prompt.
|
| 18 |
+
DEFAULT_PROMPT = "photorealistic, 4k, ultra high quality, sharp focus, masterpiece, high detail, professional photo"
|
| 19 |
+
DEFAULT_NEGATIVE_PROMPT = "blurry, pixelated, distorted, deformed, ugly, disfigured, cartoon, anime, low quality, watermark, text"
|
| 20 |
+
|
| 21 |
# --- The Inpainting Function ---
|
| 22 |
+
# It now handles the logic for an optional user prompt.
|
| 23 |
+
def inpaint_image(input_dict, user_prompt, guidance_scale, num_steps, progress=gr.Progress()):
|
| 24 |
"""
|
| 25 |
+
Performs inpainting. Uses a default prompt if the user_prompt is empty.
|
|
|
|
| 26 |
"""
|
| 27 |
image = input_dict["image"].convert("RGB")
|
| 28 |
mask_image = input_dict["mask"].convert("RGB")
|
| 29 |
|
| 30 |
+
# --- This is the core logic for the hybrid approach ---
|
| 31 |
+
if user_prompt and user_prompt.strip():
|
| 32 |
+
# If the user provided a prompt, use it.
|
| 33 |
+
prompt = user_prompt
|
| 34 |
+
# For custom prompts, a general negative prompt is still useful.
|
| 35 |
+
negative_prompt = DEFAULT_NEGATIVE_PROMPT
|
| 36 |
+
print(f"Using custom prompt: '{prompt}'")
|
| 37 |
+
else:
|
| 38 |
+
# If the user left the prompt box empty, use our high-quality defaults.
|
| 39 |
+
prompt = DEFAULT_PROMPT
|
| 40 |
+
negative_prompt = DEFAULT_NEGATIVE_PROMPT
|
| 41 |
+
print(f"User prompt is empty. Using default 'General Fix' prompt.")
|
| 42 |
+
|
| 43 |
+
print(f"Starting inpainting on CPU...")
|
| 44 |
start_time = time.time()
|
| 45 |
|
| 46 |
# Callback to update the progress bar in the UI
|
| 47 |
def progress_callback(step, timestep, latents):
|
| 48 |
progress(step / int(num_steps), desc=f"Running step {step}/{int(num_steps)}")
|
| 49 |
|
| 50 |
+
# Run the pipeline
|
| 51 |
result_image = pipe(
|
| 52 |
prompt=prompt,
|
| 53 |
image=image,
|
|
|
|
| 64 |
|
| 65 |
return result_image
|
| 66 |
|
| 67 |
+
|
| 68 |
# --- Gradio User Interface ---
|
| 69 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 70 |
gr.Markdown(
|
| 71 |
"""
|
| 72 |
+
# 🎨 AI Image Fixer
|
| 73 |
+
|
| 74 |
+
**How to use:**
|
| 75 |
+
1. Upload an image.
|
| 76 |
+
2. Use the brush to **paint over the area you want to fix**.
|
| 77 |
+
3. **(Optional)** For precise control, write a custom prompt describing the fix.
|
| 78 |
+
4. **(Easy Mode)** Or, just leave the prompt box empty for a general quality improvement.
|
| 79 |
+
5. Click "Fix It!"
|
| 80 |
"""
|
| 81 |
)
|
| 82 |
|
|
|
|
| 83 |
gr.Warning(
|
| 84 |
"⚠️ This Space is running on a free CPU. "
|
| 85 |
+
"Image generation will be VERY SLOW (expect 5-15 minutes). "
|
| 86 |
+
"A progress bar will show the status below the button. Please be patient!"
|
| 87 |
)
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
+
# Input Column
|
| 91 |
+
with gr.Column(scale=2):
|
| 92 |
input_image = gr.Image(
|
| 93 |
+
label="1. Upload & Mask Image",
|
| 94 |
+
source="upload",
|
| 95 |
+
tool="brush",
|
| 96 |
+
type="pil"
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# The prompt textbox is back, but now it's optional!
|
| 100 |
+
prompt_textbox = gr.Textbox(
|
| 101 |
+
label="2. Describe Your Fix (Optional)",
|
| 102 |
+
placeholder="Leave empty for a general fix, or type e.g., 'a perfect human hand'"
|
| 103 |
)
|
| 104 |
+
|
| 105 |
with gr.Accordion("Advanced Settings", open=False):
|
| 106 |
+
guidance_scale = gr.Slider(minimum=0, maximum=20, value=8.0, label="Guidance Scale")
|
| 107 |
+
num_steps = gr.Slider(minimum=10, maximum=50, step=1, value=25, label="Inference Steps")
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
# Output Column
|
| 110 |
+
with gr.Column(scale=1):
|
| 111 |
+
output_image = gr.Image(label="Result", type="pil")
|
| 112 |
|
| 113 |
submit_button = gr.Button("Fix It!", variant="primary")
|
| 114 |
|
|
|
|
| 115 |
submit_button.click(
|
| 116 |
fn=inpaint_image,
|
| 117 |
+
inputs=[input_image, prompt_textbox, guidance_scale, num_steps],
|
| 118 |
outputs=output_image
|
| 119 |
)
|
| 120 |
|