Reboot2004 commited on
Commit
c893ae8
·
verified ·
1 Parent(s): 34e22a5

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +72 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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):
21
+ if init_image is None:
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
+
62
+ with gr.Column():
63
+ output_image = gr.Image(type="pil", label="Output Image")
64
+
65
+ btn.click(
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()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio==4.31.5
2
+ torch==2.3.0
3
+ diffusers==0.27.2
4
+ transformers==4.41.1
5
+ accelerate==0.30.1