Spaces:
Running
Running
gallery fix
Browse files- app/core/pipeline.py +3 -2
- app/ui/app.py +117 -52
app/core/pipeline.py
CHANGED
@@ -222,8 +222,9 @@ class CreativePipeline:
|
|
222 |
data_blob_id = parts[0]
|
223 |
execution_id = parts[2] if len(parts) > 2 else None
|
224 |
|
225 |
-
# Target directory
|
226 |
-
|
|
|
227 |
|
228 |
# Get the existing metadata filename and use it for the image
|
229 |
metadata_filename = Path(image_metadata_path).name
|
|
|
222 |
data_blob_id = parts[0]
|
223 |
execution_id = parts[2] if len(parts) > 2 else None
|
224 |
|
225 |
+
# Target directory - use the image output directory from environment or class instead of hardcoding
|
226 |
+
# Use self.text_to_image.output_dir instead of hardcoded path
|
227 |
+
images_dir = self.text_to_image.output_dir
|
228 |
|
229 |
# Get the existing metadata filename and use it for the image
|
230 |
metadata_filename = Path(image_metadata_path).name
|
app/ui/app.py
CHANGED
@@ -237,64 +237,111 @@ def main():
|
|
237 |
with gr.Tab("Gallery"):
|
238 |
# Function to update the image gallery
|
239 |
def update_image_gallery():
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
248 |
|
249 |
# Function to update the models gallery and return both the models list and model paths
|
250 |
def update_models_gallery():
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
model_paths = [] # Store just the paths for easy access by index
|
256 |
-
|
257 |
-
for model_path in sorted(
|
258 |
-
models,
|
259 |
-
key=lambda x: os.path.getmtime(x) if os.path.exists(x) else 0,
|
260 |
-
reverse=True,
|
261 |
-
):
|
262 |
-
# Try to load metadata file if available
|
263 |
-
metadata_path = model_path.with_suffix(".json")
|
264 |
-
creation_time = time.strftime(
|
265 |
-
"%Y-%m-%d %H:%M", time.localtime(os.path.getmtime(model_path))
|
266 |
)
|
267 |
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
)
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
280 |
source_image = "Unknown"
|
281 |
format_type = model_path.suffix[1:]
|
282 |
-
else:
|
283 |
-
source_image = "Unknown"
|
284 |
-
format_type = model_path.suffix[1:]
|
285 |
-
|
286 |
-
# Add to data table and path list
|
287 |
-
model_paths.append(str(model_path))
|
288 |
-
model_data.append(
|
289 |
-
[
|
290 |
-
str(model_path),
|
291 |
-
source_image,
|
292 |
-
format_type,
|
293 |
-
creation_time,
|
294 |
-
]
|
295 |
-
)
|
296 |
|
297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
298 |
|
299 |
# Function to view model by index instead of relying on DataFrame selection
|
300 |
def view_model_by_index(evt: gr.SelectData):
|
@@ -320,8 +367,26 @@ def main():
|
|
320 |
model_path = view_model_by_index.model_paths[row_index]
|
321 |
logger.info(f"Selected model at index {row_index}: {model_path}")
|
322 |
|
323 |
-
if not
|
324 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
325 |
return None, None
|
326 |
|
327 |
except (IndexError, AttributeError, TypeError) as e:
|
|
|
237 |
with gr.Tab("Gallery"):
|
238 |
# Function to update the image gallery
|
239 |
def update_image_gallery():
|
240 |
+
try:
|
241 |
+
# Find all PNG and JPG images in the images directory
|
242 |
+
images = list(images_path.glob("*.png")) + list(
|
243 |
+
images_path.glob("*.jpg")
|
244 |
+
)
|
245 |
+
|
246 |
+
# Debug output to help diagnose issues
|
247 |
+
logger.info(
|
248 |
+
f"Found {len(images)} images in gallery directory: {images_path}"
|
249 |
+
)
|
250 |
+
for img in images[:5]: # Log first 5 images for debugging
|
251 |
+
logger.info(
|
252 |
+
f"Image file: {img} (exists: {os.path.exists(img)})"
|
253 |
+
)
|
254 |
+
|
255 |
+
# Convert Path objects to strings for the gallery
|
256 |
+
image_paths = sorted(
|
257 |
+
[str(img) for img in images if os.path.exists(img)],
|
258 |
+
key=lambda x: os.path.getmtime(x) if os.path.exists(x) else 0,
|
259 |
+
reverse=True,
|
260 |
+
)
|
261 |
+
|
262 |
+
logger.info(
|
263 |
+
f"Returning {len(image_paths)} valid images for gallery display"
|
264 |
+
)
|
265 |
+
return image_paths
|
266 |
+
except Exception as e:
|
267 |
+
logger.error(f"Error updating image gallery: {e}")
|
268 |
+
return []
|
269 |
|
270 |
# Function to update the models gallery and return both the models list and model paths
|
271 |
def update_models_gallery():
|
272 |
+
try:
|
273 |
+
# Find all model files in the models directory
|
274 |
+
models = list(models_path.glob("*.glb")) + list(
|
275 |
+
models_path.glob("*.gltf")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
276 |
)
|
277 |
|
278 |
+
# Debug output to help diagnose issues
|
279 |
+
logger.info(
|
280 |
+
f"Found {len(models)} models in gallery directory: {models_path}"
|
281 |
+
)
|
282 |
+
for model in models[:5]: # Log first 5 models for debugging
|
283 |
+
logger.info(
|
284 |
+
f"Model file: {model} (exists: {os.path.exists(model)})"
|
285 |
+
)
|
286 |
+
|
287 |
+
model_data = []
|
288 |
+
model_paths = [] # Store just the paths for easy access by index
|
289 |
+
|
290 |
+
for model_path in sorted(
|
291 |
+
models,
|
292 |
+
key=lambda x: os.path.getmtime(x) if os.path.exists(x) else 0,
|
293 |
+
reverse=True,
|
294 |
+
):
|
295 |
+
# Skip files that don't exist (should not happen, but just in case)
|
296 |
+
if not os.path.exists(model_path):
|
297 |
+
logger.warning(f"Listed model doesn't exist: {model_path}")
|
298 |
+
continue
|
299 |
+
|
300 |
+
# Try to load metadata file if available
|
301 |
+
metadata_path = model_path.with_suffix(".json")
|
302 |
+
creation_time = time.strftime(
|
303 |
+
"%Y-%m-%d %H:%M",
|
304 |
+
time.localtime(os.path.getmtime(model_path)),
|
305 |
+
)
|
306 |
+
|
307 |
+
if metadata_path.exists():
|
308 |
+
try:
|
309 |
+
with open(metadata_path, "r") as f:
|
310 |
+
metadata = json.load(f)
|
311 |
+
source_image = metadata.get(
|
312 |
+
"source_image_filename", "Unknown"
|
313 |
+
)
|
314 |
+
format_type = metadata.get(
|
315 |
+
"format", model_path.suffix[1:]
|
316 |
+
)
|
317 |
+
except Exception as e:
|
318 |
+
logger.error(
|
319 |
+
f"Failed to read metadata for {model_path}: {e}"
|
320 |
+
)
|
321 |
+
source_image = "Unknown"
|
322 |
+
format_type = model_path.suffix[1:]
|
323 |
+
else:
|
324 |
source_image = "Unknown"
|
325 |
format_type = model_path.suffix[1:]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
326 |
|
327 |
+
# Add to data table and path list
|
328 |
+
model_paths.append(str(model_path))
|
329 |
+
model_data.append(
|
330 |
+
[
|
331 |
+
str(model_path),
|
332 |
+
source_image,
|
333 |
+
format_type,
|
334 |
+
creation_time,
|
335 |
+
]
|
336 |
+
)
|
337 |
+
|
338 |
+
logger.info(
|
339 |
+
f"Returning {len(model_data)} valid models for gallery display"
|
340 |
+
)
|
341 |
+
return model_data, model_paths
|
342 |
+
except Exception as e:
|
343 |
+
logger.error(f"Error updating models gallery: {e}")
|
344 |
+
return [], []
|
345 |
|
346 |
# Function to view model by index instead of relying on DataFrame selection
|
347 |
def view_model_by_index(evt: gr.SelectData):
|
|
|
367 |
model_path = view_model_by_index.model_paths[row_index]
|
368 |
logger.info(f"Selected model at index {row_index}: {model_path}")
|
369 |
|
370 |
+
# Verify the file exists, if not check if we need to adjust path
|
371 |
+
if not os.path.exists(model_path):
|
372 |
+
# Try to recover by checking if it's a path issue
|
373 |
+
# Models might have paths like: app/data/models/file.glb
|
374 |
+
# But in Spaces we need /home/user/app/data/models/file.glb
|
375 |
+
if RUNNING_IN_SPACES and "app/data" in model_path:
|
376 |
+
# Extract the relative path part
|
377 |
+
rel_path = model_path.split("app/data/", 1)[-1]
|
378 |
+
# Construct absolute path using DATA_DIR
|
379 |
+
if os.environ.get("DATA_DIR"):
|
380 |
+
corrected_path = os.path.join(
|
381 |
+
os.environ.get("DATA_DIR"), rel_path
|
382 |
+
)
|
383 |
+
logger.info(f"Trying corrected path: {corrected_path}")
|
384 |
+
if os.path.exists(corrected_path):
|
385 |
+
model_path = corrected_path
|
386 |
+
logger.info(f"Using corrected path: {model_path}")
|
387 |
+
|
388 |
+
if not os.path.exists(model_path):
|
389 |
+
logger.warning(f"Model file not found at {model_path}")
|
390 |
return None, None
|
391 |
|
392 |
except (IndexError, AttributeError, TypeError) as e:
|