Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from diffusers import
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
# ---------------------------------------------------------------------------
|
| 7 |
-
#
|
| 8 |
-
#
|
| 9 |
-
#
|
| 10 |
-
#
|
| 11 |
-
# for a free deployment.
|
| 12 |
# ---------------------------------------------------------------------------
|
| 13 |
|
| 14 |
-
print("Loading
|
| 15 |
-
pipe =
|
| 16 |
-
"
|
| 17 |
-
torch_dtype=torch.float32
|
|
|
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
image = pipe(
|
| 34 |
prompt=prompt,
|
| 35 |
image=init_image,
|
| 36 |
-
num_inference_steps=
|
| 37 |
-
|
| 38 |
-
guidance_scale=
|
| 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 //
|
| 46 |
-
gr.Markdown("Powered by
|
| 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
|
| 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 |
-
#
|
| 58 |
-
|
|
|
|
|
|
|
| 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"
|
| 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()
|