import os import shutil import subprocess import signal os.environ["GRADIO_ANALYTICS_ENABLED"] = "False" import gradio as gr from huggingface_hub import create_repo, HfApi from huggingface_hub import snapshot_download from huggingface_hub import whoami from huggingface_hub import ModelCard from huggingface_hub import login from huggingface_hub import scan_cache_dir from gradio_huggingfacehub_search import HuggingfaceHubSearch from apscheduler.schedulers.background import BackgroundScheduler from textwrap import dedent from mlx_lm import convert HF_TOKEN = os.environ.get("HF_TOKEN") def clear_cache(): scan = scan_cache_dir() to_delete = [] for repo in scan.repos: if repo.repo_type == "model": to_delete.append([rev.commit_hash for rev in repo.revisions]) scan.delete_revisions(*to_delete) print("Cache has been cleared") def process_model(model_id, q_method,oauth_token: gr.OAuthToken | None): if oauth_token.token is None: raise ValueError("You must be logged in to use MLX-my-repo") model_name = model_id.split('/')[-1] username = whoami(oauth_token.token)["name"] login(token=oauth_token.token, add_to_git_credential=True) try: upload_repo = username + "/" + model_name + "-mlx" convert(model_id, quantize=True, upload_repo=upload_repo) clear_cache() return ( f'Find your repo here', "llama.png", ) except Exception as e: return (f"Error: {e}", "error.png") finally: shutil.rmtree("mlx_model", ignore_errors=True) clear_cache() print("Folder cleaned up successfully!") css="""/* Custom CSS to allow scrolling */ .gradio-container {overflow-y: auto;} """ # Create Gradio interface with gr.Blocks(css=css) as demo: gr.Markdown("You must be logged in to use MLX-my-repo.") gr.LoginButton(min_width=250) model_id = HuggingfaceHubSearch( label="Hub Model ID", placeholder="Search for model id on Huggingface", search_type="model", ) q_method = gr.Dropdown( ["Q4", "Q8"], label="Quantization Method", info="MLX quantization type", value="Q4", filterable=False, visible=True ) iface = gr.Interface( fn=process_model, inputs=[ model_id, q_method, ], outputs=[ gr.Markdown(label="output"), gr.Image(show_label=False), ], title="Create your own MLX Quants, blazingly fast ⚡!", description="The space takes an HF repo as an input, quantizes it and creates a Public/ Private repo containing the selected quant under your HF user namespace.", api_name=False ) def restart_space(): HfApi().restart_space(repo_id="reach-vb/mlx-my-repo", token=HF_TOKEN, factory_reboot=True) scheduler = BackgroundScheduler() scheduler.add_job(restart_space, "interval", seconds=21600) scheduler.start() # Launch the interface demo.queue(default_concurrency_limit=1, max_size=5).launch(debug=True, show_api=False)