Spaces:
Running
Running
salomonsky
commited on
Commit
•
f0f180b
1
Parent(s):
113dc2c
Update app.py
Browse files
app.py
CHANGED
@@ -10,98 +10,116 @@ from gradio_imageslider import ImageSlider
|
|
10 |
MAX_SEED = np.iinfo(np.int32).max
|
11 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
12 |
HF_TOKEN_UPSCALER = os.environ.get("HF_TOKEN_UPSCALER")
|
13 |
-
|
14 |
client = AsyncInferenceClient()
|
15 |
llm_client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
16 |
|
17 |
def enable_lora(lora_add, basemodel):
|
18 |
-
return basemodel if not lora_add else lora_add
|
19 |
|
20 |
async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
|
21 |
-
try:
|
22 |
-
if seed == -1:
|
23 |
-
seed = random.randint(0, MAX_SEED)
|
24 |
-
seed = int(seed)
|
25 |
-
text = prompt + "," + lora_word
|
26 |
-
image = await client.text_to_image(
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
30 |
|
31 |
def get_upscale_finegrain(prompt, img_path, upscale_factor):
|
32 |
-
try:
|
33 |
-
client = Client("finegrain/finegrain-image-enhancer", hf_token=HF_TOKEN_UPSCALER)
|
34 |
-
result = client.predict(
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora):
|
40 |
-
model = enable_lora(lora_model, basemodel) if process_lora else basemodel
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
image,
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
if
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
return [image_path,
|
59 |
-
else:
|
60 |
-
return [image_path, image_path]
|
61 |
-
else:
|
62 |
-
return [image_path, image_path]
|
63 |
|
64 |
async def improve_prompt(prompt):
|
65 |
-
try:
|
66 |
-
instruction = "With this idea, describe in English a detailed img2vid prompt in a single paragraph of up to
|
67 |
-
formatted_prompt = f"{prompt}: {instruction}"
|
68 |
-
response = llm_client.text_generation(formatted_prompt, max_new_tokens=300, language="english")
|
69 |
-
improved_text = response['generated_text'].strip() if 'generated_text' in response else response.strip()
|
70 |
|
71 |
-
return improved_text
|
72 |
-
except Exception as e:
|
73 |
-
return f"Error mejorando el prompt: {e}"
|
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(
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
with gr.
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
MAX_SEED = np.iinfo(np.int32).max
|
11 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
12 |
HF_TOKEN_UPSCALER = os.environ.get("HF_TOKEN_UPSCALER")
|
|
|
13 |
client = AsyncInferenceClient()
|
14 |
llm_client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
15 |
|
16 |
def enable_lora(lora_add, basemodel):
|
17 |
+
return basemodel if not lora_add else lora_add
|
18 |
|
19 |
async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
|
20 |
+
try:
|
21 |
+
if seed == -1:
|
22 |
+
seed = random.randint(0, MAX_SEED)
|
23 |
+
seed = int(seed)
|
24 |
+
text = prompt + "," + lora_word
|
25 |
+
image = await client.text_to_image(
|
26 |
+
prompt=text, height=height, width=width, guidance_scale=scales,
|
27 |
+
num_inference_steps=steps, model=model
|
28 |
+
)
|
29 |
+
return image, seed
|
30 |
+
except Exception as e:
|
31 |
+
return f"Error al generar imagen: {e}", None
|
32 |
|
33 |
def get_upscale_finegrain(prompt, img_path, upscale_factor):
|
34 |
+
try:
|
35 |
+
client = Client("finegrain/finegrain-image-enhancer", hf_token=HF_TOKEN_UPSCALER)
|
36 |
+
result = client.predict(
|
37 |
+
input_image=handle_file(img_path), prompt=prompt, negative_prompt="",
|
38 |
+
seed=42, upscale_factor=upscale_factor, controlnet_scale=0.6,
|
39 |
+
controlnet_decay=1, condition_scale=6, tile_width=112,
|
40 |
+
tile_height=144, denoise_strength=0.35, num_inference_steps=18,
|
41 |
+
solver="DDIM", api_name="/process"
|
42 |
+
)
|
43 |
+
return result[1]
|
44 |
+
except Exception as e:
|
45 |
+
return None
|
46 |
|
47 |
async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora):
|
48 |
+
model = enable_lora(lora_model, basemodel) if process_lora else basemodel
|
49 |
+
improved_prompt = await improve_prompt(prompt)
|
50 |
+
combined_prompt = f"{prompt} {improved_prompt}"
|
51 |
+
image, seed = await generate_image(combined_prompt, model, "", width, height, scales, steps, seed)
|
52 |
+
|
53 |
+
if isinstance(image, str) and image.startswith("Error"):
|
54 |
+
return [image, None]
|
55 |
+
|
56 |
+
image_path = "temp_image.jpg"
|
57 |
+
image.save(image_path, format="JPEG")
|
58 |
+
|
59 |
+
if process_upscale:
|
60 |
+
upscale_image_path = get_upscale_finegrain(combined_prompt, image_path, upscale_factor)
|
61 |
+
if upscale_image_path is not None:
|
62 |
+
upscale_image = Image.open(upscale_image_path)
|
63 |
+
upscale_image.save("upscale_image.jpg", format="JPEG")
|
64 |
+
return [image_path, "upscale_image.jpg"]
|
65 |
+
else:
|
66 |
+
return [image_path, image_path]
|
67 |
+
else:
|
68 |
+
return [image_path, image_path]
|
|
|
|
|
69 |
|
70 |
async def improve_prompt(prompt):
|
71 |
+
try:
|
72 |
+
instruction = ("With this idea, describe in English a detailed img2vid prompt in a single paragraph of up to 200 characters maximun, developing atmosphere, characters, lighting, and cameras.")
|
73 |
+
formatted_prompt = f"{prompt}: {instruction}"
|
74 |
+
response = llm_client.text_generation(formatted_prompt, max_new_tokens=300, language="english")
|
75 |
+
improved_text = response['generated_text'].strip() if 'generated_text' in response else response.strip()
|
76 |
|
77 |
+
return improved_text
|
78 |
+
except Exception as e:
|
79 |
+
return f"Error mejorando el prompt: {e}"
|
80 |
|
81 |
css = """
|
82 |
#col-container{ margin: 0 auto; max-width: 1024px;}
|
83 |
"""
|
84 |
|
85 |
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
|
86 |
+
with gr.Column(elem_id="col-container"):
|
87 |
+
with gr.Row():
|
88 |
+
with gr.Column(scale=3):
|
89 |
+
output_res = ImageSlider(label="Flux / Upscaled")
|
90 |
+
with gr.Column(scale=2):
|
91 |
+
prompt = gr.Textbox(label="Descripción de imágen")
|
92 |
+
basemodel_choice = gr.Dropdown(
|
93 |
+
label="Modelo",
|
94 |
+
choices=["black-forest-labs/FLUX.1-schnell", "black-forest-labs/FLUX.1-DEV"],
|
95 |
+
value="black-forest-labs/FLUX.1-schnell"
|
96 |
+
)
|
97 |
+
lora_model_choice = gr.Dropdown(
|
98 |
+
label="LORA Realismo",
|
99 |
+
choices=["Shakker-Labs/FLUX.1-dev-LoRA-add-details", "XLabs-AI/flux-RealismLora"],
|
100 |
+
value="XLabs-AI/flux-RealismLora"
|
101 |
+
)
|
102 |
+
|
103 |
+
with gr.Row():
|
104 |
+
process_lora = gr.Checkbox(label="Procesar LORA")
|
105 |
+
process_upscale = gr.Checkbox(label="Procesar Escalador")
|
106 |
+
|
107 |
+
improved_prompt = gr.Textbox(label="Prompt Mejorado", interactive=False)
|
108 |
+
improve_btn = gr.Button("Mejora mi prompt")
|
109 |
+
improve_btn.click(fn=improve_prompt, inputs=[prompt], outputs=improved_prompt)
|
110 |
+
|
111 |
+
with gr.Accordion(label="Opciones Avanzadas", open=False):
|
112 |
+
width = gr.Slider(label="Ancho", minimum=512, maximum=1280, step=8, value=1280)
|
113 |
+
height = gr.Slider(label="Alto", minimum=512, maximum=1280, step=8, value=768)
|
114 |
+
upscale_factor = gr.Radio(label="Factor de Escala", choices=[2, 4, 8], value=2)
|
115 |
+
scales = gr.Slider(label="Escalado", minimum=1, maximum=20, step=1, value=10)
|
116 |
+
steps = gr.Slider(label="Pasos", minimum=1, maximum=100, step=1, value=20)
|
117 |
+
seed = gr.Number(label="Semilla", value=-1)
|
118 |
+
|
119 |
+
btn = gr.Button("Generar")
|
120 |
+
btn.click(
|
121 |
+
fn=gen,
|
122 |
+
inputs=[prompt, basemodel_choice, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model_choice, process_lora],
|
123 |
+
outputs=output_res
|
124 |
+
)
|
125 |
+
demo.launch()
|