salomonsky commited on
Commit
2e0910a
verified
1 Parent(s): 9a11c4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -78
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import os
2
  import gradio as gr
3
  import numpy as np
4
  import random
@@ -15,95 +15,93 @@ 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(prompt=text, height=height, width=width, guidance_scale=scales, num_inference_steps=steps, model=model)
27
- return image, seed
28
- except Exception as e:
29
- return f"Error al generar imagen: {e}", None
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(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")
35
- return result[1]
36
- except Exception as e:
37
- return None
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
- improved_prompt = await improve_prompt(prompt)
43
- combined_prompt = f"{prompt} {improved_prompt}"
44
-
45
- image, seed = await generate_image(combined_prompt, model, "", width, height, scales, steps, seed)
46
-
47
- if isinstance(image, str) and image.startswith("Error"):
48
- return [image, 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(combined_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
- return [image_path, image_path]
61
- else:
62
- return [image_path, image_path]
63
 
64
  async def improve_prompt(prompt):
65
- try:
66
- instruction = "Improve and translate this prompt into English, adding detailed descriptions of style, cinematography, cameras, atmosphere, and lighting for the best quality, up to 200 words."
67
- formatted_prompt = f"{instruction}: {prompt}"
68
- response = llm_client.text_generation(formatted_prompt, max_new_tokens=300) # Allowing more tokens for detailed description
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(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
-
89
- with gr.Row():
90
- process_lora = gr.Checkbox(label="Procesar LORA")
91
- process_upscale = gr.Checkbox(label="Procesar Escalador")
92
-
93
- upscale_factor = gr.Radio(label="Factor de Escala", choices=[2, 4, 8], value=2)
94
-
95
- improved_prompt = gr.Textbox(label="Prompt Mejorado", interactive=False)
96
-
97
- improve_btn = gr.Button("Mejora mi prompt")
98
- improve_btn.click(fn=improve_prompt, inputs=[prompt], outputs=improved_prompt)
99
-
100
- with gr.Accordion(label="Opciones Avanzadas", open=False):
101
- width = gr.Slider(label="Ancho", minimum=512, maximum=1280, step=8, value=1280)
102
- height = gr.Slider(label="Alto", minimum=512, maximum=1280, step=8, value=768)
103
- scales = gr.Slider(label="Escalado", minimum=1, maximum=20, step=1, value=10)
104
- steps = gr.Slider(label="Pasos", minimum=1, maximum=100, step=1, value=20)
105
- seed = gr.Number(label="Semilla", value=-1)
106
-
107
- btn = gr.Button("Generar")
108
- 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)
109
- demo.launch()
 
1
+ import os
2
  import gradio as gr
3
  import numpy as np
4
  import random
 
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(prompt=text, height=height, width=width, guidance_scale=scales, num_inference_steps=steps, model=model)
27
+ return image, seed
28
+ except Exception as e:
29
+ return f"Error al generar imagen: {e}", None
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(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")
35
+ return result[1]
36
+ except Exception as e:
37
+ return None
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
+ improved_prompt = await improve_prompt(prompt)
43
+ combined_prompt = f"{prompt} {improved_prompt}"
44
+
45
+ image, seed = await generate_image(combined_prompt, model, "", width, height, scales, steps, seed)
46
+
47
+ if isinstance(image, str) and image.startswith("Error"):
48
+ return [image, 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(combined_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
+ 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 300 characters, developing atmosphere, characters, lighting, and cameras."
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(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
+
89
+ with gr.Row():
90
+ process_lora = gr.Checkbox(label="Procesar LORA")
91
+ process_upscale = gr.Checkbox(label="Procesar Escalador")
92
+
93
+ improved_prompt = gr.Textbox(label="Prompt Mejorado", interactive=False)
94
+ improve_btn = gr.Button("Mejora mi prompt")
95
+ improve_btn.click(fn=improve_prompt, inputs=[prompt], outputs=improved_prompt)
96
+
97
+ with gr.Accordion(label="Opciones Avanzadas", open=False):
98
+ width = gr.Slider(label="Ancho", minimum=512, maximum=1280, step=8, value=1280)
99
+ height = gr.Slider(label="Alto", minimum=512, maximum=1280, step=8, value=768)
100
+ upscale_factor = gr.Radio(label="Factor de Escala", choices=[2, 4, 8], value=2)
101
+ scales = gr.Slider(label="Escalado", minimum=1, maximum=20, step=1, value=10)
102
+ steps = gr.Slider(label="Pasos", minimum=1, maximum=100, step=1, value=20)
103
+ seed = gr.Number(label="Semilla", value=-1)
104
+
105
+ btn = gr.Button("Generar")
106
+ 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)
107
+ demo.launch()