MSU576 commited on
Commit
f07b340
·
verified ·
1 Parent(s): ee779b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +338 -0
app.py CHANGED
@@ -871,3 +871,341 @@ def soil_classifier_ui():
871
  state["step"] = 0
872
  ss["classifier_states"][site_name] = state
873
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
871
  state["step"] = 0
872
  ss["classifier_states"][site_name] = state
873
 
874
+ # -------------------------
875
+ # Locator page (chat-style)
876
+ # -------------------------
877
+ def locator_ui():
878
+ st.header("🌍 Locator — define AOI (GeoJSON or coordinates)")
879
+ idx, sdict = active_site()
880
+ site_name = sdict["Site Name"]
881
+ st.markdown(f"**Active site:** {site_name}")
882
+ st.info("You can paste GeoJSON for your area of interest or enter a center point (lat, lon). The app will save the AOI to the active site and try to display a map.")
883
+
884
+ # Chat-like: ask for GeoJSON or coordinates
885
+ choice = st.radio("Provide:", ["GeoJSON (polygon)", "Coordinates (lat, lon)"], index=0, key=f"loc_choice_{site_name}")
886
+ if choice.startswith("GeoJSON"):
887
+ geo_text = st.text_area("Paste GeoJSON (Polygon or MultiPolygon)", value=sdict.get("map_snapshot") or "", key=f"geojson_area_{site_name}", height=180)
888
+ if st.button("Save AOI and show map", key=f"save_aoi_{site_name}"):
889
+ if not geo_text.strip():
890
+ st.error("No GeoJSON provided.")
891
+ else:
892
+ try:
893
+ gj = json.loads(geo_text)
894
+ sdict["map_snapshot"] = gj
895
+ ss["site_descriptions"][idx] = sdict
896
+ st.success("AOI saved.")
897
+ # Try to display on simple map: compute centroid and show with st.map
898
+ # Find centroid of polygon(s)
899
+ def centroid_of_geojson(gj_obj):
900
+ coords = []
901
+ if gj_obj.get("type") == "FeatureCollection":
902
+ for f in gj_obj.get("features", []):
903
+ geom = f.get("geometry", {})
904
+ coords.extend(_extract_coords_from_geom(geom))
905
+ else:
906
+ geom = gj_obj if "geometry" not in gj_obj else gj_obj["geometry"]
907
+ coords.extend(_extract_coords_from_geom(geom))
908
+ if not coords:
909
+ return None
910
+ arr = np.array(coords)
911
+ return float(arr[:,1].mean()), float(arr[:,0].mean())
912
+ def _extract_coords_from_geom(geom):
913
+ if not geom:
914
+ return []
915
+ t = geom.get("type","")
916
+ if t == "Polygon":
917
+ return [tuple(pt) for pt in geom.get("coordinates", [])[0]]
918
+ if t == "MultiPolygon":
919
+ pts=[]
920
+ for poly in geom.get("coordinates", []):
921
+ pts.extend([tuple(pt) for pt in poly[0]])
922
+ return pts
923
+ if t == "Point":
924
+ return [tuple(geom.get("coordinates",[]))]
925
+ return []
926
+ cent = centroid_of_geojson(gj)
927
+ if cent:
928
+ latc, lonc = cent
929
+ sdict["lat"] = latc; sdict["lon"] = lonc
930
+ ss["site_descriptions"][idx] = sdict
931
+ st.map(pd.DataFrame({"lat":[latc],"lon":[lonc]}))
932
+ else:
933
+ st.info("Could not compute centroid for map preview, but AOI saved.")
934
+ except Exception as e:
935
+ st.error(f"Invalid GeoJSON: {e}")
936
+ else:
937
+ # Coordinates mode
938
+ lat = st.number_input("Latitude", value=float(sdict.get("lat") or 0.0), key=f"loc_lat_{site_name}")
939
+ lon = st.number_input("Longitude", value=float(sdict.get("lon") or 0.0), key=f"loc_lon_{site_name}")
940
+ if st.button("Save coordinates and show map", key=f"save_coords_{site_name}"):
941
+ sdict["lat"] = float(lat); sdict["lon"] = float(lon)
942
+ ss["site_descriptions"][idx] = sdict
943
+ st.success("Coordinates saved.")
944
+ st.map(pd.DataFrame({"lat":[lat],"lon":[lon]}))
945
+
946
+ # If EE is available, offer to fetch raster/time series (placeholder)
947
+ st.markdown("---")
948
+ if EE_READY:
949
+ if st.button("Fetch Earth Engine data (soil profile / climate / flood / seismic) — experimental"):
950
+ st.info("Earth Engine available — fetching (placeholder).")
951
+ # Placeholder: real implementation would call ee.Dataset/time series and store results
952
+ try:
953
+ # example: add a placeholder soil profile
954
+ sdict["Soil Profile"] = "Placeholder Earth Engine soil profile data (EE initialized)."
955
+ sdict["Flood Data"] = "Placeholder flood history (20 years) from EE."
956
+ sdict["Seismic Data"] = "Placeholder seismic history (20 years) from EE."
957
+ ss["site_descriptions"][idx] = sdict
958
+ st.success("Earth Engine data fetched and saved to site (placeholder).")
959
+ except Exception as e:
960
+ st.error(f"EE fetch failed: {e}")
961
+ else:
962
+ st.info("Earth Engine not initialized in this runtime. To enable, set SERVICE_ACCOUNT and EARTH_ENGINE_KEY secrets and ensure earthengine-api is installed.")
963
+
964
+ # -------------------------
965
+ # GeoMate Ask (RAG chatbot) — simplified RAG integration
966
+ # -------------------------
967
+ def run_llm_completion(prompt: str, model: str = "llama3-8b-8192") -> str:
968
+ """Minimal LLM wrapper: uses Groq if available; else returns a dummy but structured answer."""
969
+ if GROQ_OK and GROQ_KEY:
970
+ try:
971
+ client = Groq(api_key=GROQ_KEY)
972
+ comp = client.chat.completions.create(model=model, messages=[{"role":"user", "content": prompt}], temperature=0.2, max_tokens=800)
973
+ return comp.choices[0].message.content
974
+ except Exception as e:
975
+ return f"(Groq error) {e}"
976
+ else:
977
+ # Dummy local response for demonstration
978
+ return f"(Dummy LLM) I received your prompt and would reply here. Model: {model}\n\nPrompt excerpt:\n{prompt[:400]}"
979
+
980
+ def rag_ui():
981
+ st.header("🤖 GeoMate Ask — RAG Chatbot (per-site memory)")
982
+ idx, sdict = active_site()
983
+ site_name = sdict["Site Name"]
984
+ st.markdown(f"**Active site:** {site_name}")
985
+ # Prepare chat history per site
986
+ ss.setdefault("rag_memory", {})
987
+ chat = ss["rag_memory"].setdefault(site_name, [])
988
+ # Show chat
989
+ for turn in chat[-40:]:
990
+ role, text = turn.get("role"), turn.get("text")
991
+ if role == "user":
992
+ st.markdown(f"**You:** {text}")
993
+ else:
994
+ st.markdown(f"**GeoMate:** {text}")
995
+
996
+ # user input
997
+ user_prompt = st.text_input("Ask GeoMate (technical)", key=f"rag_input_{site_name}")
998
+ col1, col2 = st.columns([3,1])
999
+ if col1.button("Send", key=f"rag_send_{site_name}") and user_prompt.strip():
1000
+ # append user
1001
+ chat.append({"role":"user", "text":user_prompt, "ts": datetime.now().isoformat()})
1002
+ ss["rag_memory"][site_name] = chat
1003
+ # Build RAG prompt using stored site data + user prompt
1004
+ context = {"site": sdict}
1005
+ full_prompt = f"Site data (json):\n{json.dumps(context, indent=2)}\n\nUser question:\n{user_prompt}\nPlease answer technically."
1006
+ # Run LLM
1007
+ with st.spinner("Running LLM..."):
1008
+ resp = run_llm_completion(full_prompt, model=ss.get("llm_model"))
1009
+ # Append bot response
1010
+ chat.append({"role":"assistant", "text":resp, "ts": datetime.now().isoformat()})
1011
+ ss["rag_memory"][site_name] = chat
1012
+ # Intelligent extraction: try to pick up numeric engineering fields (simple heuristics)
1013
+ update_site_description_from_chat(resp, site_name)
1014
+ safe_rerun()
1015
+
1016
+ # small NLP-ish extractor placeholder
1017
+ def update_site_description_from_chat(text: str, site_name: str):
1018
+ """Naive extraction: looks for keywords like 'bearing' and a numeric value followed by units."""
1019
+ idx = None
1020
+ for i, s in enumerate(ss["site_descriptions"]):
1021
+ if s["Site Name"] == site_name:
1022
+ idx = i; break
1023
+ if idx is None:
1024
+ return
1025
+ # naive patterns
1026
+ lowered = text.lower()
1027
+ site = ss["site_descriptions"][idx]
1028
+ # look for 'bearing' followed by number (psf, kpa)
1029
+ import re
1030
+ m = re.search(r"bearing.*?([0-9]{2,6})\s*(psf|kpa|kpa\.)?", lowered)
1031
+ if m:
1032
+ val = m.group(1)
1033
+ site["Load Bearing Capacity"] = m.group(0)
1034
+ ss["site_descriptions"][idx] = site
1035
+
1036
+ # -------------------------
1037
+ # Reports page (two types)
1038
+ # -------------------------
1039
+ def build_classification_pdf_bytes(site_dict: dict) -> bytes:
1040
+ """Return bytes of a classification-only PDF for a single site. Try ReportLab then FPDF fallback."""
1041
+ text = site_dict.get("classifier_decision_path") or "No classification decision path available."
1042
+ inputs = site_dict.get("classifier_inputs", {})
1043
+ title = f"GeoMate Classification Report — {site_dict['Site Name']}"
1044
+ # Try ReportLab
1045
+ if REPORTLAB_OK:
1046
+ buf = io.BytesIO()
1047
+ doc = SimpleDocTemplate(buf, pagesize=A4)
1048
+ styles = getSampleStyleSheet()
1049
+ elems = []
1050
+ elems.append(Paragraph(title, styles["Title"]))
1051
+ elems.append(Spacer(1,6))
1052
+ elems.append(Paragraph("Classification result and explanation:", styles["Heading2"]))
1053
+ elems.append(Paragraph(text.replace("\n","<br/>"), styles["BodyText"]))
1054
+ elems.append(Spacer(1,6))
1055
+ elems.append(Paragraph("Inputs:", styles["Heading3"]))
1056
+ for k,v in inputs.items():
1057
+ elems.append(Paragraph(f"{k}: {v}", styles["BodyText"]))
1058
+ doc.build(elems)
1059
+ pdf_bytes = buf.getvalue(); buf.close()
1060
+ return pdf_bytes
1061
+ elif FPDF_OK:
1062
+ pdf = FPDF()
1063
+ pdf.add_page()
1064
+ pdf.set_font("Arial", "B", 14)
1065
+ pdf.cell(0, 10, title, ln=1)
1066
+ pdf.set_font("Arial", "", 11)
1067
+ pdf.multi_cell(0, 6, text)
1068
+ pdf.ln(4)
1069
+ pdf.set_font("Arial", "B", 12)
1070
+ pdf.cell(0,6,"Inputs:", ln=1)
1071
+ pdf.set_font("Arial", "", 10)
1072
+ for k,v in inputs.items():
1073
+ pdf.cell(0,5,f"{k}: {v}", ln=1)
1074
+ out = pdf.output(dest="S").encode("latin-1")
1075
+ return out
1076
+ else:
1077
+ # fallback: simple text file disguised as pdf (not ideal)
1078
+ return ("Classification:\n"+text+"\n\nInputs:\n"+json.dumps(inputs, indent=2)).encode("utf-8")
1079
+
1080
+ def build_full_phase1_pdf_bytes(site_list: List[dict], ext_refs: List[str]) -> bytes:
1081
+ """
1082
+ Build full geotechnical report (Phase 1) combining site data, classifier results, GSD and maps (if any).
1083
+ This is a streamlined generation: for production you would expand each analysis section.
1084
+ """
1085
+ title = "GeoMate — Full Geotechnical Investigation Report"
1086
+ # Use ReportLab if available
1087
+ if REPORTLAB_OK:
1088
+ buf = io.BytesIO()
1089
+ doc = SimpleDocTemplate(buf, pagesize=A4, leftMargin=20*mm, rightMargin=20*mm, topMargin=20*mm)
1090
+ styles = getSampleStyleSheet()
1091
+ elems = []
1092
+ elems.append(Paragraph(title, styles["Title"]))
1093
+ elems.append(Spacer(1,8))
1094
+ elems.append(Paragraph(f"Generated: {datetime.now().strftime('%Y-%m-%d %H:%M')}", styles["Normal"]))
1095
+ elems.append(Spacer(1,12))
1096
+ # Summary
1097
+ elems.append(Paragraph("SUMMARY", styles["Heading2"]))
1098
+ elems.append(Paragraph("This full geotechnical report was generated by GeoMate V2. The following pages contain site descriptions, classification results and recommendations.", styles["BodyText"]))
1099
+ elems.append(PageBreak())
1100
+ # For each site:
1101
+ for s in site_list:
1102
+ elems.append(Paragraph(f"SITE: {s.get('Site Name','Unnamed')}", styles["Heading1"]))
1103
+ elems.append(Paragraph(f"Location: {s.get('Site Coordinates','Not provided')}", styles["BodyText"]))
1104
+ elems.append(Spacer(1,6))
1105
+ # classifier results
1106
+ elems.append(Paragraph("Classification", styles["Heading2"]))
1107
+ elems.append(Paragraph(s.get("classifier_decision_path","No classification available.").replace("\n","<br/>"), styles["BodyText"]))
1108
+ elems.append(Spacer(1,6))
1109
+ # GSD table
1110
+ if s.get("GSD"):
1111
+ elems.append(Paragraph("GSD Summary", styles["Heading2"]))
1112
+ g = s["GSD"]
1113
+ tdata = [["D10 (mm)", "D30 (mm)", "D60 (mm)", "Cu", "Cc"]]
1114
+ tdata.append([str(g.get("D10")), str(g.get("D30")), str(g.get("D60")), str(g.get("Cu")), str(g.get("Cc"))])
1115
+ t = Table(tdata, colWidths=[30*mm]*5)
1116
+ t.setStyle(TableStyle([("GRID",(0,0),(-1,-1),0.5,colors.grey),("BACKGROUND",(0,0),(-1,0),colors.HexColor("#1F4E79")),("TEXTCOLOR",(0,0),(-1,0),colors.white)]))
1117
+ elems.append(t)
1118
+ elems.append(PageBreak())
1119
+ # Add external references
1120
+ elems.append(Paragraph("External References", styles["Heading2"]))
1121
+ for r in ext_refs:
1122
+ elems.append(Paragraph(r, styles["BodyText"]))
1123
+ doc.build(elems)
1124
+ pdf_bytes = buf.getvalue(); buf.close()
1125
+ return pdf_bytes
1126
+ elif FPDF_OK:
1127
+ pdf = FPDF()
1128
+ pdf.add_page()
1129
+ pdf.set_font("Arial", "B", 16)
1130
+ pdf.cell(0, 10, title, ln=1)
1131
+ pdf.set_font("Arial", "", 11)
1132
+ pdf.ln(4)
1133
+ for s in site_list:
1134
+ pdf.set_font("Arial", "B", 13)
1135
+ pdf.cell(0, 8, f"Site: {s.get('Site Name','')}", ln=1)
1136
+ pdf.set_font("Arial", "", 11)
1137
+ pdf.multi_cell(0, 6, s.get("classifier_decision_path","No classification data."))
1138
+ pdf.ln(4)
1139
+ if s.get("GSD"):
1140
+ g = s["GSD"]
1141
+ pdf.cell(0, 6, f"GSD D10={g.get('D10')}, D30={g.get('D30')}, D60={g.get('D60')}", ln=1)
1142
+ pdf.add_page()
1143
+ out = pdf.output(dest="S").encode("latin-1")
1144
+ return out
1145
+ else:
1146
+ # fallback text
1147
+ parts = [title, "Generated: "+datetime.now().isoformat()]
1148
+ for s in site_list:
1149
+ parts.append("SITE: "+s.get("Site Name",""))
1150
+ parts.append("Classification:\n"+(s.get("classifier_decision_path") or "No data"))
1151
+ parts.append("GSD: "+(json.dumps(s.get("GSD") or {})))
1152
+ parts.append("REFERENCES: "+json.dumps(ext_refs))
1153
+ return ("\n\n".join(parts)).encode("utf-8")
1154
+
1155
+ # Reports UI
1156
+ def reports_ui():
1157
+ st.header("📑 Reports — Classification-only & Full Geotechnical Report")
1158
+ idx, sdict = active_site()
1159
+ st.subheader("Classification-only Report")
1160
+ st.markdown("Generates a PDF containing the classification result and the decision path for the selected site.")
1161
+ if st.button("Generate Classification PDF", key=f"gen_cls_pdf_{sdict['Site Name']}"):
1162
+ pdf_bytes = build_classification_pdf_bytes(sdict)
1163
+ st.download_button("Download Classification PDF", data=pdf_bytes, file_name=f"{sdict['Site Name']}_classification.pdf", mime="application/pdf")
1164
+ st.markdown("---")
1165
+ st.subheader("Full Geotechnical Report")
1166
+ st.markdown("Chatbot will gather missing parameters and produce a full Phase 1 report for selected sites (up to 4).")
1167
+ # multi-select sites
1168
+ all_site_names = [s["Site Name"] for s in ss["site_descriptions"]]
1169
+ chosen = st.multiselect("Select sites to include", options=all_site_names, default=all_site_names, key="report_sites_select")
1170
+ ext_refs_text = st.text_area("External references (one per line)", key="ext_refs")
1171
+ # If user wants to have the bot gather missing params, start convo
1172
+ if st.button("Start conversational data gather for Full Report", key="start_report_convo"):
1173
+ # We'll run a simple in-app conversational loop: for brevity, ask a set of required params per site
1174
+ required_questions = [
1175
+ ("Load Bearing Capacity", "What is the soil bearing capacity (e.g., 2000 psf or 'Don't know')?"),
1176
+ ("Skin Shear Strength", "Provide skin shear strength (kPa) if known, else 'Don't know'"),
1177
+ ("Relative Compaction", "Relative compaction (%)"),
1178
+ ("Rate of Consolidation", "Rate of consolidation (e.g., cv in m2/year)"),
1179
+ ("Nature of Construction", "Nature of construction (e.g., 2-storey residence)"),
1180
+ ("Other", "Any other relevant notes or 'None'")
1181
+ ]
1182
+ # For each chosen site, ask interactively. We'll perform a sequential loop inside the app (chatbot-style rudimentary)
1183
+ for site_name in chosen:
1184
+ st.info(f"Collecting data for site: {site_name}")
1185
+ # Retrieve site dict
1186
+ site_idx = [i for i,s in enumerate(ss["site_descriptions"]) if s["Site Name"]==site_name][0]
1187
+ site_obj = ss["site_descriptions"][site_idx]
1188
+ for field, q in required_questions:
1189
+ # If field exists and not None, skip confirmation ask
1190
+ current_val = site_obj.get(field)
1191
+ if current_val:
1192
+ st.write(f"{field} (existing): {current_val}")
1193
+ keep = st.radio(f"Keep existing {field} for {site_name}?", ["Keep","Replace"], key=f"keep_{site_name}_{field}")
1194
+ if keep == "Keep":
1195
+ continue
1196
+ ans = st.text_input(q, key=f"report_q_{site_name}_{field}")
1197
+ if ans.strip().lower() in ["don't know","dont know","skip","n","no","unknown",""]:
1198
+ site_obj[field] = None
1199
+ else:
1200
+ site_obj[field] = ans.strip()
1201
+ ss["site_descriptions"][site_idx] = site_obj
1202
+ st.success(f"Data saved for {site_name}.")
1203
+ st.info("Conversational gather complete. Use Generate Full Report PDF button to create the PDF.")
1204
+
1205
+ if st.button("Generate Full Report PDF", key="gen_full_pdf_btn"):
1206
+ # collect selected site dicts
1207
+ site_objs = [s for s in ss["site_descriptions"] if s["Site Name"] in chosen]
1208
+ ext_refs = [r.strip() for r in ext_refs_text.splitlines() if r.strip()]
1209
+ pdf_bytes = build_full_phase1_pdf_bytes(site_objs, ext_refs)
1210
+ st.download_button("Download Full Geotechnical Report", data=pdf_bytes, file_name=f"GeoMate_Full_Report_{datetime.now().strftime('%Y%m%d')}.pdf", mime="application/pdf")
1211
+