Update page_files/Upload_Data.py
Browse files22 unmodified lines232425262728291 unmodified line313233343536371 unmodified line39404142434445464748495051525354555657585960616263646566673 unmodified lines7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221193 unmodified lines415416417418419420421422423424425426427428429430431432433434141 unmodified lines5765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756762 unmodified lines6796806816826836846856866876886896906916926936946956966976986997007017027037047057061 unmodified line7087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437443 unmodified lines7487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011918 unmodified lines120012011202120312041205120612071208120912101211121212131214121530 unmodified lines12461247124812491250125112521253125412551256125712581259126012611262126312642 unmodified lines12671268126912701271127212731274127512761 unmodified line1278127912801281128222 unmodified linesimport streamlit as stfrom PIL import Imagefrom dotenv import load_dotenvload_dotenv()1 unmodified lineif not _GEMINI_API_KEY:raise RuntimeError("GEMINI_API_KEY not set in environment")# ββ imports from doctodb_rag (data extraction) ββββββββββββββββββββββββββββββββfrom categorized.Backend.PDF_DataExtraction import (call_gemini_from_bytes,convert_to_dataframe,1 unmodified lineverify_dataframe,)# ββ imports from figure_extractor (image extraction) βββββββββββββββββββββββββfrom categorized.Backend.Pdf_ImageExtraction import (GEMINI_MODEL as GEMINI_MODEL,get_plot_data_from_llm,extract_plots,)from data_loader import insert_material_rowsfrom categorized.Backend.plot_property_mapper import (batch_map_plots,fetch_properties_for_material,save_plot_image_mapping,save_plot_image_to_db,)from db import fetch_all# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Helpers that were previously in upload_backend# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef _df_to_meta(df: pd.DataFrame) -> dict:"""Re-create the flat metadata dict that the UI previously got from Gemini."""if df.empty:return {}row0 = df.iloc[0]3 unmodified lines"material_abbreviation": str(row0.get("material_abbreviation", "")),"trade_grade": str(row0.get("trade_grade", "")),"manufacturer": str(row0.get("manufacturer", "")),"mechanical_properties": props,}def create_zip(image_results: list, include_json: bool = True) -> bytes:"""Pack extracted plot images (and optional JSON metadata) into a ZIP.Each item in image_results has: caption, page, image_data (list of dictswith 'array' (BGR ndarray) and 'filename')."""buf = io.BytesIO()with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:meta = []for item in image_results:caption = item.get("caption", "")page = item.get("page", "?")for img_dict in item.get("image_data", []):bgr = img_dict.get("array")filename = img_dict.get("filename", "plot.png")if bgr is not None:ok, enc = cv2.imencode(".png", bgr)if ok:zf.writestr(filename, enc.tobytes())if include_json:meta.append({"caption": caption,"page": page,"image_count": len(item.get("image_data", [])),"images": [d.get("filename") for d in item.get("image_data", [])],})if include_json and meta:zf.writestr("metadata.json", json.dumps(meta, indent=4))return buf.getvalue()def save_matched_images(df: pd.DataFrame,image_results: list,save_dir: str = "images",) -> list:"""Heuristically match extracted plot captions to property names in df andsave matched images to disk. Returns list of match-info dicts."""os.makedirs(save_dir, exist_ok=True)saved = []props = df["property_name"].str.lower().tolist() if "property_name" in df.columns else []for item in image_results:caption = (item.get("caption") or "").lower()best_prop = Nonebest_score = 0for prop in props:# simple overlap score: shared wordscap_words = set(re.findall(r"\w+", caption))prop_words = set(re.findall(r"\w+", prop))score = len(cap_words & prop_words)if score > best_score:best_score = scorebest_prop = propif best_prop and best_score > 0:for idx, img_dict in enumerate(item.get("image_data", [])):bgr = img_dict.get("array")if bgr is None:continuesafe_prop = re.sub(r"[^\w\-]", "_", best_prop)filename = f"{safe_prop}_{idx}.png"filepath = os.path.join(save_dir, filename)cv2.imwrite(filepath, bgr)saved.append({"property": best_prop,"caption": item.get("caption", ""),"path": filepath,})return saveddef save_single_image_with_property(bgr: np.ndarray,property_name: str,save_dir: str = "images",) -> str:"""Save a single BGR image tagged with a property name. Returns filepath."""os.makedirs(save_dir, exist_ok=True)safe = re.sub(r"[^\w\-]", "_", property_name)filepath = os.path.join(save_dir, f"{safe}.png")cv2.imwrite(filepath, bgr)return filepath# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# extract_images adapter# Bridges figure_extractor's extract_plots API to the image_results list shape# expected by the rest of the UI (list of {caption, page, image_data}).# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ_GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "AIzaSyBzyMFKEqcjsWpR-OGAY42T250o1O39v3Y")def extract_images(pdf_path: str) -> list:"""Use figure_extractor to detect and crop plot images from a PDF path.Returns a list compatible with the image_results shape used throughout the UI:[{ "caption": str, "page": int, "image_data": [{"array": bgr_ndarray, "filename": str}] }]"""try:# gemini_model = init_gemini(_GEMINI_API_KEY)plot_data = get_plot_data_from_llm( GEMINI_MODEL, pdf_path)raw_plots = extract_plots(pdf_path=pdf_path,plot_data=plot_data,pad=22,score_thresh=0.35,)except Exception as e:log.error(f"extract_images failed: {e}")return []# raw_plots items: {caption, page, path, plot_score, plot_type}# Convert to image_results shapeimage_results = []for item in raw_plots:bgr = cv2.imread(item["path"]) if item.get("path") else None# clean up temp file written by extract_plotsif item.get("path") and os.path.exists(item["path"]):try:os.remove(item["path"])except Exception:passpage = item.get("page", 1)caption = item.get("caption", f"Figure (page {page})")safe = re.sub(r"[^\w\-]", "_", caption)[:40]filename = f"page{page}_{safe}.png"image_results.append({"caption": caption,"page": page,"image_data": [{"array": bgr, "filename": filename}] if bgr is not None else [],})return image_results# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ193 unmodified lines# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Helpers for tab2 mapping UI# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef _confidence_badge(conf: str) -> str:colors = {"high": "#16a34a", "medium": "#d97706", "low": "#dc2626"}c = colors.get((conf or "low").lower(), "#6b7280")return (f"<span class='conf-badge' style='background:{c}'>"f"{conf.upper()}</span>")# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Manual input form# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef input_form():141 unmodified lines# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Tab 1: Material Data# Uses run_pipeline from doctodb_rag instead of call_gemini_from_bytes# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# def render_material_data_tab(pdf_path: str):# st.subheader("Material Properties Data")# if not st.session_state.pdf_data_extracted:# with st.spinner("Extracting material dataβ¦"):# with open(pdf_path, "rb") as f:# pdf_bytes = f.read()# df, df_gemini, df_gpt, _chunks, api_errors, meta = run_pipeline(pdf_bytes)# if api_errors:# for err in api_errors:# st.warning(err)# if not df.empty:# # Build the metadata dict that the rest of the UI expects# data = _df_to_meta(df)# st.session_state.pdf_extracted_df = df# st.session_state.pdf_data_extracted = True# st.session_state.pdf_extracted_meta = data# else:# st.warning("No data extracted from PDF.")# df = st.session_state.pdf_extracted_df# if df.empty:# return# meta = st.session_state.get("pdf_extracted_meta", {})# st.success(f"Extracted {len(df)} properties")# col1, col2 = st.columns(2)# col1.metric("Material", meta.get("material_name", "N/A"))# col2.metric("Abbreviation", meta.get("material_abbreviation", "N/A"))# st.dataframe(df, use_container_width=True, height=400)# st.subheader("Assign Material Category")# extracted_material_class = st.selectbox(# "Select category for this material",# ["Polymer", "Fiber", "Composite"],# index=None,# placeholder="Required before adding to database",# key="tab1_material_class",# )# if st.button("+ Add to Database"):# if not extracted_material_class:# st.error("Please select a material category before adding.")# return# df["material_class"] = extracted_material_class# df["material_type"] = extracted_material_class# if st.session_state.image_results:# with st.spinner("Saving matched plot imagesβ¦"):# saved_images = save_matched_images(# df, st.session_state.image_results, save_dir="images"# )# if saved_images:# st.success(f"Saved {len(saved_images)} plot image(s)")# with st.expander("View saved images"):# for img_info in saved_images:# st.write(f"**{img_info['property']}** β {img_info['caption']}")# st.write(f"Saved to: `{img_info['path']}`")# else:# st.info("No plots matched the extracted properties automatically.")# st.session_state.setdefault("user_uploaded_data", pd.DataFrame())# st.session_state["user_uploaded_data"] = pd.concat(# [st.session_state["user_uploaded_data"], df], ignore_index=True# )# st.success(f"Added to {extracted_material_class} database!")# ββ Stage labels and estimated durations for the progress display βββββββββββββ_STAGE_LABELS = {0.00: ("Checking cache", 2),0.05: ("Extracting tables & text", 15),0.20: ("Extraction complete", 0),0.25: ("Indexing into ChromaDB", 8),0.40: ("Ranking chunks", 5),0.50: ("Ranking complete", 0),0.55: ("Building batches", 2),0.60: ("Running Gemini + GPT-4o", 30),0.90: ("Merging results", 3),0.95: ("Consensus filtering", 4),1.00: ("Done", 0),}def _nearest_stage_label(pct: float) -> tuple[str, int]:"""Return (label, est_seconds_remaining) for the closest stage."""best_key = min(_STAGE_LABELS, key=lambda k: abs(k - pct))return _STAGE_LABELS[best_key]2 unmodified linesst.subheader("Material Properties Data")if not st.session_state.pdf_data_extracted:bar = st.progress(0.0)status = st.empty() # stage label + ETAtimer = st.empty() # elapsed clockstart_ts = time.time()def _cb(msg: str, pct: float):elapsed = time.time() - start_tslabel, est_remaining = _nearest_stage_label(pct)bar.progress(min(pct, 1.0))status.markdown(f"**{label}** Β· <span style='color:#64748b'>{msg}</span>",unsafe_allow_html=True,)if est_remaining > 0:timer.caption(f"β± Elapsed: {elapsed:.0f}s Β· "f"Est. remaining: ~{est_remaining}s")else:timer.caption(f"β± Elapsed: {elapsed:.0f}s")with open(pdf_path, "rb") as f:pdf_bytes = f.read()1 unmodified line_cb("Extracting via Geminiβ¦", 0.30)data = call_gemini_from_bytes(pdf_bytes)df = convert_to_dataframe(data)api_errors = []if not df.empty:_cb("Verifying against source PDFβ¦", 0.70)sentences = _extract_sentences(pdf_bytes)df = verify_dataframe(df, sentences)row0 = df.iloc[0] if not df.empty else {}meta = {"material_name": str(row0.get("material_name", "")) if not df.empty else "","material_abbreviation": str(row0.get("material_abbreviation", "")) if not df.empty else "",}_cb("Done.", 1.0)elapsed_total = time.time() - start_tsbar.progress(1.0)status.empty()timer.empty()if api_errors:for err in api_errors:st.warning(err)if not df.empty:data = _df_to_meta(df)st.session_state.pdf_extracted_df = dfst.session_state.pdf_data_extracted = Truest.session_state.pdf_extracted_meta = datast.success(f"β Extracted {len(df)} properties in {elapsed_total:.0f}s"+ (f" Β· {meta.get('batches', '?')} batch(es)" if meta.get('batches') else ""))else:st.warning("No data extracted from PDF.")return3 unmodified linesreturnmeta = st.session_state.get("pdf_extracted_meta", {})col1, col2 = st.columns(2)col1.metric("Material", meta.get("material_name", "N/A"))col2.metric("Abbreviation", meta.get("material_abbreviation", "N/A"))st.dataframe(df, use_container_width=True, height=400)st.subheader("Assign Material Category")extracted_material_class = st.selectbox("Select category for this material",["Polymer", "Fiber", "Composite"],index=None,placeholder="Required before adding to database",key="tab1_material_class",)if st.button("+ Add to Database"):if not extracted_material_class:st.error("Please select a material category before adding.")returndf["material_class"] = extracted_material_classdf["material_type"] = extracted_material_classif st.session_state.image_results:with st.spinner("Saving matched plot imagesβ¦"):saved_images = save_matched_images(df, st.session_state.image_results, save_dir="images")if saved_images:st.success(f"Saved {len(saved_images)} plot image(s)")with st.expander("View saved images"):for img_info in saved_images:st.write(f"**{img_info['property']}** β {img_info['caption']}")st.write(f"Saved to: `{img_info['path']}`")else:st.info("No plots matched the extracted properties automatically.")st.session_state.setdefault("user_uploaded_data", pd.DataFrame())st.session_state["user_uploaded_data"] = pd.concat([st.session_state["user_uploaded_data"], df], ignore_index=True)st.success(f"Added to {extracted_material_class} database!")# βββββββββββββββββββββββοΏ½οΏ½βββββββββββββββββββββββββββββββββββββββββββββββββββββ# Tab 2: Extracted Plots + AI Property Mapping# Uses extract_images (adapter above) instead of upload_backend's version# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef render_plots_tab(pdf_path: str, paper_id: str):st.subheader("Extracted Plot Images & Property Mapping")if not st.session_state.pdf_processed:with st.spinner("Extracting plots from PDFβ¦"):st.session_state.image_results = extract_images(pdf_path)st.session_state.pdf_processed = Truest.session_state.mapping_done = Falseimage_results = st.session_state.image_resultsif not image_results:st.warning("No plots found in this PDF.")returnhas_data = not st.session_state.pdf_extracted_df.emptyif has_data:mat_abbr = st.session_state.pdf_extracted_df.iloc[0]["material_abbreviation"]property_list = st.session_state.pdf_extracted_df["property_name"].unique().tolist()st.info(f"**{len(image_results)} plots** extracted | "f"Material: **{mat_abbr}** | "f"{len(property_list)} properties available for mapping")else:st.warning("Extract material data in the **Material Data** tab first ""to enable AI property mapping.")subtab_images, subtab_json = st.tabs(["πΌ Images & Mapping", "{ } JSON Preview"])# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββwith subtab_images:col_img, col_json_dl, col_all = st.columns(3)with col_img:st.download_button("β¬ Images Only",data=create_zip(image_results, include_json=False),file_name=f"{paper_id}_images.zip",mime="application/zip",use_container_width=True,key="dl_images",)with col_json_dl:json_meta = [{"caption": r["caption"], "page": r["page"],"image_count": len(r["image_data"])}for r in image_results]st.download_button("β¬ JSON",data=json.dumps(json_meta, indent=4),file_name=f"{paper_id}_metadata.json",mime="application/json",use_container_width=True,key="dl_json",)with col_all:st.download_button("β¬ Download All",data=create_zip(image_results, include_json=True),file_name=f"{paper_id}_complete.zip",mime="application/zip",use_container_width=True,key="dl_all",)st.divider()if has_data:col_cls, col_btn = st.columns([0.45, 0.55])with col_cls:map_class = st.selectbox("Material class for DB lookup",["Polymer", "Fiber", "Composite"],key="mapping_material_class",help="Routes to the correct PostgreSQL table.",)with col_btn:st.write("")st.write("")run_mapping = st.button("π€ Run AI Property Mapping",type="primary",disabled=st.session_state.get("mapping_done", False),use_container_width=True,)if run_mapping:df = st.session_state.pdf_extracted_dfmat_abbr = df.iloc[0]["material_abbreviation"]extracted_json = st.session_state.get("pdf_extracted_meta", {})with st.spinner("Fetching properties from PostgreSQLβ¦"):try:db_properties = fetch_properties_for_material(mat_abbr, map_class, fetch_all)except Exception as exc:st.error(f"DB error: {exc}")db_properties = []if not db_properties:st.warning(f"No DB rows found for **{mat_abbr}** in the **{map_class}** table. ""Mapping will use all available properties from the extracted data.")prog = st.progress(0, text="Startingβ¦")def _on_progress(i, total, caption):pct = int((i / max(total, 1)) * 100)prog.progress(pct, text=f"Mapping {i+1}/{total}: {caption[:55]}β¦")with st.spinner("AI is analysing plotsβ¦"):mapped = batch_map_plots(image_results=image_results,extracted_json=extracted_json,db_properties=db_properties,progress_callback=_on_progress,)prog.progress(100, text="Done β")st.session_state.mapped_results = mappedst.session_state.mapping_done = Truest.success(f"β Mapped {len(mapped)} plots β review below.")st.rerun()if st.session_state.get("mapping_done"):col_info, col_reset = st.columns([0.78, 0.22])col_info.caption("AI mapping complete. The dropdown for each plot is pre-filled ""with the suggestion β override freely, then hit **Save**.")if col_reset.button("βΊ Re-run Mapping", use_container_width=True):st.session_state.mapping_done = Falsest.session_state.mapped_results = []st.rerun()st.divider()use_mapped = (has_dataand st.session_state.get("mapping_done", False)and bool(st.session_state.get("mapped_results")))display_list = (st.session_state.mapped_results if use_mapped else image_results)for idx in range(len(display_list)):if idx >= len(display_list):breakitem = display_list[idx]caption = item.get("caption", f"Figure {idx+1}")page = item.get("page", "?")img_list = item.get("image_data", [])mapping = item.get("mapping_result") if use_mapped else Nonewith st.container(border=True):col_cap, col_del = st.columns([0.87, 0.13])col_cap.markdown(f"**Page {page}** β {caption}")if col_del.button("π", key=f"del_grp_{idx}", help="Delete this figure"):display_list.pop(idx)if use_mapped:st.session_state.mapped_results = display_listelse:st.session_state.image_results = display_listst.rerun()if mapping:prop_name = mapping.get("property_name", "")section = mapping.get("section", "")confidence = mapping.get("confidence", "low")reasoning = mapping.get("reasoning", "")db_row = mapping.get("db_row")candidates = mapping.get("all_candidates", [])if prop_name:badge = _confidence_badge(confidence)st.markdown(f"π **AI Match:** `{section}` βΊ **{prop_name}** {badge}",unsafe_allow_html=True,)if reasoning:st.caption(f"π¬ {reasoning}")if db_row:with st.expander("π Matched DB row", expanded=False):c1, c2, c3 = st.columns(3)c1.metric("Value", db_row.get("value", "β"))c2.metric("Unit", db_row.get("unit", "β"))c3.metric("Condition", db_row.get("test_condition", "β"))if db_row.get("comments"):st.caption(f"Comments: {db_row['comments']}")if db_row.get("english"):st.caption(f"English units: {db_row['english']}")if candidates:with st.expander("π All candidates", expanded=False):for c in candidates:st.markdown(f"{c.get('rank','?')}. `{c.get('section','?')}` βΊ "f"**{c.get('property_name','?')}** "f"{_confidence_badge(c.get('confidence','low'))}",unsafe_allow_html=True,)else:st.warning("β οΈ AI could not match this plot to any DB property.")for p_idx in range(len(img_list)):if p_idx >= len(item.get("image_data", [])):breakimg_data = item["image_data"][p_idx]bgr = img_data.get("array")if bgr is None:continueimg_key = f"{idx}_{p_idx}_{page}"st.image(bgr, channels="BGR", width=420)if has_data:df = st.session_state.pdf_extracted_dfmat_abbr = df.iloc[0]["material_abbreviation"]property_list = df["property_name"].unique().tolist()options = ["β Select property β"] + property_listai_prop = mapping.get("property_name", "") if mapping else ""ai_section = mapping.get("section", "") if mapping else ""default_idx = (property_list.index(ai_prop) + 1if ai_prop in property_list else 0)col_sel, col_sec, col_save, col_rem = st.columns([0.40, 0.20, 0.20, 0.20])with col_sel:selected = st.selectbox("Property",options=options,index=default_idx,key=f"prop_sel_{img_key}",label_visibility="collapsed",)with col_sec:section_options = ["Mechanical","Thermal","Processing","Physical","Descriptive","Composition / Reinforcement","Architecture / Structure",]section_default = (section_options.index(ai_section)if ai_section in section_optionselse 0)section_val = st.selectbox("Section",options=section_options,index=section_default,key=f"sec_{img_key}",label_visibility="collapsed",)with col_save:if st.button("πΎ Save", key=f"save_{img_key}",use_container_width=True):if selected and selected != "β Select property β":filepath = save_plot_image_mapping(mat_abbr, selected, section_val,bgr, save_dir="images",)try:from db import execute_querysaved_to_db = save_plot_image_to_db(material_abbr=mat_abbr,property_name=selected,image_bgr=bgr,material_class=st.session_state.get("mapping_material_class", "Polymer"),execute_query_fn=execute_query,)if saved_to_db:st.success(f"β Saved to DB & disk β "f"`{os.path.basename(filepath)}`")else:st.warning("β οΈ Saved to disk only β ""no matching DB row found for this property.")except Exception as e:st.error(f"DB save failed: {e}")st.info(f"Saved locally β `{os.path.basename(filepath)}`")st.session_state.saved_image_mapping[img_key] = {"property": selected,"section": section_val,"caption": caption,"filename": os.path.basename(filepath),"path": filepath,}st.rerun()else:st.warning("Select a property first.")with col_rem:if st.button("β", key=f"rem_{img_key}",use_container_width=True, help="Remove image"):if img_key in st.session_state.saved_image_mapping:del st.session_state.saved_image_mapping[img_key]item["image_data"].pop(p_idx)if not item["image_data"]:display_list.pop(idx)if use_mapped:st.session_state.mapped_results = display_listelse:st.session_state.image_results = display_listst.rerun()if img_key in st.session_state.saved_image_mapping:saved_m = st.session_state.saved_image_mapping[img_key]st.info(f"β Saved as **{saved_m['property']}** β "f"`{saved_m['filename']}`")else:col_msg, col_rem = st.columns([0.80, 0.20])col_msg.caption("Go to **Material Data** tab to extract properties and enable mapping.")if col_rem.button("β", key=f"rem_nd_{img_key}", help="Remove"):item["image_data"].pop(p_idx)if not item["image_data"]:st.session_state.image_results.pop(idx)st.rerun()st.divider()saved_map = st.session_state.saved_image_mappingif saved_map:with st.expander(f"π Saved mappings ({len(saved_map)})", expanded=False):for key, info in saved_map.items():st.markdown(f"**{info['property']}** βΊ `{info['filename']}` \n"f"<small style='color:#64748b'>Caption: {info['caption']}</small>",unsafe_allow_html=True,)# βββββοΏ½οΏ½ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββwith subtab_json:st.subheader("Metadata Preview")json_data = [{"caption": r["caption"],"page": r["page"],"image_count": len(r["image_data"]),"images": [img["filename"] for img in r["image_data"]],}for r in image_results]st.download_button("β¬ Download JSON",data=json.dumps(json_data, indent=4),file_name="metadata.json",mime="application/json",key="dl_json_bottom",)st.json(json_data)# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ8 unmodified linesst.caption("Provide technical data and research documentation for the central repository.")defaults = {"image_results": [],"mapped_results": [],"pdf_processed": False,"mapping_done": False,"current_pdf_name": None,"form_submitted": False,"pdf_data_extracted": False,"pdf_extracted_df": pd.DataFrame(),"pdf_extracted_meta": {},"saved_image_mapping": {},}for k, v in defaults.items():if k not in st.session_state:30 unmodified linesif st.session_state.form_submitted:st.session_state.form_submitted = Falsest.info("Form submitted. Previously extracted data has been saved. ""Upload again to process a new PDF.")st.tabs(["Material Data", "Extracted Plots"])returntab1, tab2 = st.tabs(["π Material Data", "πΌ Extracted Plots"])# Write to a stable temp file (avoids Windows WinError 267 on cleanup)tmp_file = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False, prefix="matdb_")try:tmp_file.write(uploaded_file.getbuffer())tmp_file.flush()2 unmodified lineswith tab1:render_material_data_tab(pdf_path)with tab2:render_plots_tab(pdf_path, paper_id)finally:try:os.unlink(tmp_file.name)1 unmodified linepassmain()22 unmodified lines2324252627281 unmodified line3031323334353637381 unmodified line40414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697983 unmodified lines102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136193 unmodified lines330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359141 unmodified lines5015025035045055065075085095105115125135145155165175182 unmodified lines5215225235245255265275285295305315325335345355365375385395405415425431 unmodified line5455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745753 unmodified lines5795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368 unmodified lines84584684784884985085185285385485585685785885986086186286330 unmodified lines8948958968978988999009019029039049059062 unmodified lines9099109119129139149159161 unmodified line91891992092122 unmodified linesimport streamlit as stfrom PIL import Imagefrom dotenv import load_dotenvload_dotenv()1 unmodified lineif not _GEMINI_API_KEY:raise RuntimeError("GEMINI_API_KEY not set in environment")# ββ Data extraction (Code 1) βββββββββββββββββββββββββββββββββββββββββββββββββ# NOTE: DOI is injected locally below (see _extract_doi_from_pdf). If you want# the DOI produced inside PDF_DataExtraction itself, port the same regex there.from categorized.Backend.PDF_DataExtraction import (call_gemini_from_bytes,convert_to_dataframe,1 unmodified lineverify_dataframe,)# ββ New integrated stack: image2 β mapper5 β category_push ββββββββββββββββββββ# These are imported as top-level modules (the same way mapper5 imports image2# and category_push). Ensure image2.py / mapper5.py / category_push.py are on# the import path (same directory or added to sys.path above). If you keep them# under categorized/Backend/, change these three lines to:# from categorized.Backend import image2, mapper5, category_pushimport image2import mapper5import category_push# Manual-entry path (unchanged β writes via the existing data_loader).from data_loader import insert_material_rows# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# DOI extraction β regex over raw PDF text (format-verified, precision-first)# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ_DOI_CORE_RE = re.compile(r'10\.\d{4,9}/[^\s"<>\]\)]+', re.IGNORECASE)def _clean_doi(doi: str) -> str:if not doi:return ""doi = doi.strip()doi = re.sub(r'^(?:https?://)?(?:dx\.)?doi\.org/', '', doi, flags=re.IGNORECASE)doi = re.sub(r'^doi[:\s]+', '', doi, flags=re.IGNORECASE)return doi.rstrip(' .,;:)]}>"\'')def _extract_doi_from_pdf(pdf_bytes: bytes) -> str:"""Prefer a doi.org URL, then a 'doi:' label, then any bare 10.xxxx/ token."""try:with fitz.open(stream=pdf_bytes, filetype="pdf") as doc:text = "\n".join((p.get_text("text") or "") for p in doc)except Exception:return ""m = re.search(r'(?:https?://)?(?:dx\.)?doi\.org/(10\.\d{4,9}/[^\s"<>\]\)]+)', text, re.IGNORECASE)if m:return _clean_doi(m.group(1))m = re.search(r'\bdoi[:\s]+\s*(10\.\d{4,9}/[^\s"<>\]\)]+)', text, re.IGNORECASE)if m:return _clean_doi(m.group(1))m = _DOI_CORE_RE.search(text)return _clean_doi(m.group(0)) if m else ""# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Metadata helper# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef _df_to_meta(df: pd.DataFrame) -> dict:"""Re-create the flat metadata dict the UI expects."""if df.empty:return {}row0 = df.iloc[0]3 unmodified lines"material_abbreviation": str(row0.get("material_abbreviation", "")),"trade_grade": str(row0.get("trade_grade", "")),"manufacturer": str(row0.get("manufacturer", "")),"doi": str(row0.get("doi", "")),"mechanical_properties": props,}# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# extract_images adapter β now a thin wrapper over image2.extract_and_verify_plots# Returns image2's native shape:# [{caption, page, image_data:[{array, bytes, filename, subplot_label,# subplot_caption, source, verification}]}]# which is exactly what mapper5.map_plots_to_properties and the display expect.# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef extract_images(pdf_path: str) -> list:try:with open(pdf_path, "rb") as f:pdf_bytes = f.read()plot_results, coverage = image2.extract_and_verify_plots(pdf_bytes, verify_engines=["gemini"])# Drop captions naming photos/micrographs/logos and crops the verifier# marked "discard" (keeps "keep"/"recrop"). Pure selection, non-mutating.plot_results = mapper5.select_real_plots(plot_results)st.session_state["plot_coverage"] = coverageexcept Exception as e:log.error(f"extract_images failed: {e}")st.session_state["plot_coverage"] = {}return []return plot_results# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ193 unmodified lines# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Helpers for the mapping UI# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef _confidence_badge(conf: str) -> str:colors = {"high": "#16a34a", "medium": "#d97706", "low": "#dc2626"}c = colors.get((conf or "low").lower(), "#6b7280")return f"<span class='conf-badge' style='background:{c}'>{(conf or '').upper()}</span>"def _score_confidence(score) -> str:"""Map a numeric match_score to a coarse confidence label for the badge."""try:s = float(score)except (TypeError, ValueError):return "low"if s >= 0.75:return "high"if s >= 0.45:return "medium"return "low"# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Manual input form (unchanged β writes via data_loader.insert_material_rows)# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef input_form():141 unmodified lines# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Tab 1: Material Data (extraction + DOI + RDS category selection)# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ_STAGE_LABELS = {0.00: ("Checking cache", 2),0.30: ("Extracting via Gemini", 20),0.70: ("Verifying against source", 8),1.00: ("Done", 0),}def _nearest_stage_label(pct: float):best_key = min(_STAGE_LABELS, key=lambda k: abs(k - pct))return _STAGE_LABELS[best_key]2 unmodified linesst.subheader("Material Properties Data")if not st.session_state.pdf_data_extracted:bar = st.progress(0.0)status = st.empty()timer = st.empty()start_ts = time.time()def _cb(msg: str, pct: float):elapsed = time.time() - start_tslabel, est_remaining = _nearest_stage_label(pct)bar.progress(min(pct, 1.0))status.markdown(f"**{label}** Β· <span style='color:#64748b'>{msg}</span>",unsafe_allow_html=True,)timer.caption(f"β± Elapsed: {elapsed:.0f}s"+ (f" Β· Est. remaining: ~{est_remaining}s" if est_remaining > 0 else ""))with open(pdf_path, "rb") as f:pdf_bytes = f.read()1 unmodified line_cb("Extracting via Geminiβ¦", 0.30)data = call_gemini_from_bytes(pdf_bytes)df = convert_to_dataframe(data)# DOI β regex over the PDF text, attached to every row. doi_url is the# column category_push preserves into <table>_extras.doi = _extract_doi_from_pdf(pdf_bytes)if not df.empty:df["doi"] = doidf["doi_url"] = doiif not df.empty:_cb("Verifying against source PDFβ¦", 0.70)sentences = _extract_sentences(pdf_bytes)df = verify_dataframe(df, sentences)_cb("Done.", 1.0)elapsed_total = time.time() - start_tsbar.progress(1.0)status.empty()timer.empty()if not df.empty:st.session_state.pdf_extracted_df = dfst.session_state.pdf_data_extracted = Truest.session_state.pdf_extracted_meta = _df_to_meta(df)st.session_state.pdf_doi = doist.success(f"Extracted {len(df)} properties in {elapsed_total:.0f}s")else:st.warning("No data extracted from PDF.")return3 unmodified linesreturnmeta = st.session_state.get("pdf_extracted_meta", {})doi = st.session_state.get("pdf_doi", "")c1, c2, c3 = st.columns(3)c1.metric("Material", meta.get("material_name", "N/A"))c2.metric("Abbreviation", meta.get("material_abbreviation", "N/A"))c3.metric("DOI", doi or "β")st.dataframe(df, use_container_width=True, height=400)st.subheader("Assign Material Category")st.selectbox("Category table (routes to the RDS table on push)",category_push.CATEGORY_TABLES, # Composites_materials / Fibers / Polymersindex=None,placeholder="Required before pushing to the database",key="push_category",help="This is the RDS table the mapped rows are pushed into (from the ""Extracted Plots tab).",)if st.session_state.get("push_category"):st.caption(f"Category **{st.session_state['push_category']}** selected. ""Go to the **Extracted Plots** tab to map figures and push to RDS.")# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Tab 2: Extracted Plots β map (mapper5) β store (SQLite) β push (category_push)# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ_OUT_DIR = os.getenv("AIM_OUT_DIR", os.path.abspath("./aim_efrc_db"))_SOURCE_TABLE = "gemini_verified" # provenance label for the stored rowsdef _links_for_group(links_df: pd.DataFrame, gi: int) -> pd.DataFrame:if links_df is None or links_df.empty or "group_idx" not in links_df.columns:return pd.DataFrame()return links_df[links_df["group_idx"] == gi]def render_plots_tab(pdf_path: str, paper_id: str):st.subheader("Extracted Plot Images & Property Mapping")# 1) Extract plots once (image2 detection + recovery + crop verification)if not st.session_state.pdf_processed:with st.spinner("Extracting plots from PDFβ¦"):st.session_state.plot_results = extract_images(pdf_path)st.session_state.pdf_processed = Truest.session_state.mapping_done = Falsest.session_state.links_df = pd.DataFrame()st.session_state.df_aug = pd.DataFrame()st.session_state.store = Noneplot_results = st.session_state.plot_resultsif not plot_results:st.warning("No plots found in this PDF.")returndf = st.session_state.pdf_extracted_dfhas_data = not df.emptyn_imgs = sum(len(g.get("image_data", [])) for g in plot_results)cov = st.session_state.get("plot_coverage", {}) or {}if has_data:mat_abbr = df.iloc[0]["material_abbreviation"]st.info(f"**{len(plot_results)} figures / {n_imgs} crops** extracted | "f"Material: **{mat_abbr}** | {df['property_name'].nunique()} properties | "f"recovered: {cov.get('total_recovered', 0)}")else:st.warning("Extract material data in the **Material Data** tab first to enable mapping.")# Downloads (image2's native zipper)d1, d2 = st.columns(2)d1.download_button("β¬ Images + metadata (ZIP)",data=image2.create_plot_zip(plot_results, include_json=True),file_name=f"{paper_id}_plots.zip", mime="application/zip",use_container_width=True, key="dl_plots_zip",)d2.download_button("β¬ Images only (ZIP)",data=image2.create_plot_zip(plot_results, include_json=False),file_name=f"{paper_id}_images.zip", mime="application/zip",use_container_width=True, key="dl_images_zip",)st.divider()# 2) Map properties β figures (mapper5 four-signal cascade)if has_data:cA, cB = st.columns([0.6, 0.4])run_map = cA.button(" Map properties β figures",type="primary",disabled=st.session_state.get("mapping_done", False),use_container_width=True,)if st.session_state.get("mapping_done"):if cB.button("βΊ Re-run mapping", use_container_width=True):st.session_state.mapping_done = Falsest.session_state.links_df = pd.DataFrame()st.session_state.df_aug = pd.DataFrame()st.session_state.store = Nonest.rerun()if run_map:with st.spinner("Fusing figure-citation β page β SciBERT β token signalsβ¦"):links_df, df_aug = mapper5.map_plots_to_properties(df, plot_results)st.session_state.links_df = links_dfst.session_state.df_aug = df_augst.session_state.mapping_done = Truen_mapped = int((df_aug.get("map_score", pd.Series(dtype=str)).astype(str) != "").sum()) \if not df_aug.empty else 0st.success(f" {n_mapped}/{len(df)} property rows linked to a figure "f"({len(links_df)} total link(s)).")st.rerun()links_df = st.session_state.get("links_df", pd.DataFrame())mapping_done = st.session_state.get("mapping_done", False)st.divider()# 3) Figure-centric review β each figure with the property rows linked to itfor gi, group in enumerate(plot_results):caption = group.get("caption", f"Figure {gi+1}")page = group.get("page", "?")imgs = group.get("image_data", [])with st.container(border=True):st.markdown(f"**Page {page}** β {caption}")icols = st.columns(min(len(imgs), 4) or 1)for pos, im in enumerate(imgs):with icols[pos % len(icols)]:arr = im.get("array")if arr is not None:sub = im.get("subplot_label") or ""st.image(arr, channels="BGR", width=200,caption=(sub or None))v = im.get("verification") or {}act = v.get("majority_action") if isinstance(v, dict) else Noneif act and act != "keep":st.caption(f"crop verdict: {act}")if mapping_done:grp = _links_for_group(links_df, gi)if grp.empty:st.caption("No property linked to this figure.")else:hdr = st.columns([3, 1.4, 1, 2, 1.2, 1, 0.7])for h, t in zip(hdr, ["property", "value", "unit", "material","subplot", "score", ""]):h.caption(t)for _, l in grp.iterrows():row = st.columns([3, 1.4, 1, 2, 1.2, 1, 0.7])row[0].write(str(l.get("property_name", "")))row[1].write(str(l.get("value", "")))row[2].write(str(l.get("unit", "")))row[3].write(str(l.get("material_name", "")))row[4].write(str(l.get("matched_subplot") or ""))row[5].markdown(_confidence_badge(_score_confidence(l.get("match_score"))),unsafe_allow_html=True,)key = f"rmlink_{gi}_{int(l.get('prop_row', 0))}_{int(l.get('match_rank', 0))}"if row[6].button("β", key=key, help="Remove this link"):mask = ~((links_df["group_idx"] == gi)& (links_df["prop_row"] == l["prop_row"])& (links_df["match_rank"] == l["match_rank"]))st.session_state.links_df = links_df[mask].reset_index(drop=True)# rebuild mapped_* columns from the pruned linksst.session_state.df_aug = mapper5._apply_links(df, st.session_state.links_df)st.rerun()if not (has_data and mapping_done):returnst.divider()# 4) Store to SQLite (saves crops + attaches plot_image_path), then push to RDSst.markdown("**Store the mapped result, then push to RDS**")st.caption("Store writes the mapped rows + saved crops to a local SQLite DB and ""attaches each crop's path. Push conforms those rows to the chosen ""category table's 33-column schema and uploads them.")if st.button(" Store to database (SQLite)", use_container_width=True):with st.spinner("Saving crops and writing SQLiteβ¦"):st.session_state.store = mapper5.store_properties_with_plots(st.session_state.df_aug,st.session_state.links_df,plot_results,out_dir=_OUT_DIR,pdf_stem=paper_id,source_table=_SOURCE_TABLE,)s = st.session_state.storest.success(f"Stored {s['n_properties']} row(s) β {s['db_filename']}; "f"{s['n_properties_with_plot']} with a plot, {s['n_images_saved']} crop(s).")st.rerun()store = st.session_state.get("store")st.markdown("** Push to RDS database**")if not mapper5.db_configured():st.caption("Set DB_NAME / DB_USER / DB_PASSWORD in your `.env` ""(host is already configured) to enable the RDS push.")returncategory = st.session_state.get("push_category")if not category:category = st.selectbox("Category table (routes by material type)",category_push.CATEGORY_TABLES,key="push_category_tab2",help="Rows conform to this table's 33-col schema; columns it can't hold ""go to <table>_extras; matched crops copy to rds_plots/<category>/<stem>/.",)embed_img = st.checkbox("Embed matched plot into the table's image column", value=True)cT, cP = st.columns(2)if cT.button(" Test connection", use_container_width=True):try:mapper5.db_healthcheck()st.success("Connected to RDS.")except Exception as e:st.error(f"Connection failed: {e}")push_ready = bool(store) and store.get("n_properties", 0) > 0if cP.button(f" Push to {category}", type="primary",use_container_width=True, disabled=not push_ready):try:with st.spinner(f"Conforming + writing rows to '{category}'β¦"):res = category_push.push_by_category(store, category, mapper5.get_db_engine(), embed_image=embed_img)st.success(f"Pushed {res['pushed']} row(s) to '{res['table']}', "f"{res['extras']} to '{res['extras_table']}', "f"{res['plots_copied']} crop(s) β {res['plots_dir']}.")except Exception as e:st.error(f"Push failed: {e}")if not push_ready:st.caption("Click ** Store to database (SQLite)** first β the push uploads those stored rows.")# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ8 unmodified linesst.caption("Provide technical data and research documentation for the central repository.")defaults = {"plot_results": [],"plot_coverage": {},"links_df": pd.DataFrame(),"df_aug": pd.DataFrame(),"store": None,"pdf_processed": False,"mapping_done": False,"current_pdf_name": None,"form_submitted": False,"pdf_data_extracted": False,"pdf_extracted_df": pd.DataFrame(),"pdf_extracted_meta": {},"pdf_doi": "",}for k, v in defaults.items():if k not in st.session_state:30 unmodified linesif st.session_state.form_submitted:st.session_state.form_submitted = Falsest.info("Form submitted. Upload again to process a new PDF.")st.tabs(["Material Data", "Extracted Plots"])returntab1, tab2 = st.tabs([" Material Data", "Extracted Plots"])tmp_file = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False, prefix="matdb_")try:tmp_file.write(uploaded_file.getbuffer())tmp_file.flush()2 unmodified lineswith tab1:render_material_data_tab(pdf_path)with tab2:render_plots_tab(pdf_path, paper_id)finally:try:os.unlink(tmp_file.name)1 unmodified linepassmain()
page_files/Upload_Data.py+333-694