Spaces:
Running
Running
salomonsky
commited on
Commit
•
7c6a22d
1
Parent(s):
f0feb24
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import os; import numpy as np; import random; from pathlib import Path; from PIL import Image; import streamlit as st; from huggingface_hub import AsyncInferenceClient; import asyncio; import yaml
|
2 |
|
3 |
with open("config.yaml") as f: config = yaml.safe_load(f); username = config["credentials"]["username"]; password = config["credentials"]["password"]
|
4 |
-
|
5 |
client = AsyncInferenceClient(); DATA_PATH = Path("./data"); DATA_PATH.mkdir(exist_ok=True); MAX_SEED = np.iinfo(np.int32).max; PREDEFINED_SEED = random.randint(0, MAX_SEED)
|
6 |
|
7 |
async def generate_image(prompt, width, height, seed):
|
@@ -43,26 +43,50 @@ def get_prompts():
|
|
43 |
return {file.stem.replace("prompt_", ""): file for file in DATA_PATH.glob("*.txt") if file.is_file()}
|
44 |
|
45 |
def main():
|
46 |
-
st.set_page_config(layout="wide")
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
if __name__ == "__main__": main()
|
|
|
1 |
import os; import numpy as np; import random; from pathlib import Path; from PIL import Image; import streamlit as st; from huggingface_hub import AsyncInferenceClient; import asyncio; import yaml
|
2 |
|
3 |
with open("config.yaml") as f: config = yaml.safe_load(f); username = config["credentials"]["username"]; password = config["credentials"]["password"]
|
4 |
+
|
5 |
client = AsyncInferenceClient(); DATA_PATH = Path("./data"); DATA_PATH.mkdir(exist_ok=True); MAX_SEED = np.iinfo(np.int32).max; PREDEFINED_SEED = random.randint(0, MAX_SEED)
|
6 |
|
7 |
async def generate_image(prompt, width, height, seed):
|
|
|
43 |
return {file.stem.replace("prompt_", ""): file for file in DATA_PATH.glob("*.txt") if file.is_file()}
|
44 |
|
45 |
def main():
|
46 |
+
st.set_page_config(layout="wide")
|
47 |
+
st.title("Generador de Imágenes")
|
48 |
+
st.sidebar.header("Inicio de Sesión")
|
49 |
+
input_username = st.sidebar.text_input("Usuario")
|
50 |
+
input_password = st.sidebar.text_input("Contraseña", type="password")
|
51 |
+
|
52 |
+
if st.sidebar.button("Iniciar Sesión"):
|
53 |
+
if input_username == username and input_password == password:
|
54 |
+
st.sidebar.success("Inicio de sesión exitoso!")
|
55 |
+
prompt = st.sidebar.text_input("Descripción de la imagen", max_chars=500)
|
56 |
+
width, height = (720, 1280) if st.sidebar.selectbox("Formato", ["9:16", "16:9"]) == "9:16" else (1280, 720)
|
57 |
|
58 |
+
if st.sidebar.button("Generar Imagen"):
|
59 |
+
with st.spinner("Generando imagen..."):
|
60 |
+
result = asyncio.run(gen(prompt, width, height))
|
61 |
+
if (image_paths := result[0]) and Path(image_paths).exists():
|
62 |
+
st.image(image_paths, caption="Imagen Generada")
|
63 |
+
else:
|
64 |
+
st.error("El archivo de imagen no existe.")
|
65 |
+
if (prompt_file := result[1]) and Path(prompt_file).exists():
|
66 |
+
st.write(f"Prompt utilizado: {Path(prompt_file).read_text()}")
|
67 |
+
else:
|
68 |
+
st.write("El archivo del prompt no está disponible.")
|
69 |
|
70 |
+
files, usage = get_storage()
|
71 |
+
st.text(usage)
|
72 |
+
cols = st.columns(6)
|
73 |
+
prompts = get_prompts()
|
74 |
+
for idx, file in enumerate(files):
|
75 |
+
with cols[idx % 6]:
|
76 |
+
image = Image.open(file)
|
77 |
+
prompt_file = prompts.get(Path(file).stem.replace("image_", ""), None)
|
78 |
+
st.image(image, caption=f"Imagen {idx+1}")
|
79 |
+
st.write(f"Prompt: {Path(prompt_file).read_text() if prompt_file else 'No disponible'}")
|
80 |
+
if st.button(f"Borrar Imagen {idx+1}", key=f"delete_{idx}"):
|
81 |
+
try:
|
82 |
+
os.remove(file)
|
83 |
+
os.remove(prompt_file) if prompt_file else None
|
84 |
+
st.success(f"Imagen {idx+1} y su prompt fueron borrados.")
|
85 |
+
except Exception as e:
|
86 |
+
st.error(f"Error al borrar la imagen o prompt: {e}")
|
87 |
+
else:
|
88 |
+
st.sidebar.error("Usuario o contraseña incorrectos.")
|
89 |
+
else:
|
90 |
+
st.sidebar.warning("Por favor, inicie sesión.")
|
91 |
|
92 |
if __name__ == "__main__": main()
|