Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -40,7 +40,11 @@ def authenticate_user(username, password):
|
|
40 |
|
41 |
def list_saved_images():
|
42 |
try:
|
43 |
-
image_files = sorted(
|
|
|
|
|
|
|
|
|
44 |
return image_files
|
45 |
except Exception as e:
|
46 |
st.error(f"Error al listar imágenes guardadas: {e}")
|
@@ -76,7 +80,13 @@ def generate_image(prompt, width, height, seed, model_name):
|
|
76 |
if seed == -1:
|
77 |
seed = random.randint(0, MAX_SEED)
|
78 |
try:
|
79 |
-
image = client.text_to_image(
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
return image, seed
|
81 |
except Exception as e:
|
82 |
st.error(f"Error al generar imagen: {e}")
|
@@ -139,19 +149,25 @@ def gen(prompts, width, height, model_name, num_variants=1):
|
|
139 |
|
140 |
while len(seeds) < num_variants:
|
141 |
new_seed = random.randint(0, MAX_SEED)
|
142 |
-
if new_seed not in seeds:
|
143 |
seeds.append(new_seed)
|
144 |
-
|
145 |
try:
|
146 |
-
for idx, (
|
147 |
-
image,
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
|
|
|
|
|
|
|
|
|
|
153 |
except Exception as e:
|
154 |
st.error(f"Error al generar imágenes: {e}")
|
|
|
155 |
return images
|
156 |
|
157 |
def get_prompt_for_image(image_name):
|
@@ -160,7 +176,12 @@ def get_prompt_for_image(image_name):
|
|
160 |
with open(DATA_PATH / "prompts.txt", "r") as f:
|
161 |
for line in f:
|
162 |
if line.startswith(image_name):
|
163 |
-
|
|
|
|
|
|
|
|
|
|
|
164 |
except FileNotFoundError:
|
165 |
return "No hay prompt asociado."
|
166 |
return prompts.get(image_name, "No hay prompt asociado.")
|
|
|
40 |
|
41 |
def list_saved_images():
|
42 |
try:
|
43 |
+
image_files = sorted(
|
44 |
+
DATA_PATH.glob("*.jpg"),
|
45 |
+
key=lambda x: x.stat().st_mtime,
|
46 |
+
reverse=True
|
47 |
+
)
|
48 |
return image_files
|
49 |
except Exception as e:
|
50 |
st.error(f"Error al listar imágenes guardadas: {e}")
|
|
|
80 |
if seed == -1:
|
81 |
seed = random.randint(0, MAX_SEED)
|
82 |
try:
|
83 |
+
image = client.text_to_image(
|
84 |
+
prompt=prompt,
|
85 |
+
height=height,
|
86 |
+
width=width,
|
87 |
+
model=model_name,
|
88 |
+
seed=seed
|
89 |
+
)
|
90 |
return image, seed
|
91 |
except Exception as e:
|
92 |
st.error(f"Error al generar imagen: {e}")
|
|
|
149 |
|
150 |
while len(seeds) < num_variants:
|
151 |
new_seed = random.randint(0, MAX_SEED)
|
152 |
+
if new_seed not in seeds:
|
153 |
seeds.append(new_seed)
|
154 |
+
|
155 |
try:
|
156 |
+
for idx, (seed, prompt) in enumerate(zip(seeds, prompts[:num_variants])):
|
157 |
+
image, used_seed = generate_image(prompt, width, height, seed, model_name)
|
158 |
+
if image:
|
159 |
+
image_path = save_image(image, f"generated_image_{used_seed}.jpg")
|
160 |
+
if image_path:
|
161 |
+
if ": " in prompt:
|
162 |
+
clean_prompt = prompt.split(": ")[-1]
|
163 |
+
else:
|
164 |
+
clean_prompt = prompt
|
165 |
+
save_prompt(f"generated_image_{used_seed}.jpg: {clean_prompt}")
|
166 |
+
st.success(f"Imagen {idx + 1} generada")
|
167 |
+
images.append(str(image_path))
|
168 |
except Exception as e:
|
169 |
st.error(f"Error al generar imágenes: {e}")
|
170 |
+
|
171 |
return images
|
172 |
|
173 |
def get_prompt_for_image(image_name):
|
|
|
176 |
with open(DATA_PATH / "prompts.txt", "r") as f:
|
177 |
for line in f:
|
178 |
if line.startswith(image_name):
|
179 |
+
full_prompt = line.split(": ", 1)[1].strip()
|
180 |
+
if ":" in full_prompt:
|
181 |
+
prompt = full_prompt.split(": ")[-1].strip()
|
182 |
+
else:
|
183 |
+
prompt = full_prompt
|
184 |
+
prompts[image_name] = prompt
|
185 |
except FileNotFoundError:
|
186 |
return "No hay prompt asociado."
|
187 |
return prompts.get(image_name, "No hay prompt asociado.")
|