Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import list_models
|
| 3 |
+
|
| 4 |
+
def hello(profile: gr.OAuthProfile | None) -> str:
|
| 5 |
+
# ^ expect a gr.OAuthProfile object as input to get the user's profile
|
| 6 |
+
# if the user is not logged in, profile will be None
|
| 7 |
+
if profile is None:
|
| 8 |
+
return "I don't know you."
|
| 9 |
+
return f"Hello {profile.name}"
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def list_private_models(profile: gr.OAuthProfile | None, oauth_token: gr.OAuthToken | None) -> str:
|
| 13 |
+
# ^ expect a gr.OAuthToken object as input to get the user's token
|
| 14 |
+
# if the user is not logged in, oauth_token will be None
|
| 15 |
+
gr.Textbox(oauth_token)
|
| 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()
|