salomonsky commited on
Commit
757da8f
1 Parent(s): 52a0784

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -11
app.py CHANGED
@@ -2,20 +2,18 @@ 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
  client = AsyncInferenceClient()
 
19
 
20
  def enable_lora(lora_add, basemodel):
21
  return basemodel if not lora_add else lora_add
@@ -25,11 +23,10 @@ async def generate_image(prompt, model, lora_word, width, height, scales, steps,
25
  if seed == -1:
26
  seed = random.randint(0, MAX_SEED)
27
  seed = int(seed)
28
- text = str(Translator().translate(prompt, 'English')) + "," + lora_word
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 f"Error al generar imagen: {e}", None
34
 
35
  def get_upscale_finegrain(prompt, img_path, upscale_factor):
@@ -38,7 +35,6 @@ def get_upscale_finegrain(prompt, img_path, upscale_factor):
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):
@@ -58,11 +54,19 @@ async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_fac
58
  upscale_image.save("upscale_image.jpg", format="JPEG")
59
  return [image_path, "upscale_image.jpg"]
60
  else:
61
- print("Error: La ruta de la imagen escalada es None")
62
  return [image_path, image_path]
63
  else:
64
  return [image_path, image_path]
65
 
 
 
 
 
 
 
 
 
 
66
  css = """
67
  #col-container{ margin: 0 auto; max-width: 1024px;}
68
  """
@@ -79,6 +83,11 @@ with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
79
  process_lora = gr.Checkbox(label="Procesar LORA")
80
  process_upscale = gr.Checkbox(label="Procesar Escalador")
81
  upscale_factor = gr.Radio(label="Factor de Escala", choices=[2, 4, 8], value=2)
 
 
 
 
 
82
 
83
  with gr.Accordion(label="Opciones Avanzadas", open=False):
84
  width = gr.Slider(label="Ancho", minimum=512, maximum=1280, step=8, value=1280)
@@ -88,5 +97,5 @@ with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
88
  seed = gr.Number(label="Semilla", value=-1)
89
 
90
  btn = gr.Button("Generar")
91
- 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)
92
- demo.launch()
 
2
  import gradio as gr
3
  import numpy as np
4
  import random
5
+ from huggingface_hub import AsyncInferenceClient, InferenceClient
 
 
 
6
  import asyncio
7
  from PIL import Image
8
  from gradio_client import Client, handle_file
 
9
  from gradio_imageslider import ImageSlider
10
 
11
  MAX_SEED = np.iinfo(np.int32).max
12
  HF_TOKEN = os.environ.get("HF_TOKEN")
13
  HF_TOKEN_UPSCALER = os.environ.get("HF_TOKEN_UPSCALER")
14
+
15
  client = AsyncInferenceClient()
16
+ llm_client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
17
 
18
  def enable_lora(lora_add, basemodel):
19
  return basemodel if not lora_add else lora_add
 
23
  if seed == -1:
24
  seed = random.randint(0, MAX_SEED)
25
  seed = int(seed)
26
+ text = prompt + "," + lora_word
27
  image = await client.text_to_image(prompt=text, height=height, width=width, guidance_scale=scales, num_inference_steps=steps, model=model)
28
  return image, seed
29
  except Exception as e:
 
30
  return f"Error al generar imagen: {e}", None
31
 
32
  def get_upscale_finegrain(prompt, img_path, upscale_factor):
 
35
  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")
36
  return result[1]
37
  except Exception as e:
 
38
  return None
39
 
40
  async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora):
 
54
  upscale_image.save("upscale_image.jpg", format="JPEG")
55
  return [image_path, "upscale_image.jpg"]
56
  else:
 
57
  return [image_path, image_path]
58
  else:
59
  return [image_path, image_path]
60
 
61
+ def improve_prompt(prompt):
62
+ try:
63
+ instruction = "Mejora mi prompt para texto a imagen en inglés con estilo, cinematografía, cámaras, atmósfera e iluminación para la mejor calidad, de máximo 200 palabras."
64
+ formatted_prompt = f"{instruction}: {prompt}"
65
+ response = llm_client.text_generation(formatted_prompt, max_new_tokens=200)
66
+ return response.generated_text.strip()
67
+ except Exception as e:
68
+ return f"Error mejorando el prompt: {e}"
69
+
70
  css = """
71
  #col-container{ margin: 0 auto; max-width: 1024px;}
72
  """
 
83
  process_lora = gr.Checkbox(label="Procesar LORA")
84
  process_upscale = gr.Checkbox(label="Procesar Escalador")
85
  upscale_factor = gr.Radio(label="Factor de Escala", choices=[2, 4, 8], value=2)
86
+
87
+ improved_prompt = gr.Textbox(label="Prompt Mejorado", interactive=False)
88
+
89
+ improve_btn = gr.Button("Mejora mi prompt")
90
+ improve_btn.click(fn=improve_prompt, inputs=[prompt], outputs=improved_prompt)
91
 
92
  with gr.Accordion(label="Opciones Avanzadas", open=False):
93
  width = gr.Slider(label="Ancho", minimum=512, maximum=1280, step=8, value=1280)
 
97
  seed = gr.Number(label="Semilla", value=-1)
98
 
99
  btn = gr.Button("Generar")
100
+ btn.click(fn=gen, inputs=[improved_prompt, basemodel_choice, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model_choice, process_lora], outputs=output_res)
101
+ demo.launch()