Update page_files/categorized/Backend/PDF_DataExtraction.py
Browse files
page_files/categorized/Backend/PDF_DataExtraction.py
CHANGED
|
@@ -284,10 +284,14 @@ def _find_value_matches(value_str: str, sentences: List[Dict[str, Any]]) -> List
|
|
| 284 |
|
| 285 |
|
| 286 |
def verify_dataframe(df: pd.DataFrame, sentences: List[Dict[str, Any]]) -> pd.DataFrame:
|
|
|
|
|
|
|
| 287 |
if df.empty or not sentences:
|
| 288 |
if not df.empty:
|
| 289 |
df = df.copy()
|
| 290 |
df["source_verified"] = False
|
|
|
|
|
|
|
| 291 |
return df
|
| 292 |
|
| 293 |
df = df.copy()
|
|
@@ -297,7 +301,10 @@ def verify_dataframe(df: pd.DataFrame, sentences: List[Dict[str, Any]]) -> pd.Da
|
|
| 297 |
except Exception as e:
|
| 298 |
st.warning(f"Embedding model unavailable, meaning-check skipped: {e}")
|
| 299 |
|
| 300 |
-
verified = []
|
|
|
|
|
|
|
|
|
|
| 301 |
for _, row in df.iterrows():
|
| 302 |
prop = str(row.get("property_name", "") or "").strip()
|
| 303 |
mat = str(row.get("material_name", "") or "").strip()
|
|
@@ -307,24 +314,41 @@ def verify_dataframe(df: pd.DataFrame, sentences: List[Dict[str, Any]]) -> pd.Da
|
|
| 307 |
candidates = _find_value_matches(val, sentences)
|
| 308 |
if not candidates:
|
| 309 |
verified.append(False)
|
|
|
|
|
|
|
| 310 |
continue
|
| 311 |
|
| 312 |
-
# STEP 2 — of the sentences containing that value,
|
| 313 |
-
# meaning
|
|
|
|
| 314 |
if model is None:
|
| 315 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
continue
|
|
|
|
| 317 |
query = f"{prop} {mat}".strip()
|
| 318 |
try:
|
| 319 |
q_vec = model.encode([query], normalize_embeddings=True)[0]
|
| 320 |
texts = [c["text"] for c in candidates]
|
| 321 |
c_vecs = model.encode(texts, normalize_embeddings=True)
|
| 322 |
-
|
|
|
|
|
|
|
| 323 |
except Exception:
|
| 324 |
-
best_score = 0.0
|
|
|
|
|
|
|
| 325 |
verified.append(best_score >= MEANING_THRESHOLD)
|
|
|
|
|
|
|
| 326 |
|
| 327 |
df["source_verified"] = verified
|
|
|
|
|
|
|
| 328 |
return df
|
| 329 |
|
| 330 |
|
|
@@ -355,7 +379,7 @@ def main():
|
|
| 355 |
st.session_state["fv_sig"] = sig
|
| 356 |
st.session_state["fv_result"] = None
|
| 357 |
|
| 358 |
-
if st.button("
|
| 359 |
with st.spinner("Extracting…"):
|
| 360 |
data = call_gemini_from_bytes(pdf_bytes)
|
| 361 |
doi = resolve_doi(pdf_bytes, data)
|
|
|
|
| 284 |
|
| 285 |
|
| 286 |
def verify_dataframe(df: pd.DataFrame, sentences: List[Dict[str, Any]]) -> pd.DataFrame:
|
| 287 |
+
# Always emit the three provenance columns, even on the empty/short-circuit
|
| 288 |
+
# paths, so downstream schemas can rely on them existing.
|
| 289 |
if df.empty or not sentences:
|
| 290 |
if not df.empty:
|
| 291 |
df = df.copy()
|
| 292 |
df["source_verified"] = False
|
| 293 |
+
df["source_text"] = ""
|
| 294 |
+
df["source_page"] = ""
|
| 295 |
return df
|
| 296 |
|
| 297 |
df = df.copy()
|
|
|
|
| 301 |
except Exception as e:
|
| 302 |
st.warning(f"Embedding model unavailable, meaning-check skipped: {e}")
|
| 303 |
|
| 304 |
+
verified: List[bool] = []
|
| 305 |
+
source_texts: List[str] = []
|
| 306 |
+
source_pages: List[Any] = []
|
| 307 |
+
|
| 308 |
for _, row in df.iterrows():
|
| 309 |
prop = str(row.get("property_name", "") or "").strip()
|
| 310 |
mat = str(row.get("material_name", "") or "").strip()
|
|
|
|
| 314 |
candidates = _find_value_matches(val, sentences)
|
| 315 |
if not candidates:
|
| 316 |
verified.append(False)
|
| 317 |
+
source_texts.append("")
|
| 318 |
+
source_pages.append("")
|
| 319 |
continue
|
| 320 |
|
| 321 |
+
# STEP 2 — of the sentences containing that value, pick the one whose
|
| 322 |
+
# meaning best matches the claimed property + material, and record it
|
| 323 |
+
# as this row's source_text / source_page.
|
| 324 |
if model is None:
|
| 325 |
+
# No embedder: value confirmed, meaning-check unavailable. Record the
|
| 326 |
+
# first value-matched sentence rather than over-penalize.
|
| 327 |
+
best = candidates[0]
|
| 328 |
+
verified.append(True)
|
| 329 |
+
source_texts.append(best["text"])
|
| 330 |
+
source_pages.append(best["page"])
|
| 331 |
continue
|
| 332 |
+
|
| 333 |
query = f"{prop} {mat}".strip()
|
| 334 |
try:
|
| 335 |
q_vec = model.encode([query], normalize_embeddings=True)[0]
|
| 336 |
texts = [c["text"] for c in candidates]
|
| 337 |
c_vecs = model.encode(texts, normalize_embeddings=True)
|
| 338 |
+
scores = [float(np.dot(q_vec, v)) for v in c_vecs]
|
| 339 |
+
best_i = int(np.argmax(scores)) if scores else 0
|
| 340 |
+
best_score = scores[best_i] if scores else 0.0
|
| 341 |
except Exception:
|
| 342 |
+
best_i, best_score = 0, 0.0
|
| 343 |
+
|
| 344 |
+
best = candidates[best_i]
|
| 345 |
verified.append(best_score >= MEANING_THRESHOLD)
|
| 346 |
+
source_texts.append(best["text"])
|
| 347 |
+
source_pages.append(best["page"])
|
| 348 |
|
| 349 |
df["source_verified"] = verified
|
| 350 |
+
df["source_text"] = source_texts
|
| 351 |
+
df["source_page"] = source_pages
|
| 352 |
return df
|
| 353 |
|
| 354 |
|
|
|
|
| 379 |
st.session_state["fv_sig"] = sig
|
| 380 |
st.session_state["fv_result"] = None
|
| 381 |
|
| 382 |
+
if st.button("Run Extraction", type="primary", use_container_width=True):
|
| 383 |
with st.spinner("Extracting…"):
|
| 384 |
data = call_gemini_from_bytes(pdf_bytes)
|
| 385 |
doi = resolve_doi(pdf_bytes, data)
|