salomonsky commited on
Commit
61bff42
1 Parent(s): b329b3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -74
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
- 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.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
- css = """
70
- #col-container{ margin: 0 auto; max-width: 1024px;}
71
- """
 
 
 
 
 
 
 
 
72
 
 
73
  with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
74
- with gr.Column(elem_id="col-container"):
75
- with gr.Row():
76
- with gr.Column(scale=3):
77
- output_res = ImageSlider(label="Flux / Upscaled")
78
- with gr.Column(scale=2):
79
- prompt = gr.Textbox(label="Descripción de imágen")
80
- 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")
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
- width = gr.Slider(label="Ancho", minimum=512, maximum=1280, step=8, value=1280)
88
- height = gr.Slider(label="Alto", minimum=512, maximum=1280, step=8, value=768)
89
- scales = gr.Slider(label="Escalas", minimum=3.5, maximum=7, step=0.1, value=3.5)
90
- steps = gr.Slider(label="Pasos", minimum=1, maximum=100, step=1, value=24)
91
- seed = gr.Slider(label="Semillas", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
92
-
93
- submit_btn = gr.Button("Crear", scale=1)
94
- submit_btn.click(
95
- fn=lambda: None,
96
- inputs=None,
97
- outputs=[output_res],
98
- queue=False
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()