Spaces:
Running
Running
salomonsky
commited on
Commit
•
32fdddd
1
Parent(s):
2f35681
Update app.py
Browse files
app.py
CHANGED
@@ -12,72 +12,77 @@ from gradio_client import Client, handle_file
|
|
12 |
from huggingface_hub import login
|
13 |
from gradio_imageslider import ImageSlider
|
14 |
|
15 |
-
|
16 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
17 |
HF_TOKEN_UPSCALER = os.environ.get("HF_TOKEN_UPSCALER")
|
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, basemodel):
|
23 |
return basemodel if not lora_add else lora_add
|
24 |
|
25 |
async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
|
26 |
-
|
27 |
-
seed
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora):
|
35 |
-
model = lora_model
|
36 |
image, seed = await generate_image(prompt, model, "", width, height, scales, steps, seed)
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
39 |
|
40 |
if process_upscale:
|
41 |
upscale_image = get_upscale_finegrain(prompt, image_path, upscale_factor)
|
|
|
|
|
|
|
42 |
else:
|
43 |
-
|
44 |
-
|
45 |
-
return [image_path, upscale_image]
|
46 |
-
|
47 |
-
def get_upscale_finegrain(prompt, img_path, upscale_factor):
|
48 |
-
client = Client("finegrain/finegrain-image-enhancer", hf_token=HF_TOKEN_UPSCALER)
|
49 |
-
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")
|
50 |
-
return result[1]
|
51 |
|
52 |
css = """
|
53 |
-
#col-container{
|
54 |
-
margin: 0 auto;
|
55 |
-
max-width: 1024px;
|
56 |
-
}
|
57 |
"""
|
58 |
|
59 |
-
with gr.Blocks(css=
|
60 |
with gr.Column(elem_id="col-container"):
|
61 |
-
gr.Markdown("Flux Upscaled +LORA")
|
62 |
with gr.Row():
|
63 |
-
with gr.Column(scale=
|
64 |
output_res = ImageSlider(label="Flux / Upscaled")
|
65 |
-
with gr.Column(scale=
|
66 |
-
prompt = gr.Textbox(label="
|
67 |
-
basemodel_choice = gr.Dropdown(label="
|
68 |
-
lora_model_choice = gr.Dropdown(label="LORA
|
69 |
-
process_lora = gr.Checkbox(label="
|
70 |
-
|
71 |
-
|
72 |
|
73 |
-
with gr.Accordion(label="
|
74 |
-
width = gr.Slider(label="
|
75 |
-
height = gr.Slider(label="
|
76 |
-
scales = gr.Slider(label="
|
77 |
-
steps = gr.Slider(label="
|
78 |
-
seed = gr.Slider(label="
|
79 |
-
|
80 |
-
submit_btn = gr.Button("
|
81 |
submit_btn.click(
|
82 |
fn=lambda: None,
|
83 |
inputs=None,
|
@@ -88,4 +93,5 @@ with gr.Blocks(css=CSS, js=JS, theme="Nymbo/Nymbo_Theme") as demo:
|
|
88 |
inputs=[prompt, basemodel_choice, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model_choice, process_lora],
|
89 |
outputs=[output_res]
|
90 |
)
|
|
|
91 |
demo.launch()
|
|
|
12 |
from huggingface_hub import login
|
13 |
from gradio_imageslider import ImageSlider
|
14 |
|
15 |
+
MAX_SEED = np.iinfo(np.int32).max
|
16 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
17 |
HF_TOKEN_UPSCALER = os.environ.get("HF_TOKEN_UPSCALER")
|
|
|
|
|
|
|
18 |
|
19 |
+
def enable_lora(lora_add, basemodel):
|
20 |
return basemodel if not lora_add else lora_add
|
21 |
|
22 |
async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
|
23 |
+
try:
|
24 |
+
if seed == -1:
|
25 |
+
seed = random.randint(0, MAX_SEED)
|
26 |
+
seed = int(seed)
|
27 |
+
text = str(Translator().translate(prompt, 'English')) + "," + lora_word
|
28 |
+
client = AsyncInferenceClient()
|
29 |
+
image = await client.text_to_image(prompt=text, height=height, width=width, guidance_scale=scales, num_inference_steps=steps, model=model)
|
30 |
+
return image, seed
|
31 |
+
except Exception as e:
|
32 |
+
print(f"Error generating image: {e}")
|
33 |
+
return None, None
|
34 |
+
|
35 |
+
def get_upscale_finegrain(prompt, img_path, upscale_factor):
|
36 |
+
try:
|
37 |
+
client = Client("finegrain/finegrain-image-enhancer", hf_token=HF_TOKEN_UPSCALER)
|
38 |
+
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")
|
39 |
+
return result[1]
|
40 |
+
except Exception as e:
|
41 |
+
print(f"Error upscale image: {e}")
|
42 |
+
return None
|
43 |
|
44 |
async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora):
|
45 |
+
model = enable_lora(lora_model, basemodel) if process_lora else basemodel
|
46 |
image, seed = await generate_image(prompt, model, "", width, height, scales, steps, seed)
|
47 |
+
if image is None:
|
48 |
+
return [None, None]
|
49 |
+
|
50 |
+
image_path = "temp_image.jpg"
|
51 |
+
image.save(image_path, format="JPEG")
|
52 |
|
53 |
if process_upscale:
|
54 |
upscale_image = get_upscale_finegrain(prompt, image_path, upscale_factor)
|
55 |
+
upscale_image_path = "upscale_image.jpg"
|
56 |
+
upscale_image.save(upscale_image_path, format="JPEG")
|
57 |
+
return [image_path, upscale_image_path]
|
58 |
else:
|
59 |
+
return [image_path, image_path]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
css = """
|
62 |
+
#col-container{ margin: 0 auto; max-width: 1024px;}
|
|
|
|
|
|
|
63 |
"""
|
64 |
|
65 |
+
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
|
66 |
with gr.Column(elem_id="col-container"):
|
|
|
67 |
with gr.Row():
|
68 |
+
with gr.Column(scale=3):
|
69 |
output_res = ImageSlider(label="Flux / Upscaled")
|
70 |
+
with gr.Column(scale=2):
|
71 |
+
prompt = gr.Textbox(label="Descripción de imágen")
|
72 |
+
basemodel_choice = gr.Dropdown(label="Modelo", choices=["black-forest-labs/FLUX.1-schnell", "black-forest-labs/FLUX.1-DEV"], value="black-forest-labs/FLUX.1-schnell")
|
73 |
+
lora_model_choice = gr.Dropdown(label="LORA Realismo", choices=["Shakker-Labs/FLUX.1-dev-LoRA-add-details", "XLabs-AI/flux-RealismLora"], value="XLabs-AI/flux-RealismLora")
|
74 |
+
process_lora = gr.Checkbox(label="Procesar LORA")
|
75 |
+
process_upscale = gr.Checkbox(label="Procesar Escalador")
|
76 |
+
upscale_factor = gr.Radio(label="Factor de Escala", choices=[2, 4, 8], value=2)
|
77 |
|
78 |
+
with gr.Accordion(label="Opciones Avanzadas", open=False):
|
79 |
+
width = gr.Slider(label="Ancho", minimum=512, maximum=1280, step=8, value=1280)
|
80 |
+
height = gr.Slider(label="Alto", minimum=512, maximum=1280, step=8, value=768)
|
81 |
+
scales = gr.Slider(label="Escalas", minimum=3.5, maximum=7, step=0.1, value=3.5)
|
82 |
+
steps = gr.Slider(label="Pasos", minimum=1, maximum=100, step=1, value=24)
|
83 |
+
seed = gr.Slider(label="Semillas", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
|
84 |
+
|
85 |
+
submit_btn = gr.Button("Crear", scale=1)
|
86 |
submit_btn.click(
|
87 |
fn=lambda: None,
|
88 |
inputs=None,
|
|
|
93 |
inputs=[prompt, basemodel_choice, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model_choice, process_lora],
|
94 |
outputs=[output_res]
|
95 |
)
|
96 |
+
|
97 |
demo.launch()
|