Spaces:
Running
Running
salomonsky
commited on
Commit
•
5c1b2ce
1
Parent(s):
2760444
Update app.py
Browse files
app.py
CHANGED
@@ -7,12 +7,12 @@ from PIL import Image
|
|
7 |
import streamlit as st
|
8 |
from huggingface_hub import AsyncInferenceClient
|
9 |
import asyncio
|
|
|
10 |
|
11 |
MAX_SEED = np.iinfo(np.int32).max
|
12 |
client = AsyncInferenceClient()
|
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):
|
@@ -72,6 +72,7 @@ def get_prompts():
|
|
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")]
|
@@ -90,67 +91,85 @@ def download_images_as_zip():
|
|
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 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
if image_paths:
|
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 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
st.error("No puedes acceder a este espacio hasta que confirmes que eres mayor de edad.")
|
154 |
|
155 |
if __name__ == "__main__":
|
156 |
main()
|
|
|
7 |
import streamlit as st
|
8 |
from huggingface_hub import AsyncInferenceClient
|
9 |
import asyncio
|
10 |
+
from moviepy.editor import ImageSequenceClip
|
11 |
|
12 |
MAX_SEED = np.iinfo(np.int32).max
|
13 |
client = AsyncInferenceClient()
|
14 |
DATA_PATH = Path("./data")
|
15 |
DATA_PATH.mkdir(exist_ok=True)
|
|
|
16 |
PREDEFINED_SEED = random.randint(0, MAX_SEED)
|
17 |
|
18 |
async def generate_image(prompt, width, height, seed, model_name):
|
|
|
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 |
+
# Función para borrar todas las imágenes y prompts
|
76 |
def delete_all_images():
|
77 |
try:
|
78 |
files = [file for file in DATA_PATH.glob("*.jpg")]
|
|
|
91 |
with open(zip_path, "rb") as zip_file:
|
92 |
st.download_button(label="Descargar imágenes en .zip", data=zip_file, file_name="images.zip", mime="application/zip")
|
93 |
|
94 |
+
def create_video_from_images():
|
95 |
+
try:
|
96 |
+
image_files = sorted(DATA_PATH.glob("*.jpg"))
|
97 |
+
if not image_files:
|
98 |
+
st.error("No hay imágenes disponibles para crear un video.")
|
99 |
+
return
|
100 |
+
|
101 |
+
image_sequence = [Image.open(image_file) for image_file in image_files]
|
102 |
+
frame_rate = 1 # 1 segundo por cada imagen
|
103 |
+
|
104 |
+
clip = ImageSequenceClip([np.array(img) for img in image_sequence], fps=frame_rate)
|
105 |
+
video_path = DATA_PATH / "output_video.mp4"
|
106 |
+
clip.write_videofile(str(video_path), codec="libx264")
|
107 |
+
|
108 |
+
with open(video_path, "rb") as video_file:
|
109 |
+
st.download_button(label="Descargar video generado", data=video_file, file_name="output_video.mp4", mime="video/mp4")
|
110 |
+
|
111 |
+
st.success("El video ha sido generado con éxito.")
|
112 |
+
except Exception as e:
|
113 |
+
st.error(f"Error al generar el video: {e}")
|
114 |
+
|
115 |
def main():
|
116 |
st.set_page_config(layout="wide")
|
117 |
st.title("Generador de Imágenes")
|
|
|
|
|
118 |
|
119 |
+
prompt = st.sidebar.text_input("Descripción de la imagen", max_chars=500)
|
120 |
+
format_option = st.sidebar.selectbox("Formato", ["9:16", "16:9"])
|
121 |
+
model_option = st.sidebar.selectbox("Modelo", ["modelo_actual", "enhanceaiteam/Flux-Uncensored-V2"])
|
122 |
+
|
123 |
+
width, height = (720, 1280) if format_option == "9:16" else (1280, 720)
|
124 |
+
|
125 |
+
if st.sidebar.button("Generar Imagen"):
|
126 |
+
with st.spinner("Generando imagen..."):
|
127 |
+
result = asyncio.run(gen(prompt, width, height, model_option))
|
128 |
+
image_paths = result[0]
|
129 |
+
prompt_file = result[1]
|
130 |
+
|
131 |
+
if image_paths:
|
132 |
+
if Path(image_paths).exists():
|
133 |
+
st.image(image_paths, caption="Imagen Generada")
|
134 |
+
else:
|
135 |
+
st.error("El archivo de imagen no existe.")
|
136 |
+
|
137 |
+
if prompt_file and Path(prompt_file).exists():
|
138 |
+
prompt_text = Path(prompt_file).read_text()
|
139 |
+
st.write(f"Prompt utilizado: {prompt_text}")
|
140 |
+
else:
|
141 |
+
st.write("El archivo del prompt no está disponible.")
|
142 |
+
|
143 |
+
files, usage = get_storage()
|
144 |
+
st.text(usage)
|
145 |
+
cols = st.columns(6)
|
146 |
+
prompts = get_prompts()
|
147 |
+
|
148 |
+
for idx, file in enumerate(files):
|
149 |
+
with cols[idx % 6]:
|
150 |
+
image = Image.open(file)
|
151 |
+
prompt_file = prompts.get(Path(file).stem.replace("image_", ""), None)
|
152 |
+
prompt_text = Path(prompt_file).read_text() if prompt_file else "No disponible"
|
153 |
+
|
154 |
+
st.image(image, caption=f"Imagen {idx+1}")
|
155 |
+
st.write(f"Prompt: {prompt_text}")
|
156 |
+
|
157 |
+
if st.button(f"Borrar Imagen {idx+1}", key=f"delete_{idx}"):
|
158 |
+
try:
|
159 |
+
os.remove(file)
|
160 |
+
if prompt_file:
|
161 |
+
os.remove(prompt_file)
|
162 |
+
st.success(f"Imagen {idx+1} y su prompt fueron borrados.")
|
163 |
+
except Exception as e:
|
164 |
+
st.error(f"Error al borrar la imagen o prompt: {e}")
|
165 |
+
|
166 |
+
if st.button("Borrar todas las imágenes"):
|
167 |
+
delete_all_images()
|
168 |
+
|
169 |
+
download_images_as_zip()
|
170 |
+
|
171 |
+
if st.button("Generar video con las imágenes"):
|
172 |
+
create_video_from_images()
|
|
|
173 |
|
174 |
if __name__ == "__main__":
|
175 |
main()
|