salomonsky commited on
Commit
4ec4b86
1 Parent(s): ba2e19b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -23
app.py CHANGED
@@ -12,43 +12,58 @@ from gradio_client import Client, handle_file
12
  from huggingface_hub import login
13
  from gradio_imageslider import ImageSlider
14
 
15
- translator = Translator()
 
16
  HF_TOKEN = os.environ.get("HF_TOKEN")
17
  HF_TOKEN_UPSCALER = os.environ.get("HF_TOKEN_UPSCALER")
18
- MAX_SEED = np.iinfo(np.int32).max
19
- CSS = "footer { visibility: hidden; }"
20
- JS = "function () { gradioURL = window.location.href; if (!gradioURL.endsWith('?__theme=dark')) { window.location.replace(gradioURL + '?__theme=dark'); } }"
21
 
22
- def enable_lora(lora_add, basemodel):
 
 
23
  return basemodel if not lora_add else lora_add
24
 
25
  async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
26
- if seed == -1:
27
- seed = random.randint(0, MAX_SEED)
28
- seed = int(seed)
29
- text = str(translator.translate(prompt, 'English')) + "," + lora_word
30
- client = AsyncInferenceClient()
31
- image = await client.text_to_image(prompt=text, height=height, width=width, guidance_scale=scales, num_inference_steps=steps, model=model)
32
- return image, seed
 
 
 
 
 
33
 
34
  async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora):
 
35
  model = enable_lora(lora_model, basemodel) if process_lora else basemodel
36
  image, seed = await generate_image(prompt, model, "", width, height, scales, steps, seed)
37
- image_path = "temp_image.jpg"
38
- image.save(image_path, format="JPEG")
 
 
 
39
 
40
  if process_upscale:
41
  upscale_image = get_upscale_finegrain(prompt, image_path, upscale_factor)
42
  else:
43
  upscale_image = image_path
44
-
45
  return [image_path, upscale_image]
46
 
47
  def get_upscale_finegrain(prompt, img_path, upscale_factor):
48
- client = Client("finegrain/finegrain-image-enhancer", hf_token=HF_TOKEN_UPSCALER)
49
- 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")
50
- return result[1]
 
 
 
 
 
51
 
 
52
  css = """
53
  #col-container{
54
  margin: 0 auto;
@@ -56,13 +71,13 @@ css = """
56
  }
57
  """
58
 
59
- with gr.Blocks(css=CSS, js=JS, theme="Nymbo/Nymbo_Theme") as demo:
60
  with gr.Column(elem_id="col-container"):
61
  gr.Markdown("Flux Upscaled +LORA")
62
  with gr.Row():
63
- with gr.Column(scale=1.5):
64
  output_res = ImageSlider(label="Flux / Upscaled")
65
- with gr.Column(scale=0.8):
66
  prompt = gr.Textbox(label="Prompt")
67
  basemodel_choice = gr.Dropdown(label="Base Model", choices=["black-forest-labs/FLUX.1-schnell", "black-forest-labs/FLUX.1-DEV"], value="black-forest-labs/FLUX.1-schnell")
68
  lora_model_choice = gr.Dropdown(label="LORA Model", choices=["Shakker-Labs/FLUX.1-dev-LoRA-add-details", "XLabs-AI/flux-RealismLora"], value="XLabs-AI/flux-RealismLora")
@@ -76,7 +91,7 @@ with gr.Blocks(css=CSS, js=JS, theme="Nymbo/Nymbo_Theme") as demo:
76
  scales = gr.Slider(label="Guidance", minimum=3.5, maximum=7, step=0.1, value=3.5)
77
  steps = gr.Slider(label="Steps", minimum=1, maximum=100, step=1, value=24)
78
  seed = gr.Slider(label="Seeds", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
79
-
80
  submit_btn = gr.Button("Submit", scale=1)
81
  submit_btn.click(
82
  fn=lambda: None,
@@ -87,4 +102,6 @@ with gr.Blocks(css=CSS, js=JS, theme="Nymbo/Nymbo_Theme") as demo:
87
  fn=gen,
88
  inputs=[prompt, basemodel_choice, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model_choice, process_lora],
89
  outputs=[output_res]
90
- )
 
 
 
12
  from huggingface_hub import login
13
  from gradio_imageslider import ImageSlider
14
 
15
+ # Constantes
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
+ # Funciones
21
+ def enable_lora(lora_add, basemodel):
22
+ """Habilita o deshabilita el modelo LORA"""
23
  return basemodel if not lora_add else lora_add
24
 
25
  async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
26
+ """Genera una imagen a partir de un texto"""
27
+ try:
28
+ if seed == -1:
29
+ seed = random.randint(0, MAX_SEED)
30
+ seed = int(seed)
31
+ text = str(Translator().translate(prompt, 'English')) + "," + lora_word
32
+ client = AsyncInferenceClient()
33
+ image = await client.text_to_image(prompt=text, height=height, width=width, guidance_scale=scales, num_inference_steps=steps, model=model)
34
+ return image, seed
35
+ except Exception as e:
36
+ print(f"Error generating image: {e}")
37
+ return None, None
38
 
39
  async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora):
40
+ """Genera una imagen y la ajusta"""
41
  model = enable_lora(lora_model, basemodel) if process_lora else basemodel
42
  image, seed = await generate_image(prompt, model, "", width, height, scales, steps, seed)
43
+ if image is None:
44
+ return [None, None]
45
+
46
+ image_path = "temp_image.jpg"
47
+ image.save(image_path, format="JPEG")
48
 
49
  if process_upscale:
50
  upscale_image = get_upscale_finegrain(prompt, image_path, upscale_factor)
51
  else:
52
  upscale_image = image_path
53
+
54
  return [image_path, upscale_image]
55
 
56
  def get_upscale_finegrain(prompt, img_path, upscale_factor):
57
+ """Ajusta la imagen"""
58
+ try:
59
+ client = Client("finegrain/finegrain-image-enhancer", hf_token=HF_TOKEN_UPSCALER)
60
+ 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")
61
+ return result[1]
62
+ except Exception as e:
63
+ print(f"Error upscale image: {e}")
64
+ return None
65
 
66
+ # Interfaz gráfica
67
  css = """
68
  #col-container{
69
  margin: 0 auto;
 
71
  }
72
  """
73
 
74
+ with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
75
  with gr.Column(elem_id="col-container"):
76
  gr.Markdown("Flux Upscaled +LORA")
77
  with gr.Row():
78
+ with gr.Column(scale=3):
79
  output_res = ImageSlider(label="Flux / Upscaled")
80
+ with gr.Column(scale=2):
81
  prompt = gr.Textbox(label="Prompt")
82
  basemodel_choice = gr.Dropdown(label="Base Model", choices=["black-forest-labs/FLUX.1-schnell", "black-forest-labs/FLUX.1-DEV"], value="black-forest-labs/FLUX.1-schnell")
83
  lora_model_choice = gr.Dropdown(label="LORA Model", choices=["Shakker-Labs/FLUX.1-dev-LoRA-add-details", "XLabs-AI/flux-RealismLora"], value="XLabs-AI/flux-RealismLora")
 
91
  scales = gr.Slider(label="Guidance", minimum=3.5, maximum=7, step=0.1, value=3.5)
92
  steps = gr.Slider(label="Steps", minimum=1, maximum=100, step=1, value=24)
93
  seed = gr.Slider(label="Seeds", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
94
+
95
  submit_btn = gr.Button("Submit", scale=1)
96
  submit_btn.click(
97
  fn=lambda: None,
 
102
  fn=gen,
103
  inputs=[prompt, basemodel_choice, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model_choice, process_lora],
104
  outputs=[output_res]
105
+ )
106
+
107
+ demo.launch()