RageshAntony commited on
Commit
9461fdc
·
verified ·
1 Parent(s): 7054a36

dymaic gen

Browse files
Files changed (1) hide show
  1. check_app.py +88 -49
check_app.py CHANGED
@@ -1,37 +1,88 @@
1
- import spaces
2
  import torch
3
- from diffusers import FluxPipeline, StableDiffusion3Pipeline
4
- from PIL import Image
5
- from io import BytesIO
 
 
 
 
 
 
 
 
6
  import gradio as gr
7
 
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
-
11
- # Function to generate images with progress
12
  def generate_image_with_progress(pipe, prompt, num_steps, guidance_scale=None, seed=None, progress=gr.Progress()):
13
  generator = None
14
  if seed is not None:
15
  generator = torch.Generator("cuda").manual_seed(seed)
16
- print("Start generating")
17
- # Wrapper to track progress
18
- def callback(pipe, step_index, timestep, callback_kwargs): # pipe, step_index, timestep, callback_kwargs
19
- print(f" callback => {pipe}, {step_index}, {timestep}, {callback_kwargs} ")
20
- if step_index == None:
21
  step_index = 0
22
  cur_prg = step_index / num_steps
23
- print(f"Progressing {cur_prg} Step {step_index}/{num_steps}")
24
  progress(cur_prg, desc=f"Step {step_index}/{num_steps}")
25
  return callback_kwargs
26
 
27
- if isinstance(pipe, StableDiffusion3Pipeline):
28
  image = pipe(
29
  prompt,
30
  num_inference_steps=num_steps,
31
  guidance_scale=guidance_scale,
32
  callback_on_step_end=callback,
33
  ).images[0]
34
- elif isinstance(pipe, FluxPipeline):
35
  image = pipe(
36
  prompt,
37
  num_inference_steps=num_steps,
@@ -39,58 +90,46 @@ def generate_image_with_progress(pipe, prompt, num_steps, guidance_scale=None, s
39
  output_type="pil",
40
  callback_on_step_end=callback,
41
  ).images[0]
 
42
  return image
43
 
44
- # Gradio application
45
- def main():
46
-
47
- @spaces.GPU(duration=170)
48
- def tab1_logic(prompt_text):
49
  progress = gr.Progress()
50
  num_steps = 30
 
51
  seed = 42
52
- print(f"Start tab {prompt_text}")
53
- flux_pipe = FluxPipeline.from_pretrained(
54
- "black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16
 
55
  ).to("cuda")
56
  image = generate_image_with_progress(
57
- flux_pipe, prompt_text, num_steps=num_steps, seed=seed, progress=progress
58
  )
59
  return f"Seed: {seed}", image
60
-
61
- @spaces.GPU(duration=170)
62
- def tab2_logic(prompt_text):
63
- progress = gr.Progress()
64
- num_steps = 28
65
- guidance_scale = 3.5
66
- print(f"Start tab {prompt_text}")
67
- # Initialize pipelines
68
- stable_diffusion_pipe = StableDiffusion3Pipeline.from_pretrained(
69
- "stabilityai/stable-diffusion-3.5-large", torch_dtype=torch.bfloat16
70
- ).to("cuda")
71
- image = generate_image_with_progress(
72
- stable_diffusion_pipe, prompt_text, num_steps=num_steps, guidance_scale=guidance_scale, progress=progress
73
- )
74
- return "Seed: None", image
75
 
 
 
 
 
76
  with gr.Blocks() as app:
77
- gr.Markdown("# Multiple Model Image Generation with Progress Bar")
78
 
79
  prompt_text = gr.Textbox(label="Enter prompt")
80
 
81
- with gr.Tab("FLUX"):
82
- button_1 = gr.Button("Run FLUX")
83
- output_1 = gr.Textbox(label="Status")
84
- img_1 = gr.Image(label="FLUX", height=300)
85
- button_1.click(fn=tab1_logic, inputs=[prompt_text], outputs=[output_1, img_1])
86
 
87
- with gr.Tab("StableDiffusion3"):
88
- button_2 = gr.Button("Run StableDiffusion3")
89
- output_2 = gr.Textbox(label="Status")
90
- img_2 = gr.Image(label="StableDiffusion3", height=300)
91
- button_2.click(fn=tab2_logic, inputs=[prompt_text], outputs=[output_2, img_2])
92
 
93
  app.launch()
94
 
 
95
  if __name__ == "__main__":
96
  main()
 
 
1
  import torch
2
+ from diffusers import (
3
+ FluxPipeline,
4
+ StableDiffusion3Pipeline,
5
+ PixArtSigmaPipeline,
6
+ SanaPipeline,
7
+ AuraFlowPipeline,
8
+ Kandinsky3Pipeline,
9
+ HunyuanDiTPipeline,
10
+ LuminaText2ImgPipeline,
11
+ OneDiffusionPipeline,
12
+ )
13
  import gradio as gr
14
 
15
+ cache_dir = '/workspace/hf_cache'
16
 
17
+ MODEL_CONFIGS = {
18
+ "FLUX": {
19
+ "repo_id": "black-forest-labs/FLUX.1-dev",
20
+ "pipeline_class": FluxPipeline,
21
+ "cache_dir": cache_dir,
22
+ },
23
+ "Stable Diffusion 3.5": {
24
+ "repo_id": "stabilityai/stable-diffusion-3.5-large",
25
+ "pipeline_class": StableDiffusion3Pipeline,
26
+ "cache_dir": cache_dir,
27
+ },
28
+ "PixArt": {
29
+ "repo_id": "PixArt-alpha/PixArt-Sigma-XL-2-1024-MS",
30
+ "pipeline_class": PixArtSigmaPipeline,
31
+ "cache_dir": cache_dir,
32
+ },
33
+ "SANA": {
34
+ "repo_id": "Efficient-Large-Model/Sana_1600M_1024px_BF16_diffusers",
35
+ "pipeline_class": SanaPipeline,
36
+ "cache_dir": cache_dir,
37
+ },
38
+ "AuraFlow": {
39
+ "repo_id": "fal/AuraFlow",
40
+ "pipeline_class": AuraFlowPipeline,
41
+ "cache_dir": cache_dir,
42
+ },
43
+ "Kandinsky": {
44
+ "repo_id": "kandinsky-community/kandinsky-3",
45
+ "pipeline_class": Kandinsky3Pipeline,
46
+ "cache_dir": cache_dir,
47
+ },
48
+ "Hunyuan": {
49
+ "repo_id": "Tencent-Hunyuan/HunyuanDiT-Diffusers",
50
+ "pipeline_class": HunyuanDiTPipeline,
51
+ "cache_dir": cache_dir,
52
+ },
53
+ "Lumina": {
54
+ "repo_id": "Alpha-VLLM/Lumina-Next-SFT-diffusers",
55
+ "pipeline_class": LuminaText2ImgPipeline,
56
+ "cache_dir": cache_dir,
57
+ },
58
+ "OneDiffusion": {
59
+ "repo_id": "lehduong/OneDiffusion",
60
+ "pipeline_class": OneDiffusionPipeline,
61
+ "cache_dir": cache_dir,
62
+ },
63
+ }
64
 
 
 
65
  def generate_image_with_progress(pipe, prompt, num_steps, guidance_scale=None, seed=None, progress=gr.Progress()):
66
  generator = None
67
  if seed is not None:
68
  generator = torch.Generator("cuda").manual_seed(seed)
69
+
70
+ def callback(pipe, step_index, timestep, callback_kwargs):
71
+ print(f" callback => {pipe}, {step_index}, {timestep}")
72
+ if step_index is None:
 
73
  step_index = 0
74
  cur_prg = step_index / num_steps
 
75
  progress(cur_prg, desc=f"Step {step_index}/{num_steps}")
76
  return callback_kwargs
77
 
78
+ if hasattr(pipe, "guidance_scale"):
79
  image = pipe(
80
  prompt,
81
  num_inference_steps=num_steps,
82
  guidance_scale=guidance_scale,
83
  callback_on_step_end=callback,
84
  ).images[0]
85
+ else:
86
  image = pipe(
87
  prompt,
88
  num_inference_steps=num_steps,
 
90
  output_type="pil",
91
  callback_on_step_end=callback,
92
  ).images[0]
93
+
94
  return image
95
 
96
+
97
+ def create_pipeline_logic(model_name, config):
98
+ def logic(prompt_text):
 
 
99
  progress = gr.Progress()
100
  num_steps = 30
101
+ guidance_scale = 7.5 # Example guidance scale, can be adjusted per model
102
  seed = 42
103
+
104
+ pipe_class = config["pipeline_class"]
105
+ pipe = pipe_class.from_pretrained(
106
+ config["repo_id"], cache_dir=config["cache_dir"], torch_dtype=torch.bfloat16
107
  ).to("cuda")
108
  image = generate_image_with_progress(
109
+ pipe, prompt_text, num_steps=num_steps, guidance_scale=guidance_scale, seed=seed, progress=progress
110
  )
111
  return f"Seed: {seed}", image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
+ return logic
114
+
115
+
116
+ def main():
117
  with gr.Blocks() as app:
118
+ gr.Markdown("# Dynamic Multiple Model Image Generation")
119
 
120
  prompt_text = gr.Textbox(label="Enter prompt")
121
 
122
+ for model_name, config in MODEL_CONFIGS.items():
123
+ with gr.Tab(model_name):
124
+ button = gr.Button(f"Run {model_name}")
125
+ output = gr.Textbox(label="Status")
126
+ img = gr.Image(label=model_name, height=300)
127
 
128
+ logic = create_pipeline_logic(model_name, config)
129
+ button.click(fn=logic, inputs=[prompt_text], outputs=[output, img])
 
 
 
130
 
131
  app.launch()
132
 
133
+
134
  if __name__ == "__main__":
135
  main()