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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -15
app.py CHANGED
@@ -12,34 +12,27 @@ from gradio_client import Client, handle_file
12
  from huggingface_hub import login
13
  from gradio_imageslider import ImageSlider
14
 
15
-
16
  MAX_SEED = np.iinfo(np.int32).max
17
  HF_TOKEN = os.environ.get("HF_TOKEN")
18
  HF_TOKEN_UPSCALER = os.environ.get("HF_TOKEN_UPSCALER")
19
-
20
 
21
  def enable_lora(lora_add, basemodel):
22
- """Habilita o deshabilita LoRA según la opción seleccionada"""
23
  return basemodel if not lora_add else lora_add
24
 
25
-
26
  async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
27
- """Genera una imagen utilizando el modelo seleccionado"""
28
  try:
29
  if seed == -1:
30
  seed = random.randint(0, MAX_SEED)
31
  seed = int(seed)
32
  text = str(Translator().translate(prompt, 'English')) + "," + lora_word
33
- client = AsyncInferenceClient()
34
  image = await client.text_to_image(prompt=text, height=height, width=width, guidance_scale=scales, num_inference_steps=steps, model=model)
35
  return image, seed
36
  except Exception as e:
37
  print(f"Error generando imagen: {e}")
38
- return None, None
39
-
40
 
41
  def get_upscale_finegrain(prompt, img_path, upscale_factor):
42
- """Escala una imagen utilizando FineGrain"""
43
  try:
44
  client = Client("finegrain/finegrain-image-enhancer", hf_token=HF_TOKEN_UPSCALER)
45
  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")
@@ -48,13 +41,12 @@ def get_upscale_finegrain(prompt, img_path, upscale_factor):
48
  print(f"Error escalando imagen: {e}")
49
  return None
50
 
51
-
52
  async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora):
53
- """Función principal que genera y escala la imagen"""
54
  model = enable_lora(lora_model, basemodel) if process_lora else basemodel
55
  image, seed = await generate_image(prompt, model, "", width, height, scales, steps, seed)
 
56
  if image is None:
57
- return [None, None]
58
 
59
  image_path = "temp_image.jpg"
60
  image.save(image_path, format="JPEG")
@@ -71,12 +63,10 @@ async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_fac
71
  else:
72
  return [image_path, image_path]
73
 
74
-
75
  css = """
76
  #col-container{ margin: 0 auto; max-width: 1024px;}
77
  """
78
 
79
-
80
  with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
81
  with gr.Column(elem_id="col-container"):
82
  with gr.Row():
@@ -98,5 +88,5 @@ with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
98
  seed = gr.Number(label="Semilla", value=-1)
99
 
100
  btn = gr.Button("Generar")
101
- 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,)
102
  demo.launch()
 
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
22
 
 
23
  async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
 
24
  try:
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):
 
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")
 
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
+
48
  if image is None:
49
+ return [f"Error generando imagen con el modelo {model}", None]
50
 
51
  image_path = "temp_image.jpg"
52
  image.save(image_path, format="JPEG")
 
63
  else:
64
  return [image_path, image_path]
65
 
 
66
  css = """
67
  #col-container{ margin: 0 auto; max-width: 1024px;}
68
  """
69
 
 
70
  with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
71
  with gr.Column(elem_id="col-container"):
72
  with gr.Row():
 
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()