badaoui HF Staff commited on
Commit
ffec3e8
Β·
verified Β·
1 Parent(s): 96104e4

add login requirement

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import csv
2
  import os
3
  from datetime import datetime
4
- from typing import Optional
5
 
6
  import gradio as gr
7
  from huggingface_hub import HfApi, Repository
@@ -24,15 +24,17 @@ repo: Optional[Repository] = None
24
  # repo = Repository(local_dir=DATADIR, clone_from=DATASET_REPO_URL, token=HF_TOKEN)
25
 
26
 
27
- def neuron_export(model_id: str, task: str) -> str:
 
 
 
28
  if not model_id:
29
  return f"### Invalid input 🐞 Please specify a model name, got {model_id}"
30
 
31
  try:
32
- api = HfApi(token=HF_TOKEN) # Use HF_TOKEN if available, else anonymous
33
- token = HF_TOKEN # Pass token to convert only if available
34
 
35
- error, commit_info = convert(api=api, model_id=model_id, task=task, token=token)
36
  if error != "0":
37
  return error
38
 
@@ -56,7 +58,7 @@ def neuron_export(model_id: str, task: str) -> str:
56
  print("[dataset]", commit_url)
57
 
58
  pr_revision = commit_info.pr_revision.replace("/", "%2F")
59
- return f"#### Success πŸ”₯ This model was successfully exported and a PR was opened: [{commit_info.pr_url}]({commit_info.pr_url}). To use the model before the PR is approved, go to https://huggingface.co/{model_id}/tree/{pr_revision}"
60
 
61
  except Exception as e:
62
  return f"#### Error: {e}"
@@ -77,15 +79,16 @@ TITLE = """
77
  """
78
 
79
  DESCRIPTION = """
80
- Export πŸ€— Transformers models hosted on the Hugging Face Hub to AWS Neuron-optimized format for Inferentia/Trainium acceleration.
81
 
82
  **Features:**
83
  - Automatically opens PR with Neuron-optimized model
84
  - Preserves original model weights
85
  - Adds proper tags to model card
86
 
87
- **Note:**
88
- - PR creation requires the Space owner to have a valid write token set via HF_WRITE_TOKEN
 
89
  """
90
 
91
  # Custom CSS to fix dark mode compatibility and transparency issues
@@ -145,6 +148,10 @@ CUSTOM_CSS = """
145
  """
146
 
147
  with gr.Blocks(css=CUSTOM_CSS) as demo:
 
 
 
 
148
  # Centered title and image
149
  gr.HTML(TITLE_IMAGE)
150
  gr.HTML(TITLE)
 
1
  import csv
2
  import os
3
  from datetime import datetime
4
+ from typing import Optional, Union
5
 
6
  import gradio as gr
7
  from huggingface_hub import HfApi, Repository
 
24
  # repo = Repository(local_dir=DATADIR, clone_from=DATASET_REPO_URL, token=HF_TOKEN)
25
 
26
 
27
+ def neuron_export(model_id: str, task: str, oauth_token: gr.OAuthToken) -> str:
28
+ if oauth_token.token is None:
29
+ return "You must be logged in to use this space"
30
+
31
  if not model_id:
32
  return f"### Invalid input 🐞 Please specify a model name, got {model_id}"
33
 
34
  try:
35
+ api = HfApi(token=oauth_token.token)
 
36
 
37
+ error, commit_info = convert(api=api, model_id=model_id, task=task, token=oauth_token.token)
38
  if error != "0":
39
  return error
40
 
 
58
  print("[dataset]", commit_url)
59
 
60
  pr_revision = commit_info.pr_revision.replace("/", "%2F")
61
+ return f"#### Success πŸ”₯ Yay! This model was successfully exported and a PR was opened using your token: [{commit_info.pr_url}]({commit_info.pr_url}). If you would like to use the exported model without waiting for the PR to be approved, head to https://huggingface.co/{model_id}/tree/{pr_revision}"
62
 
63
  except Exception as e:
64
  return f"#### Error: {e}"
 
79
  """
80
 
81
  DESCRIPTION = """
82
+ This Space allows you to automatically export πŸ€— transformers models hosted on the Hugging Face Hub to AWS Neuron-optimized format for Inferentia/Trainium acceleration. It opens a PR on the target model, and it is up to the owner of the original model to merge the PR to allow people to leverage Neuron optimization!
83
 
84
  **Features:**
85
  - Automatically opens PR with Neuron-optimized model
86
  - Preserves original model weights
87
  - Adds proper tags to model card
88
 
89
+ **Requirements:**
90
+ - Model must be compatible with [Optimum Neuron](https://huggingface.co/docs/optimum-neuron)
91
+ - User must be logged in with write token
92
  """
93
 
94
  # Custom CSS to fix dark mode compatibility and transparency issues
 
148
  """
149
 
150
  with gr.Blocks(css=CUSTOM_CSS) as demo:
151
+ # Login requirement notice and button
152
+ gr.Markdown("**You must be logged in to use this space**")
153
+ gr.LoginButton(min_width=250)
154
+
155
  # Centered title and image
156
  gr.HTML(TITLE_IMAGE)
157
  gr.HTML(TITLE)