salomonsky
commited on
Commit
•
6bd865c
1
Parent(s):
61bff42
Update app.py
Browse files
app.py
CHANGED
@@ -17,52 +17,73 @@ MAX_SEED = np.iinfo(np.int32).max
|
|
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'):
|
|
|
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 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
34 |
|
35 |
def get_upscale_gfpgan(prompt, img_path):
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
38 |
|
39 |
def get_upscale_finegrain(prompt, img_path, upscale_factor):
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
45 |
|
46 |
async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora, upscale_model):
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
css = """#col-container{ margin: 0 auto; max-width: 1024px;}"""
|
59 |
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
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")
|
|
|
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'):
|
21 |
+
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
|
22 |
|
23 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
24 |
model_path = 'GFPGANv1.4.pth'
|
25 |
gfpgan = GFPGANer(model_path=model_path, upscale_factor=4, arch='clean', channel_multiplier=2, model_name='GPFGAN', device=device)
|
26 |
|
27 |
async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
|
28 |
+
try:
|
29 |
+
if seed == -1:
|
30 |
+
seed = random.randint(0, MAX_SEED)
|
31 |
+
seed = int(seed)
|
32 |
+
text = str(Translator().translate(prompt, 'English')) + "," + lora_word
|
33 |
+
client = AsyncInferenceClient()
|
34 |
+
image = await client.text_to_image(prompt=text, height=height, width=width, guidance_scale=scales, num_inference_steps=steps, model=model)
|
35 |
+
return image, seed
|
36 |
+
except Exception as e:
|
37 |
+
print(f"Error generating image: {e}")
|
38 |
+
return None, None
|
39 |
|
40 |
def get_upscale_gfpgan(prompt, img_path):
|
41 |
+
try:
|
42 |
+
img = gfpgan.enhance(img_path)
|
43 |
+
return img
|
44 |
+
except Exception as e:
|
45 |
+
print(f"Error upscale image: {e}")
|
46 |
+
return None
|
47 |
|
48 |
def get_upscale_finegrain(prompt, img_path, upscale_factor):
|
49 |
+
try:
|
50 |
+
client = Client("finegrain/finegrain-image-enhancer", hf_token=HF_TOKEN_UPSCALER)
|
51 |
+
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")
|
52 |
+
return result[1]
|
53 |
+
except Exception as e:
|
54 |
+
print(f"Error upscale image: {e}")
|
55 |
+
return None
|
56 |
|
57 |
async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora, upscale_model):
|
58 |
+
model = enable_lora(lora_model, basemodel) if process_lora else basemodel
|
59 |
+
image, seed = await generate_image(prompt, model, "", width, height, scales, steps, seed)
|
60 |
+
if image is None:
|
61 |
+
return [None, None]
|
62 |
+
image_path = "temp_image.jpg"
|
63 |
+
image.save(image_path, format="JPEG")
|
64 |
+
if process_upscale:
|
65 |
+
if upscale_model == "GPFGAN":
|
66 |
+
upscale_image = get_upscale_gfpgan(prompt, image_path)
|
67 |
+
elif upscale_model == "Finegrain":
|
68 |
+
upscale_image = get_upscale_finegrain(prompt, image_path, upscale_factor)
|
69 |
+
upscale_image_path = "upscale_image.jpg"
|
70 |
+
upscale_image.save(upscale_image_path, format="JPEG")
|
71 |
+
return [image_path, upscale_image_path]
|
72 |
+
else:
|
73 |
+
return [image_path, image_path]
|
74 |
+
|
75 |
+
css = """
|
76 |
+
#col-container{ margin: 0 auto; max-width: 1024px;}
|
77 |
+
"""
|
78 |
|
|
|
79 |
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
|
80 |
+
with gr.Column(elem_id="col-container"):
|
81 |
+
with gr.Row():
|
82 |
+
with gr.Column(scale=3):
|
83 |
+
output_res = ImageSlider(label="Flux / Upscaled")
|
84 |
+
with gr.Column(scale=2):
|
85 |
+
prompt = gr.Textbox(label="Descripción de imágen")
|
86 |
+
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")
|
87 |
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")
|
88 |
process_lora = gr.Checkbox(label="Procesar LORA")
|
89 |
process_upscale = gr.Checkbox(label="Procesar Escalador")
|