Swap from token to login button

#12
by multimodalart HF staff - opened
Files changed (2) hide show
  1. README.md +7 -2
  2. app.py +21 -16
README.md CHANGED
@@ -4,10 +4,15 @@ emoji: 😻
4
  colorFrom: gray
5
  colorTo: blue
6
  sdk: gradio
7
- sdk_version: 4.26.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
 
 
 
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
 
4
  colorFrom: gray
5
  colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 4.42.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
+ hf_oauth: true
12
+ hf_oauth_scopes:
13
+ - read-repos
14
+ - write-repos
15
+ - manage-repos
16
  ---
17
 
18
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
app.py CHANGED
@@ -10,16 +10,17 @@ ENDPOINT = "https://huggingface.co"
10
  REPO_TYPES = ["model", "dataset", "space"]
11
 
12
 
13
- def duplicate(source_repo, dst_repo, token, repo_type, private):
 
14
  try:
15
  if not repo_type in REPO_TYPES:
16
  raise ValueError("need to select valid repo type")
17
- _ = whoami(token)
18
  # ^ this will throw if token is invalid
19
 
20
  r = requests.post(
21
  f"{ENDPOINT}/api/{repo_type}s/{source_repo}/duplicate",
22
- headers=build_hf_headers(token=token),
23
  json={"repository": dst_repo, "private": private},
24
  )
25
  hf_raise_for_status(r)
@@ -32,15 +33,7 @@ def duplicate(source_repo, dst_repo, token, repo_type, private):
32
  )
33
 
34
  except Exception as e:
35
- return (
36
- f"""
37
- ### Error 😢😢😢
38
-
39
- {e}
40
- """,
41
- None,
42
- )
43
-
44
 
45
  interface = gr.Interface(
46
  fn=duplicate,
@@ -51,7 +44,6 @@ interface = gr.Interface(
51
  sumbit_on_select=False,
52
  ),
53
  gr.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)"),
54
- gr.Textbox(placeholder="Write access token", type="password"),
55
  gr.Dropdown(choices=REPO_TYPES, value="model"),
56
  gr.Checkbox(label="Make new repo private?"),
57
  ],
@@ -63,7 +55,20 @@ interface = gr.Interface(
63
  description="Duplicate a Hugging Face repository! You need to specify a write token obtained in https://hf.co/settings/tokens. This Space is a an experimental demo.",
64
  article="<p>Find your write token at <a href='https://huggingface.co/settings/tokens' target='_blank'>token settings</a></p>",
65
  allow_flagging="never",
66
- live=False,
67
  )
68
- interface.queue()
69
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  REPO_TYPES = ["model", "dataset", "space"]
11
 
12
 
13
+ def duplicate(source_repo, dst_repo, repo_type, private, oauth_token: gr.OAuthToken | None):
14
+ print(oauth_token.token)
15
  try:
16
  if not repo_type in REPO_TYPES:
17
  raise ValueError("need to select valid repo type")
18
+ _ = whoami(oauth_token.token)
19
  # ^ this will throw if token is invalid
20
 
21
  r = requests.post(
22
  f"{ENDPOINT}/api/{repo_type}s/{source_repo}/duplicate",
23
+ headers=build_hf_headers(token=oauth_token.token),
24
  json={"repository": dst_repo, "private": private},
25
  )
26
  hf_raise_for_status(r)
 
33
  )
34
 
35
  except Exception as e:
36
+ raise gr.Error(f"""Oops, you forgot to login. Please use the loggin button on the top left to migrate your repo {e}""")
 
 
 
 
 
 
 
 
37
 
38
  interface = gr.Interface(
39
  fn=duplicate,
 
44
  sumbit_on_select=False,
45
  ),
46
  gr.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)"),
 
47
  gr.Dropdown(choices=REPO_TYPES, value="model"),
48
  gr.Checkbox(label="Make new repo private?"),
49
  ],
 
55
  description="Duplicate a Hugging Face repository! You need to specify a write token obtained in https://hf.co/settings/tokens. This Space is a an experimental demo.",
56
  article="<p>Find your write token at <a href='https://huggingface.co/settings/tokens' target='_blank'>token settings</a></p>",
57
  allow_flagging="never",
58
+ live=False
59
  )
60
+
61
+ def swap_visibilty(profile: gr.OAuthProfile | None):
62
+ return gr.update(elem_classes=["main_ui_logged_in"]) if profile else gr.update(elem_classes=["main_ui_logged_out"])
63
+
64
+ css = '''
65
+ .main_ui_logged_out{opacity: 0.3; pointer-events: none}
66
+ '''
67
+ with gr.Blocks(css=css) as demo:
68
+ gr.LoginButton()
69
+ with gr.Column(elem_classes="main_ui_logged_out") as main_ui:
70
+ interface.render()
71
+ demo.load(fn=swap_visibilty, outputs=main_ui)
72
+
73
+ demo.queue()
74
+ demo.launch()