salomonsky commited on
Commit
4816388
1 Parent(s): c8e026c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -71
app.py CHANGED
@@ -1,24 +1,23 @@
1
  import os
2
- import gradio as gr
3
  import numpy as np
4
  import random
5
  from pathlib import Path
6
  from PIL import Image
7
- from huggingface_hub import AsyncInferenceClient, InferenceClient
 
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
  client = AsyncInferenceClient()
15
  llm_client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
16
-
17
  DATA_PATH = Path("./data")
18
- DATA_PATH.mkdir(exist_ok=True)
19
 
20
  def enable_lora(lora_add, basemodel):
21
- return basemodel if not lora_add else lora_add
22
 
23
  async def generate_image(combined_prompt, model, width, height, scales, steps, seed):
24
  try:
@@ -56,91 +55,106 @@ async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_fac
56
  seed = random.randint(0, MAX_SEED)
57
  seed = int(seed)
58
 
 
 
59
  image, seed = await generate_image(combined_prompt, model, width, height, scales, steps, seed)
60
-
 
61
  if isinstance(image, str) and image.startswith("Error"):
62
- return [image, None]
 
63
 
64
  image_path = DATA_PATH / f"image_{seed}.jpg"
65
  image.save(image_path, format="JPEG")
 
 
 
 
66
 
67
  if process_upscale:
68
  upscale_image_path = get_upscale_finegrain(combined_prompt, image_path, upscale_factor)
69
  if upscale_image_path:
70
  upscale_image = Image.open(upscale_image_path)
71
  upscale_image.save(DATA_PATH / f"upscale_image_{seed}.jpg", format="JPEG")
72
- return [image_path, DATA_PATH / f"upscale_image_{seed}.jpg"]
 
 
73
  else:
74
- return [image_path, image_path]
 
75
  else:
76
- return [image_path, image_path]
77
-
 
78
  async def improve_prompt(prompt):
79
  try:
80
- instruction = ("With this idea, describe in English a detailed img2vid prompt in a single paragraph of up to 200 characters maximum, developing atmosphere, characters, lighting, and cameras.")
81
  formatted_prompt = f"{prompt}: {instruction}"
82
  response = llm_client.text_generation(formatted_prompt, max_new_tokens=200)
83
  improved_text = response['generated_text'].strip() if 'generated_text' in response else response.strip()
84
-
85
  return improved_text
86
  except Exception as e:
87
  return f"Error mejorando el prompt: {e}"
88
 
89
  def get_storage():
90
- files = [
91
- {
92
- "name": str(file.resolve()),
93
- "size": file.stat().st_size,
94
- }
95
- for file in DATA_PATH.glob("*.jpg")
96
- if file.is_file()
97
- ]
98
- usage = sum([f['size'] for f in files])
99
- return [file["name"] for file in files], f"Uso total: {usage/(1024.0 ** 3):.3f}GB"
100
-
101
- css = """#col-container{ margin: 0 auto; max-width: 1024px;}"""
102
-
103
- with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
104
- with gr.Column(elem_id="col-container"):
105
- with gr.Row():
106
- with gr.Column(scale=3):
107
- output_res = ImageSlider(label="Generadas / Escaladas")
108
- with gr.Column(scale=2):
109
- prompt = gr.Textbox(label="Descripción de imagen")
110
- basemodel_choice = gr.Dropdown(
111
- label="Modelo",
112
- choices=["black-forest-labs/FLUX.1-schnell", "black-forest-labs/FLUX.1-DEV"],
113
- value="black-forest-labs/FLUX.1-schnell"
114
- )
115
- lora_model_choice = gr.Dropdown(
116
- label="LORA Realismo",
117
- choices=["Shakker-Labs/FLUX.1-dev-LoRA-add-details", "XLabs-AI/flux-RealismLora"],
118
- value="XLabs-AI/flux-RealismLora"
119
- )
120
- with gr.Row():
121
- process_lora = gr.Checkbox(label="Procesar LORA")
122
- process_upscale = gr.Checkbox(label="Procesar Escalador")
123
- improved_prompt = gr.Textbox(label="Prompt Mejorado", interactive=False)
124
- improve_btn = gr.Button("Mejorar prompt")
125
- improve_btn.click(fn=improve_prompt, inputs=[prompt], outputs=improved_prompt)
126
- with gr.Accordion(label="Opciones Avanzadas", open=False):
127
- width = gr.Slider(label="Ancho", minimum=512, maximum=1280, step=8, value=1280)
128
- height = gr.Slider(label="Alto", minimum=512, maximum=1280, step=8, value=768)
129
- upscale_factor = gr.Radio(label="Factor de Escala", choices=[2, 4, 8], value=2)
130
- scales = gr.Slider(label="Escalado", minimum=1, maximum=20, step=1, value=10)
131
- steps = gr.Slider(label="Pasos", minimum=1, maximum=100, step=1, value=20)
132
- seed = gr.Number(label="Semilla", value=-1)
133
- btn = gr.Button("Generar")
134
- btn.click(
135
- fn=gen,
136
- inputs=[prompt, basemodel_choice, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model_choice, process_lora],
137
- outputs=output_res
138
- )
139
- with gr.Row():
140
- with gr.Column():
141
- file_list = gr.Gallery(label="Imágenes Guardadas")
142
- storage_info = gr.Text(label="Uso de Almacenamiento")
143
- refresh_btn = gr.Button("Actualizar Galería")
144
- refresh_btn.click(fn=get_storage, inputs=None, outputs=[file_list, storage_info])
145
-
146
- demo.launch(allowed_paths=[str(DATA_PATH)])
 
 
 
 
 
1
  import os
 
2
  import numpy as np
3
  import random
4
  from pathlib import Path
5
  from PIL import Image
6
+ import streamlit as st
7
+ from huggingface_hub import InferenceClient, AsyncInferenceClient
8
  from gradio_client import Client, handle_file
9
+ import asyncio
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
  client = AsyncInferenceClient()
15
  llm_client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
 
16
  DATA_PATH = Path("./data")
17
+ DATA_PATH.mkdir(exist_ok=True)
18
 
19
  def enable_lora(lora_add, basemodel):
20
+ return lora_add if lora_add else basemodel
21
 
22
  async def generate_image(combined_prompt, model, width, height, scales, steps, seed):
23
  try:
 
55
  seed = random.randint(0, MAX_SEED)
56
  seed = int(seed)
57
 
58
+ progress_bar = st.progress(0)
59
+ progress_bar.progress(10)
60
  image, seed = await generate_image(combined_prompt, model, width, height, scales, steps, seed)
61
+ progress_bar.progress(50)
62
+
63
  if isinstance(image, str) and image.startswith("Error"):
64
+ progress_bar.empty()
65
+ return [image, None, combined_prompt]
66
 
67
  image_path = DATA_PATH / f"image_{seed}.jpg"
68
  image.save(image_path, format="JPEG")
69
+
70
+ prompt_file_path = DATA_PATH / f"prompt_{seed}.txt"
71
+ with open(prompt_file_path, "w") as prompt_file:
72
+ prompt_file.write(combined_prompt)
73
 
74
  if process_upscale:
75
  upscale_image_path = get_upscale_finegrain(combined_prompt, image_path, upscale_factor)
76
  if upscale_image_path:
77
  upscale_image = Image.open(upscale_image_path)
78
  upscale_image.save(DATA_PATH / f"upscale_image_{seed}.jpg", format="JPEG")
79
+ progress_bar.progress(100)
80
+ image_path.unlink()
81
+ return [str(DATA_PATH / f"upscale_image_{seed}.jpg"), str(prompt_file_path)]
82
  else:
83
+ progress_bar.empty()
84
+ return [str(image_path), str(prompt_file_path)]
85
  else:
86
+ progress_bar.progress(100)
87
+ return [str(image_path), str(prompt_file_path)]
88
+
89
  async def improve_prompt(prompt):
90
  try:
91
+ instruction = ("With this idea, describe in English a detailed txt2img prompt in a single paragraph of up to 200 characters maximum, developing atmosphere, characters, lighting, and cameras.")
92
  formatted_prompt = f"{prompt}: {instruction}"
93
  response = llm_client.text_generation(formatted_prompt, max_new_tokens=200)
94
  improved_text = response['generated_text'].strip() if 'generated_text' in response else response.strip()
 
95
  return improved_text
96
  except Exception as e:
97
  return f"Error mejorando el prompt: {e}"
98
 
99
  def get_storage():
100
+ files = list(DATA_PATH.glob("*.jpg"))
101
+ usage = sum(file.stat().st_size for file in files)
102
+ return files, f"Uso total: {usage/(1024.0 ** 3):.3f}GB"
103
+
104
+ def get_prompts():
105
+ prompt_files = list(DATA_PATH.glob("*.txt"))
106
+ return {file.stem.replace("prompt_", ""): file for file in prompt_files}
107
+
108
+ def run_gen(prompt, basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora):
109
+ loop = asyncio.new_event_loop()
110
+ asyncio.set_event_loop(loop)
111
+ return loop.run_until_complete(gen(prompt, basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora))
112
+
113
+ st.set_page_config(layout="wide")
114
+ st.title("Generador de Imágenes FLUX y Escalador con IA")
115
+
116
+ prompt = st.sidebar.text_input("Descripción de la imagen")
117
+ basemodel = st.sidebar.selectbox("Modelo Base", ["black-forest-labs/FLUX.1-schnell", "black-forest-labs/FLUX.1-DEV"])
118
+ lora_model = st.sidebar.selectbox("LORA Realismo", ["Shakker-Labs/FLUX.1-dev-LoRA-add-details", "XLabs-AI/flux-RealismLora"])
119
+ format_option = st.sidebar.selectbox("Formato", ["9:16", "16:9"])
120
+ process_lora = st.sidebar.checkbox("Procesar LORA")
121
+ process_upscale = st.sidebar.checkbox("Procesar Escalador")
122
+
123
+ if format_option == "9:16":
124
+ width = st.sidebar.slider("Ancho", 512, 720, 720, step=8)
125
+ height = st.sidebar.slider("Alto", 912, 1280, 1280, step=8)
126
+ else:
127
+ width = st.sidebar.slider("Ancho", 512, 1280, 1280, step=8)
128
+ height = st.sidebar.slider("Alto", 512, 720, 720, step=8)
129
+
130
+ upscale_factor = st.sidebar.selectbox("Factor de Escala", [2, 4, 8], index=0)
131
+ scales = st.sidebar.slider("Escalado", 1, 20, 10)
132
+ steps = st.sidebar.slider("Pasos", 1, 100, 20)
133
+ seed = st.sidebar.number_input("Semilla", value=-1)
134
+
135
+ if st.sidebar.button("Mejorar prompt"):
136
+ improved_prompt = asyncio.run(improve_prompt(prompt))
137
+ st.session_state.improved_prompt = improved_prompt
138
+ st.write(f"{improved_prompt}")
139
+
140
+ if st.sidebar.button("Generar Imagen"):
141
+ with st.spinner("Generando imagen..."):
142
+ image_paths, prompt_file = run_gen(st.session_state.get('improved_prompt', prompt), basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora)
143
+
144
+ if image_paths and isinstance(image_paths[0], str) and Path(image_paths[0]).exists():
145
+ st.image(image_paths[0], caption="Imagen Generada")
146
+ prompt_text = Path(prompt_file).read_text() if prompt_file else "No disponible"
147
+ st.write(f"Prompt utilizado: {prompt_text}")
148
+
149
+ files, usage = get_storage()
150
+ st.text(usage)
151
+
152
+ cols = st.columns(6)
153
+ prompts = get_prompts()
154
+ for idx, file in enumerate(files):
155
+ with cols[idx % 6]:
156
+ image = Image.open(file)
157
+ prompt_file = prompts.get(file.stem.replace("image_", ""), None)
158
+ prompt_text = Path(prompt_file).read_text() if prompt_file else "No disponible"
159
+ st.image(image, caption=f"Imagen {idx+1}")
160
+ st.write(f"Prompt: {prompt_text}")