salomonsky commited on
Commit
68ef0f8
1 Parent(s): 2d91a5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -37
app.py CHANGED
@@ -22,17 +22,7 @@ if not os.path.exists('GFPGANv1.4.pth'):
22
 
23
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
24
  model_path = 'GFPGANv1.4.pth'
25
- gfpgan = GFPGANer(
26
- model_path=model_path,
27
- upscale_factor=4,
28
- arch='clean',
29
- channel_multiplier=2,
30
- model_name='GPFGAN',
31
- device=device
32
- )
33
-
34
- def enable_lora(lora_add, basemodel):
35
- return basemodel if not lora_add else lora_add
36
 
37
  async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
38
  try:
@@ -47,19 +37,19 @@ async def generate_image(prompt, model, lora_word, width, height, scales, steps,
47
  print(f"Error generating image: {e}")
48
  return None, None
49
 
50
- def get_upscale_finegrain(prompt, img_path, upscale_factor):
51
  try:
52
- client = Client("finegrain/finegrain-image-enhancer", hf_token=HF_TOKEN_UPSCALER)
53
- 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")
54
- return result[1]
55
  except Exception as e:
56
  print(f"Error upscale image: {e}")
57
  return None
58
 
59
- def get_upscale_gfpgan(prompt, img_path):
60
  try:
61
- img = gfpgan.enhance(img_path)
62
- return img
 
63
  except Exception as e:
64
  print(f"Error upscale image: {e}")
65
  return None
@@ -74,10 +64,10 @@ async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_fac
74
  image.save(image_path, format="JPEG")
75
 
76
  if process_upscale:
77
- if upscale_model == "FineGrain":
78
- upscale_image = get_upscale_finegrain(prompt, image_path, upscale_factor)
79
- elif upscale_model == "GPFGAN":
80
  upscale_image = get_upscale_gfpgan(prompt, image_path)
 
 
81
  upscale_image_path = "upscale_image.jpg"
82
  upscale_image.save(upscale_image_path, format="JPEG")
83
  return [image_path, upscale_image_path]
@@ -100,25 +90,20 @@ with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
100
  process_lora = gr.Checkbox(label="Procesar LORA")
101
  process_upscale = gr.Checkbox(label="Procesar Escalador")
102
  upscale_factor = gr.Radio(label="Factor de Escala", choices=[2, 4, 8], value=2)
103
- upscale_model = gr.Radio(label="Modelo de Escalado", choices=["FineGrain", "GPFGAN"], value="GPFGAN")
104
 
105
  with gr.Accordion(label="Opciones Avanzadas", open=False):
106
- width = gr.Slider(label="Ancho", minimum=512, maximum=1280, step=8, value=1280)
107
- height = gr.Slider(label="Alto", minimum=512, maximum=1280, step=8, value=768)
108
- scales = gr.Slider(label="Escalas", minimum=3.5, maximum=7, step=0.1, value=3.5)
109
- steps = gr.Slider(label="Pasos", minimum=1, maximum=100, step=1, value=24)
110
- seed = gr.Slider(label="Semillas", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
111
-
112
- submit_btn = gr.Button("Crear", scale=1)
113
- submit_btn.click(
114
- fn=lambda: None,
115
- inputs=None,
116
- outputs=[output_res],
117
- queue=False
118
- ).then(
119
  fn=gen,
120
- inputs=[prompt, basemodel_choice, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model_choice, process_lora, upscale_model],
121
- outputs=[output_res]
122
  )
123
 
124
  demo.launch()
 
22
 
23
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
24
  model_path = 'GFPGANv1.4.pth'
25
+ gfpgan = GFPGANer(model_path=model_path, upscale_factor=4, arch='clean', channel_multiplier=2, model_name='GPFGAN', device=device)
 
 
 
 
 
 
 
 
 
 
26
 
27
  async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
28
  try:
 
37
  print(f"Error generating image: {e}")
38
  return None, None
39
 
40
+ def get_upscale_gfpgan(prompt, img_path):
41
  try:
42
+ img = gfpgan.enhance(img_path)
43
+ return img
 
44
  except Exception as e:
45
  print(f"Error upscale image: {e}")
46
  return None
47
 
48
+ def get_upscale_finegrain(prompt, img_path, upscale_factor):
49
  try:
50
+ client = Client("finegrain/finegrain-image-enhancer", hf_token=HF_TOKEN_UPSCALER)
51
+ 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")
52
+ return result[1]
53
  except Exception as e:
54
  print(f"Error upscale image: {e}")
55
  return None
 
64
  image.save(image_path, format="JPEG")
65
 
66
  if process_upscale:
67
+ if upscale_model == "GPFGAN":
 
 
68
  upscale_image = get_upscale_gfpgan(prompt, image_path)
69
+ elif upscale_model == "Finegrain":
70
+ upscale_image = get_upscale_finegrain(prompt, image_path, upscale_factor)
71
  upscale_image_path = "upscale_image.jpg"
72
  upscale_image.save(upscale_image_path, format="JPEG")
73
  return [image_path, upscale_image_path]
 
90
  process_lora = gr.Checkbox(label="Procesar LORA")
91
  process_upscale = gr.Checkbox(label="Procesar Escalador")
92
  upscale_factor = gr.Radio(label="Factor de Escala", choices=[2, 4, 8], value=2)
93
+ upscale_model = gr.Radio(label="Modelo de Escalado", choices=["GPFGAN", "Finegrain"], value="GPFGAN")
94
 
95
  with gr.Accordion(label="Opciones Avanzadas", open=False):
96
+ width = gr.Slider(label="Ancho", minimum=512, maximum=1280, step=8, value=512)
97
+ height = gr.Slider(label="Alto", minimum=512, maximum=1280, step=8, value=512)
98
+ scales = gr.Slider(label="Escalado", minimum=1, maximum=20, step=1, value=10)
99
+ steps = gr.Slider(label="Pasos", minimum=1, maximum=100, step=1, value=20)
100
+ seed = gr.Number(label="Semilla", value=-1)
101
+
102
+ btn = gr.Button("Generar")
103
+ btn.click(
 
 
 
 
 
104
  fn=gen,
105
+ inputs=[prompt, basemodel_choice, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model_choice, process_lora, upscale_model,],
106
+ outputs=output_res,
107
  )
108
 
109
  demo.launch()