Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,57 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
""" MCP Server using FastMCP + Hugging Face Hub SDK (No .env, uses Space secrets) """
|
| 3 |
-
|
| 4 |
import os
|
| 5 |
import json
|
|
|
|
| 6 |
from fastmcp import FastMCP
|
| 7 |
from huggingface_hub import HfApi, model_info
|
| 8 |
-
from huggingface_hub.utils import HfHubHTTPError
|
| 9 |
|
| 10 |
-
# β
|
| 11 |
-
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 12 |
hf_api = HfApi(token=HF_TOKEN) if HF_TOKEN else None
|
| 13 |
|
| 14 |
-
# β
|
| 15 |
mcp = FastMCP("hf-tagging-bot")
|
| 16 |
|
| 17 |
-
# β Tool: Get Current Tags β
|
| 18 |
@mcp.tool()
|
| 19 |
def get_current_tags(repo_id: str) -> str:
|
| 20 |
"""Get current tags from a HuggingFace model repository"""
|
| 21 |
-
print(f"π§ get_current_tags called with repo_id: {repo_id}")
|
| 22 |
if not hf_api:
|
| 23 |
-
|
| 24 |
-
return json.dumps(error_result)
|
| 25 |
|
| 26 |
try:
|
| 27 |
info = model_info(repo_id=repo_id, token=HF_TOKEN)
|
| 28 |
current_tags = info.tags if info.tags else []
|
| 29 |
-
|
| 30 |
"status": "success",
|
| 31 |
"repo_id": repo_id,
|
| 32 |
"current_tags": current_tags,
|
| 33 |
"count": len(current_tags),
|
| 34 |
-
}
|
| 35 |
-
return json.dumps(result)
|
| 36 |
except Exception as e:
|
| 37 |
-
|
| 38 |
"status": "error",
|
| 39 |
"repo_id": repo_id,
|
| 40 |
"error": str(e),
|
| 41 |
-
}
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
# β Run MCP Server β
|
| 45 |
if __name__ == "__main__":
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
+
import gradio as gr
|
| 4 |
from fastmcp import FastMCP
|
| 5 |
from huggingface_hub import HfApi, model_info
|
|
|
|
| 6 |
|
| 7 |
+
# β Hugging Face Token from Space Secrets β
|
| 8 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 9 |
hf_api = HfApi(token=HF_TOKEN) if HF_TOKEN else None
|
| 10 |
|
| 11 |
+
# β FastMCP Server Setup β
|
| 12 |
mcp = FastMCP("hf-tagging-bot")
|
| 13 |
|
|
|
|
| 14 |
@mcp.tool()
|
| 15 |
def get_current_tags(repo_id: str) -> str:
|
| 16 |
"""Get current tags from a HuggingFace model repository"""
|
|
|
|
| 17 |
if not hf_api:
|
| 18 |
+
return json.dumps({"error": "HF token not configured"})
|
|
|
|
| 19 |
|
| 20 |
try:
|
| 21 |
info = model_info(repo_id=repo_id, token=HF_TOKEN)
|
| 22 |
current_tags = info.tags if info.tags else []
|
| 23 |
+
return json.dumps({
|
| 24 |
"status": "success",
|
| 25 |
"repo_id": repo_id,
|
| 26 |
"current_tags": current_tags,
|
| 27 |
"count": len(current_tags),
|
| 28 |
+
})
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
+
return json.dumps({
|
| 31 |
"status": "error",
|
| 32 |
"repo_id": repo_id,
|
| 33 |
"error": str(e),
|
| 34 |
+
})
|
| 35 |
+
|
| 36 |
+
# β Gradio UI Wrapper β
|
| 37 |
+
def gradio_tag_checker(repo_id):
|
| 38 |
+
return get_current_tags(repo_id)
|
| 39 |
+
|
| 40 |
+
# β Launch Gradio Interface β
|
| 41 |
+
demo = gr.Interface(
|
| 42 |
+
fn=gradio_tag_checker,
|
| 43 |
+
inputs=gr.Textbox(label="Enter HuggingFace Model Repo ID", placeholder="e.g. bert-base-uncased"),
|
| 44 |
+
outputs=gr.Textbox(label="Current Tags (JSON)"),
|
| 45 |
+
title="π HuggingFace Tag Checker",
|
| 46 |
+
description="Uses FastMCP + HuggingFace Hub SDK to fetch current tags from any model repo."
|
| 47 |
+
)
|
| 48 |
|
| 49 |
+
# β Run Both MCP Server and Gradio β
|
| 50 |
if __name__ == "__main__":
|
| 51 |
+
import threading
|
| 52 |
+
|
| 53 |
+
# Run FastMCP in background thread
|
| 54 |
+
threading.Thread(target=mcp.run, daemon=True).start()
|
| 55 |
+
|
| 56 |
+
# Run Gradio UI
|
| 57 |
+
demo.launch()
|