Nelly-43 commited on
Commit
1ed2d64
Β·
verified Β·
1 Parent(s): 22a2a3e

Update app_web.py

Browse files
Files changed (1) hide show
  1. app_web.py +14 -41
app_web.py CHANGED
@@ -9,56 +9,29 @@ from src.query_engine import understand_query
9
  from src.retriever import clear_collection_cache, retrieve
10
  from src.verifier import verify_and_respond
11
  from src.llm import list_models
12
- from huggingface_hub import snapshot_download
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
- if "OAUTH_CLIENT_ID" in os.environ:
21
- scrts = {
22
- "client_id": os.environ["OAUTH_CLIENT_ID"],
23
- "client_secret": os.environ["OAUTH_CLIENT_SECRET"],
24
- "server_metadata_url": f"{os.environ['OPENID_PROVIDER_URL']}/.well-known/openid-configuration",
25
- "redirect_uri": "https://huggingface.co/spaces/" + os.environ.get("SPACE_ID", "") + "/oauth2/callback",
26
- "cookie_secret": os.environ.get("OAUTH_CLIENT_SECRET")[:32] # Needs to be string safe
27
- }
28
- from huggingface_hub import whoami
29
-
30
- try:
31
- # Fetch user info using the cached or environment token (HF_TOKEN)
32
- user_info = whoami()
33
-
34
- username = user_info.get("name")
35
- # Organizations are listed under the 'orgs' key as a list of dicts
36
- organizations = [org["name"] for org in user_info.get("orgs", [])]
37
 
38
- print(f"Logged in username: {username}")
39
- print(f"Organizations: {organizations}")
40
-
41
- except Exception as e:
42
- print("User is not logged in or the token is invalid:", e)
43
  def cg_admin():
44
- headers = st.context.headers
45
- print(headers)
46
- if "X-Forwarded-User" in headers:
47
- username = headers.get("X-Forwarded-User")
48
- token = headers.get("X-Forwarded-Access-Token")
49
- userinfo_url = "https://huggingface.co/oauth/userinfo"
50
- response = requests.get(userinfo_url, headers={"Authorization": f"Bearer {token}"})
51
- print(username, token)
52
- if response.status_code == 200:
53
- user_data = response.json()
54
-
55
- # 3. Extract and display the list of organizations
56
- # The 'orgs' list contains objects with organization details (name, role, etc.)
57
- orgs = user_data.get("orgs", [])
58
- print(orgs)
59
 
60
- if "CGIAR" in [org.get("name") for org in orgs]:
61
- return True
62
 
63
  # ── Session initialisation ───────────────────────────────────────────────────
64
  def init_session():
 
9
  from src.retriever import clear_collection_cache, retrieve
10
  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
+ if "CGIAR" in orgs:
34
+ return True
35
 
36
  # ── Session initialisation ───────────────────────────────────────────────────
37
  def init_session():