Chris Addis commited on
Commit
4b5c5ff
·
1 Parent(s): 163b56c
Files changed (1) hide show
  1. app.py +29 -31
app.py CHANGED
@@ -19,27 +19,37 @@ def get_user_access_level(request: gr.Request):
19
  """
20
  Checks the request object to determine if a user is logged in via HF OAuth
21
  and their access level based on the AUTHORIZED_USER_IDS list.
 
22
  """
23
- user_info = request.auth # This contains user info if hf_oauth is enabled and user is logged in
 
 
 
 
 
 
 
 
 
24
 
25
  if user_info is None:
26
- # User is not logged in
27
- print("User not logged in.")
28
  return {
29
- "status_message": "Please sign in to see your access level and potentially unlock full features.",
30
- "user_identity": "Not Logged In",
31
  "show_login_prompt": gr.update(visible=True),
32
  "show_limited_content": gr.update(visible=True),
33
  "show_full_content": gr.update(visible=False)
34
  }
35
  else:
36
- # User is logged in, get their HF user ID and username
37
  user_id = user_info.get('sub') # 'sub' is the standard claim for user ID
38
  username = user_info.get('preferred_username', user_id) # Use username or ID as fallback
39
 
40
  print(f"Logged in user: Username={username}, ID={user_id}") # Log for debugging
41
 
42
- # Check if user_id is in the authorized set and is not just an empty string from split("")
43
  if user_id in AUTHORIZED_USER_IDS and user_id != "":
44
  # User is authorized for full access
45
  print(f"User {user_id} is authorized for full access.")
@@ -67,23 +77,23 @@ with gr.Blocks() as demo:
67
  gr.Markdown("# Hugging Face Spaces Tiered Access Example")
68
 
69
  # Components to display status and user identity
70
- status_message = gr.Markdown("Checking access status...")
71
  user_identity_text = gr.Textbox(label="Logged In User Info", interactive=False)
72
 
73
- # Placeholder for the Login Prompt (hidden if user is logged in)
74
- with gr.Column(visible=False) as login_prompt_column:
75
  gr.Markdown("### Please Sign in with Hugging Face")
76
  gr.Markdown("*(A 'Sign in with Hugging Face' button will appear automatically above this section when deployed on HF Spaces with OAuth enabled)*")
77
  # The actual login button is added by Hugging Face Spaces when hf_oauth: true is set in README.md
78
 
79
- # Content for users with Limited Access (visible by default or for non-authorized users)
80
  with gr.Column(visible=True) as limited_content_column:
81
  gr.Markdown("## Limited Access Content")
82
  gr.Textbox(value="This is content visible to everyone or users with limited access.", interactive=False)
83
- gr.Markdown("*(Sign in and/or get authorized for more features!)*")
84
  # Add other limited features here
85
 
86
- # Content for users with Full Access (only visible to authorized users)
87
  with gr.Column(visible=False) as full_content_column:
88
  gr.Markdown("## Full Access Content")
89
  gr.Textbox(value="🥳 Congratulations! You have unlocked the full version! 🥳", interactive=False)
@@ -91,9 +101,7 @@ with gr.Blocks() as demo:
91
  # Add other full features here
92
 
93
  # This button triggers the check_access_level function.
94
- # In a real application, you might want this check to happen
95
- # automatically on page load or after the OAuth redirect.
96
- # For demonstration purposes, a button makes the flow clear.
97
  check_button = gr.Button("Check My Access Level")
98
 
99
  # Link the button click to the access check function
@@ -110,22 +118,12 @@ with gr.Blocks() as demo:
110
  ]
111
  )
112
 
113
- # Initial check when the page loads. This helps set the initial state
114
- # based on whether the user is already logged in from a previous session/redirect.
115
- demo.load(
116
- fn=get_user_access_level,
117
- inputs=None,
118
- outputs=[
119
- status_message,
120
- user_identity_text,
121
- login_prompt_column,
122
- limited_content_column,
123
- full_content_column
124
- ]
125
- )
126
 
127
  # --- Launch the App ---
128
- # For local testing, hf_oauth won't work, request.auth will be None.
129
- # The app will show the login prompt and limited content.
 
130
  # For deployment on HF Spaces, configure README.md and Secrets.
131
  demo.launch()
 
19
  """
20
  Checks the request object to determine if a user is logged in via HF OAuth
21
  and their access level based on the AUTHORIZED_USER_IDS list.
22
+ Includes a check to prevent AssertionError if request.auth is not available.
23
  """
24
+ user_info = None
25
+ # Defensive check: Only try to access request.auth if the 'auth' attribute exists
26
+ # and is not None. This prevents the AssertionError if the middleware hasn't run
27
+ # or request.auth isn't populated in this context.
28
+ if hasattr(request, 'auth') and request.auth is not None:
29
+ user_info = request.auth
30
+ # print(f"Accessed request.auth: {user_info}") # Debugging line
31
+ else:
32
+ # print("request.auth not available or is None.") # Debugging line
33
+ pass # Keep print statements minimal in final deployed code
34
 
35
  if user_info is None:
36
+ # User is not logged in or auth info is not available in the request context
37
+ print("User not logged in or auth info not retrieved via request.auth.")
38
  return {
39
+ "status_message": "Please sign in with Hugging Face to check your access.",
40
+ "user_identity": "Not Logged In or Auth Info Unavailable",
41
  "show_login_prompt": gr.update(visible=True),
42
  "show_limited_content": gr.update(visible=True),
43
  "show_full_content": gr.update(visible=False)
44
  }
45
  else:
46
+ # User info is available (presumably logged in via HF OAuth)
47
  user_id = user_info.get('sub') # 'sub' is the standard claim for user ID
48
  username = user_info.get('preferred_username', user_id) # Use username or ID as fallback
49
 
50
  print(f"Logged in user: Username={username}, ID={user_id}") # Log for debugging
51
 
52
+ # Check if user_id is in the authorized set and is not just an empty string from split(",")
53
  if user_id in AUTHORIZED_USER_IDS and user_id != "":
54
  # User is authorized for full access
55
  print(f"User {user_id} is authorized for full access.")
 
77
  gr.Markdown("# Hugging Face Spaces Tiered Access Example")
78
 
79
  # Components to display status and user identity
80
+ status_message = gr.Markdown("Click 'Check My Access Level' after signing in.")
81
  user_identity_text = gr.Textbox(label="Logged In User Info", interactive=False)
82
 
83
+ # Placeholder for the Login Prompt (visible by default)
84
+ with gr.Column(visible=True) as login_prompt_column:
85
  gr.Markdown("### Please Sign in with Hugging Face")
86
  gr.Markdown("*(A 'Sign in with Hugging Face' button will appear automatically above this section when deployed on HF Spaces with OAuth enabled)*")
87
  # The actual login button is added by Hugging Face Spaces when hf_oauth: true is set in README.md
88
 
89
+ # Content for users with Limited Access (visible by default)
90
  with gr.Column(visible=True) as limited_content_column:
91
  gr.Markdown("## Limited Access Content")
92
  gr.Textbox(value="This is content visible to everyone or users with limited access.", interactive=False)
93
+ gr.Markdown("*(Sign in with Hugging Face and click 'Check My Access Level' to see your status and potentially unlock full features!)*")
94
  # Add other limited features here
95
 
96
+ # Content for users with Full Access (hidden by default)
97
  with gr.Column(visible=False) as full_content_column:
98
  gr.Markdown("## Full Access Content")
99
  gr.Textbox(value="🥳 Congratulations! You have unlocked the full version! 🥳", interactive=False)
 
101
  # Add other full features here
102
 
103
  # This button triggers the check_access_level function.
104
+ # The user should click this *after* potentially signing in via the HF button.
 
 
105
  check_button = gr.Button("Check My Access Level")
106
 
107
  # Link the button click to the access check function
 
118
  ]
119
  )
120
 
121
+ # Removed the demo.load() call again, as it seems problematic with request.auth
122
+ # The initial state is controlled by component 'visible' attributes.
 
 
 
 
 
 
 
 
 
 
 
123
 
124
  # --- Launch the App ---
125
+ # For local testing, hf_oauth won't work, request.auth will not be available,
126
+ # but the app should now run without the AssertionError on button click due to the check.
127
+ # It will show the login prompt and limited content.
128
  # For deployment on HF Spaces, configure README.md and Secrets.
129
  demo.launch()