abidlabs HF Staff Wauplin HF Staff commited on
Commit
0a1bcd8
·
0 Parent(s):

Duplicate from Wauplin/gradio-oauth-test

Browse files

Co-authored-by: Lucain Pouget <[email protected]>

Files changed (10) hide show
  1. .gitattributes +35 -0
  2. .gitignore +140 -0
  3. Dockerfile +21 -0
  4. README.md +12 -0
  5. app.py +40 -0
  6. app2.py +42 -0
  7. happy.gif +0 -0
  8. pyproject.toml +11 -0
  9. requirements.txt +2 -0
  10. setup.cfg +14 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib64/
18
+ parts/
19
+ sdist/
20
+ var/
21
+ wheels/
22
+ pip-wheel-metadata/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+
53
+ # Translations
54
+ *.mo
55
+ *.pot
56
+
57
+ # Django stuff:
58
+ *.log
59
+ local_settings.py
60
+ db.sqlite3
61
+ db.sqlite3-journal
62
+
63
+ # Flask stuff:
64
+ instance/
65
+ .webassets-cache
66
+
67
+ # Scrapy stuff:
68
+ .scrapy
69
+
70
+ # Sphinx documentation
71
+ docs/_build/
72
+
73
+ # PyBuilder
74
+ target/
75
+
76
+ # Jupyter Notebook
77
+ .ipynb_checkpoints
78
+
79
+ # IPython
80
+ profile_default/
81
+ ipython_config.py
82
+
83
+ # pyenv
84
+ .python-version
85
+
86
+ # pipenv
87
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
88
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
89
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
90
+ # install all needed dependencies.
91
+ #Pipfile.lock
92
+
93
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
94
+ __pypackages__/
95
+
96
+ # Celery stuff
97
+ celerybeat-schedule
98
+ celerybeat.pid
99
+
100
+ # SageMath parsed files
101
+ *.sage.py
102
+
103
+ # Environments
104
+ .env
105
+ .venv
106
+ .venv*
107
+ env/
108
+ venv/
109
+ ENV/
110
+ env.bak/
111
+ venv.bak/
112
+ .venv*
113
+
114
+ # Spyder project settings
115
+ .spyderproject
116
+ .spyproject
117
+
118
+ # Rope project settings
119
+ .ropeproject
120
+
121
+ # mkdocs documentation
122
+ /site
123
+
124
+ # mypy
125
+ .mypy_cache/
126
+ .dmypy.json
127
+ dmypy.json
128
+
129
+ # Pyre type checker
130
+ .pyre/
131
+ .vscode/
132
+ .idea/
133
+
134
+ .DS_Store
135
+
136
+ # Ruff
137
+ .ruff_cache
138
+
139
+ # Spell checker config
140
+ cspell.json
Dockerfile ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+
3
+ # User
4
+ RUN useradd -m -u 1000 user
5
+ USER user
6
+ ENV HOME=/home/user \
7
+ PATH=/home/user/.local/bin:$PATH
8
+ WORKDIR /home/user/app
9
+
10
+ RUN pip install --no-cache-dir https://gradio-builds.s3.amazonaws.com/ab307b331cfd3656255c5a2c46e9659727625970/gradio-3.39.0-py3-none-any.whl
11
+
12
+ COPY --link --chown=1000 ./ /home/user/app
13
+ ENV PYTHONPATH=$HOME/app \
14
+ PYTHONUNBUFFERED=1 \
15
+ GRADIO_ALLOW_FLAGGING=never \
16
+ GRADIO_NUM_PORTS=1 \
17
+ GRADIO_SERVER_NAME=0.0.0.0 \
18
+ GRADIO_THEME=huggingface \
19
+ SYSTEM=spaces
20
+
21
+ ENTRYPOINT ["python", "app.py"]
README.md ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Gradio Oauth Test
3
+ emoji: 🏆
4
+ colorFrom: pink
5
+ colorTo: pink
6
+ sdk: docker
7
+ hf_oauth: true
8
+ app_port: 7860
9
+ duplicated_from: Wauplin/gradio-oauth-test
10
+ ---
11
+
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
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()
app2.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import gradio as gr
4
+ import gradio.utils
5
+
6
+
7
+ if gradio.utils.get_space() is not None:
8
+ URL, PORT = "https://wauplin-gradio-oauth-test.hf.space", 7860
9
+ else:
10
+ URL, PORT = "http://localhost:5173", 5173
11
+
12
+ TEMPLATE = """
13
+ ### Name: {name}
14
+ ### Username: {preferred_username}
15
+ ### Profile: {profile}
16
+ ### Website: {website}
17
+
18
+ ![Profile Picture]({picture})
19
+
20
+ You can manage your connected applications in your [settings](https://huggingface.co/settings/connected-applications).
21
+ """
22
+
23
+
24
+ def show_profile(profile: gr.OAuthProfile) -> str:
25
+ # request.session in case of websockets (see `def get_request_params`)
26
+ # request.request.session in case of direct call
27
+ return TEMPLATE.format(**profile)
28
+
29
+
30
+ with gr.Blocks() as demo:
31
+ with gr.Row():
32
+ gr.LoginButton()
33
+ gr.LogoutButton()
34
+
35
+ profile_btn = gr.Button("Show profile")
36
+ output = gr.Markdown()
37
+ profile_btn.click(fn=show_profile, outputs=output)
38
+
39
+ print(URL)
40
+
41
+ demo.queue()
42
+ demo.launch(server_port=PORT)
happy.gif ADDED
pyproject.toml ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [tool.black]
2
+ line-length = 119
3
+ target_version = ['py38', 'py39', 'py310']
4
+ preview = true
5
+
6
+ [tool.ruff]
7
+ select = ["E", "F", "I", "W"]
8
+ line-length = 119
9
+
10
+ [tool.ruff.isort]
11
+ lines-after-imports = 2
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ authlib==1.2.1
2
+ itsdangerous==2.1.2
setup.cfg ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [isort]
2
+ default_section = FIRSTPARTY
3
+ ensure_newline_before_comments = True
4
+ force_grid_wrap = 0
5
+ include_trailing_comma = True
6
+ line_length = 119
7
+ lines_after_imports = 2
8
+ multi_line_output = 3
9
+ use_parentheses = True
10
+
11
+ [flake8]
12
+ exclude = .git,__pycache__,old,build,dist,.venv*
13
+ ignore = B028, E203, E501, E741, W503
14
+ max-line-length = 119