abidlabs HF Staff commited on
Commit
43f7874
·
1 Parent(s): e906361

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from typing import Optional, List
3
+ import requests
4
+
5
+
6
+ def list_org_users() -> List[str]:
7
+ return [
8
+ user["user"] # e.g. "julien-c"
9
+ for user in requests.get("https://huggingface.co/api/organizations/OAuthTesters/members").json()
10
+ ]
11
+
12
+
13
+ def hello(profile: Optional[gr.OAuthProfile]) -> str:
14
+ if profile is None:
15
+ return "## You must be logged in to access this section."
16
+ if profile["preferred_username"] not in list_org_users():
17
+ return (
18
+ "## You must be a member of the OAuthTesters organization to access this section.\n\nGo to"
19
+ " https://huggingface.co/organizations/OAuthTesters to join."
20
+ )
21
+ return (
22
+ "## Yay! You are part of the team and can see this very secret section!\n\n<img"
23
+ " src='https://huggingface.co/spaces/Wauplin/gradio-oauth-test/resolve/main/happy.gif' height='700'>"
24
+ )
25
+
26
+
27
+ with gr.Blocks() as demo:
28
+ gr.Markdown(
29
+ "# Gradio OAuth Space\n\nThis Space is a demo for the new **Sign in with Hugging Face** feature.\n\nIt shows"
30
+ " how you can use it to restrict access only to members of an organization. First it requires users to log in"
31
+ " and then checks if the user meets the requirements (been part of"
32
+ " [OAuthTesters](https://huggingface.co/OAuthTesters) in this case)\n\nSee"
33
+ " https://github.com/gradio-app/gradio/pull/4943 for technical details."
34
+ )
35
+ with gr.Row():
36
+ gr.LoginButton()
37
+ gr.LogoutButton()
38
+ gr.Markdown().attach_load_event(hello, None)
39
+
40
+ demo.launch()