Inframat-x kmanch3 commited on
Commit
c028c3b
·
1 Parent(s): 3c02c91

Update app.py (#13)

Browse files

- Update app.py (473f974b0b8fbd33279ee6e1e3cc868bddaf8bbe)


Co-authored-by: Kendrick Manchester <kmanch3@users.noreply.huggingface.co>

Files changed (1) hide show
  1. app.py +101 -14
app.py CHANGED
@@ -55,6 +55,33 @@ CF_COL = "Conductive Filler Conc. (wt%)"
55
  TARGET_COL = "Stress GF (MPa-1)"
56
  CANON_NA = "NA" # canonical placeholder for categoricals
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  MAIN_VARIABLES = [
59
  "Filler 1 Type",
60
  "Filler 1 Diameter (µm)",
@@ -671,14 +698,33 @@ def rag_reply(
671
  selected = mmr_select_sentences(question, hits, top_n=int(n_sentences), pool_per_chunk=6, lambda_div=0.7)
672
 
673
  # Header citations: short codes only, joined by '; ' (e.g., "S55; S71; S92")
674
- header_codes = []
 
 
 
 
 
 
675
  for _, r in hits.head(6).iterrows():
676
- code = _short_doc_code(r["doc_path"])
677
- if code not in header_codes:
678
- header_codes.append(code)
679
- header_cites = "; ".join(header_codes)
680
- src_codes = set(header_codes)
681
- coverage_note = "" if len(src_codes) >= 3 else f"\n\n> Note: Only {len(src_codes)} unique source(s) contributed. Add more PDFs or increase Top-K."
 
 
 
 
 
 
 
 
 
 
 
 
 
682
 
683
  # Prepare retrieval list for logging (full filenames kept here)
684
  retr_list = []
@@ -1026,6 +1072,41 @@ input[type="checkbox"], .gr-checkbox, .gr-checkbox > * { pointer-events: auto !i
1026
  #eval-tab.eval-active .label {
1027
  color: #e6f2ff !important;
1028
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1029
  """
1030
 
1031
  theme = gr.themes.Soft(
@@ -1083,18 +1164,17 @@ with gr.Blocks(css=CSS, theme=theme, fill_height=True) as demo:
1083
  with gr.Row():
1084
  with gr.Column(scale=7):
1085
  with gr.Accordion("Primary conductive filler", open=True, elem_classes=["card"]):
1086
- f1_type = gr.Textbox(label="Filler 1 Type *", placeholder="e.g., CNT, Graphite, Steel fiber")
1087
  f1_diam = gr.Number(label="Filler 1 Diameter (µm) *")
1088
  f1_len = gr.Number(label="Filler 1 Length (mm) *")
1089
  cf_conc = gr.Number(label=f"{CF_COL} *", info="Weight percent of total binder")
1090
- f1_dim = gr.Dropdown(DIM_CHOICES, value=CANON_NA, label="Filler 1 Dimensionality *")
1091
 
1092
  with gr.Accordion("Secondary filler (optional)", open=False, elem_classes=["card"]):
1093
  f2_type = gr.Textbox(label="Filler 2 Type", placeholder="Optional")
1094
  f2_diam = gr.Number(label="Filler 2 Diameter (µm)")
1095
  f2_len = gr.Number(label="Filler 2 Length (mm)")
1096
- f2_dim = gr.Dropdown(DIM_CHOICES, value=CANON_NA, label="Filler 2 Dimensionality")
1097
-
1098
  with gr.Accordion("Mix design & specimen", open=False, elem_classes=["card"]):
1099
  spec_vol = gr.Number(label="Specimen Volume (mm3) *")
1100
  probe_cnt = gr.Number(label="Probe Count *")
@@ -1112,7 +1192,7 @@ with gr.Blocks(css=CSS, theme=theme, fill_height=True) as demo:
1112
  with gr.Accordion("Mechanical & electrical loading", open=False, elem_classes=["card"]):
1113
  load_rate = gr.Number(label="Loading Rate (MPa/s)")
1114
  E_mod = gr.Number(label="Modulus of Elasticity (GPa) *")
1115
- current = gr.Dropdown(CURRENT_CHOICES, value=CANON_NA, label="Current Type")
1116
  voltage = gr.Number(label="Applied Voltage (V)")
1117
 
1118
  with gr.Column(scale=5):
@@ -1244,12 +1324,19 @@ with gr.Blocks(css=CSS, theme=theme, fill_height=True) as demo:
1244
 
1245
  # ------------- Launch -------------
1246
  if __name__ == "__main__":
1247
- demo.queue().launch()
 
 
 
 
 
1248
 
1249
- # After launch: export a simple list of PDFs as paper_list.csv
 
1250
  import os as _os
1251
  import pandas as _pd
1252
  folder = "papers"
1253
  files = sorted(_os.listdir(folder)) if _os.path.exists(folder) else []
1254
  _pd.DataFrame({"doc": files}).to_csv("paper_list.csv", index=False)
1255
  print("✅ Saved paper_list.csv with", len(files), "papers")
 
 
55
  TARGET_COL = "Stress GF (MPa-1)"
56
  CANON_NA = "NA" # canonical placeholder for categoricals
57
 
58
+ TYPE_CHOICES = [
59
+ "CNT",
60
+ "Brass fiber",
61
+ "GNP",
62
+ "Steel fiber",
63
+ "Carbon fiber",
64
+ "Graphene oxide",
65
+ "Graphene",
66
+ "Carbon black",
67
+ "Graphite",
68
+ "Shungite",
69
+ "Nickel powder",
70
+ "Glass cullet",
71
+ "MWCNT",
72
+ "Nano carbon black",
73
+ "Carbon powder",
74
+ "Gasification char",
75
+ "Used foundry sand",
76
+ "Nickel fiber",
77
+ "Nickel aggregate",
78
+ "Steel slag aggregate",
79
+ "TiO2",
80
+ "Carbonyl iron powder",
81
+ "Magnetite aggregate",
82
+ CANON_NA
83
+ ]
84
+
85
  MAIN_VARIABLES = [
86
  "Filler 1 Type",
87
  "Filler 1 Diameter (µm)",
 
698
  selected = mmr_select_sentences(question, hits, top_n=int(n_sentences), pool_per_chunk=6, lambda_div=0.7)
699
 
700
  # Header citations: short codes only, joined by '; ' (e.g., "S55; S71; S92")
701
+ from urllib.parse import quote
702
+
703
+
704
+
705
+ header_links = []
706
+ unique_codes = set()
707
+
708
  for _, r in hits.head(6).iterrows():
709
+ doc_path = r["doc_path"]
710
+ filename = Path(doc_path).name
711
+ short_code = _short_doc_code(doc_path)
712
+
713
+ # ✅ Correct Gradio route is /file= (NOT /file/)
714
+ abs_pdf = (LOCAL_PDF_DIR / filename).resolve()
715
+ href = f"/file={quote('papers/' + filename)}"
716
+ link = f'<a href="/file={quote("papers/" + filename)}" target="_blank" rel="noopener noreferrer">{short_code}</a>'
717
+
718
+ if short_code not in unique_codes:
719
+ header_links.append(link)
720
+ unique_codes.add(short_code)
721
+
722
+ header_cites = "; ".join(header_links)
723
+
724
+ coverage_note = "" if len(unique_codes) >= 3 else (
725
+ f"\n\n> Note: Only {len(unique_codes)} unique source(s) contributed. "
726
+ "Add more PDFs or increase Top-K."
727
+ )
728
 
729
  # Prepare retrieval list for logging (full filenames kept here)
730
  retr_list = []
 
1072
  #eval-tab.eval-active .label {
1073
  color: #e6f2ff !important;
1074
  }
1075
+
1076
+ /* --- THE UNIVERSAL DROPDOWN OVERRIDE --- */
1077
+
1078
+ /* 1. All boxes show white text on the dark background */
1079
+ #filler-dropdown .single-select, #filler-dropdown input,
1080
+ #dim-dropdown .single-select, #dim-dropdown input,
1081
+ #dim2-dropdown .single-select, #dim2-dropdown input,
1082
+ #current-dropdown .single-select, #current-dropdown input {
1083
+ color: #ffffff !important;
1084
+ -webkit-text-fill-color: #ffffff !important;
1085
+ }
1086
+
1087
+ /* 2. All dropdown menus (the pop-outs) have a white background */
1088
+ #filler-dropdown .options,
1089
+ #dim-dropdown .options,
1090
+ #dim2-dropdown .options,
1091
+ #current-dropdown .options {
1092
+ background-color: #ffffff !important;
1093
+ }
1094
+
1095
+ /* 3. All items in the lists are forced to PURE BLACK */
1096
+ #filler-dropdown .item, #filler-dropdown .item span,
1097
+ #dim-dropdown .item, #dim-dropdown .item span,
1098
+ #dim2-dropdown .item, #dim2-dropdown .item span,
1099
+ #current-dropdown .item, #current-dropdown .item span,
1100
+ .gr-dropdown .options .item, .gr-dropdown .options .item * {
1101
+ color: #000000 !important;
1102
+ -webkit-text-fill-color: #000000 !important;
1103
+ }
1104
+
1105
+ /* 4. Hover effect for all dropdowns */
1106
+ .gr-dropdown .item:hover {
1107
+ background-color: #dbeafe !important;
1108
+ }
1109
+
1110
  """
1111
 
1112
  theme = gr.themes.Soft(
 
1164
  with gr.Row():
1165
  with gr.Column(scale=7):
1166
  with gr.Accordion("Primary conductive filler", open=True, elem_classes=["card"]):
1167
+ f1_type = gr.Dropdown(TYPE_CHOICES,label="Filler 1 Type *", value="CNT", allow_custom_value=True, elem_id="filler-dropdown")
1168
  f1_diam = gr.Number(label="Filler 1 Diameter (µm) *")
1169
  f1_len = gr.Number(label="Filler 1 Length (mm) *")
1170
  cf_conc = gr.Number(label=f"{CF_COL} *", info="Weight percent of total binder")
1171
+ f1_dim = gr.Dropdown(DIM_CHOICES, value=CANON_NA, label="Filler 1 Dimensionality *",elem_id="dim-dropdown")
1172
 
1173
  with gr.Accordion("Secondary filler (optional)", open=False, elem_classes=["card"]):
1174
  f2_type = gr.Textbox(label="Filler 2 Type", placeholder="Optional")
1175
  f2_diam = gr.Number(label="Filler 2 Diameter (µm)")
1176
  f2_len = gr.Number(label="Filler 2 Length (mm)")
1177
+ f2_dim = gr.Dropdown(DIM_CHOICES, value=CANON_NA, label="Filler 2 Dimensionality", elem_id="dim2-dropdown")
 
1178
  with gr.Accordion("Mix design & specimen", open=False, elem_classes=["card"]):
1179
  spec_vol = gr.Number(label="Specimen Volume (mm3) *")
1180
  probe_cnt = gr.Number(label="Probe Count *")
 
1192
  with gr.Accordion("Mechanical & electrical loading", open=False, elem_classes=["card"]):
1193
  load_rate = gr.Number(label="Loading Rate (MPa/s)")
1194
  E_mod = gr.Number(label="Modulus of Elasticity (GPa) *")
1195
+ current = gr.Dropdown(CURRENT_CHOICES, value=CANON_NA, label="Current Type", elem_id="current-dropdown")
1196
  voltage = gr.Number(label="Applied Voltage (V)")
1197
 
1198
  with gr.Column(scale=5):
 
1324
 
1325
  # ------------- Launch -------------
1326
  if __name__ == "__main__":
1327
+ # 1. Start the Chatbot (This is what gives you the link)
1328
+ # If using Gradio:
1329
+ demo.launch()
1330
+
1331
+ # Or if using Flask:
1332
+ # app.run(debug=True)
1333
 
1334
+ # 2. Everything below here only runs AFTER the server stops
1335
+ # (or might not run at all depending on how the server handles the exit)
1336
  import os as _os
1337
  import pandas as _pd
1338
  folder = "papers"
1339
  files = sorted(_os.listdir(folder)) if _os.path.exists(folder) else []
1340
  _pd.DataFrame({"doc": files}).to_csv("paper_list.csv", index=False)
1341
  print("✅ Saved paper_list.csv with", len(files), "papers")
1342
+