we2app commited on
Commit
2556f4e
·
verified ·
1 Parent(s): 7790ee6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -0
app.py CHANGED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import AutoPipelineForImage2Image, AutoPipelineForText2Image
2
+ import torch
3
+ import os
4
+ from PIL import Image
5
+ import numpy as np
6
+ import gradio as gr
7
+ import time
8
+ import math
9
+
10
+ # FORCE CPU SETTINGS
11
+ device = torch.device("cpu")
12
+ torch_device = "cpu"
13
+ # CPUs generally require float32 for stability and compatibility
14
+ torch_dtype = torch.float32
15
+
16
+ print(f"Running on DEVICE: {device}")
17
+
18
+ # Load pipelines
19
+ # We remove variant="fp16" because we are using float32 for CPU
20
+ pipe_kwargs = {
21
+ "torch_dtype": torch_dtype,
22
+ "safety_checker": None if os.environ.get("SAFETY_CHECKER") != "True" else None,
23
+ "use_safetensors": True
24
+ }
25
+
26
+ i2i_pipe = AutoPipelineForImage2Image.from_pretrained(
27
+ "stabilityai/sdxl-turbo", **pipe_kwargs
28
+ )
29
+ t2i_pipe = AutoPipelineForText2Image.from_pretrained(
30
+ "stabilityai/sdxl-turbo", **pipe_kwargs
31
+ )
32
+
33
+ # OPTIMIZATION FOR 16GB RAM
34
+ # enables slicing the attention computation into steps to save memory
35
+ i2i_pipe.enable_attention_slicing()
36
+ t2i_pipe.enable_attention_slicing()
37
+
38
+ # Moves models to CPU
39
+ i2i_pipe.to("cpu")
40
+ t2i_pipe.to("cpu")
41
+
42
+ i2i_pipe.set_progress_bar_config(disable=False) # Enabled so you can see it's working
43
+ t2i_pipe.set_progress_bar_config(disable=False)
44
+
45
+ def resize_crop(image, size=512):
46
+ image = image.convert("RGB")
47
+ w, h = image.size
48
+ image = image.resize((size, int(size * (h / w))), Image.BICUBIC)
49
+ return image
50
+
51
+ async def predict(init_image, prompt, strength, steps, seed=1231231):
52
+ generator = torch.manual_seed(seed)
53
+ last_time = time.time()
54
+
55
+ # Ensure steps are sufficient for Turbo
56
+ if int(steps * strength) < 1:
57
+ steps = math.ceil(1 / max(0.10, strength))
58
+
59
+ if init_image is not None:
60
+ init_image = resize_crop(init_image)
61
+ results = i2i_pipe(
62
+ prompt=prompt,
63
+ image=init_image,
64
+ generator=generator,
65
+ num_inference_steps=int(steps),
66
+ guidance_scale=0.0,
67
+ strength=strength,
68
+ width=512,
69
+ height=512,
70
+ output_type="pil",
71
+ )
72
+ else:
73
+ results = t2i_pipe(
74
+ prompt=prompt,
75
+ generator=generator,
76
+ num_inference_steps=int(steps),
77
+ guidance_scale=0.0,
78
+ width=512,
79
+ height=512,
80
+ output_type="pil",
81
+ )
82
+
83
+ print(f"Inference took {time.time() - last_time:.2f} seconds")
84
+ return results.images[0]
85
+
86
+ # --- Gradio UI Section (Keep your existing CSS and Blocks) ---
87
+ css = """#container{ margin: 0 auto; max-width: 80rem; }"""
88
+ with gr.Blocks(css=css) as demo:
89
+ # ... (Keep your UI layout exactly the same)
90
+ # Ensure the button calls the predict function
91
+ # Note: Removed the automatic 'change' triggers to prevent CPU freezing
92
+ # while typing. Better to use the Generate button only.
93
+
94
+ init_image_state = gr.State()
95
+ with gr.Column(elem_id="container"):
96
+ gr.Markdown("# SDXL Turbo CPU Edition")
97
+ with gr.Row():
98
+ prompt = gr.Textbox(placeholder="Prompt...", scale=5)
99
+ generate_bt = gr.Button("Generate", scale=1)
100
+
101
+ with gr.Row():
102
+ with gr.Column():
103
+ image_input = gr.Image(sources=["upload", "webcam"], type="pil")
104
+ with gr.Column():
105
+ image_output = gr.Image(type="filepath")
106
+
107
+ with gr.Accordion("Settings", open=True):
108
+ strength = gr.Slider(label="Strength", value=0.7, minimum=0.0, maximum=1.0)
109
+ steps = gr.Slider(label="Steps (1-4 is best for Turbo)", value=2, minimum=1, maximum=10, step=1)
110
+ seed = gr.Slider(label="Seed", minimum=0, maximum=999999, step=1, randomize=True)
111
+
112
+ inputs = [image_input, prompt, strength, steps, seed]
113
+ generate_bt.click(fn=predict, inputs=inputs, outputs=image_output)
114
+
115
+ demo.queue()
116
+ demo.launch()