multimodalart HF Staff commited on
Commit
5d54e97
·
verified ·
1 Parent(s): 3d0f1d6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +154 -0
app.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ import spaces #[uncomment to use ZeroGPU]
5
+ from diffusers import ChromaPipeline
6
+ import torch
7
+
8
+ device = "cuda" if torch.cuda.is_available() else "cpu"
9
+ model_repo_id = "lodestones/Chroma1-HD"
10
+
11
+ if torch.cuda.is_available():
12
+ torch_dtype = torch.bfloat16
13
+ else:
14
+ torch_dtype = torch.float32
15
+
16
+ pipe = ChromaPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
17
+ pipe = pipe.to(device)
18
+
19
+ MAX_SEED = np.iinfo(np.int32).max
20
+ MAX_IMAGE_SIZE = 1024
21
+
22
+ @spaces.GPU(duration=75) #[uncomment to use ZeroGPU]
23
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, lora_model, progress=gr.Progress(track_tqdm=True)):
24
+
25
+ if randomize_seed:
26
+ seed = random.randint(0, MAX_SEED)
27
+
28
+ generator = torch.Generator(device).manual_seed(seed)
29
+
30
+ # Handle LoRA if needed (ChromaPipeline may not support LoRA by default)
31
+ # Uncomment and adapt if LoRA support is available
32
+ # if lora_model:
33
+ # pipe.load_lora_weights(lora_model)
34
+
35
+ image = pipe(
36
+ prompt=prompt,
37
+ negative_prompt=negative_prompt,
38
+ guidance_scale=guidance_scale,
39
+ num_inference_steps=num_inference_steps,
40
+ width=width,
41
+ height=height,
42
+ generator=generator,
43
+ num_images_per_prompt=1
44
+ ).images[0]
45
+
46
+ return image, seed
47
+
48
+ examples = [
49
+ "A high-fashion close-up portrait of a blonde woman in clear sunglasses. The image uses a bold teal and red color split for dramatic lighting. The background is a simple teal-green. The photo is sharp and well-composed, and is designed for viewing with anaglyph 3D glasses for optimal effect. It looks professionally done.",
50
+ "A surreal landscape with vibrant red and cyan color separation, featuring mountains and a lake, optimized for anaglyph 3D viewing",
51
+ "A futuristic cityscape at night with neon lights, rendered in stereoscopic 3D style with red-cyan color separation",
52
+ ]
53
+
54
+ css="""
55
+ #col-container {
56
+ margin: 0 auto;
57
+ max-width: 760px;
58
+ }
59
+ #button{
60
+ align-self: stretch;
61
+ }
62
+ """
63
+
64
+ with gr.Blocks(css=css) as demo:
65
+
66
+ with gr.Column(elem_id="col-container"):
67
+ gr.Markdown(f"""
68
+ # Chroma1-HD - Anaglyph 3D Image Generation
69
+ Generate stereoscopic 3D images optimized for red-cyan anaglyph glasses
70
+ """)
71
+
72
+ with gr.Row():
73
+ prompt = gr.Text(
74
+ label="Prompt",
75
+ max_lines=1,
76
+ placeholder="Enter your prompt",
77
+ )
78
+ negative_prompt = gr.Text(
79
+ label="Negative prompt",
80
+ max_lines=1,
81
+ placeholder="Enter a negative prompt",
82
+ value="low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors"
83
+ )
84
+
85
+ with gr.Row():
86
+ run_button = gr.Button("Run", scale=1, elem_id="button")
87
+
88
+ result = gr.Image(label="Result", show_label=False)
89
+
90
+ with gr.Accordion("Advanced Settings", open=False):
91
+
92
+ lora_model = gr.Textbox(
93
+ label="LoRA model id (if supported)",
94
+ placeholder="Leave empty if not using LoRA",
95
+ visible=False # Hidden by default as ChromaPipeline may not support LoRA
96
+ )
97
+
98
+ guidance_scale = gr.Slider(
99
+ label="Guidance Scale",
100
+ minimum=1.0,
101
+ maximum=10.0,
102
+ step=0.1,
103
+ value=3.0, # Default from your example
104
+ )
105
+
106
+ seed = gr.Slider(
107
+ label="Seed",
108
+ minimum=0,
109
+ maximum=MAX_SEED,
110
+ step=1,
111
+ value=433, # Using the seed from your example
112
+ )
113
+
114
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
115
+
116
+ with gr.Row():
117
+
118
+ width = gr.Slider(
119
+ label="Width",
120
+ minimum=256,
121
+ maximum=MAX_IMAGE_SIZE,
122
+ step=32,
123
+ value=1024, # Default resolution
124
+ )
125
+
126
+ height = gr.Slider(
127
+ label="Height",
128
+ minimum=256,
129
+ maximum=MAX_IMAGE_SIZE,
130
+ step=32,
131
+ value=1024, # Default resolution
132
+ )
133
+
134
+ num_inference_steps = gr.Slider(
135
+ label="Number of inference steps",
136
+ minimum=1,
137
+ maximum=100,
138
+ step=1,
139
+ value=40, # Default from your example
140
+ )
141
+
142
+ gr.Examples(
143
+ examples=examples,
144
+ inputs=[prompt]
145
+ )
146
+
147
+ gr.on(
148
+ triggers=[run_button.click, prompt.submit, negative_prompt.submit],
149
+ fn=infer,
150
+ inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, lora_model],
151
+ outputs=[result, seed]
152
+ )
153
+
154
+ demo.queue().launch()