Spaces:
Runtime error
Runtime error
Add Error Message for not login, Change the readme format, Change the repo name
Browse files
app.py
CHANGED
|
@@ -11,7 +11,6 @@ from gradio_huggingfacehub_search import HuggingfaceHubSearch
|
|
| 11 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 12 |
from datetime import datetime
|
| 13 |
import numpy as np
|
| 14 |
-
import shutil
|
| 15 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 16 |
|
| 17 |
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
|
@@ -195,7 +194,11 @@ def process_model(model_id, q_method, use_imatrix, imatrix_q_method, private_rep
|
|
| 195 |
export_to_org, repo_owner, org_token, oauth_token: gr.OAuthToken | None):
|
| 196 |
if oauth_token is None or oauth_token.token is None:
|
| 197 |
raise gr.Error("You must be logged in to use GGUF-my-repo")
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
user_info = whoami(oauth_token.token)
|
| 200 |
username = user_info["name"]
|
| 201 |
user_orgs = user_info.get("orgs", [])
|
|
@@ -205,7 +208,7 @@ def process_model(model_id, q_method, use_imatrix, imatrix_q_method, private_rep
|
|
| 205 |
|
| 206 |
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 207 |
logger.info(f"Time {current_time}, Username {username}, Model_ID, {model_id}, q_method {','.join(q_method)}")
|
| 208 |
-
|
| 209 |
repo_namespace = get_repo_namespace(repo_owner, username, user_orgs)
|
| 210 |
model_name = model_id.split('/')[-1]
|
| 211 |
try:
|
|
@@ -226,8 +229,7 @@ def process_model(model_id, q_method, use_imatrix, imatrix_q_method, private_rep
|
|
| 226 |
fp16 = str(Path(outdir)/f"{model_name}.fp16.gguf")
|
| 227 |
|
| 228 |
with tempfile.TemporaryDirectory(dir=downloads_dir) as tmpdir:
|
| 229 |
-
print(
|
| 230 |
-
logger.info(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " Start download")
|
| 231 |
local_dir = Path(tmpdir)/model_name
|
| 232 |
api.snapshot_download(repo_id=model_id, local_dir=local_dir, local_dir_use_symlinks=False, allow_patterns=dl_pattern)
|
| 233 |
|
|
@@ -236,16 +238,12 @@ def process_model(model_id, q_method, use_imatrix, imatrix_q_method, private_rep
|
|
| 236 |
if os.path.exists(adapter_config_dir) and not os.path.exists(config_dir):
|
| 237 |
raise Exception("adapter_config.json is present. If converting LoRA, use GGUF-my-lora.")
|
| 238 |
|
| 239 |
-
print(
|
| 240 |
-
logger.info(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " Download successfully")
|
| 241 |
result = subprocess.run(["python", CONVERSION_SCRIPT, local_dir, "--outtype", "f16", "--outfile", fp16], shell=False, capture_output=True)
|
| 242 |
-
print(
|
| 243 |
-
logger.info(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " Converted to f16")
|
| 244 |
if result.returncode != 0:
|
| 245 |
raise Exception(f"Error converting to fp16: {result.stderr.decode()}")
|
| 246 |
|
| 247 |
-
shutil.rmtree(downloads_dir)
|
| 248 |
-
|
| 249 |
imatrix_path = Path(outdir)/"imatrix.dat"
|
| 250 |
if use_imatrix:
|
| 251 |
train_data_path = train_data_file.name if train_data_file else "llama.cpp/groups_merged.txt"
|
|
@@ -258,8 +256,7 @@ def process_model(model_id, q_method, use_imatrix, imatrix_q_method, private_rep
|
|
| 258 |
|
| 259 |
gguf_files = []
|
| 260 |
for method in quant_methods:
|
| 261 |
-
print(
|
| 262 |
-
logger.info(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " Begin quantize")
|
| 263 |
name = f"{model_name.lower()}-{method.lower()}-{suffix}.gguf" if suffix else f"{model_name.lower()}-{method.lower()}.gguf"
|
| 264 |
path = str(Path(outdir)/name)
|
| 265 |
quant_cmd = ["./llama.cpp/llama-quantize", "--imatrix", imatrix_path, fp16, path, method] if use_imatrix else ["./llama.cpp/llama-quantize", fp16, path, method]
|
|
@@ -269,10 +266,9 @@ def process_model(model_id, q_method, use_imatrix, imatrix_q_method, private_rep
|
|
| 269 |
size = os.path.getsize(path)/1024/1024/1024
|
| 270 |
gguf_files.append((name, path, size, method))
|
| 271 |
|
| 272 |
-
print(
|
| 273 |
-
logger.info(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " Quantize successfully!")
|
| 274 |
suffix_for_repo = f"{imatrix_q_method}-imat" if use_imatrix else "-".join(quant_methods)
|
| 275 |
-
repo_id = f"{repo_namespace}/{model_name}-
|
| 276 |
new_repo_url = api.create_repo(repo_id=repo_id, exist_ok=True, private=private_repo)
|
| 277 |
|
| 278 |
try:
|
|
@@ -420,6 +416,7 @@ with gr.Blocks(css=".gradio-container {overflow-y: auto;}") as demo:
|
|
| 420 |
iface.render()
|
| 421 |
|
| 422 |
|
|
|
|
| 423 |
def restart_space():
|
| 424 |
HfApi().restart_space(repo_id="Antigma/quantize-my-repo", token=HF_TOKEN, factory_reboot=True)
|
| 425 |
|
|
|
|
| 11 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 12 |
from datetime import datetime
|
| 13 |
import numpy as np
|
|
|
|
| 14 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 15 |
|
| 16 |
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
|
|
|
| 194 |
export_to_org, repo_owner, org_token, oauth_token: gr.OAuthToken | None):
|
| 195 |
if oauth_token is None or oauth_token.token is None:
|
| 196 |
raise gr.Error("You must be logged in to use GGUF-my-repo")
|
| 197 |
+
try:
|
| 198 |
+
whoami(oauth_token.token)
|
| 199 |
+
except Exception as e:
|
| 200 |
+
raise gr.Error("You must be logged in to use GGUF-my-repo")
|
| 201 |
+
|
| 202 |
user_info = whoami(oauth_token.token)
|
| 203 |
username = user_info["name"]
|
| 204 |
user_orgs = user_info.get("orgs", [])
|
|
|
|
| 208 |
|
| 209 |
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 210 |
logger.info(f"Time {current_time}, Username {username}, Model_ID, {model_id}, q_method {','.join(q_method)}")
|
| 211 |
+
|
| 212 |
repo_namespace = get_repo_namespace(repo_owner, username, user_orgs)
|
| 213 |
model_name = model_id.split('/')[-1]
|
| 214 |
try:
|
|
|
|
| 229 |
fp16 = str(Path(outdir)/f"{model_name}.fp16.gguf")
|
| 230 |
|
| 231 |
with tempfile.TemporaryDirectory(dir=downloads_dir) as tmpdir:
|
| 232 |
+
print("Downloading")
|
|
|
|
| 233 |
local_dir = Path(tmpdir)/model_name
|
| 234 |
api.snapshot_download(repo_id=model_id, local_dir=local_dir, local_dir_use_symlinks=False, allow_patterns=dl_pattern)
|
| 235 |
|
|
|
|
| 238 |
if os.path.exists(adapter_config_dir) and not os.path.exists(config_dir):
|
| 239 |
raise Exception("adapter_config.json is present. If converting LoRA, use GGUF-my-lora.")
|
| 240 |
|
| 241 |
+
print("Download successfully")
|
|
|
|
| 242 |
result = subprocess.run(["python", CONVERSION_SCRIPT, local_dir, "--outtype", "f16", "--outfile", fp16], shell=False, capture_output=True)
|
| 243 |
+
print("Converted to f16")
|
|
|
|
| 244 |
if result.returncode != 0:
|
| 245 |
raise Exception(f"Error converting to fp16: {result.stderr.decode()}")
|
| 246 |
|
|
|
|
|
|
|
| 247 |
imatrix_path = Path(outdir)/"imatrix.dat"
|
| 248 |
if use_imatrix:
|
| 249 |
train_data_path = train_data_file.name if train_data_file else "llama.cpp/groups_merged.txt"
|
|
|
|
| 256 |
|
| 257 |
gguf_files = []
|
| 258 |
for method in quant_methods:
|
| 259 |
+
print("Begin quantize")
|
|
|
|
| 260 |
name = f"{model_name.lower()}-{method.lower()}-{suffix}.gguf" if suffix else f"{model_name.lower()}-{method.lower()}.gguf"
|
| 261 |
path = str(Path(outdir)/name)
|
| 262 |
quant_cmd = ["./llama.cpp/llama-quantize", "--imatrix", imatrix_path, fp16, path, method] if use_imatrix else ["./llama.cpp/llama-quantize", fp16, path, method]
|
|
|
|
| 266 |
size = os.path.getsize(path)/1024/1024/1024
|
| 267 |
gguf_files.append((name, path, size, method))
|
| 268 |
|
| 269 |
+
print("Quantize successfully!")
|
|
|
|
| 270 |
suffix_for_repo = f"{imatrix_q_method}-imat" if use_imatrix else "-".join(quant_methods)
|
| 271 |
+
repo_id = f"{repo_namespace}/{model_name}-GGUF"
|
| 272 |
new_repo_url = api.create_repo(repo_id=repo_id, exist_ok=True, private=private_repo)
|
| 273 |
|
| 274 |
try:
|
|
|
|
| 416 |
iface.render()
|
| 417 |
|
| 418 |
|
| 419 |
+
|
| 420 |
def restart_space():
|
| 421 |
HfApi().restart_space(repo_id="Antigma/quantize-my-repo", token=HF_TOKEN, factory_reboot=True)
|
| 422 |
|