Hermes Bot commited on
Commit
117bc79
·
1 Parent(s): 1e17100

Fix Gallery metadata extraction: robust path handling, fallback, include filename

Browse files
Files changed (1) hide show
  1. ui/events/run_handlers.py +42 -11
ui/events/run_handlers.py CHANGED
@@ -115,22 +115,53 @@ def create_run_event(prefix: str, task_type: str, ui_components: dict):
115
  outputs=[res_gal]
116
  )
117
  # When a user selects an image from the global Gallery tab, load its generation info
118
- def _show_meta(selected_path, *_):
119
- # `selected_path` may be a string (filepath) or a list/tuple of filepaths.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  try:
121
- # Recursively flatten until we get a string path
122
- path = selected_path
123
- while isinstance(path, (list, tuple)) and len(path) > 0:
124
- path = path[0]
125
- print(f"[DEBUG] Extracted image path: {path}")
126
  from PIL import Image
127
  with Image.open(path) as im:
128
  meta = im.info.get("parameters", "")
 
129
  except Exception as e:
130
- print(f"[DEBUG] Failed to read metadata: {e}")
131
- meta = ""
132
- # If no metadata present, show a friendly placeholder.
133
- return meta if meta else "(ไม่มีข้อมูลเมตาดาต้า)"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
  # Connect the select event of the global gallery to update its metadata textbox
136
  gallery_tab = ui_components.get('gallery')
 
115
  outputs=[res_gal]
116
  )
117
  # When a user selects an image from the global Gallery tab, load its generation info
118
+ def _show_meta(selected, *_):
119
+ # `selected` may be a string (filepath), a list, or a tuple containing the path.
120
+ # Gradio can sometimes pass a tuple like (path, None) or a list of such tuples.
121
+ # We extract the first string path we find.
122
+ def extract_path(val):
123
+ if isinstance(val, str):
124
+ return val
125
+ if isinstance(val, (list, tuple)):
126
+ for item in val:
127
+ result = extract_path(item)
128
+ if result:
129
+ return result
130
+ return None
131
+
132
+ path = extract_path(selected)
133
+ if not path:
134
+ print(f"[DEBUG] No valid image path extracted from {selected}")
135
+ return "(ไม่มีข้อมูลเมตาดาต้า)"
136
+
137
+ # Try to read metadata from the extracted path. If this fails (e.g., path points to a temporary Gradio copy), fall back to the most‑recent file in the output directory.
138
+ meta = ""
139
  try:
 
 
 
 
 
140
  from PIL import Image
141
  with Image.open(path) as im:
142
  meta = im.info.get("parameters", "")
143
+ print(f"[DEBUG] Metadata extracted from {path}: {meta}")
144
  except Exception as e:
145
+ print(f"[DEBUG] Failed to read metadata from {path}: {e}")
146
+ # Fallback: find latest file in the configured OUTPUT_DIR
147
+ try:
148
+ from pathlib import Path
149
+ out_dir = Path(os.path.abspath(OUTPUT_DIR))
150
+ files = sorted(out_dir.glob("*.png"), key=lambda p: p.stat().st_mtime, reverse=True)
151
+ if files:
152
+ fallback_path = files[0]
153
+ with Image.open(fallback_path) as im2:
154
+ meta = im2.info.get("parameters", "")
155
+ print(f"[DEBUG] Fallback metadata from {fallback_path}: {meta}")
156
+ except Exception as e2:
157
+ print(f"[DEBUG] Fallback metadata extraction failed: {e2}")
158
+
159
+ # If metadata exists, we can also include the filename for clarity
160
+ if meta:
161
+ filename = os.path.basename(path)
162
+ return f"{filename}\n{meta}"
163
+ else:
164
+ return "(ไม่มีข้อมูลเมตาดาต้า)"
165
 
166
  # Connect the select event of the global gallery to update its metadata textbox
167
  gallery_tab = ui_components.get('gallery')