Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,10 +33,10 @@ QUANT_PARAMS = {
|
|
| 33 |
def list_files_in_folder(folder_path):
|
| 34 |
# List all files and directories in the specified folder
|
| 35 |
all_items = os.listdir(folder_path)
|
| 36 |
-
|
| 37 |
# Filter out only files
|
| 38 |
files = [item for item in all_items if os.path.isfile(os.path.join(folder_path, item))]
|
| 39 |
-
|
| 40 |
return files
|
| 41 |
|
| 42 |
def clear_hf_cache_space():
|
|
@@ -48,8 +48,8 @@ def clear_hf_cache_space():
|
|
| 48 |
scan.delete_revisions(*to_delete).execute()
|
| 49 |
print("Cache has been cleared")
|
| 50 |
|
| 51 |
-
def upload_to_hub(path, upload_repo, hf_path,
|
| 52 |
-
card = ModelCard.load(hf_path, token=
|
| 53 |
card.data.tags = ["mlx"] if card.data.tags is None else card.data.tags + ["mlx", "mlx-my-repo"]
|
| 54 |
card.data.base_model = hf_path
|
| 55 |
card.text = dedent(
|
|
@@ -85,7 +85,7 @@ def upload_to_hub(path, upload_repo, hf_path, oauth_token):
|
|
| 85 |
|
| 86 |
logging.set_verbosity_info()
|
| 87 |
|
| 88 |
-
api = HfApi(token=
|
| 89 |
api.create_repo(repo_id=upload_repo, exist_ok=True)
|
| 90 |
|
| 91 |
files = list_files_in_folder(path)
|
|
@@ -98,35 +98,49 @@ def upload_to_hub(path, upload_repo, hf_path, oauth_token):
|
|
| 98 |
path_in_repo=file,
|
| 99 |
repo_id=upload_repo,
|
| 100 |
)
|
| 101 |
-
|
| 102 |
-
print(f"Upload successful, go to https://huggingface.co/{upload_repo} for details.")
|
| 103 |
|
| 104 |
def process_model(model_id, q_method, oauth_token: gr.OAuthToken | None):
|
| 105 |
if oauth_token.token is None:
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
if q_method == "FP16":
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
# The target directory must not exist
|
| 115 |
-
mlx_path = os.path.join(tmpdir, "mlx")
|
| 116 |
-
convert(model_id, mlx_path=mlx_path, quantize=False, dtype="float16")
|
| 117 |
-
print("Conversion done")
|
| 118 |
-
upload_to_hub(path=mlx_path, upload_repo=upload_repo, hf_path=model_id, oauth_token=oauth_token)
|
| 119 |
-
print("Upload done")
|
| 120 |
else:
|
| 121 |
q_bits = QUANT_PARAMS[q_method]
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
convert(model_id, mlx_path=mlx_path, quantize=True, q_bits=q_bits)
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
| 130 |
return (
|
| 131 |
f'Find your repo <a href="https://hf.co/{upload_repo}" target="_blank" style="text-decoration:underline">here</a>',
|
| 132 |
"llama.png",
|
|
@@ -141,7 +155,7 @@ css="""/* Custom CSS to allow scrolling */
|
|
| 141 |
.gradio-container {overflow-y: auto;}
|
| 142 |
"""
|
| 143 |
# Create Gradio interface
|
| 144 |
-
with gr.Blocks(css=css) as demo:
|
| 145 |
gr.Markdown("You must be logged in to use MLX-my-repo.")
|
| 146 |
gr.LoginButton(min_width=250)
|
| 147 |
|
|
@@ -159,7 +173,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 159 |
filterable=False,
|
| 160 |
visible=True
|
| 161 |
)
|
| 162 |
-
|
| 163 |
iface = gr.Interface(
|
| 164 |
fn=process_model,
|
| 165 |
inputs=[
|
|
|
|
| 33 |
def list_files_in_folder(folder_path):
|
| 34 |
# List all files and directories in the specified folder
|
| 35 |
all_items = os.listdir(folder_path)
|
| 36 |
+
|
| 37 |
# Filter out only files
|
| 38 |
files = [item for item in all_items if os.path.isfile(os.path.join(folder_path, item))]
|
| 39 |
+
|
| 40 |
return files
|
| 41 |
|
| 42 |
def clear_hf_cache_space():
|
|
|
|
| 48 |
scan.delete_revisions(*to_delete).execute()
|
| 49 |
print("Cache has been cleared")
|
| 50 |
|
| 51 |
+
def upload_to_hub(path, upload_repo, hf_path, token):
|
| 52 |
+
card = ModelCard.load(hf_path, token=token)
|
| 53 |
card.data.tags = ["mlx"] if card.data.tags is None else card.data.tags + ["mlx", "mlx-my-repo"]
|
| 54 |
card.data.base_model = hf_path
|
| 55 |
card.text = dedent(
|
|
|
|
| 85 |
|
| 86 |
logging.set_verbosity_info()
|
| 87 |
|
| 88 |
+
api = HfApi(token=token)
|
| 89 |
api.create_repo(repo_id=upload_repo, exist_ok=True)
|
| 90 |
|
| 91 |
files = list_files_in_folder(path)
|
|
|
|
| 98 |
path_in_repo=file,
|
| 99 |
repo_id=upload_repo,
|
| 100 |
)
|
| 101 |
+
|
| 102 |
+
print(f"Upload successful, go to https://huggingface.co/{upload_repo} for details.")
|
| 103 |
|
| 104 |
def process_model(model_id, q_method, oauth_token: gr.OAuthToken | None):
|
| 105 |
if oauth_token.token is None:
|
| 106 |
+
return ("You must be logged in to use MLX-my-repo", "error.png")
|
| 107 |
+
|
| 108 |
+
print(f"Token {token}")
|
| 109 |
+
|
| 110 |
+
# Verify the token
|
| 111 |
+
username = None
|
| 112 |
+
try:
|
| 113 |
+
user_info = whoami(token=token)
|
| 114 |
+
username = user_info["name"]
|
| 115 |
+
print(f"✅ Logged in as {username}")
|
| 116 |
+
except Exception as e:
|
| 117 |
+
return (f"❌ Authentication failed: {e}", "error.png")
|
| 118 |
+
|
| 119 |
try:
|
| 120 |
+
model_name = model_id.split('/')[-1]
|
| 121 |
+
repo_name = None
|
| 122 |
+
q_bits = None
|
| 123 |
+
|
| 124 |
if q_method == "FP16":
|
| 125 |
+
q_bits = "float16"
|
| 126 |
+
repo_name = f"{model_name}-fp16"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
else:
|
| 128 |
q_bits = QUANT_PARAMS[q_method]
|
| 129 |
+
repo_name = f"{model_name}-{q_bits}bit"
|
| 130 |
+
|
| 131 |
+
upload_repo = f"{username}/${repo_name}"
|
| 132 |
+
|
| 133 |
+
with tempfile.TemporaryDirectory(dir=f"converted/${repo_name}") as tmpdir:
|
| 134 |
+
# The target directory must not exist
|
| 135 |
+
mlx_path = os.path.join(tmpdir, "mlx")
|
| 136 |
+
if q_method == "FP16":
|
| 137 |
+
convert(model_id, mlx_path=mlx_path, quantize=False, dtype="float16")
|
| 138 |
+
else:
|
| 139 |
convert(model_id, mlx_path=mlx_path, quantize=True, q_bits=q_bits)
|
| 140 |
+
print("Conversion done")
|
| 141 |
+
upload_to_hub(path=mlx_path, upload_repo=upload_repo, hf_path=model_id, token=token)
|
| 142 |
+
print("Upload done")
|
| 143 |
+
|
| 144 |
return (
|
| 145 |
f'Find your repo <a href="https://hf.co/{upload_repo}" target="_blank" style="text-decoration:underline">here</a>',
|
| 146 |
"llama.png",
|
|
|
|
| 155 |
.gradio-container {overflow-y: auto;}
|
| 156 |
"""
|
| 157 |
# Create Gradio interface
|
| 158 |
+
with gr.Blocks(css=css) as demo:
|
| 159 |
gr.Markdown("You must be logged in to use MLX-my-repo.")
|
| 160 |
gr.LoginButton(min_width=250)
|
| 161 |
|
|
|
|
| 173 |
filterable=False,
|
| 174 |
visible=True
|
| 175 |
)
|
| 176 |
+
|
| 177 |
iface = gr.Interface(
|
| 178 |
fn=process_model,
|
| 179 |
inputs=[
|