Spaces:
Runtime error
Runtime error
apple muncy
commited on
Commit
Β·
878d9df
1
Parent(s):
90fdfa0
initial commit
Browse filesSigned-off-by: apple muncy <apple@llama-3.local>
- README.md +1 -1
- app.py +224 -0
- requirements.txt +4 -0
README.md
CHANGED
|
@@ -6,7 +6,7 @@ colorTo: purple
|
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.35.0
|
| 8 |
app_file: app.py
|
| 9 |
-
pinned:
|
| 10 |
license: gpl-3.0
|
| 11 |
short_description: MCPcourse Unit3.1 Server
|
| 12 |
---
|
|
|
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.35.0
|
| 8 |
app_file: app.py
|
| 9 |
+
pinned: true
|
| 10 |
license: gpl-3.0
|
| 11 |
short_description: MCPcourse Unit3.1 Server
|
| 12 |
---
|
app.py
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Simplified MCP Server for HuggingFace Hub Tagging Operations using FastMCP
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import json
|
| 8 |
+
from fastmcp import FastMCP
|
| 9 |
+
from huggingface_hub import HfApi, model_info, ModelCard, ModelCardData
|
| 10 |
+
from huggingface_hub.utils import HfHubHTTPError
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
import gradio as gr
|
| 13 |
+
import uvicorn
|
| 14 |
+
|
| 15 |
+
load_dotenv()
|
| 16 |
+
#add logging
|
| 17 |
+
import logging
|
| 18 |
+
logger = logging.getLogger(__name__)
|
| 19 |
+
logging.basicConfig(filename='/home/user/tag_server.log', encoding='utf-8', level=logging.DEBUG)
|
| 20 |
+
logger.debug(' This message should go to the log file')
|
| 21 |
+
logger.info(' So should this')
|
| 22 |
+
|
| 23 |
+
# Configuration
|
| 24 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 25 |
+
|
| 26 |
+
# Initialize HF API client
|
| 27 |
+
hf_api = HfApi(token=HF_TOKEN) if HF_TOKEN else None
|
| 28 |
+
|
| 29 |
+
# Create the FastMCP server
|
| 30 |
+
mcp = FastMCP("hf-tagging-bot-server")
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
@mcp.tool()
|
| 34 |
+
def get_current_tags(repo_id: str) -> str:
|
| 35 |
+
"""Get current tags from a HuggingFace model repository"""
|
| 36 |
+
print(f"π§ get_current_tags called with repo_id: {repo_id}")
|
| 37 |
+
|
| 38 |
+
if not hf_api:
|
| 39 |
+
error_result = {"error": "HF token not configured"}
|
| 40 |
+
json_str = json.dumps(error_result)
|
| 41 |
+
print(f"β No HF API token - returning: {json_str}")
|
| 42 |
+
return json_str
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
print(f"π‘ Fetching model info for: {repo_id}")
|
| 46 |
+
info = model_info(repo_id=repo_id, token=HF_TOKEN)
|
| 47 |
+
current_tags = info.tags if info.tags else []
|
| 48 |
+
print(f"π·οΈ Found {len(current_tags)} tags: {current_tags}")
|
| 49 |
+
global g_results
|
| 50 |
+
g_results = {
|
| 51 |
+
"status": "success",
|
| 52 |
+
"repo_id": repo_id,
|
| 53 |
+
"current_tags": current_tags,
|
| 54 |
+
"count": len(current_tags),
|
| 55 |
+
}
|
| 56 |
+
json_str = json.dumps(g_results)
|
| 57 |
+
print(f"β
get_current_tags returning: {json_str}")
|
| 58 |
+
return json_str
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"β Error in get_current_tags: {str(e)}")
|
| 62 |
+
error_result = {"status": "error", "repo_id": repo_id, "error": str(e)}
|
| 63 |
+
json_str = json.dumps(error_result)
|
| 64 |
+
print(f"β get_current_tags error returning: {json_str}")
|
| 65 |
+
return json_str
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
@mcp.tool()
|
| 69 |
+
def add_new_tag(repo_id: str, new_tag: str) -> str:
|
| 70 |
+
"""Add a new tag to a HuggingFace model repository via PR"""
|
| 71 |
+
print(f"π§ add_new_tag called with repo_id: {repo_id}, new_tag: {new_tag}")
|
| 72 |
+
|
| 73 |
+
if not hf_api:
|
| 74 |
+
error_result = {"error": "HF token not configured"}
|
| 75 |
+
json_str = json.dumps(error_result)
|
| 76 |
+
print(f"β No HF API token - returning: {json_str}")
|
| 77 |
+
return json_str
|
| 78 |
+
|
| 79 |
+
try:
|
| 80 |
+
# Get current model info and tags
|
| 81 |
+
print(f"π‘ Fetching current model info for: {repo_id}")
|
| 82 |
+
info = model_info(repo_id=repo_id, token=HF_TOKEN)
|
| 83 |
+
current_tags = info.tags if info.tags else []
|
| 84 |
+
print(f"π·οΈ Current tags: {current_tags}")
|
| 85 |
+
|
| 86 |
+
# Check if tag already exists
|
| 87 |
+
if new_tag in current_tags:
|
| 88 |
+
print(f"β οΈ Tag '{new_tag}' already exists in {current_tags}")
|
| 89 |
+
result = {
|
| 90 |
+
"status": "already_exists",
|
| 91 |
+
"repo_id": repo_id,
|
| 92 |
+
"tag": new_tag,
|
| 93 |
+
"message": f"Tag '{new_tag}' already exists",
|
| 94 |
+
}
|
| 95 |
+
json_str = json.dumps(result)
|
| 96 |
+
print(f"π·οΈ add_new_tag (already exists) returning: {json_str}")
|
| 97 |
+
return json_str
|
| 98 |
+
|
| 99 |
+
# Add the new tag to existing tags
|
| 100 |
+
updated_tags = current_tags + [new_tag]
|
| 101 |
+
print(f"π Will update tags from {current_tags} to {updated_tags}")
|
| 102 |
+
|
| 103 |
+
# Create model card content with updated tags
|
| 104 |
+
try:
|
| 105 |
+
# Load existing model card
|
| 106 |
+
print(f"π Loading existing model card...")
|
| 107 |
+
card = ModelCard.load(repo_id, token=HF_TOKEN)
|
| 108 |
+
if not hasattr(card, "data") or card.data is None:
|
| 109 |
+
card.data = ModelCardData()
|
| 110 |
+
except HfHubHTTPError:
|
| 111 |
+
# Create new model card if none exists
|
| 112 |
+
print(f"π Creating new model card (none exists)")
|
| 113 |
+
card = ModelCard("")
|
| 114 |
+
card.data = ModelCardData()
|
| 115 |
+
|
| 116 |
+
# Update tags - create new ModelCardData with updated tags
|
| 117 |
+
card_dict = card.data.to_dict()
|
| 118 |
+
card_dict["tags"] = updated_tags
|
| 119 |
+
card.data = ModelCardData(**card_dict)
|
| 120 |
+
|
| 121 |
+
# Create a pull request with the updated model card
|
| 122 |
+
pr_title = f"Add '{new_tag}' tag"
|
| 123 |
+
pr_description = f"""
|
| 124 |
+
## Add tag: {new_tag}
|
| 125 |
+
|
| 126 |
+
This PR adds the `{new_tag}` tag to the model repository.
|
| 127 |
+
|
| 128 |
+
**Changes:**
|
| 129 |
+
- Added `{new_tag}` to model tags
|
| 130 |
+
- Updated from {len(current_tags)} to {len(updated_tags)} tags
|
| 131 |
+
|
| 132 |
+
**Current tags:** {", ".join(current_tags) if current_tags else "None"}
|
| 133 |
+
**New tags:** {", ".join(updated_tags)}
|
| 134 |
+
"""
|
| 135 |
+
|
| 136 |
+
print(f"π Creating PR with title: {pr_title}")
|
| 137 |
+
|
| 138 |
+
# Create commit with updated model card using CommitOperationAdd
|
| 139 |
+
from huggingface_hub import CommitOperationAdd
|
| 140 |
+
|
| 141 |
+
commit_info = hf_api.create_commit(
|
| 142 |
+
repo_id=repo_id,
|
| 143 |
+
operations=[
|
| 144 |
+
CommitOperationAdd(
|
| 145 |
+
path_in_repo="README.md", path_or_fileobj=str(card).encode("utf-8")
|
| 146 |
+
)
|
| 147 |
+
],
|
| 148 |
+
commit_message=pr_title,
|
| 149 |
+
commit_description=pr_description,
|
| 150 |
+
token=HF_TOKEN,
|
| 151 |
+
create_pr=True,
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
# Extract PR URL from commit info
|
| 155 |
+
pr_url_attr = commit_info.pr_url
|
| 156 |
+
pr_url = pr_url_attr if hasattr(commit_info, "pr_url") else str(commit_info)
|
| 157 |
+
|
| 158 |
+
print(f"β
PR created successfully! URL: {pr_url}")
|
| 159 |
+
|
| 160 |
+
result = {
|
| 161 |
+
"status": "success",
|
| 162 |
+
"repo_id": repo_id,
|
| 163 |
+
"tag": new_tag,
|
| 164 |
+
"pr_url": pr_url,
|
| 165 |
+
"previous_tags": current_tags,
|
| 166 |
+
"new_tags": updated_tags,
|
| 167 |
+
"message": f"Created PR to add tag '{new_tag}'",
|
| 168 |
+
}
|
| 169 |
+
json_str = json.dumps(result)
|
| 170 |
+
print(f"β
add_new_tag success returning: {json_str}")
|
| 171 |
+
return json_str
|
| 172 |
+
|
| 173 |
+
except Exception as e:
|
| 174 |
+
print(f"β Error in add_new_tag: {str(e)}")
|
| 175 |
+
print(f"β Error type: {type(e)}")
|
| 176 |
+
import traceback
|
| 177 |
+
|
| 178 |
+
print(f"β Traceback: {traceback.format_exc()}")
|
| 179 |
+
|
| 180 |
+
error_result = {
|
| 181 |
+
"status": "error",
|
| 182 |
+
"repo_id": repo_id,
|
| 183 |
+
"tag": new_tag,
|
| 184 |
+
"error": str(e),
|
| 185 |
+
}
|
| 186 |
+
json_str = json.dumps(error_result)
|
| 187 |
+
print(f"β add_new_tag error returning: {json_str}")
|
| 188 |
+
return json_str
|
| 189 |
+
|
| 190 |
+
def create_gradio_app():
|
| 191 |
+
"""Create Gradio interface"""
|
| 192 |
+
with gr.Blocks(title="HF Tagging Bot", theme=gr.themes.Soft( font=[gr.themes.GoogleFont("Roboto"), "Open Sans", "sans-serif"]) ) as demo:
|
| 193 |
+
gr.Markdown("# π·οΈ HF Tagging Bot Dashboard")
|
| 194 |
+
|
| 195 |
+
with gr.Column():
|
| 196 |
+
sim_repo = gr.Textbox(
|
| 197 |
+
label="Repository",
|
| 198 |
+
value="burtenshaw/play-mcp-repo-bot",
|
| 199 |
+
placeholder="username/model-name",
|
| 200 |
+
)
|
| 201 |
+
sim_btn = gr.Button("π·οΈ Test Tag Detection")
|
| 202 |
+
|
| 203 |
+
with gr.Column():
|
| 204 |
+
sim_result = gr.Textbox(label="Result", lines=8)
|
| 205 |
+
|
| 206 |
+
sim_btn.click(
|
| 207 |
+
fn=get_current_tags,
|
| 208 |
+
inputs=[sim_repo,],
|
| 209 |
+
outputs=sim_result,
|
| 210 |
+
)
|
| 211 |
+
return demo
|
| 212 |
+
# Mount Gradio app
|
| 213 |
+
gradio_app = create_gradio_app()
|
| 214 |
+
app_gradio = gr.mount_gradio_app(mcp, gradio_app, path="/gradio")
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
if __name__ == "__main__":
|
| 220 |
+
print("π Starting HF Tagging Bot server")
|
| 221 |
+
print("π Dashboard: http://localhost:7860/gradio")
|
| 222 |
+
|
| 223 |
+
uvicorn.run("app:my_app", host="0.0.0.0", port=7860, reload=True)
|
| 224 |
+
mcp.run()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastmcp
|
| 2 |
+
gradio
|
| 3 |
+
uvicorn
|
| 4 |
+
|