Spaces:
Running
Running
salomonsky
commited on
Commit
•
61bff42
1
Parent(s):
b329b3a
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
import numpy as np
|
4 |
import random
|
@@ -16,90 +17,70 @@ 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 |
-
|
20 |
-
|
|
|
|
|
|
|
21 |
|
22 |
async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
return image, seed
|
31 |
-
except Exception as e:
|
32 |
-
print(f"Error generating image: {e}")
|
33 |
-
return None, None
|
34 |
|
35 |
-
def
|
36 |
-
|
37 |
-
|
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 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
image_path = "temp_image.png"
|
51 |
-
try:
|
52 |
-
image.save(image_path, format="PNG")
|
53 |
-
except Exception as e:
|
54 |
-
print(f"Error al guardar la imagen: {e}")
|
55 |
-
return [None, None]
|
56 |
-
|
57 |
-
if process_upscale:
|
58 |
-
upscale_image = get_upscale_finegrain(prompt, image_path, upscale_factor)
|
59 |
-
if upscale_image is None:
|
60 |
-
return [image_path, image_path]
|
61 |
-
upscale_image_path = "upscale_image.png"
|
62 |
-
try:
|
63 |
-
upscale_image.save(upscale_image_path, format="PNG")
|
64 |
-
except Exception as e:
|
65 |
-
print(f"Error al guardar la imagen escalada: {e}")
|
66 |
-
return [image_path, None]
|
67 |
-
return [image_path, upscale_image_path]
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
|
|
73 |
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
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")
|
82 |
process_lora = gr.Checkbox(label="Procesar LORA")
|
83 |
process_upscale = gr.Checkbox(label="Procesar Escalador")
|
84 |
upscale_factor = gr.Radio(label="Factor de Escala", choices=[2, 4, 8], value=2)
|
|
|
85 |
|
86 |
with gr.Accordion(label="Opciones Avanzadas", open=False):
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
).then(
|
100 |
-
fn=gen,
|
101 |
-
inputs=[prompt, basemodel_choice, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model_choice, process_lora],
|
102 |
-
outputs=[output_res]
|
103 |
-
)
|
104 |
|
105 |
-
demo.launch()
|
|
|
1 |
import os
|
2 |
+
import torch
|
3 |
import gradio as gr
|
4 |
import numpy as np
|
5 |
import random
|
|
|
17 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
18 |
HF_TOKEN_UPSCALER = os.environ.get("HF_TOKEN_UPSCALER")
|
19 |
|
20 |
+
if not os.path.exists('GFPGANv1.4.pth'): os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
|
21 |
+
|
22 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
23 |
+
model_path = 'GFPGANv1.4.pth'
|
24 |
+
gfpgan = GFPGANer(model_path=model_path, upscale_factor=4, arch='clean', channel_multiplier=2, model_name='GPFGAN', device=device)
|
25 |
|
26 |
async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
|
27 |
+
try:
|
28 |
+
if seed == -1: seed = random.randint(0, MAX_SEED); seed = int(seed)
|
29 |
+
text = str(Translator().translate(prompt, 'English')) + "," + lora_word
|
30 |
+
client = AsyncInferenceClient()
|
31 |
+
image = await client.text_to_image(prompt=text, height=height, width=width, guidance_scale=scales, num_inference_steps=steps, model=model)
|
32 |
+
return image, seed
|
33 |
+
except Exception as e: print(f"Error generating image: {e}"); return None, None
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
def get_upscale_gfpgan(prompt, img_path):
|
36 |
+
try: return gfpgan.enhance(img_path)
|
37 |
+
except Exception as e: print(f"Error upscale image: {e}"); return None
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
def get_upscale_finegrain(prompt, img_path, upscale_factor):
|
40 |
+
try:
|
41 |
+
client = Client("finegrain/finegrain-image-enhancer", hf_token=HF_TOKEN_UPSCALER)
|
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 |
+
except Exception as e: print(f"Error upscale image: {e}"); return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora, upscale_model):
|
47 |
+
model = enable_lora(lora_model, basemodel) if process_lora else basemodel
|
48 |
+
image, seed = await generate_image(prompt, model, "", width, height, scales, steps, seed)
|
49 |
+
if image is None: return [None, None]
|
50 |
+
image_path = "temp_image.jpg"; image.save(image_path, format="JPEG")
|
51 |
+
if process_upscale:
|
52 |
+
if upscale_model == "GPFGAN": upscale_image = get_upscale_gfpgan(prompt, image_path)
|
53 |
+
elif upscale_model == "Finegrain": upscale_image = get_upscale_finegrain(prompt, image_path, upscale_factor)
|
54 |
+
upscale_image_path = "upscale_image.jpg"; upscale_image.save(upscale_image_path, format="JPEG")
|
55 |
+
return [image_path, upscale_image_path]
|
56 |
+
else: return [image_path, image_path]
|
57 |
|
58 |
+
css = """#col-container{ margin: 0 auto; max-width: 1024px;}"""
|
59 |
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
|
60 |
+
with gr.Column(elem_id="col-container"):
|
61 |
+
with gr.Row():
|
62 |
+
with gr.Column(scale=3): output_res = ImageSlider(label="Flux / Upscaled")
|
63 |
+
with gr.Column(scale=2):
|
64 |
+
prompt = gr.Textbox(label="Descripción de imágen")
|
65 |
+
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")
|
66 |
+
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")
|
|
|
67 |
process_lora = gr.Checkbox(label="Procesar LORA")
|
68 |
process_upscale = gr.Checkbox(label="Procesar Escalador")
|
69 |
upscale_factor = gr.Radio(label="Factor de Escala", choices=[2, 4, 8], value=2)
|
70 |
+
upscale_model = gr.Radio(label="Modelo de Escalado", choices=["GPFGAN", "Finegrain"], value="GPFGAN")
|
71 |
|
72 |
with gr.Accordion(label="Opciones Avanzadas", open=False):
|
73 |
+
width = gr.Slider(label="Ancho", minimum=512, maximum=1280, step=8, value=512)
|
74 |
+
height = gr.Slider(label="Alto", minimum=512, maximum=1280, step=8, value=512)
|
75 |
+
scales = gr.Slider(label="Escalado", minimum=1, maximum=20, step=1, value=10)
|
76 |
+
steps = gr.Slider(label="Pasos", minimum=1, maximum=100, step=1, value=20)
|
77 |
+
seed = gr.Number(label="Semilla", value=-1)
|
78 |
+
|
79 |
+
btn = gr.Button("Generar")
|
80 |
+
btn.click(
|
81 |
+
fn=gen,
|
82 |
+
inputs=[prompt, basemodel_choice, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model_choice, process_lora, upscale_model],
|
83 |
+
outputs=output_res,
|
84 |
+
)
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
+
demo.launch()
|