Nelly-43 commited on
Commit
a5a33f5
Β·
verified Β·
1 Parent(s): ca715c2

Update app_web.py

Browse files
Files changed (1) hide show
  1. app_web.py +24 -18
app_web.py CHANGED
@@ -11,32 +11,38 @@ from src.verifier import verify_and_respond
11
  from src.llm import list_models
12
  from huggingface_hub import snapshot_download, whoami
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  snapshot_download(repo_id="CGIAR/weai-kb",
15
  repo_type="dataset",
16
  token=os.getenv('HF_TOKEN'),
17
  local_dir="./"
18
  )
19
-
 
20
  def cg_admin():
21
- try:
22
- # Fetch user info using the cached or environment token (HF_TOKEN)
23
- user_info = whoami()
24
-
25
- username = user_info.get("name")
26
- # Organizations are listed under the 'orgs' key as a list of dicts
27
- orgs = [org["name"] for org in user_info.get("orgs", [])]
28
-
29
- except Exception as e:
30
- print("User is not logged in or the token is invalid:", e)
31
- orgs = []
32
 
33
  return orgs
34
-
35
- # 1. Force a clean state wipe on first load of the browser session
36
- if "orgs" in st.session_state:
37
- st.session_state.clear() # Wipes out any residual states
38
- st.session_state["orgs"] = cg_admin()
39
- st.write(st.session_state.orgs)
40
 
41
  # ── Session initialisation ───────────────────────────────────────────────────
42
  def init_session():
 
11
  from src.llm import list_models
12
  from huggingface_hub import snapshot_download, whoami
13
 
14
+ js_code = """
15
+ (async () => {
16
+ return new Promise((resolve) => {
17
+ // Send a request up to the Hugging Face container
18
+ window.parent.postMessage({ type: "USER_PLAN_REQUEST" }, "https://huggingface.co");
19
+
20
+ // Listen for the returning credential data payload
21
+ window.addEventListener("message", function listener(event) {
22
+ if (event.origin === "https://huggingface.co" && event.data.type === "USER_PLAN") {
23
+ window.removeEventListener("message", listener);
24
+ resolve(event.data.plan); // returns { user: "...", org: "..." }
25
+ }
26
+ });
27
+ });
28
+ })()
29
+ """
30
  snapshot_download(repo_id="CGIAR/weai-kb",
31
  repo_type="dataset",
32
  token=os.getenv('HF_TOKEN'),
33
  local_dir="./"
34
  )
35
+ # 1. Define the asynchronous JavaScript logic to request user plans
36
+
37
  def cg_admin():
38
+ user_data = st_javascript(js_code)
39
+ if user_data:
40
+ username = user_data.get("user", "anonymous")
41
+ orgs = user_data.get("org", [])
42
+ if isinstance(orgs, str):
43
+ orgs = [orgs]
 
 
 
 
 
44
 
45
  return orgs
 
 
 
 
 
 
46
 
47
  # ── Session initialisation ───────────────────────────────────────────────────
48
  def init_session():