Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
|
2 |
import spaces
|
3 |
import gradio as gr
|
4 |
import torch
|
@@ -8,9 +8,13 @@ import random
|
|
8 |
import uuid
|
9 |
from typing import Tuple
|
10 |
import numpy as np
|
|
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
14 |
|
15 |
def save_image(img):
|
16 |
unique_name = str(uuid.uuid4()) + ".png"
|
@@ -23,21 +27,21 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
|
23 |
return seed
|
24 |
|
25 |
MAX_SEED = np.iinfo(np.int32).max
|
26 |
-
# Define DESCRIPTIONz before using it
|
27 |
-
DESCRIPTIONz = "" # Initialize as an empty string
|
28 |
|
|
|
|
|
29 |
if not torch.cuda.is_available():
|
30 |
-
DESCRIPTIONz += "\n<p>⚠️Running on CPU
|
31 |
|
|
|
32 |
base_model = "black-forest-labs/FLUX.1-dev"
|
33 |
-
pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
|
34 |
-
|
35 |
lora_repo = "strangerzonehf/Flux-Super-Realism-LoRA"
|
36 |
trigger_word = "Super Realism" # Leave trigger_word blank if not used.
|
37 |
-
|
38 |
pipe.load_lora_weights(lora_repo)
|
39 |
pipe.to("cuda")
|
40 |
|
|
|
41 |
style_list = [
|
42 |
{
|
43 |
"name": "3840 x 2160",
|
@@ -56,9 +60,7 @@ style_list = [
|
|
56 |
"prompt": "{prompt}",
|
57 |
},
|
58 |
]
|
59 |
-
|
60 |
styles = {k["name"]: k["prompt"] for k in style_list}
|
61 |
-
|
62 |
DEFAULT_STYLE_NAME = "3840 x 2160"
|
63 |
STYLE_NAMES = list(styles.keys())
|
64 |
|
@@ -77,12 +79,11 @@ def generate(
|
|
77 |
progress=gr.Progress(track_tqdm=True),
|
78 |
):
|
79 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
80 |
-
|
81 |
positive_prompt = apply_style(style_name, prompt)
|
82 |
|
83 |
if trigger_word:
|
84 |
positive_prompt = f"{trigger_word} {positive_prompt}"
|
85 |
-
|
86 |
images = pipe(
|
87 |
prompt=positive_prompt,
|
88 |
width=width,
|
@@ -100,25 +101,46 @@ examples = [
|
|
100 |
"Woman in a red jacket, snowy, in the style of hyper-realistic portraiture, caninecore, mountainous vistas, timeless beauty, palewave, iconic, distinctive noses --ar 72:101 --stylize 750 --v 6",
|
101 |
"Super Realism, Headshot of handsome young man, wearing dark gray sweater with buttons and big shawl collar, brown hair and short beard, serious look on his face, black background, soft studio lighting, portrait photography --ar 85:128 --v 6.0 --style",
|
102 |
"Super Realism, High-resolution photograph, woman, UHD, photorealistic, shot on a Sony A7III --chaos 20 --ar 1:2 --style raw --stylize 250",
|
103 |
-
"Super-realism, Purple Dreamy, a medium-angle shot of a young woman with long brown hair, wearing a pair of eye-level glasses, stands in front of a backdrop of purple and white lights. The
|
104 |
]
|
105 |
|
|
|
106 |
css = '''
|
107 |
-
.gradio-container{
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
footer {
|
110 |
-
visibility: hidden
|
111 |
}
|
112 |
.submit-btn {
|
113 |
-
background-color: #
|
114 |
color: white !important;
|
|
|
|
|
|
|
115 |
}
|
116 |
.submit-btn:hover {
|
117 |
-
background-color: #
|
|
|
|
|
|
|
|
|
118 |
}
|
119 |
'''
|
120 |
|
121 |
-
with gr.Blocks(css=css, theme="
|
|
|
|
|
|
|
122 |
with gr.Row():
|
123 |
with gr.Column(scale=1):
|
124 |
prompt = gr.Text(
|
@@ -128,9 +150,9 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
128 |
placeholder="Enter your prompt",
|
129 |
container=False,
|
130 |
)
|
131 |
-
run_button = gr.Button("Generate
|
132 |
|
133 |
-
with gr.Accordion("Advanced options", open=True
|
134 |
seed = gr.Slider(
|
135 |
label="Seed",
|
136 |
minimum=0,
|
@@ -141,7 +163,7 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
141 |
)
|
142 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
143 |
|
144 |
-
with gr.Row(
|
145 |
width = gr.Slider(
|
146 |
label="Width",
|
147 |
minimum=512,
|
@@ -181,10 +203,8 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
181 |
value=DEFAULT_STYLE_NAME,
|
182 |
label="Quality Style",
|
183 |
)
|
184 |
-
|
185 |
with gr.Column(scale=2):
|
186 |
result = gr.Gallery(label="Result", columns=1, show_label=False)
|
187 |
-
|
188 |
gr.Examples(
|
189 |
examples=examples,
|
190 |
inputs=prompt,
|
@@ -213,4 +233,4 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
213 |
)
|
214 |
|
215 |
if __name__ == "__main__":
|
216 |
-
demo.queue(max_size=40).launch()
|
|
|
1 |
+
import os
|
2 |
import spaces
|
3 |
import gradio as gr
|
4 |
import torch
|
|
|
8 |
import uuid
|
9 |
from typing import Tuple
|
10 |
import numpy as np
|
11 |
+
from huggingface_hub import login
|
12 |
|
13 |
+
# Authenticate with Hugging Face once at startup
|
14 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
15 |
+
if HF_TOKEN:
|
16 |
+
login(HF_TOKEN)
|
17 |
+
print("Authenticated with Hugging Face.")
|
18 |
|
19 |
def save_image(img):
|
20 |
unique_name = str(uuid.uuid4()) + ".png"
|
|
|
27 |
return seed
|
28 |
|
29 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
|
30 |
|
31 |
+
# Description for the UI
|
32 |
+
DESCRIPTIONz = ""
|
33 |
if not torch.cuda.is_available():
|
34 |
+
DESCRIPTIONz += "\n<p>⚠️ Running on CPU. This may not work as expected.</p>"
|
35 |
|
36 |
+
# Load base model and LoRA weights using the HF token
|
37 |
base_model = "black-forest-labs/FLUX.1-dev"
|
38 |
+
pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16, use_auth_token=HF_TOKEN)
|
|
|
39 |
lora_repo = "strangerzonehf/Flux-Super-Realism-LoRA"
|
40 |
trigger_word = "Super Realism" # Leave trigger_word blank if not used.
|
|
|
41 |
pipe.load_lora_weights(lora_repo)
|
42 |
pipe.to("cuda")
|
43 |
|
44 |
+
# Define style list for prompt enhancements
|
45 |
style_list = [
|
46 |
{
|
47 |
"name": "3840 x 2160",
|
|
|
60 |
"prompt": "{prompt}",
|
61 |
},
|
62 |
]
|
|
|
63 |
styles = {k["name"]: k["prompt"] for k in style_list}
|
|
|
64 |
DEFAULT_STYLE_NAME = "3840 x 2160"
|
65 |
STYLE_NAMES = list(styles.keys())
|
66 |
|
|
|
79 |
progress=gr.Progress(track_tqdm=True),
|
80 |
):
|
81 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
|
|
82 |
positive_prompt = apply_style(style_name, prompt)
|
83 |
|
84 |
if trigger_word:
|
85 |
positive_prompt = f"{trigger_word} {positive_prompt}"
|
86 |
+
|
87 |
images = pipe(
|
88 |
prompt=positive_prompt,
|
89 |
width=width,
|
|
|
101 |
"Woman in a red jacket, snowy, in the style of hyper-realistic portraiture, caninecore, mountainous vistas, timeless beauty, palewave, iconic, distinctive noses --ar 72:101 --stylize 750 --v 6",
|
102 |
"Super Realism, Headshot of handsome young man, wearing dark gray sweater with buttons and big shawl collar, brown hair and short beard, serious look on his face, black background, soft studio lighting, portrait photography --ar 85:128 --v 6.0 --style",
|
103 |
"Super Realism, High-resolution photograph, woman, UHD, photorealistic, shot on a Sony A7III --chaos 20 --ar 1:2 --style raw --stylize 250",
|
104 |
+
"Super-realism, Purple Dreamy, a medium-angle shot of a young woman with long brown hair, wearing a pair of eye-level glasses, stands in front of a backdrop of purple and white lights. The woman's eyes are closed, her lips slightly parted, as if she is looking up at the sky. Her hair cascades over her shoulders, framing her face. She wears a sleeveless top adorned with tiny white dots and a gold chain necklace around her neck. Her left earrings add a pop of color to the scene."
|
105 |
]
|
106 |
|
107 |
+
# Updated CSS for a violet-themed, improved UI
|
108 |
css = '''
|
109 |
+
.gradio-container {
|
110 |
+
max-width: 888px !important;
|
111 |
+
background-color: #f3f0ff;
|
112 |
+
padding: 20px;
|
113 |
+
border-radius: 12px;
|
114 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
|
115 |
+
}
|
116 |
+
h1 {
|
117 |
+
text-align: center;
|
118 |
+
color: #4b0082;
|
119 |
+
margin-bottom: 10px;
|
120 |
+
}
|
121 |
footer {
|
122 |
+
visibility: hidden;
|
123 |
}
|
124 |
.submit-btn {
|
125 |
+
background-color: #8A2BE2 !important;
|
126 |
color: white !important;
|
127 |
+
border-radius: 8px;
|
128 |
+
padding: 10px 20px;
|
129 |
+
font-weight: bold;
|
130 |
}
|
131 |
.submit-btn:hover {
|
132 |
+
background-color: #6a0dad !important;
|
133 |
+
}
|
134 |
+
.accordion-header {
|
135 |
+
background-color: #e6e0ff;
|
136 |
+
color: #4b0082;
|
137 |
}
|
138 |
'''
|
139 |
|
140 |
+
with gr.Blocks(css=css, theme="default") as demo:
|
141 |
+
gr.Markdown("<h1>FLUX Image Generator</h1>")
|
142 |
+
gr.Markdown(DESCRIPTIONz)
|
143 |
+
|
144 |
with gr.Row():
|
145 |
with gr.Column(scale=1):
|
146 |
prompt = gr.Text(
|
|
|
150 |
placeholder="Enter your prompt",
|
151 |
container=False,
|
152 |
)
|
153 |
+
run_button = gr.Button("Generate 🤗", elem_classes="submit-btn")
|
154 |
|
155 |
+
with gr.Accordion("Advanced options", open=True):
|
156 |
seed = gr.Slider(
|
157 |
label="Seed",
|
158 |
minimum=0,
|
|
|
163 |
)
|
164 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
165 |
|
166 |
+
with gr.Row():
|
167 |
width = gr.Slider(
|
168 |
label="Width",
|
169 |
minimum=512,
|
|
|
203 |
value=DEFAULT_STYLE_NAME,
|
204 |
label="Quality Style",
|
205 |
)
|
|
|
206 |
with gr.Column(scale=2):
|
207 |
result = gr.Gallery(label="Result", columns=1, show_label=False)
|
|
|
208 |
gr.Examples(
|
209 |
examples=examples,
|
210 |
inputs=prompt,
|
|
|
233 |
)
|
234 |
|
235 |
if __name__ == "__main__":
|
236 |
+
demo.queue(max_size=40).launch()
|