salomonsky commited on
Commit
f0f180b
1 Parent(s): 113dc2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -76
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(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()
 
 
 
 
 
 
 
 
 
 
 
 
 
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()