Spaces:
Running
Running
salomonsky
commited on
Commit
•
aec9996
1
Parent(s):
ab442ba
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,10 @@
|
|
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 |
-
import yaml
|
8 |
from huggingface_hub import AsyncInferenceClient
|
9 |
import asyncio
|
10 |
|
@@ -13,23 +13,15 @@ client = AsyncInferenceClient()
|
|
13 |
DATA_PATH = Path("./data")
|
14 |
DATA_PATH.mkdir(exist_ok=True)
|
15 |
|
16 |
-
# Cargar credenciales desde archivo config.yaml
|
17 |
-
try:
|
18 |
-
with open("config.yaml", "r") as file:
|
19 |
-
credentials = yaml.safe_load(file)
|
20 |
-
except Exception as e:
|
21 |
-
st.error(f"Error al cargar el archivo de configuración: {e}")
|
22 |
-
credentials = {"username": "", "password": ""} # Valores predeterminados
|
23 |
-
|
24 |
PREDEFINED_SEED = random.randint(0, MAX_SEED)
|
25 |
|
26 |
-
async def generate_image(prompt, width, height, seed):
|
27 |
try:
|
28 |
if seed == -1:
|
29 |
seed = PREDEFINED_SEED
|
30 |
seed = int(seed)
|
31 |
image = await client.text_to_image(
|
32 |
-
prompt=prompt, height=height, width=width, model=
|
33 |
)
|
34 |
return image, seed
|
35 |
except Exception as e:
|
@@ -45,11 +37,11 @@ def save_prompt(prompt_text, seed):
|
|
45 |
st.error(f"Error al guardar el prompt: {e}")
|
46 |
return None
|
47 |
|
48 |
-
async def gen(prompt, width, height):
|
49 |
combined_prompt = prompt
|
50 |
seed = PREDEFINED_SEED
|
51 |
progress_bar = st.progress(0)
|
52 |
-
image, seed = await generate_image(combined_prompt, width, height, seed)
|
53 |
progress_bar.progress(100)
|
54 |
|
55 |
if isinstance(image, str) and image.startswith("Error"):
|
@@ -80,31 +72,40 @@ def get_prompts():
|
|
80 |
prompt_files = [file for file in DATA_PATH.glob("*.txt") if file.is_file()]
|
81 |
return {file.stem.replace("prompt_", ""): file for file in prompt_files}
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
def main():
|
84 |
st.set_page_config(layout="wide")
|
85 |
-
st.title("
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
if not st.session_state.logged_in:
|
91 |
-
username = st.text_input("Usuario")
|
92 |
-
password = st.text_input("Contraseña", type="password")
|
93 |
-
if st.button("Iniciar Sesión"):
|
94 |
-
if username == credentials["username"] and password == credentials["password"]:
|
95 |
-
st.session_state.logged_in = True
|
96 |
-
st.success("Inicio de sesión exitoso.")
|
97 |
-
else:
|
98 |
-
st.error("Usuario o contraseña incorrectos.")
|
99 |
-
else:
|
100 |
prompt = st.sidebar.text_input("Descripción de la imagen", max_chars=500)
|
101 |
format_option = st.sidebar.selectbox("Formato", ["9:16", "16:9"])
|
|
|
102 |
|
103 |
width, height = (720, 1280) if format_option == "9:16" else (1280, 720)
|
104 |
|
105 |
if st.sidebar.button("Generar Imagen"):
|
106 |
with st.spinner("Generando imagen..."):
|
107 |
-
result = asyncio.run(gen(prompt, width, height))
|
108 |
image_paths = result[0]
|
109 |
prompt_file = result[1]
|
110 |
|
@@ -143,5 +144,13 @@ def main():
|
|
143 |
except Exception as e:
|
144 |
st.error(f"Error al borrar la imagen o prompt: {e}")
|
145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
if __name__ == "__main__":
|
147 |
main()
|
|
|
1 |
import os
|
2 |
+
import zipfile
|
3 |
import numpy as np
|
4 |
import random
|
5 |
from pathlib import Path
|
6 |
from PIL import Image
|
7 |
import streamlit as st
|
|
|
8 |
from huggingface_hub import AsyncInferenceClient
|
9 |
import asyncio
|
10 |
|
|
|
13 |
DATA_PATH = Path("./data")
|
14 |
DATA_PATH.mkdir(exist_ok=True)
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
PREDEFINED_SEED = random.randint(0, MAX_SEED)
|
17 |
|
18 |
+
async def generate_image(prompt, width, height, seed, model_name):
|
19 |
try:
|
20 |
if seed == -1:
|
21 |
seed = PREDEFINED_SEED
|
22 |
seed = int(seed)
|
23 |
image = await client.text_to_image(
|
24 |
+
prompt=prompt, height=height, width=width, model=model_name
|
25 |
)
|
26 |
return image, seed
|
27 |
except Exception as e:
|
|
|
37 |
st.error(f"Error al guardar el prompt: {e}")
|
38 |
return None
|
39 |
|
40 |
+
async def gen(prompt, width, height, model_name):
|
41 |
combined_prompt = prompt
|
42 |
seed = PREDEFINED_SEED
|
43 |
progress_bar = st.progress(0)
|
44 |
+
image, seed = await generate_image(combined_prompt, width, height, seed, model_name)
|
45 |
progress_bar.progress(100)
|
46 |
|
47 |
if isinstance(image, str) and image.startswith("Error"):
|
|
|
72 |
prompt_files = [file for file in DATA_PATH.glob("*.txt") if file.is_file()]
|
73 |
return {file.stem.replace("prompt_", ""): file for file in prompt_files}
|
74 |
|
75 |
+
def delete_all_images():
|
76 |
+
try:
|
77 |
+
files = [file for file in DATA_PATH.glob("*.jpg")]
|
78 |
+
prompts = [file for file in DATA_PATH.glob("*.txt")]
|
79 |
+
for file in files + prompts:
|
80 |
+
os.remove(file)
|
81 |
+
st.success("Todas las imágenes y prompts han sido borrados.")
|
82 |
+
except Exception as e:
|
83 |
+
st.error(f"Error al borrar archivos: {e}")
|
84 |
+
|
85 |
+
def download_images_as_zip():
|
86 |
+
zip_path = DATA_PATH / "images.zip"
|
87 |
+
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
88 |
+
for file in DATA_PATH.glob("*.jpg"):
|
89 |
+
zipf.write(file, arcname=file.name)
|
90 |
+
with open(zip_path, "rb") as zip_file:
|
91 |
+
st.download_button(label="Descargar imágenes en .zip", data=zip_file, file_name="images.zip", mime="application/zip")
|
92 |
+
|
93 |
def main():
|
94 |
st.set_page_config(layout="wide")
|
95 |
+
st.title("Generador de Imágenes")
|
96 |
+
st.warning("Este espacio contiene contenido que no es adecuado para todas las audiencias. Se recomienda discreción.")
|
97 |
+
agree = st.checkbox("Soy mayor de 18 años y entiendo que el contenido puede no ser apropiado.")
|
98 |
+
|
99 |
+
if agree:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
prompt = st.sidebar.text_input("Descripción de la imagen", max_chars=500)
|
101 |
format_option = st.sidebar.selectbox("Formato", ["9:16", "16:9"])
|
102 |
+
model_option = st.sidebar.selectbox("Modelo", ["modelo_actual", "enhanceaiteam/Flux-Uncensored-V2"])
|
103 |
|
104 |
width, height = (720, 1280) if format_option == "9:16" else (1280, 720)
|
105 |
|
106 |
if st.sidebar.button("Generar Imagen"):
|
107 |
with st.spinner("Generando imagen..."):
|
108 |
+
result = asyncio.run(gen(prompt, width, height, model_option))
|
109 |
image_paths = result[0]
|
110 |
prompt_file = result[1]
|
111 |
|
|
|
144 |
except Exception as e:
|
145 |
st.error(f"Error al borrar la imagen o prompt: {e}")
|
146 |
|
147 |
+
if st.button("Borrar todas las imágenes"):
|
148 |
+
delete_all_images()
|
149 |
+
|
150 |
+
download_images_as_zip()
|
151 |
+
|
152 |
+
else:
|
153 |
+
st.error("No puedes acceder a este espacio hasta que confirmes que eres mayor de edad.")
|
154 |
+
|
155 |
if __name__ == "__main__":
|
156 |
main()
|