zainjaved commited on
Commit
7359e88
·
verified ·
1 Parent(s): e564fcc

uploaded app with image upload

Browse files
Files changed (2) hide show
  1. app.py +205 -0
  2. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ from PIL import Image
5
+
6
+ import spaces
7
+ from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, StableDiffusionImg2ImgPipeline
8
+ import torch
9
+
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
11
+ model_repo_id = "tensorart/stable-diffusion-3.5-large-TurboX"
12
+
13
+ if torch.cuda.is_available():
14
+ torch_dtype = torch.float16
15
+ else:
16
+ torch_dtype = torch.float32
17
+
18
+ # For text-to-image
19
+ pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
20
+ pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(model_repo_id, subfolder="scheduler", shift=5)
21
+ pipe = pipe.to(device)
22
+
23
+ # For image-to-image
24
+ img2img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
25
+ model_repo_id,
26
+ torch_dtype=torch_dtype
27
+ )
28
+ img2img_pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(model_repo_id, subfolder="scheduler", shift=5)
29
+ img2img_pipe = img2img_pipe.to(device)
30
+
31
+ MAX_SEED = np.iinfo(np.int32).max
32
+ MAX_IMAGE_SIZE = 1024
33
+
34
+
35
+ @spaces.GPU(duration=65)
36
+ def infer(
37
+ prompt,
38
+ negative_prompt="",
39
+ seed=42,
40
+ randomize_seed=False,
41
+ width=1024,
42
+ height=1024,
43
+ guidance_scale=1.5,
44
+ num_inference_steps=8,
45
+ input_image=None,
46
+ strength=0.8,
47
+ progress=gr.Progress(track_tqdm=True),
48
+ ):
49
+ if randomize_seed:
50
+ seed = random.randint(0, MAX_SEED)
51
+
52
+ generator = torch.Generator().manual_seed(seed)
53
+
54
+ # Text-to-image if no input image is provided
55
+ if input_image is None:
56
+ image = pipe(
57
+ prompt=prompt,
58
+ negative_prompt=negative_prompt,
59
+ guidance_scale=guidance_scale,
60
+ num_inference_steps=num_inference_steps,
61
+ width=width,
62
+ height=height,
63
+ generator=generator,
64
+ ).images[0]
65
+ # Image-to-image if an input image is provided
66
+ else:
67
+ # Convert to PIL Image if it's a numpy array
68
+ if isinstance(input_image, np.ndarray):
69
+ input_image = Image.fromarray(input_image)
70
+
71
+ # Resize image to match requested dimensions
72
+ input_image = input_image.resize((width, height), Image.LANCZOS)
73
+
74
+ image = img2img_pipe(
75
+ prompt=prompt,
76
+ negative_prompt=negative_prompt,
77
+ image=input_image,
78
+ strength=strength,
79
+ guidance_scale=guidance_scale,
80
+ num_inference_steps=num_inference_steps,
81
+ generator=generator,
82
+ ).images[0]
83
+
84
+ return image, seed
85
+
86
+
87
+ examples = [
88
+ "A capybara wearing a suit holding a sign that reads Hello World",
89
+ ]
90
+
91
+ css = """
92
+ #col-container {
93
+ margin: 0 auto;
94
+ max-width: 640px;
95
+ }
96
+ """
97
+
98
+ with gr.Blocks(css=css) as demo:
99
+ with gr.Column(elem_id="col-container"):
100
+ gr.Markdown(" # TensorArt Stable Diffusion 3.5 Large TurboX")
101
+ gr.Markdown(
102
+ "[8-step distilled turbo model](https://huggingface.co/tensorart/stable-diffusion-3.5-large-TurboX)")
103
+ with gr.Row():
104
+ prompt = gr.Text(
105
+ label="Prompt",
106
+ show_label=False,
107
+ max_lines=1,
108
+ placeholder="Enter your prompt",
109
+ container=False,
110
+ )
111
+
112
+ run_button = gr.Button("Run", scale=0, variant="primary")
113
+
114
+ # Add image upload component
115
+ input_image = gr.Image(
116
+ label="Input Image (Optional)",
117
+ type="pil",
118
+ sources=["upload", "clipboard"],
119
+ )
120
+
121
+ result = gr.Image(label="Result", show_label=False)
122
+
123
+ with gr.Accordion("Advanced Settings", open=False):
124
+ negative_prompt = gr.Text(
125
+ label="Negative prompt",
126
+ max_lines=1,
127
+ placeholder="Enter a negative prompt",
128
+ )
129
+
130
+ seed = gr.Slider(
131
+ label="Seed",
132
+ minimum=0,
133
+ maximum=MAX_SEED,
134
+ step=1,
135
+ value=0,
136
+ )
137
+
138
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
139
+
140
+ with gr.Row():
141
+ width = gr.Slider(
142
+ label="Width",
143
+ minimum=512,
144
+ maximum=MAX_IMAGE_SIZE,
145
+ step=32,
146
+ value=1024,
147
+ )
148
+
149
+ height = gr.Slider(
150
+ label="Height",
151
+ minimum=512,
152
+ maximum=MAX_IMAGE_SIZE,
153
+ step=32,
154
+ value=1024,
155
+ )
156
+
157
+ with gr.Row():
158
+ guidance_scale = gr.Slider(
159
+ label="Guidance scale",
160
+ minimum=0.0,
161
+ maximum=7.5,
162
+ step=0.1,
163
+ value=1.5,
164
+ )
165
+
166
+ num_inference_steps = gr.Slider(
167
+ label="Number of inference steps",
168
+ minimum=1,
169
+ maximum=50,
170
+ step=1,
171
+ value=8,
172
+ )
173
+
174
+ # Add strength parameter for image-to-image
175
+ strength = gr.Slider(
176
+ label="Strength (for image-to-image)",
177
+ minimum=0.0,
178
+ maximum=1.0,
179
+ step=0.01,
180
+ value=0.8,
181
+ info="How much to transform the reference image. 1.0 means complete transformation."
182
+ )
183
+
184
+ gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=True,
185
+ cache_mode="lazy")
186
+ gr.on(
187
+ triggers=[run_button.click, prompt.submit],
188
+ fn=infer,
189
+ inputs=[
190
+ prompt,
191
+ negative_prompt,
192
+ seed,
193
+ randomize_seed,
194
+ width,
195
+ height,
196
+ guidance_scale,
197
+ num_inference_steps,
198
+ input_image,
199
+ strength,
200
+ ],
201
+ outputs=[result, seed],
202
+ )
203
+
204
+ if __name__ == "__main__":
205
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ accelerate
2
+ diffusers
3
+ torch
4
+ transformers
5
+ git+https://github.com/huggingface/diffusers.git
6
+ sentencepiece
7
+ gradio
8
+ spaces