crazycrazypete commited on
Commit
16d96ed
·
verified ·
1 Parent(s): 8d356d0

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +32 -8
app.py CHANGED
@@ -3,6 +3,7 @@ import re
3
  import json
4
  import math
5
  import hashlib
 
6
  from dataclasses import dataclass
7
  from datetime import datetime, date
8
  from typing import Dict, List, Optional, Tuple, Any
@@ -86,6 +87,26 @@ def gpt_json(system: str, payload: Dict[str, Any], max_tokens: int = 700) -> Dic
86
  )
87
  return _json_load_safe(getattr(resp, "output_text", "") or "")
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  # ============================
91
  # Load data files (must exist in repo)
@@ -808,10 +829,10 @@ def run_batch(text_blob: str, file_obj: Optional[Any], include_antennas: bool):
808
  # ============================
809
  # Gradio app (Single + Batch + Install-ready)
810
  # ============================
811
- def run_lookup(user_text: str, st: Dict[str,Any]):
812
  user_text = str(user_text or "").strip()
813
  if not user_text:
814
- return "Enter a router SKU/model.", gr.update(visible=False), gr.update(visible=False), {}, gr.update(value="")
815
 
816
  res = resolve_device(user_text)
817
  if res.get("mode") == "pick":
@@ -821,7 +842,7 @@ def run_lookup(user_text: str, st: Dict[str,Any]):
821
  return "Did you mean A or B? Pick one, then click Use selection.", gr.update(choices=choices, value=None, visible=True), gr.update(visible=True), st2, gr.update(value="")
822
 
823
  if res.get("mode") != "ok":
824
- return "Not found.", gr.update(visible=False), gr.update(visible=False), {}, gr.update(value="")
825
 
826
  life_row = df_eos.iloc[int(res["row_idx"])]
827
  eos, eol, status = row_to_dates_and_status(life_row)
@@ -837,9 +858,11 @@ def run_lookup(user_text: str, st: Dict[str,Any]):
837
  st_out = {"row_idx": int(res["row_idx"]), "repl": repl, "ant": ant}
838
  return output, gr.update(visible=False), gr.update(visible=False), st_out, gr.update(value="")
839
 
840
- def use_selection(selected_label: str, st: Dict[str,Any]):
 
 
841
  if not st or st.get("mode") != "pick":
842
- return "Run a search first.", gr.update(visible=False), gr.update(visible=False), {}, gr.update(value="")
843
  if not selected_label:
844
  return "Pick A or B first.", gr.update(visible=True), gr.update(visible=True), st, gr.update(value="")
845
 
@@ -864,7 +887,8 @@ def use_selection(selected_label: str, st: Dict[str,Any]):
864
  st_out = {"row_idx": int(chosen_row), "repl": repl, "ant": ant}
865
  return output, gr.update(visible=False), gr.update(visible=False), st_out, gr.update(value="")
866
 
867
- def make_install_ready(st_state: Dict[str,Any]):
 
868
  if not st_state or "row_idx" not in st_state:
869
  return "Run a lookup first."
870
  life_row = df_eos.iloc[int(st_state["row_idx"])]
@@ -878,7 +902,7 @@ with gr.Blocks(title="Only-Routers") as demo:
878
  with gr.Tabs():
879
  with gr.Tab("Single"):
880
  user_text = gr.Textbox(label="Router SKU or model", placeholder="Examples: IBR650B, AER1600, ES450, WR21, RUT240", lines=1)
881
- st = gr.State({})
882
 
883
  check_btn = gr.Button("Check", variant="primary")
884
  pick_dd = gr.Dropdown(label="Pick A or B", choices=[], visible=False)
@@ -907,4 +931,4 @@ with gr.Blocks(title="Only-Routers") as demo:
907
 
908
  run_btn.click(fn=run_batch, inputs=[batch_text, batch_file, include_ant], outputs=[summary_md, table, dl, rollup_md])
909
 
910
- demo.launch()
 
3
  import json
4
  import math
5
  import hashlib
6
+ import tempfile
7
  from dataclasses import dataclass
8
  from datetime import datetime, date
9
  from typing import Dict, List, Optional, Tuple, Any
 
87
  )
88
  return _json_load_safe(getattr(resp, "output_text", "") or "")
89
 
90
+ # ----------------------------
91
+ # Gradio state helpers (string JSON to avoid schema issues on Spaces)
92
+ # ----------------------------
93
+ def _state_load(st_json: str) -> Dict[str, Any]:
94
+ try:
95
+ if not st_json:
96
+ return {}
97
+ if isinstance(st_json, str):
98
+ return json.loads(st_json)
99
+ return {}
100
+ except Exception:
101
+ return {}
102
+
103
+ def _state_dump(st: Dict[str, Any]) -> str:
104
+ try:
105
+ return json.dumps(st or {}, ensure_ascii=False)
106
+ except Exception:
107
+ return "{}"
108
+
109
+
110
 
111
  # ============================
112
  # Load data files (must exist in repo)
 
829
  # ============================
830
  # Gradio app (Single + Batch + Install-ready)
831
  # ============================
832
+ def run_lookup(user_text: str, st_json: str):
833
  user_text = str(user_text or "").strip()
834
  if not user_text:
835
+ return "Enter a router SKU/model.", gr.update(visible=False), gr.update(visible=False), "{}", gr.update(value="")
836
 
837
  res = resolve_device(user_text)
838
  if res.get("mode") == "pick":
 
842
  return "Did you mean A or B? Pick one, then click Use selection.", gr.update(choices=choices, value=None, visible=True), gr.update(visible=True), st2, gr.update(value="")
843
 
844
  if res.get("mode") != "ok":
845
+ return "Not found.", gr.update(visible=False), gr.update(visible=False), "{}", gr.update(value="")
846
 
847
  life_row = df_eos.iloc[int(res["row_idx"])]
848
  eos, eol, status = row_to_dates_and_status(life_row)
 
858
  st_out = {"row_idx": int(res["row_idx"]), "repl": repl, "ant": ant}
859
  return output, gr.update(visible=False), gr.update(visible=False), st_out, gr.update(value="")
860
 
861
+ def use_selection(selected_label: str, st_json: str):
862
+ st = _state_load(st_json)
863
+ st = _state_load(st_json)
864
  if not st or st.get("mode") != "pick":
865
+ return "Run a search first.", gr.update(visible=False), gr.update(visible=False), "{}", gr.update(value="")
866
  if not selected_label:
867
  return "Pick A or B first.", gr.update(visible=True), gr.update(visible=True), st, gr.update(value="")
868
 
 
887
  st_out = {"row_idx": int(chosen_row), "repl": repl, "ant": ant}
888
  return output, gr.update(visible=False), gr.update(visible=False), st_out, gr.update(value="")
889
 
890
+ def make_install_ready(st_json: str):
891
+ st_state = _state_load(st_json)
892
  if not st_state or "row_idx" not in st_state:
893
  return "Run a lookup first."
894
  life_row = df_eos.iloc[int(st_state["row_idx"])]
 
902
  with gr.Tabs():
903
  with gr.Tab("Single"):
904
  user_text = gr.Textbox(label="Router SKU or model", placeholder="Examples: IBR650B, AER1600, ES450, WR21, RUT240", lines=1)
905
+ st = gr.State("{}")
906
 
907
  check_btn = gr.Button("Check", variant="primary")
908
  pick_dd = gr.Dropdown(label="Pick A or B", choices=[], visible=False)
 
931
 
932
  run_btn.click(fn=run_batch, inputs=[batch_text, batch_file, include_ant], outputs=[summary_md, table, dl, rollup_md])
933
 
934
+ demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT","7860")), share=False, show_api=False)