Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
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 |
+
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 generando imagen: {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 escalando imagen: {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_path = get_upscale_finegrain(prompt, image_path, upscale_factor)
|
55 |
+
if upscale_image_path is not None:
|
56 |
+
upscale_image = Image.open(upscale_image_path)
|
57 |
+
upscale_image.save("upscale_image.jpg", format="JPEG")
|
58 |
+
return [image_path, "upscale_image.jpg"]
|
59 |
+
else:
|
60 |
+
print("Error: The scaled image path is None")
|
61 |
+
return [image_path, image_path]
|
62 |
+
else:
|
63 |
+
return [image_path, image_path]
|
64 |
+
|
65 |
+
css = """
|
66 |
+
#col-container{ margin: 0 auto; max-width: 1024px;}
|
67 |
+
"""
|
68 |
+
|
69 |
+
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
|
70 |
+
with gr.Column(elem_id="col-container"):
|
71 |
+
with gr.Row():
|
72 |
+
with gr.Column(scale=3):
|
73 |
+
output_res = ImageSlider(label="Flux / Upscaled")
|
74 |
+
with gr.Column(scale=2):
|
75 |
+
prompt = gr.Textbox(label="Image Description")
|
76 |
+
basemodel_choice = gr.Dropdown(label="Model", choices=["black-forest-labs/FLUX.1-schnell", "black-forest-labs/FLUX.1-DEV", "enhanceaiteam/Flux-uncensored", "Shakker-Labs/FLUX.1-dev-ControlNet-Union-Pro", "Shakker-Labs/FLUX.1-dev-LoRA-add-details", "city96/FLUX.1-dev-gguf"], value="black-forest-labs/FLUX.1-schnell")
|
77 |
+
lora_model_choice = gr.Dropdown(label="LoRA", choices=["Shakker-Labs/FLUX.1-dev-LoRA-add-details", "XLabs-AI/flux-RealismLora", "enhanceaiteam/Flux-uncensored"], value="XLabs-AI/flux-RealismLora")
|
78 |
+
process_lora = gr.Checkbox(label="LoRA Process")
|
79 |
+
process_upscale = gr.Checkbox(label="Scale Process")
|
80 |
+
upscale_factor = gr.Radio(label="Scaling Factor", choices=[2, 4, 8], value=2)
|
81 |
+
|
82 |
+
with gr.Accordion(label="Advanced Options", open=False):
|
83 |
+
width = gr.Slider(label="Width", minimum=512, maximum=1280, step=8, value=1280)
|
84 |
+
height = gr.Slider(label="Height", minimum=512, maximum=1280, step=8, value=768)
|
85 |
+
scales = gr.Slider(label="Scale", minimum=1, maximum=20, step=1, value=8)
|
86 |
+
steps = gr.Slider(label="Steps", minimum=1, maximum=100, step=1, value=8)
|
87 |
+
seed = gr.Number(label="Seed", value=-1)
|
88 |
+
|
89 |
+
btn = gr.Button("Generate")
|
90 |
+
btn.click(fn=gen, inputs=[prompt, basemodel_choice, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model_choice, process_lora], outputs=output_res,)
|
91 |
+
demo.launch()
|