Spaces:
Running
Running
salomonsky
commited on
Commit
•
134fe34
1
Parent(s):
4dbb52e
Update app.py
Browse files
app.py
CHANGED
@@ -31,18 +31,6 @@ DATA_PATH.mkdir(exist_ok=True)
|
|
31 |
|
32 |
PREDEFINED_SEED = random.randint(0, MAX_SEED)
|
33 |
|
34 |
-
async def generate_image(prompt, width, height, seed):
|
35 |
-
try:
|
36 |
-
if seed == -1:
|
37 |
-
seed = PREDEFINED_SEED
|
38 |
-
seed = int(seed)
|
39 |
-
image = await client.text_to_image(
|
40 |
-
prompt=prompt, height=height, width=width, model="enhanceaiteam/Flux-uncensored"
|
41 |
-
)
|
42 |
-
return image, seed
|
43 |
-
except Exception as e:
|
44 |
-
return f"Error al generar imagen: {e}", None
|
45 |
-
|
46 |
def save_prompt(prompt_text, seed):
|
47 |
try:
|
48 |
prompt_file_path = DATA_PATH / f"prompt_{seed}.txt"
|
@@ -70,15 +58,6 @@ async def gen(prompt, width, height):
|
|
70 |
|
71 |
return [str(image_path), str(prompt_file_path)]
|
72 |
|
73 |
-
def save_image(image, seed):
|
74 |
-
try:
|
75 |
-
image_path = DATA_PATH / f"image_{seed}.jpg"
|
76 |
-
image.save(image_path, format="JPEG")
|
77 |
-
return image_path
|
78 |
-
except Exception as e:
|
79 |
-
st.error(f"Error al guardar la imagen: {e}")
|
80 |
-
return None
|
81 |
-
|
82 |
def get_storage():
|
83 |
files = [file for file in DATA_PATH.glob("*.jpg") if file.is_file()]
|
84 |
files.sort(key=lambda x: x.stat().st_mtime, reverse=True)
|
@@ -89,68 +68,63 @@ def get_prompts():
|
|
89 |
prompt_files = [file for file in DATA_PATH.glob("*.txt") if file.is_file()]
|
90 |
return {file.stem.replace("prompt_", ""): file for file in prompt_files}
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
def main():
|
93 |
-
# Configuración de página
|
94 |
st.set_page_config(layout="wide")
|
95 |
-
|
96 |
-
# Autenticación
|
97 |
name, authentication_status, username = authenticator.login("Login", "main")
|
98 |
|
99 |
if authentication_status:
|
100 |
st.success(f"Bienvenido {name}")
|
101 |
|
102 |
-
# Aquí comienza la lógica de generación de imágenes después de autenticarse
|
103 |
st.title("Generador de Imágenes FLUX")
|
104 |
prompt = st.sidebar.text_input("Descripción de la imagen", max_chars=500)
|
105 |
format_option = st.sidebar.selectbox("Formato", ["9:16", "16:9"])
|
106 |
|
107 |
-
if format_option == "9:16"
|
108 |
-
width = 720
|
109 |
-
height = 1280
|
110 |
-
else:
|
111 |
-
width = 1280
|
112 |
-
height = 720
|
113 |
|
114 |
if st.sidebar.button("Generar Imagen"):
|
115 |
with st.spinner("Generando imagen..."):
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
if prompt_file and Path(prompt_file).exists():
|
127 |
-
prompt_text = Path(prompt_file).read_text()
|
128 |
-
st.write(f"Prompt utilizado: {prompt_text}")
|
129 |
-
else:
|
130 |
-
st.write("El archivo del prompt no está disponible.")
|
131 |
-
|
132 |
files, usage = get_storage()
|
133 |
-
st.
|
134 |
-
cols = st.columns(6)
|
135 |
-
prompts = get_prompts()
|
136 |
|
137 |
for idx, file in enumerate(files):
|
138 |
-
with cols[idx %
|
139 |
image = Image.open(file)
|
140 |
-
|
141 |
-
prompt_text = Path(prompt_file).read_text() if prompt_file else "No disponible"
|
142 |
-
|
143 |
-
st.image(image, caption=f"Imagen {idx+1}")
|
144 |
-
st.write(f"Prompt: {prompt_text}")
|
145 |
|
146 |
-
if st.button(f"Borrar Imagen {idx+1}", key=f"delete_{idx}"):
|
147 |
try:
|
148 |
os.remove(file)
|
149 |
-
|
150 |
-
os.remove(prompt_file)
|
151 |
-
st.success(f"Imagen {idx+1} y su prompt fueron borrados.")
|
152 |
except Exception as e:
|
153 |
-
st.error(f"Error al borrar la imagen
|
154 |
|
155 |
elif authentication_status == False:
|
156 |
st.error("Nombre de usuario/contraseña incorrectos")
|
|
|
31 |
|
32 |
PREDEFINED_SEED = random.randint(0, MAX_SEED)
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
def save_prompt(prompt_text, seed):
|
35 |
try:
|
36 |
prompt_file_path = DATA_PATH / f"prompt_{seed}.txt"
|
|
|
58 |
|
59 |
return [str(image_path), str(prompt_file_path)]
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
def get_storage():
|
62 |
files = [file for file in DATA_PATH.glob("*.jpg") if file.is_file()]
|
63 |
files.sort(key=lambda x: x.stat().st_mtime, reverse=True)
|
|
|
68 |
prompt_files = [file for file in DATA_PATH.glob("*.txt") if file.is_file()]
|
69 |
return {file.stem.replace("prompt_", ""): file for file in prompt_files}
|
70 |
|
71 |
+
async def generate_image(prompt, width, height):
|
72 |
+
try:
|
73 |
+
image = await client.text_to_image(
|
74 |
+
prompt=prompt, height=height, width=width, model="enhanceaiteam/Flux-uncensored"
|
75 |
+
)
|
76 |
+
return image
|
77 |
+
except Exception as e:
|
78 |
+
return f"Error al generar imagen: {e}"
|
79 |
+
|
80 |
+
def save_image(image, seed):
|
81 |
+
try:
|
82 |
+
image_path = DATA_PATH / f"image_{seed}.jpg"
|
83 |
+
image.save(image_path, format="JPEG")
|
84 |
+
return image_path
|
85 |
+
except Exception as e:
|
86 |
+
st.error(f"Error al guardar la imagen: {e}")
|
87 |
+
return None
|
88 |
+
|
89 |
def main():
|
|
|
90 |
st.set_page_config(layout="wide")
|
|
|
|
|
91 |
name, authentication_status, username = authenticator.login("Login", "main")
|
92 |
|
93 |
if authentication_status:
|
94 |
st.success(f"Bienvenido {name}")
|
95 |
|
|
|
96 |
st.title("Generador de Imágenes FLUX")
|
97 |
prompt = st.sidebar.text_input("Descripción de la imagen", max_chars=500)
|
98 |
format_option = st.sidebar.selectbox("Formato", ["9:16", "16:9"])
|
99 |
|
100 |
+
width, height = (720, 1280) if format_option == "9:16" else (1280, 720)
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
if st.sidebar.button("Generar Imagen"):
|
103 |
with st.spinner("Generando imagen..."):
|
104 |
+
image = await generate_image(prompt, width, height)
|
105 |
+
|
106 |
+
if isinstance(image, str) and image.startswith("Error"):
|
107 |
+
st.error(image)
|
108 |
+
else:
|
109 |
+
image_path = save_image(image, PREDEFINED_SEED)
|
110 |
+
if image_path and Path(image_path).exists():
|
111 |
+
st.image(image_path, caption="Imagen Generada")
|
112 |
+
|
113 |
+
st.subheader("Galería de Imágenes Generadas")
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
files, usage = get_storage()
|
115 |
+
cols = st.columns(3)
|
|
|
|
|
116 |
|
117 |
for idx, file in enumerate(files):
|
118 |
+
with cols[idx % 3]:
|
119 |
image = Image.open(file)
|
120 |
+
st.image(image, caption=f"Imagen {idx + 1}")
|
|
|
|
|
|
|
|
|
121 |
|
122 |
+
if st.button(f"Borrar Imagen {idx + 1}", key=f"delete_{idx}"):
|
123 |
try:
|
124 |
os.remove(file)
|
125 |
+
st.success(f"Imagen {idx + 1} fue borrada.")
|
|
|
|
|
126 |
except Exception as e:
|
127 |
+
st.error(f"Error al borrar la imagen: {e}")
|
128 |
|
129 |
elif authentication_status == False:
|
130 |
st.error("Nombre de usuario/contraseña incorrectos")
|