Spaces:
Sleeping
Sleeping
Upload summaries.py
Browse files- local_rag/summaries.py +45 -2
local_rag/summaries.py
CHANGED
|
@@ -5,10 +5,50 @@ import os
|
|
| 5 |
import re
|
| 6 |
from pathlib import Path
|
| 7 |
|
|
|
|
| 8 |
from local_rag.config import LocalRagConfig
|
| 9 |
|
| 10 |
TITLE_PATTERN = re.compile(r"\*\*Title:\*\*\s*(.+)", re.MULTILINE)
|
| 11 |
-
_FILENAME_TITLE = re.compile(r"^.+?\s-\s\d{4}\s-\s(.+)$")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
def summary_search_dirs() -> list[Path]:
|
|
@@ -73,11 +113,14 @@ def extract_title(file_name: str, summary: str | None = None) -> str:
|
|
| 73 |
|
| 74 |
|
| 75 |
def summary_path(file_name: str) -> Path | None:
|
|
|
|
| 76 |
for root in summary_search_dirs():
|
| 77 |
candidate = root / file_name
|
| 78 |
if candidate.exists():
|
| 79 |
return candidate
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
|
| 82 |
|
| 83 |
def load_summary(file_name: str) -> str | None:
|
|
|
|
| 5 |
import re
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
+
from huggingface_hub import hf_hub_download
|
| 9 |
from local_rag.config import LocalRagConfig
|
| 10 |
|
| 11 |
TITLE_PATTERN = re.compile(r"\*\*Title:\*\*\s*(.+)", re.MULTILINE)
|
| 12 |
+
_FILENAME_TITLE = re.compile(r"^.+?\\s-\\s\\d{4}\\s-\\s(.+)$")
|
| 13 |
+
_FILENAME_YEAR = re.compile(r"^.+?\\s-\\s(\\d{4})\\s-\\s")
|
| 14 |
+
|
| 15 |
+
HF_DATASET_REPO = os.getenv("WATERSCOPE_DATASET_REPO", "EnvironmentalAI/WaterScopeAI-Data")
|
| 16 |
+
HF_SUMMARY_PREFIX = os.getenv("WATERSCOPE_SUMMARY_PREFIX", "Summary")
|
| 17 |
+
HF_SUMMARY_CACHE = Path(
|
| 18 |
+
os.getenv("WATERSCOPE_SUMMARY_CACHE", "/tmp/waterscope-summary-cache")
|
| 19 |
+
).expanduser().resolve()
|
| 20 |
+
HF_SUMMARY_ON_DEMAND = os.getenv("WATERSCOPE_SUMMARY_ON_DEMAND", "1").lower() in {
|
| 21 |
+
"1", "true", "yes"
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def _year_from_filename(file_name: str) -> str | None:
|
| 26 |
+
match = _FILENAME_YEAR.match(Path(file_name).name)
|
| 27 |
+
return match.group(1) if match else None
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def _download_cloud_summary(file_name: str) -> Path | None:
|
| 31 |
+
"""Download one summary from the HF Dataset and cache it locally."""
|
| 32 |
+
if not HF_SUMMARY_ON_DEMAND:
|
| 33 |
+
return None
|
| 34 |
+
|
| 35 |
+
year = _year_from_filename(file_name)
|
| 36 |
+
if not year:
|
| 37 |
+
return None
|
| 38 |
+
|
| 39 |
+
repo_file = f"{HF_SUMMARY_PREFIX}/{year}/{Path(file_name).name}"
|
| 40 |
+
try:
|
| 41 |
+
downloaded = hf_hub_download(
|
| 42 |
+
repo_id=HF_DATASET_REPO,
|
| 43 |
+
repo_type="dataset",
|
| 44 |
+
filename=repo_file,
|
| 45 |
+
local_dir=str(HF_SUMMARY_CACHE),
|
| 46 |
+
)
|
| 47 |
+
except Exception:
|
| 48 |
+
return None
|
| 49 |
+
|
| 50 |
+
path = Path(downloaded)
|
| 51 |
+
return path if path.is_file() else None
|
| 52 |
|
| 53 |
|
| 54 |
def summary_search_dirs() -> list[Path]:
|
|
|
|
| 113 |
|
| 114 |
|
| 115 |
def summary_path(file_name: str) -> Path | None:
|
| 116 |
+
# Preserve existing local behavior first.
|
| 117 |
for root in summary_search_dirs():
|
| 118 |
candidate = root / file_name
|
| 119 |
if candidate.exists():
|
| 120 |
return candidate
|
| 121 |
+
|
| 122 |
+
# Cloud production fallback: fetch only the requested summary.
|
| 123 |
+
return _download_cloud_summary(file_name)
|
| 124 |
|
| 125 |
|
| 126 |
def load_summary(file_name: str) -> str | None:
|