Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import shutil | |
| from huggingface_hub import snapshot_download, list_models | |
| DOWNLOAD_DIR = "./models" | |
| os.makedirs(DOWNLOAD_DIR, exist_ok=True) | |
| # π Search Hugging Face Models | |
| def search_models(query, limit=5): | |
| try: | |
| models = list_models(search=query, limit=limit) | |
| results = [] | |
| for m in models: | |
| results.append(f"π¦ {m.modelId} | π· {m.pipeline_tag} | β° {m.lastModified}") | |
| return "\n".join(results) | |
| except Exception as e: | |
| return f"β Error: {str(e)}" | |
| # π₯ Download Model | |
| def download_model(model_name): | |
| try: | |
| path = snapshot_download(model_name, cache_dir=DOWNLOAD_DIR) | |
| return f"β Downloaded: {model_name}\nπ Path: {path}" | |
| except Exception as e: | |
| return f"β Error: {str(e)}" | |
| # π List Downloaded Models | |
| def list_downloaded_models(): | |
| models_list = [] | |
| for root, dirs, files in os.walk(DOWNLOAD_DIR): | |
| for d in dirs: | |
| models_list.append(os.path.relpath(os.path.join(root, d), DOWNLOAD_DIR)) | |
| return models_list if models_list else ["No models downloaded yet."] | |
| # π Show files inside model folder | |
| def show_model_files(model_folder): | |
| folder_path = os.path.join(DOWNLOAD_DIR, model_folder) | |
| if not os.path.exists(folder_path): | |
| return "β οΈ Folder not found" | |
| files = [] | |
| for root, dirs, filenames in os.walk(folder_path): | |
| for f in filenames: | |
| rel_path = os.path.relpath(os.path.join(root, f), folder_path) | |
| files.append(rel_path) | |
| return "\n".join(files) if files else "π Empty folder" | |
| # π Delete a downloaded model | |
| def delete_model(model_folder): | |
| folder_path = os.path.join(DOWNLOAD_DIR, model_folder) | |
| if not os.path.exists(folder_path): | |
| return "β οΈ Model folder not found" | |
| try: | |
| shutil.rmtree(folder_path) | |
| return f"π Deleted model: {model_folder}" | |
| except Exception as e: | |
| return f"β Error deleting model: {str(e)}" | |
| # β‘ Gradio UI | |
| with gr.Blocks(theme="soft") as demo: | |
| gr.Markdown("# π Model Downloader Hub") | |
| gr.Markdown("Search, download, explore, and delete Hugging Face models locally.") | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown("### π Search Models") | |
| query = gr.Textbox(label="Enter keyword or model name") | |
| limit = gr.Slider(1, 10, value=5, step=1, label="Number of Results") | |
| search_btn = gr.Button("Search") | |
| search_results = gr.Textbox(label="Results", interactive=False, lines=8) | |
| with gr.Column(): | |
| gr.Markdown("### π₯ Download Models") | |
| model_name = gr.Textbox(label="Model Name (from Hugging Face)") | |
| download_btn = gr.Button("Download") | |
| status = gr.Textbox(label="Status", interactive=False) | |
| gr.Markdown("### π Local Model Explorer") | |
| refresh_btn = gr.Button("π Refresh Model List") | |
| model_list = gr.Dropdown(choices=[], label="Downloaded Models") | |
| show_files_btn = gr.Button("π Show Files") | |
| delete_btn = gr.Button("π Delete Selected Model") | |
| file_list = gr.Textbox(label="Files in Model Folder", interactive=False, lines=12) | |
| delete_status = gr.Textbox(label="Delete Status", interactive=False) | |
| # π Button Actions | |
| search_btn.click(search_models, [query, limit], search_results) | |
| download_btn.click(download_model, [model_name], status) | |
| refresh_btn.click(list_downloaded_models, outputs=model_list) | |
| show_files_btn.click(show_model_files, [model_list], file_list) | |
| delete_btn.click(delete_model, [model_list], delete_status).then(list_downloaded_models, outputs=model_list) | |
| demo.launch() | |