Spaces:
Sleeping
Sleeping
Update mcp_server.py
Browse files- mcp_server.py +185 -184
mcp_server.py
CHANGED
@@ -1,184 +1,185 @@
|
|
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 |
-
|
13 |
-
load_dotenv()
|
14 |
-
|
15 |
-
# Configuration
|
16 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
info
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
"
|
45 |
-
"
|
46 |
-
"
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
info
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
if
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
"
|
84 |
-
"
|
85 |
-
"
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
card
|
106 |
-
card
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
card_dict
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
-
|
123 |
-
|
124 |
-
|
125 |
-
**
|
126 |
-
""
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
"
|
155 |
-
"
|
156 |
-
"
|
157 |
-
"
|
158 |
-
"
|
159 |
-
"
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
print(f"β Error
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
"
|
175 |
-
"
|
176 |
-
"
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
|
|
|
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 |
+
|
13 |
+
load_dotenv()
|
14 |
+
|
15 |
+
# Configuration
|
16 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
17 |
+
if HF_TOKEN:
|
18 |
+
HF_TOKEN = HF_TOKEN.strip()
|
19 |
+
# Initialize HF API client
|
20 |
+
hf_api = HfApi(token=HF_TOKEN) if HF_TOKEN else None
|
21 |
+
|
22 |
+
# Create the FastMCP server
|
23 |
+
mcp = FastMCP("hf-tagging-bot")
|
24 |
+
|
25 |
+
|
26 |
+
@mcp.tool()
|
27 |
+
def get_current_tags(repo_id: str) -> str:
|
28 |
+
"""Get current tags from a HuggingFace model repository"""
|
29 |
+
print(f"π§ get_current_tags called with repo_id: {repo_id}")
|
30 |
+
|
31 |
+
if not hf_api:
|
32 |
+
error_result = {"error": "HF token not configured"}
|
33 |
+
json_str = json.dumps(error_result)
|
34 |
+
print(f"β No HF API token - returning: {json_str}")
|
35 |
+
return json_str
|
36 |
+
|
37 |
+
try:
|
38 |
+
print(f"π‘ Fetching model info for: {repo_id}")
|
39 |
+
info = model_info(repo_id=repo_id, token=HF_TOKEN)
|
40 |
+
current_tags = info.tags if info.tags else []
|
41 |
+
print(f"π·οΈ Found {len(current_tags)} tags: {current_tags}")
|
42 |
+
|
43 |
+
result = {
|
44 |
+
"status": "success",
|
45 |
+
"repo_id": repo_id,
|
46 |
+
"current_tags": current_tags,
|
47 |
+
"count": len(current_tags),
|
48 |
+
}
|
49 |
+
json_str = json.dumps(result)
|
50 |
+
print(f"β
get_current_tags returning: {json_str}")
|
51 |
+
return json_str
|
52 |
+
|
53 |
+
except Exception as e:
|
54 |
+
print(f"β Error in get_current_tags: {str(e)}")
|
55 |
+
error_result = {"status": "error", "repo_id": repo_id, "error": str(e)}
|
56 |
+
json_str = json.dumps(error_result)
|
57 |
+
print(f"β get_current_tags error returning: {json_str}")
|
58 |
+
return json_str
|
59 |
+
|
60 |
+
|
61 |
+
@mcp.tool()
|
62 |
+
def add_new_tag(repo_id: str, new_tag: str) -> str:
|
63 |
+
"""Add a new tag to a HuggingFace model repository via PR"""
|
64 |
+
print(f"π§ add_new_tag called with repo_id: {repo_id}, new_tag: {new_tag}")
|
65 |
+
|
66 |
+
if not hf_api:
|
67 |
+
error_result = {"error": "HF token not configured"}
|
68 |
+
json_str = json.dumps(error_result)
|
69 |
+
print(f"β No HF API token - returning: {json_str}")
|
70 |
+
return json_str
|
71 |
+
|
72 |
+
try:
|
73 |
+
# Get current model info and tags
|
74 |
+
print(f"π‘ Fetching current model info for: {repo_id}")
|
75 |
+
info = model_info(repo_id=repo_id, token=HF_TOKEN)
|
76 |
+
current_tags = info.tags if info.tags else []
|
77 |
+
print(f"π·οΈ Current tags: {current_tags}")
|
78 |
+
|
79 |
+
# Check if tag already exists
|
80 |
+
if new_tag in current_tags:
|
81 |
+
print(f"β οΈ Tag '{new_tag}' already exists in {current_tags}")
|
82 |
+
result = {
|
83 |
+
"status": "already_exists",
|
84 |
+
"repo_id": repo_id,
|
85 |
+
"tag": new_tag,
|
86 |
+
"message": f"Tag '{new_tag}' already exists",
|
87 |
+
}
|
88 |
+
json_str = json.dumps(result)
|
89 |
+
print(f"π·οΈ add_new_tag (already exists) returning: {json_str}")
|
90 |
+
return json_str
|
91 |
+
|
92 |
+
# Add the new tag to existing tags
|
93 |
+
updated_tags = current_tags + [new_tag]
|
94 |
+
print(f"π Will update tags from {current_tags} to {updated_tags}")
|
95 |
+
|
96 |
+
# Create model card content with updated tags
|
97 |
+
try:
|
98 |
+
# Load existing model card
|
99 |
+
print(f"π Loading existing model card...")
|
100 |
+
card = ModelCard.load(repo_id, token=HF_TOKEN)
|
101 |
+
if not hasattr(card, "data") or card.data is None:
|
102 |
+
card.data = ModelCardData()
|
103 |
+
except HfHubHTTPError:
|
104 |
+
# Create new model card if none exists
|
105 |
+
print(f"π Creating new model card (none exists)")
|
106 |
+
card = ModelCard("")
|
107 |
+
card.data = ModelCardData()
|
108 |
+
|
109 |
+
# Update tags - create new ModelCardData with updated tags
|
110 |
+
card_dict = card.data.to_dict()
|
111 |
+
card_dict["tags"] = updated_tags
|
112 |
+
card.data = ModelCardData(**card_dict)
|
113 |
+
|
114 |
+
# Create a pull request with the updated model card
|
115 |
+
pr_title = f"Add '{new_tag}' tag"
|
116 |
+
pr_description = f"""
|
117 |
+
## Add tag: {new_tag}
|
118 |
+
|
119 |
+
This PR adds the `{new_tag}` tag to the model repository.
|
120 |
+
|
121 |
+
**Changes:**
|
122 |
+
- Added `{new_tag}` to model tags
|
123 |
+
- Updated from {len(current_tags)} to {len(updated_tags)} tags
|
124 |
+
|
125 |
+
**Current tags:** {", ".join(current_tags) if current_tags else "None"}
|
126 |
+
**New tags:** {", ".join(updated_tags)}
|
127 |
+
"""
|
128 |
+
|
129 |
+
print(f"π Creating PR with title: {pr_title}")
|
130 |
+
|
131 |
+
# Create commit with updated model card using CommitOperationAdd
|
132 |
+
from huggingface_hub import CommitOperationAdd
|
133 |
+
|
134 |
+
commit_info = hf_api.create_commit(
|
135 |
+
repo_id=repo_id,
|
136 |
+
operations=[
|
137 |
+
CommitOperationAdd(
|
138 |
+
path_in_repo="README.md", path_or_fileobj=str(card).encode("utf-8")
|
139 |
+
)
|
140 |
+
],
|
141 |
+
commit_message=pr_title,
|
142 |
+
commit_description=pr_description,
|
143 |
+
token=HF_TOKEN,
|
144 |
+
create_pr=True,
|
145 |
+
)
|
146 |
+
|
147 |
+
# Extract PR URL from commit info
|
148 |
+
pr_url_attr = commit_info.pr_url
|
149 |
+
pr_url = pr_url_attr if hasattr(commit_info, "pr_url") else str(commit_info)
|
150 |
+
|
151 |
+
print(f"β
PR created successfully! URL: {pr_url}")
|
152 |
+
|
153 |
+
result = {
|
154 |
+
"status": "success",
|
155 |
+
"repo_id": repo_id,
|
156 |
+
"tag": new_tag,
|
157 |
+
"pr_url": pr_url,
|
158 |
+
"previous_tags": current_tags,
|
159 |
+
"new_tags": updated_tags,
|
160 |
+
"message": f"Created PR to add tag '{new_tag}'",
|
161 |
+
}
|
162 |
+
json_str = json.dumps(result)
|
163 |
+
print(f"β
add_new_tag success returning: {json_str}")
|
164 |
+
return json_str
|
165 |
+
|
166 |
+
except Exception as e:
|
167 |
+
print(f"β Error in add_new_tag: {str(e)}")
|
168 |
+
print(f"β Error type: {type(e)}")
|
169 |
+
import traceback
|
170 |
+
|
171 |
+
print(f"β Traceback: {traceback.format_exc()}")
|
172 |
+
|
173 |
+
error_result = {
|
174 |
+
"status": "error",
|
175 |
+
"repo_id": repo_id,
|
176 |
+
"tag": new_tag,
|
177 |
+
"error": str(e),
|
178 |
+
}
|
179 |
+
json_str = json.dumps(error_result)
|
180 |
+
print(f"β add_new_tag error returning: {json_str}")
|
181 |
+
return json_str
|
182 |
+
|
183 |
+
|
184 |
+
if __name__ == "__main__":
|
185 |
+
mcp.run()
|