salomonsky commited on
Commit
8159177
·
verified ·
1 Parent(s): 05bdce0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -12
app.py CHANGED
@@ -40,7 +40,11 @@ def authenticate_user(username, password):
40
 
41
  def list_saved_images():
42
  try:
43
- image_files = sorted(DATA_PATH.glob("*.jpg"))
 
 
 
 
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(prompt=prompt, height=height, width=width, model=model_name)
 
 
 
 
 
 
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, (prompt, seed) in enumerate(zip(prompts[:num_variants], seeds)):
147
- image, _ = generate_image(prompt, width, height, seed, model_name)
148
- image_path = save_image(image, f"generated_image_{seed}.jpg")
149
- if image_path:
150
- save_prompt(f"generated_image_{seed}.jpg: {prompt}")
151
- st.success(f"Imagen {idx + 1} generada")
152
- images.append(str(image_path))
 
 
 
 
 
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
- prompts[image_name] = line.split(": ", 1)[1].strip()
 
 
 
 
 
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.")