Spaces:
Running
on
Zero
Running
on
Zero
TheAwakenOne
commited on
Commit
•
efc213f
1
Parent(s):
3840fa6
Updated requirements, README, and app code
Browse files- README.md +15 -7
- app.py +57 -149
- requirements.txt +5 -4
README.md
CHANGED
@@ -1,14 +1,22 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
-
license: other
|
11 |
-
short_description: 8B parameter transformer model distilled by Freepik
|
12 |
---
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Flux Image Generator (Zero-GPU)
|
3 |
+
emoji: 🎨
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: blue
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.7.1
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
|
|
10 |
---
|
11 |
|
12 |
+
# Flux Image Generator with Zero-GPU
|
13 |
+
|
14 |
+
This Space runs the Flux.1-lite-8B-alpha model from Freepik using Zero-GPU allocation to generate images from text descriptions. The interface allows you to adjust various parameters:
|
15 |
+
|
16 |
+
- Guidance Scale: Controls how closely the image follows the prompt
|
17 |
+
- Number of Steps: Determines the quality of the generation
|
18 |
+
- Seed: Controls reproducibility
|
19 |
+
- Width/Height: Image dimensions
|
20 |
+
|
21 |
+
Note: This Space uses the @spaces.GPU decorator to allocate GPU resources only when needed, making it more efficient and cost-effective.
|
22 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
@@ -1,154 +1,62 @@
|
|
1 |
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
-
import random
|
4 |
-
|
5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
6 |
-
from diffusers import DiffusionPipeline
|
7 |
import torch
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
# @spaces.GPU #[uncomment to use ZeroGPU]
|
25 |
-
def infer(
|
26 |
prompt,
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
width,
|
31 |
-
height
|
32 |
-
guidance_scale,
|
33 |
-
num_inference_steps,
|
34 |
-
progress=gr.Progress(track_tqdm=True),
|
35 |
):
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
with
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
prompt = gr.Text(
|
73 |
-
label="Prompt",
|
74 |
-
show_label=False,
|
75 |
-
max_lines=1,
|
76 |
-
placeholder="Enter your prompt",
|
77 |
-
container=False,
|
78 |
-
)
|
79 |
-
|
80 |
-
run_button = gr.Button("Run", scale=0, variant="primary")
|
81 |
-
|
82 |
-
result = gr.Image(label="Result", show_label=False)
|
83 |
-
|
84 |
-
with gr.Accordion("Advanced Settings", open=False):
|
85 |
-
negative_prompt = gr.Text(
|
86 |
-
label="Negative prompt",
|
87 |
-
max_lines=1,
|
88 |
-
placeholder="Enter a negative prompt",
|
89 |
-
visible=False,
|
90 |
-
)
|
91 |
-
|
92 |
-
seed = gr.Slider(
|
93 |
-
label="Seed",
|
94 |
-
minimum=0,
|
95 |
-
maximum=MAX_SEED,
|
96 |
-
step=1,
|
97 |
-
value=0,
|
98 |
-
)
|
99 |
-
|
100 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
101 |
-
|
102 |
-
with gr.Row():
|
103 |
-
width = gr.Slider(
|
104 |
-
label="Width",
|
105 |
-
minimum=256,
|
106 |
-
maximum=MAX_IMAGE_SIZE,
|
107 |
-
step=32,
|
108 |
-
value=1024, # Replace with defaults that work for your model
|
109 |
-
)
|
110 |
-
|
111 |
-
height = gr.Slider(
|
112 |
-
label="Height",
|
113 |
-
minimum=256,
|
114 |
-
maximum=MAX_IMAGE_SIZE,
|
115 |
-
step=32,
|
116 |
-
value=1024, # Replace with defaults that work for your model
|
117 |
-
)
|
118 |
-
|
119 |
-
with gr.Row():
|
120 |
-
guidance_scale = gr.Slider(
|
121 |
-
label="Guidance scale",
|
122 |
-
minimum=0.0,
|
123 |
-
maximum=10.0,
|
124 |
-
step=0.1,
|
125 |
-
value=0.0, # Replace with defaults that work for your model
|
126 |
-
)
|
127 |
-
|
128 |
-
num_inference_steps = gr.Slider(
|
129 |
-
label="Number of inference steps",
|
130 |
-
minimum=1,
|
131 |
-
maximum=50,
|
132 |
-
step=1,
|
133 |
-
value=2, # Replace with defaults that work for your model
|
134 |
-
)
|
135 |
-
|
136 |
-
gr.Examples(examples=examples, inputs=[prompt])
|
137 |
-
gr.on(
|
138 |
-
triggers=[run_button.click, prompt.submit],
|
139 |
-
fn=infer,
|
140 |
-
inputs=[
|
141 |
-
prompt,
|
142 |
-
negative_prompt,
|
143 |
-
seed,
|
144 |
-
randomize_seed,
|
145 |
-
width,
|
146 |
-
height,
|
147 |
-
guidance_scale,
|
148 |
-
num_inference_steps,
|
149 |
-
],
|
150 |
-
outputs=[result, seed],
|
151 |
-
)
|
152 |
-
|
153 |
if __name__ == "__main__":
|
154 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
2 |
import torch
|
3 |
+
from diffusers import FluxPipeline
|
4 |
+
from huggingface_hub import HfApi
|
5 |
+
import spaces
|
6 |
+
|
7 |
+
@spaces.GPU(duration=70) # Allocate GPU for 70 seconds
|
8 |
+
def initialize_model():
|
9 |
+
model_id = "Freepik/flux.1-lite-8B-alpha"
|
10 |
+
pipe = FluxPipeline.from_pretrained(
|
11 |
+
model_id,
|
12 |
+
torch_dtype=torch.bfloat16
|
13 |
+
).to("cuda")
|
14 |
+
return pipe
|
15 |
+
|
16 |
+
@spaces.GPU(duration=70)
|
17 |
+
def generate_image(
|
|
|
|
|
|
|
18 |
prompt,
|
19 |
+
guidance_scale=3.5,
|
20 |
+
num_steps=28,
|
21 |
+
seed=11,
|
22 |
+
width=1024,
|
23 |
+
height=1024
|
|
|
|
|
|
|
24 |
):
|
25 |
+
# Initialize model within the GPU context
|
26 |
+
pipe = initialize_model()
|
27 |
+
|
28 |
+
with torch.inference_mode():
|
29 |
+
image = pipe(
|
30 |
+
prompt=prompt,
|
31 |
+
generator=torch.Generator(device="cuda").manual_seed(seed),
|
32 |
+
num_inference_steps=num_steps,
|
33 |
+
guidance_scale=guidance_scale,
|
34 |
+
height=height,
|
35 |
+
width=width,
|
36 |
+
).images[0]
|
37 |
+
|
38 |
+
return image
|
39 |
+
|
40 |
+
# Create the Gradio interface
|
41 |
+
demo = gr.Interface(
|
42 |
+
fn=generate_image,
|
43 |
+
inputs=[
|
44 |
+
gr.Textbox(label="Prompt", placeholder="Enter your image description here..."),
|
45 |
+
gr.Slider(minimum=1, maximum=20, value=3.5, label="Guidance Scale", step=0.5),
|
46 |
+
gr.Slider(minimum=1, maximum=50, value=28, label="Number of Steps", step=1),
|
47 |
+
gr.Slider(minimum=1, maximum=1000000, value=11, label="Seed", step=1),
|
48 |
+
gr.Slider(minimum=128, maximum=1024, value=1024, label="Width", step=64),
|
49 |
+
gr.Slider(minimum=128, maximum=1024, value=1024, label="Height", step=64)
|
50 |
+
],
|
51 |
+
outputs=gr.Image(type="pil", label="Generated Image"),
|
52 |
+
title="Flux Image Generator (Zero-GPU)",
|
53 |
+
description="Generate images using Freepik's Flux model with Zero-GPU allocation",
|
54 |
+
examples=[
|
55 |
+
["A close-up image of a green alien with fluorescent skin in the middle of a dark purple forest", 3.5, 28, 11, 1024, 1024],
|
56 |
+
["A serene landscape with mountains at sunset", 3.5, 28, 42, 1024, 1024],
|
57 |
+
]
|
58 |
+
)
|
59 |
+
|
60 |
+
# Launch the app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
if __name__ == "__main__":
|
62 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
-
accelerate
|
2 |
-
diffusers
|
3 |
-
invisible_watermark
|
4 |
torch
|
|
|
5 |
transformers
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
torch
|
2 |
+
diffusers
|
3 |
transformers
|
4 |
+
gradio
|
5 |
+
pillow
|
6 |
+
huggingface-hub
|
7 |
+
spaces
|