Spaces:
Running
Running
Update app.py
Browse files
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:
|
| 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 |
-
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
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 100 |
-
|
| 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
|
| 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:
|
| 168 |
# --------------------------------------------------------------------------------
|
| 169 |
compress_button.click(
|
| 170 |
fn=compress_and_upload,
|
| 171 |
-
inputs=[model_input, quant_method_dropdown],
|
| 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 |
|