Reboot2004 commited on
Commit
ca8d54a
·
verified ·
1 Parent(s): 33a24c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -30
app.py CHANGED
@@ -1,20 +1,20 @@
1
  import gradio as gr
2
  import torch
3
- from diffusers import AutoPipelineForImage2Image
4
  from PIL import Image
5
 
6
  # ---------------------------------------------------------------------------
7
- # Why SD-Turbo?
8
- # Hugging Face Free Spaces only have 2 vCPUs. Standard Image models take
9
- # 3-5 minutes per image on CPU because they require 30-50 steps.
10
- # SD-Turbo only requires 1 to 3 steps! It is incredibly fast and perfect
11
- # for a free deployment.
12
  # ---------------------------------------------------------------------------
13
 
14
- print("Loading SD-Turbo Model... (This may take a minute on boot)")
15
- pipe = AutoPipelineForImage2Image.from_pretrained(
16
- "stabilityai/sd-turbo",
17
- torch_dtype=torch.float32 # Use float32 for CPU compatibility
 
18
  )
19
 
20
  def process_image(init_image, prompt, strength, steps):
@@ -22,40 +22,36 @@ def process_image(init_image, prompt, strength, steps):
22
  return None
23
 
24
  print(f"Received request: '{prompt}'")
25
-
26
- # Resize image to SD-Turbo's preferred 512x512 resolution
27
- # Maintaining aspect ratio by cropping or padding would be better,
28
- # but exact 512x512 prevents memory spikes on the free CPU tier.
29
  init_image = init_image.convert("RGB")
30
- init_image = init_image.resize((512, 512))
31
 
32
- # Run the pipeline
 
 
 
 
33
  image = pipe(
34
  prompt=prompt,
35
  image=init_image,
36
- num_inference_steps=int(steps),
37
- strength=float(strength),
38
- guidance_scale=0.0 # Turbo mathematically requires guidance_scale=0.0
39
  ).images[0]
40
 
41
  return image
42
 
43
- # Define the Gradio Interface
44
  with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
45
- gr.Markdown("# 🪄 WiggleAgent // Free Img2Img Backend")
46
- gr.Markdown("Powered by SD-Turbo (Optimized for Free CPU Tiers)")
47
 
48
  with gr.Row():
49
  with gr.Column():
50
  input_image = gr.Image(type="pil", label="Input Image (Your Screenshot)")
51
- prompt = gr.Textbox(label="Prompt", value="cyberpunk style, dark neon city, glowing interface")
52
-
53
- # Strength determines how much of the original image is preserved.
54
- # 0.1 = Almost no change. 1.0 = Completely new image.
55
- strength = gr.Slider(minimum=0.1, maximum=1.0, value=0.6, step=0.05, label="Transformation Strength")
56
 
57
- # Steps determines quality. 2 is the sweet spot for SD-Turbo.
58
- steps = gr.Slider(minimum=1, maximum=4, value=2, step=1, label="Inference Steps (Keep low for CPU)")
 
 
59
 
60
  btn = gr.Button("Generate", variant="primary")
61
 
@@ -66,7 +62,7 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
66
  fn=process_image,
67
  inputs=[input_image, prompt, strength, steps],
68
  outputs=output_image,
69
- api_name="predict" # Exposes this function to our gradio_client in WiggleAgent!
70
  )
71
 
72
  demo.launch()
 
1
  import gradio as gr
2
  import torch
3
+ from diffusers import StableDiffusionInstructPix2PixPipeline
4
  from PIL import Image
5
 
6
  # ---------------------------------------------------------------------------
7
+ # Instruct-Pix2Pix
8
+ # This model is specifically trained to edit images while preserving
9
+ # their exact structure. It takes much longer on CPU (approx 2-3 mins),
10
+ # but the text and layout will look significantly more accurate.
 
11
  # ---------------------------------------------------------------------------
12
 
13
+ print("Loading Instruct-Pix2Pix Model... (This takes a moment)")
14
+ pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(
15
+ "timbrooks/instruct-pix2pix",
16
+ torch_dtype=torch.float32,
17
+ safety_checker=None
18
  )
19
 
20
  def process_image(init_image, prompt, strength, steps):
 
22
  return None
23
 
24
  print(f"Received request: '{prompt}'")
 
 
 
 
25
  init_image = init_image.convert("RGB")
 
26
 
27
+ # Resize keeping aspect ratio to max 512 for CPU memory limits
28
+ init_image.thumbnail((512, 512))
29
+
30
+ # InstructPix2Pix uses image_guidance_scale.
31
+ # 1.5 strictly preserves original image structure.
32
  image = pipe(
33
  prompt=prompt,
34
  image=init_image,
35
+ num_inference_steps=20, # Higher quality, but takes 2 mins on free CPU
36
+ image_guidance_scale=1.5,
37
+ guidance_scale=7.5
38
  ).images[0]
39
 
40
  return image
41
 
 
42
  with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
43
+ gr.Markdown("# 🪄 WiggleAgent // Instruct-Pix2Pix Backend")
44
+ gr.Markdown("Powered by Instruct-Pix2Pix (High Structural Preservation)")
45
 
46
  with gr.Row():
47
  with gr.Column():
48
  input_image = gr.Image(type="pil", label="Input Image (Your Screenshot)")
49
+ prompt = gr.Textbox(label="Prompt", value="cyberpunk style, dark neon city")
 
 
 
 
50
 
51
+ # These sliders exist so the Gradio Client in main.py doesn't crash,
52
+ # but the actual function hardcodes the best values for Instruct-Pix2Pix.
53
+ strength = gr.Slider(minimum=0.1, maximum=1.0, value=0.6, label="Ignored by Backend")
54
+ steps = gr.Slider(minimum=1, maximum=4, value=2, label="Ignored by Backend")
55
 
56
  btn = gr.Button("Generate", variant="primary")
57
 
 
62
  fn=process_image,
63
  inputs=[input_image, prompt, strength, steps],
64
  outputs=output_image,
65
+ api_name="/predict"
66
  )
67
 
68
  demo.launch()