clockclock commited on
Commit
4bdb54f
·
verified ·
1 Parent(s): 77c7489

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -57
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app.py
2
 
3
  import gradio as gr
4
  import torch
@@ -6,89 +6,64 @@ 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
- # This is the corrected line that fixes the error.
12
- pipe = AutoPipelineForInpainting.from_pretrained(
13
- "stabilityai/stable-diffusion-2-inpainting",
14
- torch_dtype=torch.float32, # Use float32 for broad CPU compatibility
15
- safety_checker=None # Explicitly disable the safety checker to prevent loading errors
16
- )
17
- print("Model loaded successfully.")
 
18
 
 
 
 
19
 
20
- # --- Default "Magic" Prompts ---
21
- DEFAULT_PROMPT = "photorealistic, 4k, ultra high quality, sharp focus, masterpiece, high detail, professional photo"
22
- DEFAULT_NEGATIVE_PROMPT = "blurry, pixelated, distorted, deformed, ugly, disfigured, cartoon, anime, low quality, watermark, text"
23
-
24
- # --- The Inpainting Function ---
25
- def inpaint_image(input_dict, user_prompt, guidance_scale, num_steps, progress=gr.Progress()):
26
- image = input_dict["image"].convert("RGB")
27
- mask_image = input_dict["mask"].convert("RGB")
28
-
29
  if user_prompt and user_prompt.strip():
30
  prompt = user_prompt
31
  negative_prompt = DEFAULT_NEGATIVE_PROMPT
32
- print(f"Using custom prompt: '{prompt}'")
33
  else:
34
  prompt = DEFAULT_PROMPT
35
  negative_prompt = DEFAULT_NEGATIVE_PROMPT
36
- print(f"User prompt is empty. Using default 'General Fix' prompt.")
37
 
38
  print(f"Starting inpainting on CPU...")
39
- start_time = time.time()
40
-
41
- def progress_callback(step, timestep, latents):
42
- progress(step / int(num_steps), desc=f"Running step {step}/{int(num_steps)}")
43
-
44
  result_image = pipe(
45
- prompt=prompt,
46
- image=image,
47
- mask_image=mask_image,
48
- negative_prompt=negative_prompt,
49
- guidance_scale=guidance_scale,
50
- num_inference_steps=int(num_steps),
51
- callback_steps=1,
52
- callback=progress_callback,
53
  ).images[0]
54
 
55
- end_time = time.time()
56
- print(f"Inpainting finished in {end_time - start_time:.2f} seconds.")
57
  return result_image
58
 
59
-
60
- # --- Gradio User Interface ---
61
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
62
- gr.Markdown(
63
- """
64
- # 🎨 AI Image Fixer
65
- **How to use:**
66
- 1. Upload an image.
67
- 2. Use the brush to **paint over the area you want to fix**.
68
- 3. **(Optional)** For precise control, write a custom prompt describing the fix.
69
- 4. **(Easy Mode)** Or, just leave the prompt box empty for a general quality improvement.
70
- 5. Click "Fix It!"
71
- """
72
- )
73
-
74
- gr.Warning(
75
- "⚠️ This Space is running on a free CPU. "
76
- "Image generation will be VERY SLOW (expect 5-20 minutes). "
77
- "A progress bar will show the status below the button. Please be patient!"
78
- )
79
 
80
  with gr.Row():
81
  with gr.Column(scale=2):
 
82
  input_image = gr.Image(label="1. Upload & Mask Image", source="upload", tool="brush", type="pil")
83
- prompt_textbox = gr.Textbox(label="2. Describe Your Fix (Optional)", placeholder="Leave empty for a general fix, or type e.g., 'a perfect human hand'")
84
  with gr.Accordion("Advanced Settings", open=False):
85
  guidance_scale = gr.Slider(minimum=0, maximum=20, value=8.0, label="Guidance Scale")
86
- num_steps = gr.Slider(minimum=10, maximum=50, step=1, value=25, label="Inference Steps")
87
  with gr.Column(scale=1):
88
  output_image = gr.Image(label="Result", type="pil")
89
 
90
  submit_button = gr.Button("Fix It!", variant="primary")
91
 
 
92
  submit_button.click(
93
  fn=inpaint_image,
94
  inputs=[input_image, prompt_textbox, guidance_scale, num_steps],
 
1
+ # app.py (Final Corrected Version)
2
 
3
  import gradio as gr
4
  import torch
 
6
  from PIL import Image
7
  import time
8
 
9
+ # --- Model Loading ---
10
+ print("Loading model for low-RAM CPU environment...")
11
+ model_id = "runwayml/stable-diffusion-inpainting"
12
+ try:
13
+ pipe = AutoPipelineForInpainting.from_pretrained(model_id, torch_dtype=torch.float32)
14
+ pipe.enable_model_cpu_offload()
15
+ print("Model loaded successfully with CPU offloading enabled.")
16
+ except Exception as e:
17
+ print(f"An error occurred during model loading: {e}")
18
+ raise e
19
 
20
+ # --- Prompts ---
21
+ DEFAULT_PROMPT = "photorealistic, 4k, ultra high quality, sharp focus, masterpiece, high detail"
22
+ DEFAULT_NEGATIVE_PROMPT = "blurry, pixelated, distorted, deformed, ugly, disfigured, cartoon, watermark"
23
 
24
+ # --- Inpainting Function (Correct Signature) ---
25
+ def inpaint_image(image_and_mask, user_prompt, guidance_scale, num_steps, progress=gr.Progress(track_tqdm=True)):
26
+ # The input is now a dictionary with 'image' and 'mask' keys
27
+ image = image_and_mask["image"].convert("RGB")
28
+ mask = image_and_mask["mask"].convert("RGB")
29
+
30
+ if image is None or mask is None:
31
+ raise gr.Error("Please upload an image and draw a mask on it first!")
32
+
33
  if user_prompt and user_prompt.strip():
34
  prompt = user_prompt
35
  negative_prompt = DEFAULT_NEGATIVE_PROMPT
 
36
  else:
37
  prompt = DEFAULT_PROMPT
38
  negative_prompt = DEFAULT_NEGATIVE_PROMPT
 
39
 
40
  print(f"Starting inpainting on CPU...")
 
 
 
 
 
41
  result_image = pipe(
42
+ prompt=prompt, image=image, mask_image=mask, negative_prompt=negative_prompt,
43
+ guidance_scale=guidance_scale, num_inference_steps=int(num_steps)
 
 
 
 
 
 
44
  ).images[0]
45
 
 
 
46
  return result_image
47
 
48
+ # --- UI ---
 
49
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
50
+ gr.Markdown("# 🎨 AI Image Fixer (Stable Version)")
51
+ gr.Warning("‼️ PATIENCE REQUIRED! Generation can take 15-30 minutes on free hardware.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  with gr.Row():
54
  with gr.Column(scale=2):
55
+ # This component returns a dictionary when tool='brush'
56
  input_image = gr.Image(label="1. Upload & Mask Image", source="upload", tool="brush", type="pil")
57
+ prompt_textbox = gr.Textbox(label="2. Describe Your Fix (Optional)", placeholder="Leave empty for a general fix")
58
  with gr.Accordion("Advanced Settings", open=False):
59
  guidance_scale = gr.Slider(minimum=0, maximum=20, value=8.0, label="Guidance Scale")
60
+ num_steps = gr.Slider(minimum=10, maximum=50, step=1, value=20, label="Inference Steps")
61
  with gr.Column(scale=1):
62
  output_image = gr.Image(label="Result", type="pil")
63
 
64
  submit_button = gr.Button("Fix It!", variant="primary")
65
 
66
+ # The `inputs` list is simple. The function signature must match what Gradio provides.
67
  submit_button.click(
68
  fn=inpaint_image,
69
  inputs=[input_image, prompt_textbox, guidance_scale, num_steps],