Spaces:
Sleeping
Sleeping
Deploy RAG Tourism API
Browse files- .idea/workspace.xml +1 -1
- app/main.py +21 -4
.idea/workspace.xml
CHANGED
|
@@ -157,7 +157,7 @@
|
|
| 157 |
<workItem from="1780806816923" duration="5651000" />
|
| 158 |
<workItem from="1781019152997" duration="1850000" />
|
| 159 |
<workItem from="1784951212519" duration="2143000" />
|
| 160 |
-
<workItem from="1785045303244" duration="
|
| 161 |
</task>
|
| 162 |
<servers />
|
| 163 |
</component>
|
|
|
|
| 157 |
<workItem from="1780806816923" duration="5651000" />
|
| 158 |
<workItem from="1781019152997" duration="1850000" />
|
| 159 |
<workItem from="1784951212519" duration="2143000" />
|
| 160 |
+
<workItem from="1785045303244" duration="8380000" />
|
| 161 |
</task>
|
| 162 |
<servers />
|
| 163 |
</component>
|
app/main.py
CHANGED
|
@@ -141,6 +141,15 @@ def _same_category(value: str, target: str) -> bool:
|
|
| 141 |
return _strip_diacritics(value) == _strip_diacritics(target)
|
| 142 |
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
# Cache of known old district/city names for fuzzy matching (filled lazily)
|
| 145 |
_district_names_cache = None
|
| 146 |
_province_names_cache = None
|
|
@@ -232,8 +241,13 @@ async def startup_event():
|
|
| 232 |
for m in metadatas
|
| 233 |
if isinstance(m, dict)
|
| 234 |
)
|
| 235 |
-
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
kg_locations = kg_service.search_locations_by_name('', limit=5000)
|
| 238 |
if kg_locations and vector_service.build_index(kg_locations):
|
| 239 |
logger.info("ChromaDB index rebuilt successfully from KG data")
|
|
@@ -268,8 +282,11 @@ async def startup_event():
|
|
| 268 |
sample = phobert_service.collection.get(limit=5, include=["metadatas"]) if phobert_service.collection else None
|
| 269 |
metas = (sample or {}).get("metadatas") or []
|
| 270 |
ok = any(isinstance(m, dict) and (m.get("ward") or "").strip() for m in metas)
|
| 271 |
-
|
| 272 |
-
|
|
|
|
|
|
|
|
|
|
| 273 |
kg_locations = kg_service.search_locations_by_name('', limit=5000)
|
| 274 |
if kg_locations and phobert_service.build_index(kg_locations):
|
| 275 |
logger.info("PhoBERT index rebuilt successfully")
|
|
|
|
| 141 |
return _strip_diacritics(value) == _strip_diacritics(target)
|
| 142 |
|
| 143 |
|
| 144 |
+
def _kg_location_count(kg) -> Optional[int]:
|
| 145 |
+
"""Số Location trong KG — để so với số trong vector index, tự rebuild khi lệch."""
|
| 146 |
+
try:
|
| 147 |
+
with kg.driver.session() as s:
|
| 148 |
+
return s.run("MATCH (l:Location) RETURN count(l) AS c").single()["c"]
|
| 149 |
+
except Exception:
|
| 150 |
+
return None
|
| 151 |
+
|
| 152 |
+
|
| 153 |
# Cache of known old district/city names for fuzzy matching (filled lazily)
|
| 154 |
_district_names_cache = None
|
| 155 |
_province_names_cache = None
|
|
|
|
| 241 |
for m in metadatas
|
| 242 |
if isinstance(m, dict)
|
| 243 |
)
|
| 244 |
+
# Rebuild khi thiếu metadata HOẶC số lượng trong index lệch với KG
|
| 245 |
+
# (vd vừa thêm khách sạn/nhà hàng vào KG mà index chưa cập nhật).
|
| 246 |
+
_idx_n = vector_service.collection.count() if vector_service.collection else 0
|
| 247 |
+
_kg_n = _kg_location_count(kg_service) if kg_service else None
|
| 248 |
+
_stale = (_kg_n is not None and _idx_n < _kg_n)
|
| 249 |
+
if (not has_location_fields or _stale) and kg_service:
|
| 250 |
+
logger.warning(f"ChromaDB index cần rebuild (index={_idx_n}, KG={_kg_n}); rebuilding từ Neo4j KG...")
|
| 251 |
kg_locations = kg_service.search_locations_by_name('', limit=5000)
|
| 252 |
if kg_locations and vector_service.build_index(kg_locations):
|
| 253 |
logger.info("ChromaDB index rebuilt successfully from KG data")
|
|
|
|
| 282 |
sample = phobert_service.collection.get(limit=5, include=["metadatas"]) if phobert_service.collection else None
|
| 283 |
metas = (sample or {}).get("metadatas") or []
|
| 284 |
ok = any(isinstance(m, dict) and (m.get("ward") or "").strip() for m in metas)
|
| 285 |
+
_idx_n = phobert_service.collection.count() if phobert_service.collection else 0
|
| 286 |
+
_kg_n = _kg_location_count(kg_service) if kg_service else None
|
| 287 |
+
_stale = (_kg_n is not None and _idx_n < _kg_n)
|
| 288 |
+
if (not ok or _stale) and kg_service:
|
| 289 |
+
logger.warning(f"PhoBERT index cần rebuild (index={_idx_n}, KG={_kg_n}); rebuilding từ KG...")
|
| 290 |
kg_locations = kg_service.search_locations_by_name('', limit=5000)
|
| 291 |
if kg_locations and phobert_service.build_index(kg_locations):
|
| 292 |
logger.info("PhoBERT index rebuilt successfully")
|