Felladrin commited on
Commit
2af96bc
·
1 Parent(s): f2501ef

Allow users to provide their own tokens in case they want to upload the model to under their own account

Browse files
Files changed (2) hide show
  1. README.md +0 -2
  2. app.py +15 -7
README.md CHANGED
@@ -52,11 +52,9 @@ This project provides a Streamlit application that converts Hugging Face models
52
  ```bash
53
  # Option 1: Environment variables
54
  export HF_TOKEN="your_token"
55
- export HF_USERNAME="your_username"
56
 
57
  # Option 2: Create .streamlit/secrets.toml
58
  echo 'HF_TOKEN = "your_token"' > .streamlit/secrets.toml
59
- echo 'HF_USERNAME = "your_username"' >> .streamlit/secrets.toml
60
  ```
61
 
62
  ## Usage
 
52
  ```bash
53
  # Option 1: Environment variables
54
  export HF_TOKEN="your_token"
 
55
 
56
  # Option 2: Create .streamlit/secrets.toml
57
  echo 'HF_TOKEN = "your_token"' > .streamlit/secrets.toml
 
58
  ```
59
 
60
  ## Usage
app.py CHANGED
@@ -8,7 +8,7 @@ from typing import Optional, Tuple
8
  from urllib.request import urlopen, urlretrieve
9
 
10
  import streamlit as st
11
- from huggingface_hub import HfApi
12
 
13
  logging.basicConfig(level=logging.INFO)
14
  logger = logging.getLogger(__name__)
@@ -30,15 +30,17 @@ class Config:
30
  @classmethod
31
  def from_env(cls) -> "Config":
32
  """Create config from environment variables and secrets."""
33
- hf_token = st.secrets.get("HF_TOKEN") or os.getenv("HF_TOKEN", "")
 
34
  hf_username = (
35
- st.secrets.get("HF_USERNAME")
36
- or os.getenv("HF_USERNAME")
37
- or os.getenv("SPACE_AUTHOR_NAME", "")
38
  )
 
39
 
40
- if not hf_token or not hf_username:
41
- raise ValueError("HF_TOKEN and HF_USERNAME must be set")
42
 
43
  return cls(hf_token=hf_token, hf_username=hf_username)
44
 
@@ -162,6 +164,12 @@ def main():
162
  if not input_model_id:
163
  return
164
 
 
 
 
 
 
 
165
  model_name = (
166
  input_model_id.replace(f"{config.hf_base_url}/", "")
167
  .replace("/", "-")
 
8
  from urllib.request import urlopen, urlretrieve
9
 
10
  import streamlit as st
11
+ from huggingface_hub import whoami, HfApi
12
 
13
  logging.basicConfig(level=logging.INFO)
14
  logger = logging.getLogger(__name__)
 
30
  @classmethod
31
  def from_env(cls) -> "Config":
32
  """Create config from environment variables and secrets."""
33
+ system_token = st.secrets.get("HF_TOKEN") or os.getenv("HF_TOKEN", "")
34
+ user_token = st.session_state.get("user_hf_token", "")
35
  hf_username = (
36
+ whoami(token=user_token)["name"]
37
+ or os.getenv("SPACE_AUTHOR_NAME")
38
+ or whoami(token=system_token)["name"]
39
  )
40
+ hf_token = user_token or system_token
41
 
42
+ if not hf_token:
43
+ raise ValueError("HF_TOKEN must be set")
44
 
45
  return cls(hf_token=hf_token, hf_username=hf_username)
46
 
 
164
  if not input_model_id:
165
  return
166
 
167
+ st.text_input(
168
+ f"Optional: Your Hugging Face write token. Leave empty to upload under {config.hf_username}'s account.",
169
+ type="password",
170
+ key="user_hf_token",
171
+ )
172
+
173
  model_name = (
174
  input_model_id.replace(f"{config.hf_base_url}/", "")
175
  .replace("/", "-")