n00b001 commited on
Commit
b1f5ebf
·
verified ·
1 Parent(s): 3717818

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -11
app.py CHANGED
@@ -58,19 +58,23 @@ def get_quantization_recipe(method, model_architecture):
58
  raise ValueError(f"Unsupported quantization method: {method}")
59
 
60
  # --------------------------------------------------------------------------------
61
- # CHANGE #1: Modified function signature to use gr.Request
62
  # --------------------------------------------------------------------------------
63
- def compress_and_upload(model_id: str, quant_method: str, request: gr.Request):
64
  """
65
  Compresses a model using llm-compressor and uploads it to a new HF repo.
66
  """
67
- oauth_token = request.token # Get the token from the request object
68
-
69
  if not model_id:
70
  raise gr.Error("Please select a model from the search bar.")
71
 
72
- if oauth_token is None:
73
- raise gr.Error("Please log in with your Hugging Face account to continue.")
 
 
 
 
 
 
74
 
75
  try:
76
  # --- 1. Load Model and Tokenizer ---
@@ -96,8 +100,8 @@ def compress_and_upload(model_id: str, quant_method: str, request: gr.Request):
96
  )
97
 
98
  # --- 4. Create Repo and Upload ---
99
- api = HfApi(token=oauth_token)
100
- username = whoami(token=oauth_token)["name"]
101
  repo_id = f"{username}/{output_dir}"
102
 
103
  repo_url = api.create_repo(repo_id=repo_id, exist_ok=True)
@@ -130,7 +134,7 @@ This conversion was performed by the `llm-compressor-my-repo` Hugging Face Space
130
  For more details on the recipe used, refer to the `recipe.yaml` file in this repository.
131
  """
132
  card = ModelCard(card_content)
133
- card.push_to_hub(repo_id, token=oauth_token)
134
 
135
  return f'<h1>✅ Success!</h1><br/>Model compressed and saved to your new repo: <a href="{repo_url}" target="_blank" style="text-decoration:underline">{repo_id}</a>'
136
 
@@ -164,11 +168,11 @@ with gr.Blocks(css="footer {display: none !important;}") as demo:
164
  output_html = gr.HTML(label="Result")
165
 
166
  # --------------------------------------------------------------------------------
167
- # CHANGE #2: Removed `login_button` from the inputs list
168
  # --------------------------------------------------------------------------------
169
  compress_button.click(
170
  fn=compress_and_upload,
171
- inputs=[model_input, quant_method_dropdown], # login_button removed
172
  outputs=output_html
173
  )
174
 
 
58
  raise ValueError(f"Unsupported quantization method: {method}")
59
 
60
  # --------------------------------------------------------------------------------
61
+ # CHANGE #1: The function no longer needs the `request` or `oauth_token` argument.
62
  # --------------------------------------------------------------------------------
63
+ def compress_and_upload(model_id: str, quant_method: str):
64
  """
65
  Compresses a model using llm-compressor and uploads it to a new HF repo.
66
  """
 
 
67
  if not model_id:
68
  raise gr.Error("Please select a model from the search bar.")
69
 
70
+ # Check for login status by calling whoami(). It will raise an error if not logged in.
71
+ try:
72
+ user_info = whoami()
73
+ if user_info is None:
74
+ raise gr.Error("Authentication error. Please log in to continue.")
75
+ username = user_info["name"]
76
+ except Exception as e:
77
+ raise gr.Error(f"Authentication error. Please log in to continue. Details: {e}")
78
 
79
  try:
80
  # --- 1. Load Model and Tokenizer ---
 
100
  )
101
 
102
  # --- 4. Create Repo and Upload ---
103
+ # HfApi() will automatically use the logged-in user's token.
104
+ api = HfApi()
105
  repo_id = f"{username}/{output_dir}"
106
 
107
  repo_url = api.create_repo(repo_id=repo_id, exist_ok=True)
 
134
  For more details on the recipe used, refer to the `recipe.yaml` file in this repository.
135
  """
136
  card = ModelCard(card_content)
137
+ card.push_to_hub(repo_id) # No token needed here either
138
 
139
  return f'<h1>✅ Success!</h1><br/>Model compressed and saved to your new repo: <a href="{repo_url}" target="_blank" style="text-decoration:underline">{repo_id}</a>'
140
 
 
168
  output_html = gr.HTML(label="Result")
169
 
170
  # --------------------------------------------------------------------------------
171
+ # CHANGE #2: The inputs list is now simpler and matches the function signature.
172
  # --------------------------------------------------------------------------------
173
  compress_button.click(
174
  fn=compress_and_upload,
175
+ inputs=[model_input, quant_method_dropdown],
176
  outputs=output_html
177
  )
178