Spaces:
Sleeping
Sleeping
Upload app.py
Browse files- metadata_search/app.py +66 -11
metadata_search/app.py
CHANGED
|
@@ -199,22 +199,77 @@ def cors_headers(response):
|
|
| 199 |
return response
|
| 200 |
|
| 201 |
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
-
|
| 209 |
-
if not root.is_dir():
|
| 210 |
-
return jsonify({"error": "PDF directory not found."}), 404
|
| 211 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
safe_name = Path(filename).name
|
| 213 |
-
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
return jsonify({"error": "PDF file not found."}), 404
|
| 216 |
|
| 217 |
-
return send_from_directory(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
|
| 220 |
@app.get("/figures/<path:paper_key>/<path:filename>")
|
|
|
|
| 199 |
return response
|
| 200 |
|
| 201 |
|
| 202 |
+
def _year_from_asset_filename(file_name: str) -> str | None:
|
| 203 |
+
import re
|
| 204 |
+
|
| 205 |
+
match = re.search(r"\s-\s(\d{4})\s-\s", Path(file_name).name)
|
| 206 |
+
return match.group(1) if match else None
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
def _download_pdf_on_demand(file_name: str) -> Path | None:
|
| 210 |
+
"""Download one PDF from the HF Dataset and cache it locally."""
|
| 211 |
+
if os.getenv("WATERSCOPE_PDF_ON_DEMAND", "1").lower() not in {"1", "true", "yes"}:
|
| 212 |
+
return None
|
| 213 |
+
|
| 214 |
+
year = _year_from_asset_filename(file_name)
|
| 215 |
+
if not year:
|
| 216 |
+
return None
|
| 217 |
+
|
| 218 |
+
from huggingface_hub import hf_hub_download
|
| 219 |
+
|
| 220 |
+
repo_id = os.getenv(
|
| 221 |
+
"WATERSCOPE_DATASET_REPO",
|
| 222 |
+
"EnvironmentalAI/WaterScopeAI-Data",
|
| 223 |
+
).strip()
|
| 224 |
+
pdf_prefix = os.getenv("WATERSCOPE_PDF_PREFIX", "PDFs").strip() or "PDFs"
|
| 225 |
+
cache_root = Path(
|
| 226 |
+
os.getenv("WATERSCOPE_PDF_CACHE", "/tmp/waterscope-pdf-cache")
|
| 227 |
+
).resolve()
|
| 228 |
+
cache_root.mkdir(parents=True, exist_ok=True)
|
| 229 |
|
| 230 |
+
repo_file = f"{pdf_prefix}/{year}/{Path(file_name).name}"
|
|
|
|
|
|
|
| 231 |
|
| 232 |
+
try:
|
| 233 |
+
downloaded = hf_hub_download(
|
| 234 |
+
repo_id=repo_id,
|
| 235 |
+
repo_type="dataset",
|
| 236 |
+
filename=repo_file,
|
| 237 |
+
local_dir=str(cache_root),
|
| 238 |
+
)
|
| 239 |
+
except Exception as exc:
|
| 240 |
+
print(
|
| 241 |
+
f"[pdf] download failed for {repo_file}: {type(exc).__name__}: {exc}",
|
| 242 |
+
flush=True,
|
| 243 |
+
)
|
| 244 |
+
return None
|
| 245 |
+
|
| 246 |
+
path = Path(downloaded)
|
| 247 |
+
return path if path.is_file() else None
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
@app.get("/pdfs/<path:filename>")
|
| 251 |
+
def paper_pdf_file(filename: str):
|
| 252 |
safe_name = Path(filename).name
|
| 253 |
+
|
| 254 |
+
# 1) Preserve existing local behavior first.
|
| 255 |
+
root_raw = os.getenv("PAPER_PDF_DIR", "").strip()
|
| 256 |
+
if root_raw:
|
| 257 |
+
root = Path(root_raw).resolve()
|
| 258 |
+
if root.is_dir():
|
| 259 |
+
target = (root / safe_name).resolve()
|
| 260 |
+
if target.parent == root and target.is_file():
|
| 261 |
+
return send_from_directory(root, safe_name, as_attachment=True)
|
| 262 |
+
|
| 263 |
+
# 2) Cloud fallback: download only the requested PDF from the HF Dataset.
|
| 264 |
+
downloaded = _download_pdf_on_demand(safe_name)
|
| 265 |
+
if downloaded is None:
|
| 266 |
return jsonify({"error": "PDF file not found."}), 404
|
| 267 |
|
| 268 |
+
return send_from_directory(
|
| 269 |
+
downloaded.parent,
|
| 270 |
+
downloaded.name,
|
| 271 |
+
as_attachment=True,
|
| 272 |
+
)
|
| 273 |
|
| 274 |
|
| 275 |
@app.get("/figures/<path:paper_key>/<path:filename>")
|