Update app.py
Browse files
app.py
CHANGED
|
@@ -2,24 +2,30 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
import random
|
| 5 |
-
from huggingface_hub import AsyncInferenceClient
|
| 6 |
from translatepy import Translator
|
| 7 |
import requests
|
| 8 |
import re
|
| 9 |
import asyncio
|
| 10 |
from PIL import Image
|
| 11 |
-
from gradio_client import Client, handle_file
|
| 12 |
-
|
| 13 |
|
| 14 |
translator = Translator()
|
| 15 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
| 16 |
basemodel = "black-forest-labs/FLUX.1-schnell"
|
| 17 |
MAX_SEED = np.iinfo(np.int32).max
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def enable_lora(lora_add):
|
| 25 |
if not lora_add:
|
|
@@ -27,93 +33,131 @@ def enable_lora(lora_add):
|
|
| 27 |
else:
|
| 28 |
return lora_add
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
def get_upscale_finegrain(prompt, img_path, upscale_factor):
|
| 32 |
-
client = Client("finegrain/finegrain-image-enhancer")
|
| 33 |
-
result = client.predict(
|
| 34 |
-
input_image=handle_file(img_path),
|
| 35 |
-
prompt=prompt,
|
| 36 |
-
negative_prompt="",
|
| 37 |
-
seed=42,
|
| 38 |
-
upscale_factor=upscale_factor,
|
| 39 |
-
controlnet_scale=0.6,
|
| 40 |
-
controlnet_decay=1,
|
| 41 |
-
condition_scale=6,
|
| 42 |
-
tile_width=112,
|
| 43 |
-
tile_height=144,
|
| 44 |
-
denoise_strength=0.35,
|
| 45 |
-
num_inference_steps=18,
|
| 46 |
-
solver="DDIM",
|
| 47 |
-
api_name="/process"
|
| 48 |
-
)
|
| 49 |
-
return result[1]
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
|
| 53 |
if seed == -1:
|
| 54 |
seed = random.randint(0, MAX_SEED)
|
| 55 |
seed = int(seed)
|
|
|
|
|
|
|
| 56 |
text = str(translator.translate(prompt, 'English')) + "," + lora_word
|
| 57 |
-
async with AsyncInferenceClient() as client:
|
| 58 |
-
try:
|
| 59 |
-
image = await client.text_to_image(
|
| 60 |
-
prompt=text,
|
| 61 |
-
height=height,
|
| 62 |
-
width=width,
|
| 63 |
-
guidance_scale=scales,
|
| 64 |
-
num_inference_steps=steps,
|
| 65 |
-
model=model,
|
| 66 |
-
)
|
| 67 |
-
except Exception as e:
|
| 68 |
-
raise gr.Error(f"Error in {e}")
|
| 69 |
-
return image, seed
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
async def gen(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
model = enable_lora(lora_add)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
combined_image.paste(image, (0, 0))
|
| 79 |
-
combined_image.paste(upscaled_image, (image.width, 0))
|
| 80 |
-
return combined_image, seed
|
| 81 |
-
else:
|
| 82 |
-
return image, seed
|
| 83 |
-
|
| 84 |
-
|
| 85 |
with gr.Blocks(css=CSS, js=JS, theme="Nymbo/Nymbo_Theme") as demo:
|
| 86 |
gr.HTML("<h1><center>Flux Lab Light</center></h1>")
|
| 87 |
with gr.Row():
|
| 88 |
with gr.Column(scale=4):
|
| 89 |
with gr.Row():
|
| 90 |
-
img = gr.Image(type="filepath", label='
|
| 91 |
with gr.Row():
|
| 92 |
prompt = gr.Textbox(label='Enter Your Prompt (Multi-Languages)', placeholder="Enter prompt...", scale=6)
|
| 93 |
sendBtn = gr.Button(scale=1, variant='primary')
|
| 94 |
with gr.Accordion("Advanced Options", open=True):
|
| 95 |
with gr.Column(scale=1):
|
| 96 |
-
width = gr.Slider(
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
gr.on(
|
| 105 |
-
triggers=[
|
|
|
|
|
|
|
|
|
|
| 106 |
fn=gen,
|
| 107 |
inputs=[
|
| 108 |
prompt,
|
| 109 |
lora_add,
|
| 110 |
lora_word,
|
| 111 |
-
width,
|
| 112 |
-
height,
|
| 113 |
-
scales,
|
| 114 |
-
steps,
|
| 115 |
-
seed
|
| 116 |
-
upscale_factor
|
| 117 |
],
|
| 118 |
outputs=[img, seed]
|
| 119 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
import random
|
| 5 |
+
from huggingface_hub import AsyncInferenceClient
|
| 6 |
from translatepy import Translator
|
| 7 |
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 |
+
CSS = """
|
| 18 |
+
footer {
|
| 19 |
+
visibility: hidden;
|
| 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:
|
|
|
|
| 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 |
+
image = await client.text_to_image(
|
| 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 |
+
print(model)
|
| 81 |
+
image, seed = await generate_image(prompt,model,lora_word,width,height,scales,steps,seed)
|
| 82 |
+
return image, seed
|
| 83 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
with gr.Blocks(css=CSS, js=JS, theme="Nymbo/Nymbo_Theme") as demo:
|
| 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 |
gr.on(
|
| 144 |
+
triggers=[
|
| 145 |
+
prompt.submit,
|
| 146 |
+
sendBtn.click,
|
| 147 |
+
],
|
| 148 |
fn=gen,
|
| 149 |
inputs=[
|
| 150 |
prompt,
|
| 151 |
lora_add,
|
| 152 |
lora_word,
|
| 153 |
+
width,
|
| 154 |
+
height,
|
| 155 |
+
scales,
|
| 156 |
+
steps,
|
| 157 |
+
seed
|
|
|
|
| 158 |
],
|
| 159 |
outputs=[img, seed]
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
if __name__ == "__main__":
|
| 163 |
+
demo.queue(api_open=False).launch(show_api=False, share=False)
|