Spaces:
Running
Running
salomonsky
commited on
Commit
•
2fc432b
1
Parent(s):
de6051a
Update app.py
Browse files
app.py
CHANGED
@@ -8,156 +8,55 @@ import requests
|
|
8 |
import re
|
9 |
import asyncio
|
10 |
from PIL import Image
|
|
|
|
|
|
|
11 |
|
12 |
translator = Translator()
|
13 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
14 |
basemodel = "black-forest-labs/FLUX.1-schnell"
|
15 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
}
|
21 |
-
"""
|
22 |
-
|
23 |
-
JS = """function () {
|
24 |
-
gradioURL = window.location.href
|
25 |
-
if (!gradioURL.endsWith('?__theme=dark')) {
|
26 |
-
window.location.replace(gradioURL + '?__theme=dark');
|
27 |
-
}
|
28 |
-
}"""
|
29 |
-
|
30 |
-
def enable_lora(lora_add):
|
31 |
-
if not lora_add:
|
32 |
-
return basemodel
|
33 |
-
else:
|
34 |
-
return lora_add
|
35 |
-
|
36 |
-
async def generate_image(
|
37 |
-
prompt:str,
|
38 |
-
model:str,
|
39 |
-
lora_word:str,
|
40 |
-
width:int=768,
|
41 |
-
height:int=1024,
|
42 |
-
scales:float=3.5,
|
43 |
-
steps:int=24,
|
44 |
-
seed:int=-1):
|
45 |
-
|
46 |
-
if seed == -1:
|
47 |
-
seed = random.randint(0, MAX_SEED)
|
48 |
seed = int(seed)
|
49 |
-
print(f'prompt:{prompt}')
|
50 |
-
|
51 |
text = str(translator.translate(prompt, 'English')) + "," + lora_word
|
52 |
-
|
53 |
client = AsyncInferenceClient()
|
54 |
-
try:
|
55 |
-
|
56 |
-
prompt=text,
|
57 |
-
height=height,
|
58 |
-
width=width,
|
59 |
-
guidance_scale=scales,
|
60 |
-
num_inference_steps=steps,
|
61 |
-
model=model,
|
62 |
-
)
|
63 |
-
except Exception as e:
|
64 |
-
raise gr.Error(f"Error in {e}")
|
65 |
-
|
66 |
return image, seed
|
67 |
|
68 |
-
async def gen(
|
69 |
-
prompt:str,
|
70 |
-
lora_add:str="",
|
71 |
-
lora_word:str="",
|
72 |
-
width:int=768,
|
73 |
-
height:int=1024,
|
74 |
-
scales:float=3.5,
|
75 |
-
steps:int=24,
|
76 |
-
seed:int=-1,
|
77 |
-
progress=gr.Progress(track_tqdm=True)
|
78 |
-
):
|
79 |
model = enable_lora(lora_add)
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
gr.HTML("<h1><center>Flux Lab Light</center></h1>")
|
86 |
-
with gr.Row():
|
87 |
-
with gr.Column(scale=4):
|
88 |
-
with gr.Row():
|
89 |
-
img = gr.Image(type="filepath", label='flux Generated Image', height=600)
|
90 |
-
with gr.Row():
|
91 |
-
prompt = gr.Textbox(label='Enter Your Prompt (Multi-Languages)', placeholder="Enter prompt...", scale=6)
|
92 |
-
sendBtn = gr.Button(scale=1, variant='primary')
|
93 |
-
with gr.Accordion("Advanced Options", open=True):
|
94 |
-
with gr.Column(scale=1):
|
95 |
-
width = gr.Slider(
|
96 |
-
label="Width",
|
97 |
-
minimum=512,
|
98 |
-
maximum=1280,
|
99 |
-
step=8,
|
100 |
-
value=768,
|
101 |
-
)
|
102 |
-
height = gr.Slider(
|
103 |
-
label="Height",
|
104 |
-
minimum=512,
|
105 |
-
maximum=1280,
|
106 |
-
step=8,
|
107 |
-
value=1024,
|
108 |
-
)
|
109 |
-
scales = gr.Slider(
|
110 |
-
label="Guidance",
|
111 |
-
minimum=3.5,
|
112 |
-
maximum=7,
|
113 |
-
step=0.1,
|
114 |
-
value=3.5,
|
115 |
-
)
|
116 |
-
steps = gr.Slider(
|
117 |
-
label="Steps",
|
118 |
-
minimum=1,
|
119 |
-
maximum=100,
|
120 |
-
step=1,
|
121 |
-
value=24,
|
122 |
-
)
|
123 |
-
seed = gr.Slider(
|
124 |
-
label="Seeds",
|
125 |
-
minimum=-1,
|
126 |
-
maximum=MAX_SEED,
|
127 |
-
step=1,
|
128 |
-
value=-1,
|
129 |
-
)
|
130 |
-
lora_add = gr.Textbox(
|
131 |
-
label="Add Flux LoRA",
|
132 |
-
info="Copy the HF LoRA model name here",
|
133 |
-
lines=1,
|
134 |
-
placeholder="Please use Warm status model",
|
135 |
-
)
|
136 |
-
lora_word = gr.Textbox(
|
137 |
-
label="Add Flux LoRA Trigger Word",
|
138 |
-
info="Add the Trigger Word",
|
139 |
-
lines=1,
|
140 |
-
value="",
|
141 |
-
)
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
|
|
|
|
|
8 |
import re
|
9 |
import asyncio
|
10 |
from PIL import Image
|
11 |
+
from gradio_client import Client, handle_file
|
12 |
+
from huggingface_hub import login
|
13 |
+
from gradio_imageslider import ImageSlider
|
14 |
|
15 |
translator = Translator()
|
16 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
17 |
basemodel = "black-forest-labs/FLUX.1-schnell"
|
18 |
MAX_SEED = np.iinfo(np.int32).max
|
19 |
+
CSS = "footer { visibility: hidden; }"
|
20 |
+
JS = "function () { gradioURL = window.location.href; if (!gradioURL.endsWith('?__theme=dark')) { window.location.replace(gradioURL + '?__theme=dark'); } }"
|
21 |
|
22 |
+
def enable_lora(lora_add): return basemodel if not lora_add else lora_add
|
23 |
+
async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
|
24 |
+
if seed == -1: seed = random.randint(0, MAX_SEED)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
seed = int(seed)
|
|
|
|
|
26 |
text = str(translator.translate(prompt, 'English')) + "," + lora_word
|
|
|
27 |
client = AsyncInferenceClient()
|
28 |
+
try: image = await client.text_to_image(prompt=text, height=height, width=width, guidance_scale=scales, num_inference_steps=steps, model=model)
|
29 |
+
except Exception as e: raise gr.Error(f"Error in {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
return image, seed
|
31 |
|
32 |
+
async def gen(prompt, lora_add, lora_word, width, height, scales, steps, seed, upscale_factor, progress):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
model = enable_lora(lora_add)
|
34 |
+
image, seed = await generate_image(prompt, model, lora_word, width, height, scales, steps, seed)
|
35 |
+
image_path = "temp_image.png"
|
36 |
+
image.save(image_path)
|
37 |
+
upscale_image = get_upscale_finegrain(prompt, image_path, upscale_factor)
|
38 |
+
return upscale_image, seed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
def get_upscale_finegrain(prompt, img_path, upscale_factor):
|
41 |
+
client = Client("finegrain/finegrain-image-enhancer")
|
42 |
+
result = client.predict(input_image=handle_file(img_path), prompt=prompt, negative_prompt="", seed=42, upscale_factor=upscale_factor, controlnet_scale=0.6, controlnet_decay=1, condition_scale=6, tile_width=112, tile_height=144, denoise_strength=0.35, num_inference_steps=18, solver="DDIM", api_name="/process")
|
43 |
+
return result[1]
|
44 |
+
|
45 |
+
with gr.Blocks(css=CSS, js=JS, theme="Nymbo/Nymbo_Theme") as demo:
|
46 |
+
gr.HTML("<h1><center>Flux Lab Light</center></h1>");
|
47 |
+
with gr.Row():
|
48 |
+
with gr.Column(scale=4):
|
49 |
+
with gr.Row(): img = gr.Image(type="filepath", label='flux Generated Image', height=600);
|
50 |
+
with gr.Row(): prompt = gr.Textbox(label='Enter Your Prompt (Multi-Languages)', placeholder="Enter prompt...", scale=6); sendBtn = gr.Button(scale=1, variant='primary');
|
51 |
+
with gr.Accordion("Advanced Options", open=True):
|
52 |
+
with gr.Column(scale=1):
|
53 |
+
width = gr.Slider(label="Width", minimum=512, maximum=1280, step=8, value=768);
|
54 |
+
height = gr.Slider(label="Height", minimum=512, maximum=1280, step=8, value=1024);
|
55 |
+
scales = gr.Slider(label="Guidance", minimum=3.5, maximum=7, step=0.1, value=3.5);
|
56 |
+
steps = gr.Slider(label="Steps", minimum=1, maximum=100, step=1, value=24);
|
57 |
+
seed = gr.Slider(label="Seeds", minimum=-1, maximum=MAX_SEED, step=1, value=-1);
|
58 |
+
lora_add = gr.Textbox(label="Add Flux LoRA", info="Copy the HF LoRA model name here", lines=1, placeholder="Please use Warm status model");
|
59 |
+
lora_word = gr.Textbox(label="Add Flux LoRA Trigger Word", info="Add the Trigger Word", lines=1, value="");
|
60 |
+
upscale_factor = gr.Radio(label="UpScale Factor", choices=[2, 3, 4], value=2, scale=2)
|
61 |
+
gr.on([prompt.submit, sendBtn.click], gen, [prompt, lora_add, lora_word, width, height, scales, steps, seed, upscale_factor], [img, seed])
|
62 |
+
demo.queue(api_open=False).launch(show_api=False, share=False)
|