akhaliq HF Staff commited on
Commit
b01fe58
·
verified ·
1 Parent(s): c0b519f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -100
app.py CHANGED
@@ -1,14 +1,15 @@
1
  import spaces
2
  import gradio as gr
3
  import torch
4
- from diffusers import ZImagePipeline, AutoPipelineForText2Image
5
  import os
6
  from pathlib import Path
7
 
8
- # Load the base model directly at startup
9
  print("Loading Z-Image Turbo model...")
10
  print("This may take a few minutes on first run while the model downloads...")
11
 
 
12
  # Load the pipeline with optimal settings
13
  pipe = ZImagePipeline.from_pretrained(
14
  "Tongyi-MAI/Z-Image-Turbo",
@@ -23,27 +24,21 @@ print(f"Model loaded on {device}")
23
 
24
  print("Model loaded successfully!")
25
 
26
- # Store the current model state
27
- current_model = "base"
28
- lora_loaded = False
29
-
30
  @spaces.GPU()
31
  def generate_image(
32
  prompt,
33
- model_choice,
34
  progress=gr.Progress(track_tqdm=True)
35
  ):
36
  """
37
- Generate an image using Z-Image Turbo model with optional LoRA.
38
 
39
  Args:
40
  prompt: Text description of the desired image
41
- model_choice: Either "Base Model" or "Classic Painting LoRA"
42
 
43
  Returns:
44
  Generated PIL Image
45
  """
46
- global pipe, current_model, lora_loaded
47
 
48
  if pipe is None:
49
  raise gr.Error("Model failed to load on startup. Please restart the application.")
@@ -54,69 +49,22 @@ def generate_image(
54
  # Determine device
55
  device = "cuda" if torch.cuda.is_available() else "cpu"
56
 
57
- # Handle model switching
58
- progress(0.05, desc="Loading model...")
 
 
 
59
 
60
  try:
61
- if model_choice == "Classic Painting LoRA" and current_model != "lora":
62
- # Load LoRA weights
63
- if not lora_loaded:
64
- print("Loading Classic Painting LoRA...")
65
- pipe.load_lora_weights(
66
- "renderartist/Classic-Painting-Z-Image-Turbo-LoRA",
67
- adapter_name="classic_painting",
68
- weight_name="pytorch_lora_weights.safetensors"
69
- )
70
- lora_loaded = True
71
-
72
- # Set LoRA adapter
73
- pipe.set_adapters(["classic_painting"], adapter_weights=[0.8])
74
- current_model = "lora"
75
- progress(0.15, desc="LoRA loaded, generating image...")
76
-
77
- # Generate with LoRA settings
78
- generator = torch.Generator(device).manual_seed(42)
79
- result = pipe(
80
- prompt=prompt,
81
- negative_prompt=None,
82
- height=1024,
83
- width=1024,
84
- num_inference_steps=9,
85
- guidance_scale=0.0,
86
- generator=generator,
87
- )
88
-
89
- elif model_choice == "Base Model" and current_model != "base":
90
- # Disable LoRA
91
- pipe.disable_lora()
92
- current_model = "base"
93
- progress(0.15, desc="Generating image...")
94
-
95
- # Generate with base model settings
96
- generator = torch.Generator(device).manual_seed(42)
97
- result = pipe(
98
- prompt=prompt,
99
- negative_prompt=None,
100
- height=1024,
101
- width=1024,
102
- num_inference_steps=9,
103
- guidance_scale=0.0,
104
- generator=generator,
105
- )
106
-
107
- else:
108
- # Model already loaded, just generate
109
- progress(0.15, desc="Generating image...")
110
- generator = torch.Generator(device).manual_seed(42)
111
- result = pipe(
112
- prompt=prompt,
113
- negative_prompt=None,
114
- height=1024,
115
- width=1024,
116
- num_inference_steps=9,
117
- guidance_scale=0.0,
118
- generator=generator,
119
- )
120
 
121
  image = result.images[0]
122
  progress(1.0, desc="Complete!")
@@ -182,19 +130,6 @@ apple_css = """
182
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
183
  }
184
 
185
- /* Model Selector */
186
- .model-selector {
187
- margin-bottom: 24px;
188
- }
189
-
190
- .model-selector label {
191
- font-size: 15px !important;
192
- font-weight: 500 !important;
193
- color: #1d1d1f !important;
194
- margin-bottom: 8px !important;
195
- display: block !important;
196
- }
197
-
198
  /* Textbox */
199
  textarea {
200
  font-size: 17px !important;
@@ -296,10 +231,6 @@ button.primary:active {
296
  color: #86868b !important;
297
  }
298
 
299
- .dark .model-selector label {
300
- color: #f5f5f7 !important;
301
- }
302
-
303
  /* Responsive */
304
  @media (max-width: 734px) {
305
  .main-title {
@@ -345,15 +276,6 @@ with gr.Blocks(
345
 
346
  # Input Section
347
  with gr.Column(elem_classes="input-section"):
348
- # Model Selector
349
- with gr.Group(elem_classes="model-selector"):
350
- model_choice = gr.Radio(
351
- choices=["Base Model", "Classic Painting LoRA"],
352
- value="Base Model",
353
- label="Select Model",
354
- info="Choose between the base Z-Image Turbo model or the Classic Painting LoRA variant"
355
- )
356
-
357
  prompt = gr.Textbox(
358
  placeholder="Describe the image you want to create...",
359
  lines=3,
@@ -384,21 +306,20 @@ with gr.Blocks(
384
  gr.HTML("""
385
  <div class="footer-text">
386
  <p>Powered by Z-Image Turbo from Tongyi-MAI</p>
387
- <p>Classic Painting LoRA by renderartist</p>
388
  </div>
389
  """)
390
 
391
  # Event handlers
392
  generate_btn.click(
393
  fn=generate_image,
394
- inputs=[prompt, model_choice],
395
  outputs=output_image,
396
  api_visibility="public"
397
  )
398
 
399
  prompt.submit(
400
  fn=generate_image,
401
- inputs=[prompt, model_choice],
402
  outputs=output_image,
403
  api_visibility="public"
404
  )
 
1
  import spaces
2
  import gradio as gr
3
  import torch
4
+ from diffusers import ZImagePipeline
5
  import os
6
  from pathlib import Path
7
 
8
+ # Load the model directly at startup
9
  print("Loading Z-Image Turbo model...")
10
  print("This may take a few minutes on first run while the model downloads...")
11
 
12
+
13
  # Load the pipeline with optimal settings
14
  pipe = ZImagePipeline.from_pretrained(
15
  "Tongyi-MAI/Z-Image-Turbo",
 
24
 
25
  print("Model loaded successfully!")
26
 
 
 
 
 
27
  @spaces.GPU()
28
  def generate_image(
29
  prompt,
 
30
  progress=gr.Progress(track_tqdm=True)
31
  ):
32
  """
33
+ Generate an image using Z-Image Turbo model.
34
 
35
  Args:
36
  prompt: Text description of the desired image
 
37
 
38
  Returns:
39
  Generated PIL Image
40
  """
41
+ global pipe
42
 
43
  if pipe is None:
44
  raise gr.Error("Model failed to load on startup. Please restart the application.")
 
49
  # Determine device
50
  device = "cuda" if torch.cuda.is_available() else "cpu"
51
 
52
+ # Set random seed for reproducibility
53
+ generator = torch.Generator(device).manual_seed(42)
54
+
55
+ # Generate the image with optimal settings
56
+ progress(0.1, desc="Generating image...")
57
 
58
  try:
59
+ result = pipe(
60
+ prompt=prompt,
61
+ negative_prompt=None,
62
+ height=1024,
63
+ width=1024,
64
+ num_inference_steps=9,
65
+ guidance_scale=0.0,
66
+ generator=generator,
67
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
  image = result.images[0]
70
  progress(1.0, desc="Complete!")
 
130
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
131
  }
132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  /* Textbox */
134
  textarea {
135
  font-size: 17px !important;
 
231
  color: #86868b !important;
232
  }
233
 
 
 
 
 
234
  /* Responsive */
235
  @media (max-width: 734px) {
236
  .main-title {
 
276
 
277
  # Input Section
278
  with gr.Column(elem_classes="input-section"):
 
 
 
 
 
 
 
 
 
279
  prompt = gr.Textbox(
280
  placeholder="Describe the image you want to create...",
281
  lines=3,
 
306
  gr.HTML("""
307
  <div class="footer-text">
308
  <p>Powered by Z-Image Turbo from Tongyi-MAI</p>
 
309
  </div>
310
  """)
311
 
312
  # Event handlers
313
  generate_btn.click(
314
  fn=generate_image,
315
+ inputs=prompt,
316
  outputs=output_image,
317
  api_visibility="public"
318
  )
319
 
320
  prompt.submit(
321
  fn=generate_image,
322
+ inputs=prompt,
323
  outputs=output_image,
324
  api_visibility="public"
325
  )