Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from huggingface_hub import list_models | |
| from openpyxl import Workbook | |
| import tempfile | |
| def get_models(username): | |
| if not username.strip(): | |
| result = [["-", "Please enter a valid username or organization.", "-", "-", "-"]] | |
| xlsx_path = create_excel(result) | |
| return [row[:-1] for row in result], xlsx_path | |
| try: | |
| models = list_models(author=username.strip()) | |
| results = [] | |
| for model in models: | |
| model_url = f"https://huggingface.co/{model.modelId}" | |
| if hasattr(model, "cardData") and model.cardData and "description" in model.cardData: | |
| desc = model.cardData["description"] | |
| elif hasattr(model, "description"): | |
| desc = model.description | |
| else: | |
| desc = "No description." | |
| downloads = getattr(model, "likes", 0) | |
| tags = getattr(model, "tags", []) | |
| tags_str = ", ".join(tags) if tags else "No tags" | |
| results.append([ | |
| model.modelId, | |
| desc, | |
| downloads, | |
| tags_str, | |
| model_url | |
| ]) | |
| if results: | |
| xlsx_path = create_excel(results) | |
| table_results = [row[:-1] for row in results] # Do not show URL column in Gradio table | |
| return table_results, xlsx_path | |
| else: | |
| result = [["-", f"No models found for '{username}'.", "-", "-", "-"]] | |
| xlsx_path = create_excel(result) | |
| return [row[:-1] for row in result], xlsx_path | |
| except Exception as e: | |
| result = [["-", f"Error: {str(e)}", "-", "-", "-"]] | |
| xlsx_path = create_excel(result) | |
| return [row[:-1] for row in result], xlsx_path | |
| def create_excel(data_rows): | |
| headers = ["Model Name", "Description", "Downloads", "Tags", "URL"] | |
| wb = Workbook() | |
| ws = wb.active | |
| ws.append(headers) | |
| for row in data_rows: | |
| ws.append(row) | |
| tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") | |
| wb.save(tmp.name) | |
| tmp.close() | |
| return tmp.name | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## 🤗 Hugging Face Model Explorer\nEnter a username/org to see all public models and download info as Excel!") | |
| username = gr.Textbox(label="HuggingFace Username or Organization", placeholder="e.g. Snaseem2026") | |
| table = gr.Dataframe(headers=["Name", "Description", "Downloads", "Tags"], interactive=False) | |
| download_excel = gr.File(label="Download Excel") # Shows a download link/button | |
| def search_and_excel(user): | |
| df, excel_path = get_models(user) | |
| return df, excel_path | |
| search_btn = gr.Button("Search & Generate Excel") | |
| search_btn.click( | |
| search_and_excel, | |
| inputs=username, | |
| outputs=[table, download_excel] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |