multimodalart HF Staff commited on
Commit
5246183
·
verified ·
1 Parent(s): 290c73c

Upload /tmp/ideogram4_space/app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. tmp/ideogram4_space/app.py +98 -0
tmp/ideogram4_space/app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+
4
+ os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
5
+
6
+ # Use the bundled diffusers source (PR #2: huggingface/diffusers-new-model-addition-ideogram).
7
+ _HERE = os.path.dirname(os.path.abspath(__file__))
8
+ sys.path.insert(0, os.path.join(_HERE, "diffusers_src", "src"))
9
+
10
+ import random
11
+
12
+ import spaces
13
+ import torch
14
+ import gradio as gr
15
+ from diffusers import Ideogram4Pipeline
16
+
17
+ MODEL_ID = "diffusers-internal-dev/ideogram-4-nf4"
18
+
19
+ pipe = Ideogram4Pipeline.from_pretrained(MODEL_ID, torch_dtype=torch.bfloat16)
20
+ pipe.to("cuda")
21
+
22
+ MAX_SEED = 2**31 - 1
23
+
24
+
25
+ @spaces.GPU(duration=180)
26
+ def generate(
27
+ prompt: str,
28
+ width: int,
29
+ height: int,
30
+ num_inference_steps: int,
31
+ guidance_scale: float,
32
+ seed: int,
33
+ randomize_seed: bool,
34
+ progress=gr.Progress(track_tqdm=True),
35
+ ):
36
+ if randomize_seed or seed < 0:
37
+ seed = random.randint(0, MAX_SEED)
38
+ generator = torch.Generator(device="cuda").manual_seed(int(seed))
39
+
40
+ steps = int(num_inference_steps)
41
+ kwargs = dict(
42
+ prompt=prompt,
43
+ width=int(width),
44
+ height=int(height),
45
+ num_inference_steps=steps,
46
+ generator=generator,
47
+ )
48
+ if guidance_scale > 0:
49
+ kwargs["guidance_scale"] = float(guidance_scale)
50
+ kwargs["guidance_schedule"] = None
51
+ else:
52
+ # PR default is len 48 (7.0 x45 + 3.0 x3); rebuild it for any step count.
53
+ tail = min(3, max(0, steps - 1))
54
+ kwargs["guidance_schedule"] = (7.0,) * (steps - tail) + (3.0,) * tail
55
+
56
+ image = pipe(**kwargs).images[0]
57
+ return image, seed
58
+
59
+
60
+ with gr.Blocks(title="Ideogram 4 (NF4) — diffusers preview") as demo:
61
+ gr.Markdown(
62
+ "## Ideogram 4 (NF4) — diffusers preview\n"
63
+ f"Private demo of [`{MODEL_ID}`](https://huggingface.co/{MODEL_ID}) using the "
64
+ "[diffusers PR #2](https://github.com/huggingface/diffusers-new-model-addition-ideogram/pull/2) "
65
+ "branch, running on ZeroGPU."
66
+ )
67
+
68
+ with gr.Row():
69
+ with gr.Column():
70
+ prompt = gr.Textbox(
71
+ label="Prompt",
72
+ value="A photo of a cat holding a sign that says hello world",
73
+ lines=3,
74
+ )
75
+ run = gr.Button("Generate", variant="primary")
76
+ with gr.Accordion("Advanced", open=False):
77
+ with gr.Row():
78
+ width = gr.Slider(512, 2048, value=1024, step=64, label="Width")
79
+ height = gr.Slider(512, 2048, value=1024, step=64, label="Height")
80
+ steps = gr.Slider(8, 64, value=48, step=1, label="Inference steps")
81
+ guidance = gr.Slider(
82
+ 0.0, 15.0, value=0.0, step=0.1,
83
+ label="Guidance scale (0 = recommended schedule: 7.0 → 3.0)",
84
+ )
85
+ with gr.Row():
86
+ seed = gr.Number(label="Seed", value=0, precision=0)
87
+ randomize = gr.Checkbox(label="Randomize seed", value=True)
88
+ with gr.Column():
89
+ out_image = gr.Image(label="Output", type="pil")
90
+ out_seed = gr.Number(label="Seed used", interactive=False, precision=0)
91
+
92
+ run.click(
93
+ generate,
94
+ inputs=[prompt, width, height, steps, guidance, seed, randomize],
95
+ outputs=[out_image, out_seed],
96
+ )
97
+
98
+ demo.queue().launch()