Spaces:
Running
Running
salomonsky
commited on
Commit
•
4816388
1
Parent(s):
c8e026c
Update app.py
Browse files
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 |
-
|
|
|
8 |
from gradio_client import Client, handle_file
|
9 |
-
|
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
|
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 |
-
|
|
|
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 |
-
|
|
|
|
|
73 |
else:
|
74 |
-
|
|
|
75 |
else:
|
76 |
-
|
77 |
-
|
|
|
78 |
async def improve_prompt(prompt):
|
79 |
try:
|
80 |
-
instruction = ("With this idea, describe in English a detailed
|
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 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
|
|
|
|
|
|
|
|
|
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}")
|