Create OAuth.py
Browse files
OAuth.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import list_models
|
3 |
+
|
4 |
+
|
5 |
+
def hello(profile: gr.OAuthProfile | None) -> str:
|
6 |
+
# ^ expect a gr.OAuthProfile object as input to get the user's profile
|
7 |
+
# if the user is not logged in, profile will be None
|
8 |
+
if profile is None:
|
9 |
+
return "I don't know you."
|
10 |
+
return f"Hello {profile.name}"
|
11 |
+
|
12 |
+
|
13 |
+
def list_private_models(profile: gr.OAuthProfile | None, oauth_token: gr.OAuthToken | None) -> str:
|
14 |
+
# ^ expect a gr.OAuthToken object as input to get the user's token
|
15 |
+
# if the user is not logged in, oauth_token will be None
|
16 |
+
if oauth_token is None:
|
17 |
+
return "Please log in to list private models."
|
18 |
+
models = [
|
19 |
+
f"{model.id} ({'private' if model.private else 'public'})"
|
20 |
+
for model in list_models(author=profile.username, token=oauth_token.token)
|
21 |
+
]
|
22 |
+
return "Models:\n\n" + "\n - ".join(models) + "."
|
23 |
+
|
24 |
+
|
25 |
+
with gr.Blocks() as demo:
|
26 |
+
gr.Markdown(
|
27 |
+
"# Gradio OAuth Space"
|
28 |
+
"\n\nThis Space is a demo for the **Sign in with Hugging Face** feature. "
|
29 |
+
"Duplicate this Space to get started."
|
30 |
+
"\n\nFor more details, check out:"
|
31 |
+
"\n- https://www.gradio.app/guides/sharing-your-app#o-auth-login-via-hugging-face"
|
32 |
+
"\n- https://huggingface.co/docs/hub/spaces-oauth"
|
33 |
+
)
|
34 |
+
gr.LoginButton()
|
35 |
+
# ^ add a login button to the Space
|
36 |
+
m1 = gr.Markdown()
|
37 |
+
m2 = gr.Markdown()
|
38 |
+
demo.load(hello, inputs=None, outputs=m1)
|
39 |
+
demo.load(list_private_models, inputs=None, outputs=m2)
|
40 |
+
|
41 |
+
demo.launch()
|