shashwatIDR commited on
Commit
bb6042d
·
verified ·
1 Parent(s): b678ab9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -0
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ import spaces
5
+ import torch
6
+ from diffusers import DiffusionPipeline
7
+ from huggingface_hub import login
8
+ import os
9
+
10
+ # Login with HF token from secrets
11
+ hf_token = os.getenv("HF_TOKEN")
12
+ if hf_token is None:
13
+ raise ValueError("❌ Missing HF_TOKEN. Please set it in your Space settings (Repository secrets).")
14
+ login(token=hf_token)
15
+
16
+ dtype = torch.bfloat16
17
+ device = "cuda" if torch.cuda.is_available() else "cpu"
18
+
19
+ # Load model (requires gated access + token)
20
+ pipe = DiffusionPipeline.from_pretrained(
21
+ "black-forest-labs/FLUX.1-schnell",
22
+ torch_dtype=dtype,
23
+ use_auth_token=hf_token
24
+ ).to(device)
25
+
26
+ MAX_SEED = np.iinfo(np.int32).max
27
+ MAX_IMAGE_SIZE = 2048
28
+
29
+ @spaces.GPU()
30
+ def infer(
31
+ prompt,
32
+ seed=42,
33
+ randomize_seed=False,
34
+ width=1024,
35
+ height=1024,
36
+ num_inference_steps=4,
37
+ progress=gr.Progress(track_tqdm=True)
38
+ ):
39
+ if randomize_seed:
40
+ seed = random.randint(0, MAX_SEED)
41
+ generator = torch.Generator().manual_seed(seed)
42
+ image = pipe(
43
+ prompt=prompt,
44
+ width=width,
45
+ height=height,
46
+ num_inference_steps=num_inference_steps,
47
+ generator=generator,
48
+ guidance_scale=0.0
49
+ ).images[0]
50
+ return image, seed
51
+
52
+ examples = [
53
+ "a tiny astronaut hatching from an egg on the moon",
54
+ "a cat holding a sign that says hello world",
55
+ "an anime illustration of a wiener schnitzel",
56
+ ]
57
+
58
+ css = """
59
+ #col-container {
60
+ margin: 0 auto;
61
+ max-width: 520px;
62
+ }
63
+ """
64
+
65
+ with gr.Blocks(css=css) as demo:
66
+ with gr.Column(elem_id="col-container"):
67
+ gr.Markdown("""# FLUX.1 [schnell]
68
+ 12B param rectified flow transformer distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) for 4 step generation
69
+ [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-schnell)]
70
+ """)
71
+
72
+ with gr.Row():
73
+ prompt = gr.Text(
74
+ label="Prompt",
75
+ show_label=False,
76
+ max_lines=1,
77
+ placeholder="Enter your prompt",
78
+ container=False,
79
+ )
80
+ run_button = gr.Button("Run", scale=0)
81
+
82
+ result = gr.Image(label="Result", show_label=False)
83
+
84
+ with gr.Accordion("Advanced Settings", open=False):
85
+ seed = gr.Slider(
86
+ label="Seed",
87
+ minimum=0,
88
+ maximum=MAX_SEED,
89
+ step=1,
90
+ value=0,
91
+ )
92
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
93
+ with gr.Row():
94
+ width = gr.Slider(
95
+ label="Width",
96
+ minimum=256,
97
+ maximum=MAX_IMAGE_SIZE,
98
+ step=32,
99
+ value=1024,
100
+ )
101
+ height = gr.Slider(
102
+ label="Height",
103
+ minimum=256,
104
+ maximum=MAX_IMAGE_SIZE,
105
+ step=32,
106
+ value=1024,
107
+ )
108
+ with gr.Row():
109
+ num_inference_steps = gr.Slider(
110
+ label="Number of inference steps",
111
+ minimum=1,
112
+ maximum=50,
113
+ step=1,
114
+ value=4,
115
+ )
116
+
117
+ gr.Examples(
118
+ examples=examples,
119
+ fn=infer,
120
+ inputs=[prompt],
121
+ outputs=[result, seed],
122
+ cache_examples="lazy"
123
+ )
124
+
125
+ gr.on(
126
+ triggers=[run_button.click, prompt.submit],
127
+ fn=infer,
128
+ inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
129
+ outputs=[result, seed]
130
+ )
131
+
132
+ demo.launch()