Spaces:
Running
Running
salomonsky
commited on
Commit
•
1903294
1
Parent(s):
acffa8b
Update app.py
Browse files
app.py
CHANGED
@@ -68,18 +68,18 @@ def swap_faces(source_image, source_face_index, destination_image, destination_f
|
|
68 |
result = swapper.get(destination_image, res_face, source_face, paste_back=True)
|
69 |
return result
|
70 |
|
71 |
-
|
72 |
if seed == -1:
|
73 |
seed = random.randint(0, MAX_SEED)
|
74 |
-
image =
|
75 |
return image, seed
|
76 |
|
77 |
-
|
78 |
images = []
|
79 |
try:
|
80 |
for idx, prompt in enumerate(prompts):
|
81 |
seed = random.randint(0, MAX_SEED)
|
82 |
-
image, seed =
|
83 |
image_path = save_image(image, f"generated_image_{seed}.jpg")
|
84 |
if image_path:
|
85 |
st.success(f"Imagen {idx + 1} generada")
|
@@ -88,43 +88,7 @@ async def gen(prompts, width, height, model_name, num_variants=1, use_enhanced=T
|
|
88 |
st.error(f"Error al generar imágenes: {e}")
|
89 |
return images
|
90 |
|
91 |
-
def
|
92 |
-
return list(DATA_PATH.glob("*.jpg"))
|
93 |
-
|
94 |
-
def display_gallery():
|
95 |
-
st.header("Galería de Imágenes Guardadas")
|
96 |
-
images = list_saved_images()
|
97 |
-
if images:
|
98 |
-
cols = st.columns(8)
|
99 |
-
for i, image_file in enumerate(images):
|
100 |
-
with cols[i % 8]:
|
101 |
-
st.image(str(image_file), caption=image_file.name, use_column_width=True)
|
102 |
-
prompt = get_prompt_for_image(image_file.name)
|
103 |
-
st.write(prompt[:300])
|
104 |
-
|
105 |
-
if st.button(f"FaceSwap", key=f"select_{i}_{image_file.name}"):
|
106 |
-
st.session_state['generated_image_path'] = str(image_file)
|
107 |
-
st.success("Imagen seleccionada")
|
108 |
-
|
109 |
-
if st.button(f"Borrar", key=f"delete_{i}_{image_file.name}"):
|
110 |
-
if os.path.exists(image_file):
|
111 |
-
os.remove(image_file)
|
112 |
-
st.success("Imagen borrada")
|
113 |
-
display_gallery()
|
114 |
-
else:
|
115 |
-
st.warning("La imagen no existe.")
|
116 |
-
else:
|
117 |
-
st.info("No hay imágenes guardadas.")
|
118 |
-
|
119 |
-
def save_prompt(prompt):
|
120 |
-
with open(DATA_PATH / "prompts.txt", "a") as f:
|
121 |
-
f.write(prompt + "\n")
|
122 |
-
st.success("Prompt guardado.")
|
123 |
-
|
124 |
-
def run_async(func, *args):
|
125 |
-
return asyncio.run(func(*args))
|
126 |
-
|
127 |
-
async def improve_prompt(prompt):
|
128 |
try:
|
129 |
instructions = [
|
130 |
"With my idea create a vibrant description for a detailed txt2img prompt, 300 characters max.",
|
@@ -133,8 +97,7 @@ async def improve_prompt(prompt):
|
|
133 |
"With my idea describe a photorealistic with illumination txt2img prompt in English, 300 characters max.",
|
134 |
"With my idea give a realistic and elegant txt2img prompt in English, 300 characters max.",
|
135 |
"With my idea conform a visually dynamic and surreal txt2img prompt in English, 300 characters max.",
|
136 |
-
"With my idea realize an artistic and cinematic txt2img prompt in English, 300 characters max."
|
137 |
-
"With my idea make a narrative and immersive txt2img prompt in English, 300 characters max."
|
138 |
]
|
139 |
instruction = random.choice(instructions)
|
140 |
formatted_prompt = f"{prompt}: {instruction}"
|
@@ -143,58 +106,35 @@ async def improve_prompt(prompt):
|
|
143 |
except Exception as e:
|
144 |
return f"Error mejorando el prompt: {e}"
|
145 |
|
146 |
-
|
147 |
prompts = set()
|
148 |
while len(prompts) < num_variants:
|
149 |
if use_enhanced:
|
150 |
-
enhanced_prompt =
|
151 |
prompts.add(enhanced_prompt)
|
152 |
else:
|
153 |
prompts.add(prompt)
|
154 |
return list(prompts)
|
155 |
|
156 |
-
def
|
157 |
-
prompts = {}
|
158 |
try:
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
prompts[image_name] = line.split(": ", 1)[1].strip()
|
163 |
-
except FileNotFoundError:
|
164 |
-
return "No hay prompt asociado."
|
165 |
-
|
166 |
-
return prompts.get(image_name, "No hay prompt asociado.")
|
167 |
-
|
168 |
-
def login_form():
|
169 |
-
st.title("Iniciar Sesión")
|
170 |
-
username = st.text_input("Usuario", value="admin")
|
171 |
-
password = st.text_input("Contraseña", value="flux3x", type="password")
|
172 |
-
if st.button("Iniciar Sesión"):
|
173 |
-
if authenticate_user(username, password):
|
174 |
-
st.success("Autenticación exitosa.")
|
175 |
-
st.session_state['authenticated'] = True
|
176 |
-
else:
|
177 |
-
st.error("Credenciales incorrectas. Intenta de nuevo.")
|
178 |
-
|
179 |
-
def save_image(image, filename):
|
180 |
-
try:
|
181 |
-
image_path = DATA_PATH / filename
|
182 |
-
image.save(image_path)
|
183 |
-
return image_path
|
184 |
except Exception as e:
|
185 |
-
st.error(f"Error al
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
|
197 |
-
|
198 |
st.set_page_config(layout="wide")
|
199 |
|
200 |
if 'authenticated' not in st.session_state or not st.session_state['authenticated']:
|
@@ -202,29 +142,33 @@ async def main():
|
|
202 |
return
|
203 |
|
204 |
st.title("Flux +Upscale +Prompt Enhancer +FaceSwap")
|
|
|
|
|
|
|
|
|
205 |
generated_image_path = st.session_state.get('generated_image_path')
|
206 |
-
st.header("Generador de Imágenes")
|
207 |
prompt = st.sidebar.text_area("Descripción de la imagen", height=150, max_chars=500)
|
208 |
format_option = st.sidebar.selectbox("Formato", ["9:16", "16:9"])
|
209 |
model_option = st.sidebar.selectbox("Modelo", ["black-forest-labs/FLUX.1-schnell", "black-forest-labs/FLUX.1-dev", "enhanceaiteam/Flux-Uncensored-V2", "enhanceaiteam/Flux-Uncensored"])
|
210 |
prompt_checkbox = st.sidebar.checkbox("Prompt Enhancer")
|
211 |
upscale_checkbox = st.sidebar.checkbox("Escalar imagen")
|
212 |
-
width, height = (360, 640) if format_option == "9:16" else (640, 360) if format_option == "16:9" else (640, 640)
|
213 |
|
|
|
214 |
num_variants = st.sidebar.slider("Número de imágenes a generar", 1, 8, 1) if prompt_checkbox else 1
|
215 |
|
216 |
if prompt_checkbox:
|
217 |
with st.spinner("Generando prompts mejorados..."):
|
218 |
-
prompts =
|
219 |
else:
|
220 |
prompts = [prompt]
|
221 |
|
222 |
-
|
|
|
223 |
|
224 |
if st.sidebar.button("Generar Imágenes"):
|
225 |
with st.spinner("Generando imágenes..."):
|
226 |
try:
|
227 |
-
results =
|
228 |
st.session_state['generated_image_paths'] = results
|
229 |
for result in results:
|
230 |
st.image(result, caption="Imagen Generada")
|
@@ -274,4 +218,4 @@ async def main():
|
|
274 |
display_gallery()
|
275 |
|
276 |
if __name__ == "__main__":
|
277 |
-
|
|
|
68 |
result = swapper.get(destination_image, res_face, source_face, paste_back=True)
|
69 |
return result
|
70 |
|
71 |
+
def generate_image(prompt, width, height, seed, model_name):
|
72 |
if seed == -1:
|
73 |
seed = random.randint(0, MAX_SEED)
|
74 |
+
image = client.text_to_image(prompt=prompt, height=height, width=width, model=model_name)
|
75 |
return image, seed
|
76 |
|
77 |
+
def gen(prompts, width, height, model_name, num_variants=1, use_enhanced=True):
|
78 |
images = []
|
79 |
try:
|
80 |
for idx, prompt in enumerate(prompts):
|
81 |
seed = random.randint(0, MAX_SEED)
|
82 |
+
image, seed = generate_image(prompt, width, height, seed, model_name)
|
83 |
image_path = save_image(image, f"generated_image_{seed}.jpg")
|
84 |
if image_path:
|
85 |
st.success(f"Imagen {idx + 1} generada")
|
|
|
88 |
st.error(f"Error al generar imágenes: {e}")
|
89 |
return images
|
90 |
|
91 |
+
def improve_prompt(prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
try:
|
93 |
instructions = [
|
94 |
"With my idea create a vibrant description for a detailed txt2img prompt, 300 characters max.",
|
|
|
97 |
"With my idea describe a photorealistic with illumination txt2img prompt in English, 300 characters max.",
|
98 |
"With my idea give a realistic and elegant txt2img prompt in English, 300 characters max.",
|
99 |
"With my idea conform a visually dynamic and surreal txt2img prompt in English, 300 characters max.",
|
100 |
+
"With my idea realize an artistic and cinematic txt2img prompt in English, 300 characters max."
|
|
|
101 |
]
|
102 |
instruction = random.choice(instructions)
|
103 |
formatted_prompt = f"{prompt}: {instruction}"
|
|
|
106 |
except Exception as e:
|
107 |
return f"Error mejorando el prompt: {e}"
|
108 |
|
109 |
+
def generate_variations(prompt, num_variants, use_enhanced):
|
110 |
prompts = set()
|
111 |
while len(prompts) < num_variants:
|
112 |
if use_enhanced:
|
113 |
+
enhanced_prompt = improve_prompt(prompt)
|
114 |
prompts.add(enhanced_prompt)
|
115 |
else:
|
116 |
prompts.add(prompt)
|
117 |
return list(prompts)
|
118 |
|
119 |
+
def delete_all_images():
|
|
|
120 |
try:
|
121 |
+
for image_file in list_saved_images():
|
122 |
+
os.remove(image_file)
|
123 |
+
st.success("Todas las imágenes han sido eliminadas.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
except Exception as e:
|
125 |
+
st.error(f"Error al eliminar las imágenes: {e}")
|
126 |
+
|
127 |
+
def upload_images_to_gallery():
|
128 |
+
uploaded_images = st.sidebar.file_uploader("Sube imágenes a la galería", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
|
129 |
+
if uploaded_images:
|
130 |
+
for uploaded_image in uploaded_images:
|
131 |
+
image = Image.open(uploaded_image)
|
132 |
+
image_path = save_image(image, f"{uploaded_image.name}")
|
133 |
+
if image_path:
|
134 |
+
save_prompt("uploaded by user")
|
135 |
+
st.sidebar.success(f"Imagen subida: {image_path}")
|
136 |
|
137 |
+
def main():
|
138 |
st.set_page_config(layout="wide")
|
139 |
|
140 |
if 'authenticated' not in st.session_state or not st.session_state['authenticated']:
|
|
|
142 |
return
|
143 |
|
144 |
st.title("Flux +Upscale +Prompt Enhancer +FaceSwap")
|
145 |
+
|
146 |
+
if st.sidebar.button("Borrar todas las imágenes"):
|
147 |
+
delete_all_images()
|
148 |
+
|
149 |
generated_image_path = st.session_state.get('generated_image_path')
|
|
|
150 |
prompt = st.sidebar.text_area("Descripción de la imagen", height=150, max_chars=500)
|
151 |
format_option = st.sidebar.selectbox("Formato", ["9:16", "16:9"])
|
152 |
model_option = st.sidebar.selectbox("Modelo", ["black-forest-labs/FLUX.1-schnell", "black-forest-labs/FLUX.1-dev", "enhanceaiteam/Flux-Uncensored-V2", "enhanceaiteam/Flux-Uncensored"])
|
153 |
prompt_checkbox = st.sidebar.checkbox("Prompt Enhancer")
|
154 |
upscale_checkbox = st.sidebar.checkbox("Escalar imagen")
|
|
|
155 |
|
156 |
+
width, height = (360, 640) if format_option == "9:16" else (640, 360) if format_option == "16:9" else (640, 640)
|
157 |
num_variants = st.sidebar.slider("Número de imágenes a generar", 1, 8, 1) if prompt_checkbox else 1
|
158 |
|
159 |
if prompt_checkbox:
|
160 |
with st.spinner("Generando prompts mejorados..."):
|
161 |
+
prompts = generate_variations(prompt, num_variants, True)
|
162 |
else:
|
163 |
prompts = [prompt]
|
164 |
|
165 |
+
# Subida múltiple de imágenes
|
166 |
+
upload_images_to_gallery()
|
167 |
|
168 |
if st.sidebar.button("Generar Imágenes"):
|
169 |
with st.spinner("Generando imágenes..."):
|
170 |
try:
|
171 |
+
results = gen(prompts, width, height, model_option, num_variants, prompt_checkbox)
|
172 |
st.session_state['generated_image_paths'] = results
|
173 |
for result in results:
|
174 |
st.image(result, caption="Imagen Generada")
|
|
|
218 |
display_gallery()
|
219 |
|
220 |
if __name__ == "__main__":
|
221 |
+
main()
|