nykadamec commited on
Commit
07492d9
·
1 Parent(s): d2b6d64

simplify: basic working version

Browse files
Files changed (1) hide show
  1. app.py +34 -177
app.py CHANGED
@@ -1,133 +1,57 @@
1
  """
2
  Hugging Face Image Generator - ZeroGPU Version
3
- Supports LoRA models from HuggingFace
4
  """
5
 
6
- # IMPORTANT: Import spaces FIRST before any CUDA-related packages
7
  import spaces
8
  import torch
9
- from typing import Tuple, Optional
10
  from PIL import Image
11
 
12
  import gradio as gr
13
- from diffusers import ZImagePipeline, UNet2DConditionModel
14
- from huggingface_hub import hf_hub_download
15
- import os
16
 
17
- # Model ID - Z-Image Turbo
18
  MODEL_ID = "Tongyi-MAI/Z-Image-Turbo"
19
-
20
- # Global pipeline
21
  pipe = None
22
- current_lora = None
23
-
24
-
25
- def load_lora(pipe, lora_path: str, lora_scale: float = 1.0):
26
- """Load a LoRA model"""
27
- try:
28
- from diffusers import DDPMScheduler, DDIMScheduler, PNDMScheduler, EulerDiscreteScheduler
29
-
30
- # Load LoRA weights
31
- state_dict = torch.load(lora_path, map_location="cpu")
32
-
33
- # Load into unet
34
- unet = pipe.unet
35
- unet.load_state_dict(state_dict, strict=False)
36
-
37
- return True, f"LoRA loaded: {lora_path}"
38
- except Exception as e:
39
- error_msg = str(e)
40
- return False, f"LoRA error: {error_msg}"
41
 
42
 
43
  @spaces.GPU(duration=180)
44
- def generate(
45
- prompt: str,
46
- negative_prompt: str = "",
47
- steps: int = 9,
48
- width: int = 1024,
49
- height: int = 1024,
50
- seed: int = 0,
51
- lora_repo: str = "",
52
- lora_scale: float = 1.0,
53
- guidance_scale: float = 0.0,
54
- scheduler: str = "EulerDiscrete",
55
- enable_vae_slicing: bool = True,
56
- enable_vae_tiling: bool = False,
57
- ) -> Tuple[Image.Image | None, str]:
58
- """Generate an image using Z-Image-Turbo model with optional LoRA."""
59
- global pipe, current_lora
60
 
61
  if not prompt:
62
  return None, "Please enter a prompt"
63
 
64
  try:
65
- # Load model if not loaded
66
  if pipe is None:
67
- print("Loading Z-Image-Turbo model...")
68
  pipe = ZImagePipeline.from_pretrained(
69
  MODEL_ID,
70
  torch_dtype=torch.bfloat16,
71
  low_cpu_mem_usage=False,
72
  )
73
  pipe.to("cuda")
74
-
75
- # Enable optimizations
76
- if enable_vae_slicing:
77
- pipe.enable_vae_slicing()
78
- if enable_vae_tiling:
79
- pipe.enable_vae_tiling()
80
-
81
  print("Model loaded!")
82
 
83
- # Convert lora_scale to float
84
- try:
85
- lora_scale_float = float(lora_scale) if lora_scale else 1.0
86
- except (ValueError, TypeError):
87
- lora_scale_float = 1.0
88
-
89
- # Load LoRA if specified and valid
90
- if lora_repo and isinstance(lora_repo, str) and lora_repo.strip() and "/" in lora_repo and lora_repo != current_lora:
91
- try:
92
- print(f"Loading LoRA: {lora_repo}")
93
- # Download LoRA from HF Hub
94
- lora_path = hf_hub_download(
95
- repo_id=lora_repo,
96
- filename="pytorch_lora_weights.safetensors",
97
- )
98
- # Apply LoRA
99
- pipe.load_lora_weights(lora_path)
100
- current_lora = lora_repo
101
- print(f"LoRA loaded: {lora_repo}")
102
- except Exception as e:
103
- error_msg = str(e)
104
- print(f"LoRA loading failed: {error_msg}")
105
- current_lora = None
106
-
107
- # Convert guidance_scale to float
108
- try:
109
- guidance_scale_float = float(guidance_scale) if guidance_scale else 0.0
110
- except (ValueError, TypeError):
111
- guidance_scale_float = 0.0
112
-
113
- # Convert steps to int
114
  try:
115
  steps_int = int(steps) if steps else 9
116
- except (ValueError, TypeError):
117
  steps_int = 9
118
 
119
- # Set scheduler
120
- if scheduler == "EulerDiscrete":
121
- pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)
122
-
123
- # Set seed for reproducibility
124
- generator = None
125
  try:
126
  seed_int = int(seed) if seed else 0
127
- except (ValueError, TypeError):
128
  seed_int = 0
 
 
 
 
 
129
 
130
- if seed_int and seed_int > 0:
 
 
131
  generator = torch.Generator("cuda").manual_seed(seed_int)
132
 
133
  # Generate
@@ -135,112 +59,45 @@ def generate(
135
  prompt=prompt,
136
  negative_prompt=negative_prompt if negative_prompt else None,
137
  num_inference_steps=steps_int,
138
- guidance_scale=guidance_scale_float,
139
  width=width,
140
  height=height,
141
  generator=generator,
142
  )
143
 
144
- # Get the image
145
  image = result.images[0]
146
-
147
- # Convert to RGB if needed
148
  if image.mode != 'RGB':
149
  image = image.convert('RGB')
150
 
151
  return image, "Success!"
152
 
153
  except Exception as e:
154
- error_msg = str(e)
155
- return None, f"Error: {error_msg}"
156
 
157
 
158
- # Build UI
159
- with gr.Blocks(title="AI Image Generator", theme=gr.themes.Soft()) as demo:
160
  gr.Markdown("# 🎨 AI Image Generator")
161
- gr.Markdown("**Z-Image-Turbo** with LoRA support")
162
 
163
  with gr.Row():
164
- with gr.Column(scale=2):
165
- prompt = gr.Textbox(
166
- label="Prompt",
167
- placeholder="A beautiful sunset over the ocean, highly detailed, 8k...",
168
- lines=4,
169
- )
170
- negative_prompt = gr.Textbox(
171
- label="Negative Prompt",
172
- placeholder="blurry, low quality, distorted, deformed...",
173
- lines=2,
174
- )
175
-
176
- with gr.Column(scale=1):
177
- # LoRA settings
178
- gr.Markdown("### 🎭 LoRA (optional)")
179
- lora_repo = gr.Textbox(
180
- label="LoRA Repo ID",
181
- placeholder="e.g., art原来/your-lora",
182
- value="",
183
- )
184
- lora_scale = gr.Slider(
185
- minimum=0.0, maximum=2.0, value=1.0, step=0.1,
186
- label="LoRA Scale",
187
- )
188
 
189
  with gr.Row():
190
  with gr.Column():
191
- gr.Markdown("### ⚙️ Generation Settings")
192
- steps = gr.Slider(
193
- minimum=1, maximum=30, value=9, step=1,
194
- label="Steps",
195
- )
196
- guidance_scale = gr.Slider(
197
- minimum=0.0, maximum=10.0, value=0.0, step=0.5,
198
- label="Guidance Scale (0 for Turbo)",
199
- )
200
- scheduler = gr.Dropdown(
201
- ["EulerDiscrete", "DDIM", "PNDM", "DDPMS"],
202
- value="EulerDiscrete",
203
- label="Scheduler",
204
- )
205
-
206
  with gr.Column():
207
- gr.Markdown("### 📐 Resolution")
208
- resolution = gr.Dropdown(
209
- ["512x512", "768x768", "1024x1024", "1024x768", "768x1024", "1280x720", "720x1280"],
210
- value="1024x1024",
211
- label="Resolution",
212
- )
213
- seed = gr.Number(value=0, label="Seed (0=random)")
214
-
215
- with gr.Row():
216
- vae_slicing = gr.Checkbox(label="VAE Slicing", value=True)
217
- vae_tiling = gr.Checkbox(label="VAE Tiling", value=False)
218
-
219
- generate_btn = gr.Button("🎨 Generate Image", variant="primary", size="lg")
220
-
221
- with gr.Row():
222
- output = gr.Image(label="Generated Image", type="pil")
223
-
224
- status = gr.Textbox(label="Status", interactive=False)
225
 
226
- # Parse resolution
227
- def parse_res(resolution):
228
- if resolution:
229
- w, h = resolution.split("x")
230
- return int(w), int(h)
231
- return 1024, 1024
232
 
233
- # Generate on click
234
- generate_btn.click(
235
- generate,
236
- inputs=[
237
- prompt, negative_prompt, steps,
238
- resolution, seed,
239
- lora_repo, lora_scale,
240
- guidance_scale, scheduler,
241
- vae_slicing, vae_tiling
242
- ],
243
- outputs=[output, status],
244
- )
245
 
246
  demo.launch()
 
1
  """
2
  Hugging Face Image Generator - ZeroGPU Version
3
+ Simple version with Z-Image-Turbo
4
  """
5
 
 
6
  import spaces
7
  import torch
8
+ from typing import Tuple
9
  from PIL import Image
10
 
11
  import gradio as gr
12
+ from diffusers import ZImagePipeline
 
 
13
 
 
14
  MODEL_ID = "Tongyi-MAI/Z-Image-Turbo"
 
 
15
  pipe = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
 
18
  @spaces.GPU(duration=180)
19
+ def generate(prompt, negative_prompt, steps, width, height, seed, guidance_scale):
20
+ global pipe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  if not prompt:
23
  return None, "Please enter a prompt"
24
 
25
  try:
 
26
  if pipe is None:
27
+ print("Loading model...")
28
  pipe = ZImagePipeline.from_pretrained(
29
  MODEL_ID,
30
  torch_dtype=torch.bfloat16,
31
  low_cpu_mem_usage=False,
32
  )
33
  pipe.to("cuda")
 
 
 
 
 
 
 
34
  print("Model loaded!")
35
 
36
+ # Parse parameters
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  try:
38
  steps_int = int(steps) if steps else 9
39
+ except:
40
  steps_int = 9
41
 
 
 
 
 
 
 
42
  try:
43
  seed_int = int(seed) if seed else 0
44
+ except:
45
  seed_int = 0
46
+
47
+ try:
48
+ guidance_float = float(guidance_scale) if guidance_scale else 0.0
49
+ except:
50
+ guidance_float = 0.0
51
 
52
+ # Seed
53
+ generator = None
54
+ if seed_int > 0:
55
  generator = torch.Generator("cuda").manual_seed(seed_int)
56
 
57
  # Generate
 
59
  prompt=prompt,
60
  negative_prompt=negative_prompt if negative_prompt else None,
61
  num_inference_steps=steps_int,
62
+ guidance_scale=guidance_float,
63
  width=width,
64
  height=height,
65
  generator=generator,
66
  )
67
 
 
68
  image = result.images[0]
 
 
69
  if image.mode != 'RGB':
70
  image = image.convert('RGB')
71
 
72
  return image, "Success!"
73
 
74
  except Exception as e:
75
+ err = str(e)
76
+ return None, "Error: " + err
77
 
78
 
79
+ with gr.Blocks(title="AI Image Generator") as demo:
 
80
  gr.Markdown("# 🎨 AI Image Generator")
81
+ gr.Markdown("Z-Image-Turbo - 1024x1024")
82
 
83
  with gr.Row():
84
+ with gr.Column():
85
+ prompt = gr.Textbox(label="Prompt", placeholder="A cat...", lines=3)
86
+ negative_prompt = gr.Textbox(label="Negative Prompt", lines=2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
  with gr.Row():
89
  with gr.Column():
90
+ steps = gr.Slider(1, 30, value=9, step=1, label="Steps")
91
+ guidance_scale = gr.Slider(0, 10, value=0, step=0.5, label="Guidance Scale")
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  with gr.Column():
93
+ width = gr.Dropdown([512, 768, 1024], value=1024, label="Width")
94
+ height = gr.Dropdown([512, 768, 1024], value=1024, label="Height")
95
+ seed = gr.Number(value=0, label="Seed")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
+ generate_btn = gr.Button("Generate", variant="primary")
98
+ output = gr.Image(label="Result")
99
+ status = gr.Textbox(label="Status")
 
 
 
100
 
101
+ generate_btn.click(generate, inputs=[prompt, negative_prompt, steps, width, height, seed, guidance_scale], outputs=[output, status])
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  demo.launch()