CognitiveScience commited on
Commit
184d88a
·
verified ·
1 Parent(s): 886474b

Update app.py

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