salomonsky commited on
Commit
7809429
1 Parent(s): 0a48097

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -5
app.py CHANGED
@@ -12,14 +12,19 @@ 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
 
 
19
  def enable_lora(lora_add, basemodel):
 
20
  return basemodel if not lora_add else lora_add
21
 
 
22
  async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
 
23
  try:
24
  if seed == -1:
25
  seed = random.randint(0, MAX_SEED)
@@ -29,19 +34,23 @@ async def generate_image(prompt, model, lora_word, width, height, scales, steps,
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 generating image: {e}")
33
  return None, 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")
39
  return result[1]
40
  except Exception as e:
41
- print(f"Error upscale image: {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
  if image is None:
@@ -52,16 +61,22 @@ async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_fac
52
 
53
  if process_upscale:
54
  upscale_image_path = get_upscale_finegrain(prompt, image_path, upscale_factor)
55
- upscale_image = Image.open(upscale_image_path)
56
- upscale_image.save("upscale_image.jpg", format="JPEG")
57
- return [image_path, "upscale_image.jpg"]
 
 
 
 
58
  else:
59
  return [image_path, image_path]
60
 
 
61
  css = """
62
  #col-container{ margin: 0 auto; max-width: 1024px;}
63
  """
64
 
 
65
  with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
66
  with gr.Column(elem_id="col-container"):
67
  with gr.Row():
 
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)
 
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")
46
  return result[1]
47
  except Exception as e:
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:
 
61
 
62
  if process_upscale:
63
  upscale_image_path = get_upscale_finegrain(prompt, image_path, upscale_factor)
64
+ if upscale_image_path is not None:
65
+ upscale_image = Image.open(upscale_image_path)
66
+ upscale_image.save("upscale_image.jpg", format="JPEG")
67
+ return [image_path, "upscale_image.jpg"]
68
+ else:
69
+ print("Error: La ruta de la imagen escalada es None")
70
+ return [image_path, image_path]
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():