Spaces:
Running
Running
File size: 26,425 Bytes
cf9a1ba 49da546 cf9a1ba 49da546 cf9a1ba 49da546 cf9a1ba 49da546 cf9a1ba 49da546 571cc8c 49da546 cf9a1ba 988d01d cf9a1ba 49da546 cf9a1ba b750fb8 cf9a1ba b750fb8 cf9a1ba 49da546 988d01d cf9a1ba 988d01d b750fb8 988d01d b750fb8 988d01d b750fb8 988d01d b750fb8 988d01d b750fb8 988d01d b750fb8 988d01d cf9a1ba 988d01d cf9a1ba d0a5416 988d01d d0a5416 988d01d cf9a1ba 988d01d cf9a1ba 69722f6 49da546 cf9a1ba 49da546 cf9a1ba 49da546 cf9a1ba 49da546 cf9a1ba 988d01d cf9a1ba 49da546 cf9a1ba beee5bd 9d51e99 7f3b9ae 9d51e99 cf9a1ba b84f99a cf9a1ba 49da546 c902a3f 7390cf0 c909896 7390cf0 c909896 7390cf0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 75 76 77 78 79 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 106 107 108 109 110 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 |
import os
import re
import json
from datetime import datetime
from typing import List, Dict, Any, Optional, Literal
from fastapi import FastAPI, Request, BackgroundTasks
from fastapi.middleware.cors import CORSMiddleware
import gradio as gr
import uvicorn
from pydantic import BaseModel
from huggingface_hub.inference._mcp.agent import Agent
from dotenv import load_dotenv
load_dotenv()
# Configuration
WEBHOOK_SECRET = os.getenv("WEBHOOK_SECRET", "your-webhook-secret")
HF_TOKEN = os.getenv("HF_TOKEN")
HF_MODEL = os.getenv("HF_MODEL", "microsoft/DialoGPT-medium")
# Use a valid provider literal from the documentation
DEFAULT_PROVIDER: Literal["hf-inference"] = "hf-inference"
HF_PROVIDER = os.getenv("HF_PROVIDER", DEFAULT_PROVIDER)
# Simple storage for processed tag operations
tag_operations_store: List[Dict[str, Any]] = []
# Agent instance
agent_instance: Optional[Agent] = None
# Common ML tags that we recognize for auto-tagging
RECOGNIZED_TAGS = {
"pytorch",
"tensorflow",
"jax",
"transformers",
"diffusers",
"text-generation",
"text-classification",
"question-answering",
"text-to-image",
"image-classification",
"object-detection",
" ",
"fill-mask",
"token-classification",
"translation",
"summarization",
"feature-extraction",
"sentence-similarity",
"zero-shot-classification",
"image-to-text",
"automatic-speech-recognition",
"audio-classification",
"voice-activity-detection",
"depth-estimation",
"image-segmentation",
"video-classification",
"reinforcement-learning",
"tabular-classification",
"tabular-regression",
"time-series-forecasting",
"graph-ml",
"robotics",
"computer-vision",
"nlp",
"cv",
"multimodal",
}
class WebhookEvent(BaseModel):
event: Dict[str, str] # Contains action and scope information
comment: Dict[str, Any] # Comment content and metadata
discussion: Dict[str, Any] # Discussion information
repo: Dict[str, str] # Repository details
app = FastAPI(title="HF Tagging Bot")
app.add_middleware(CORSMiddleware, allow_origins=["*"])
async def get_agent():
"""Get or create Agent instance"""
print("π€ get_agent() called...")
global agent_instance
if agent_instance is None and HF_TOKEN:
print("π§ Creating new Agent instance...")
print(f"π HF_TOKEN present: {bool(HF_TOKEN)}")
print(f"π€ Model: {HF_MODEL}")
print(f"π Provider: {DEFAULT_PROVIDER}")
try:
agent_instance = Agent(
model=HF_MODEL,
provider=DEFAULT_PROVIDER,
api_key=HF_TOKEN,
servers=[
{
"type": "stdio",
"config": {
"command": "python",
"args": ["mcp_server.py"],
"cwd": ".", # Ensure correct working directory
"env": {"HF_TOKEN": HF_TOKEN} if HF_TOKEN else {},
},
}
],
)
print("β
Agent instance created successfully")
print("π§ Loading tools...")
await agent_instance.load_tools()
print("β
Tools loaded successfully")
except Exception as e:
print(f"β Error creating/loading agent: {str(e)}")
agent_instance = None
elif agent_instance is None:
print("β No HF_TOKEN available, cannot create agent")
else:
print("β
Using existing agent instance")
return agent_instance
def extract_tags_from_text(text: str) -> List[str]:
"""Extract potential tags from discussion text"""
text_lower = text.lower()
# Look for explicit tag mentions like "tag: pytorch" or "#pytorch"
explicit_tags = []
# Pattern 1: "tag: something" or "tags: something"
tag_pattern = r"tags?:\s*([a-zA-Z0-9-_,\s]+)"
matches = re.findall(tag_pattern, text_lower)
for match in matches:
# Split by comma and clean up
tags = [tag.strip() for tag in match.split(",")]
explicit_tags.extend(tags)
# Pattern 2: "#hashtag" style
hashtag_pattern = r"#([a-zA-Z0-9-_]+)"
hashtag_matches = re.findall(hashtag_pattern, text_lower)
explicit_tags.extend(hashtag_matches)
# Pattern 3: Look for recognized tags mentioned in natural text
mentioned_tags = []
for tag in RECOGNIZED_TAGS:
if tag in text_lower:
mentioned_tags.append(tag)
# Combine and deduplicate
all_tags = list(set(explicit_tags + mentioned_tags))
# Filter to only include recognized tags or explicitly mentioned ones
valid_tags = []
for tag in all_tags:
if tag in RECOGNIZED_TAGS or tag in explicit_tags:
valid_tags.append(tag)
return valid_tags
# async def process_webhook_comment(webhook_data: Dict[str, Any]):
# """Process webhook to detect and add tags"""
# print("π·οΈ Starting process_webhook_comment...")
# try:
# comment_content = webhook_data["comment"]["content"]
# discussion_title = webhook_data["discussion"]["title"]
# repo_name = webhook_data["repo"]["name"]
# discussion_num = webhook_data["discussion"]["num"]
# # Author is an object with "id" field
# comment_author = webhook_data["comment"]["author"].get("id", "unknown")
# print(f"π Comment content: {comment_content}")
# print(f"π° Discussion title: {discussion_title}")
# print(f"π¦ Repository: {repo_name}")
# # Extract potential tags from the comment and discussion title
# comment_tags = extract_tags_from_text(comment_content)
# title_tags = extract_tags_from_text(discussion_title)
# all_tags = list(set(comment_tags + title_tags))
# print(f"π Comment tags found: {comment_tags}")
# print(f"π Title tags found: {title_tags}")
# print(f"π·οΈ All unique tags: {all_tags}")
# result_messages = []
# if not all_tags:
# msg = "No recognizable tags found in the discussion."
# print(f"β {msg}")
# result_messages.append(msg)
# else:
# print("π€ Getting agent instance...")
# agent = await get_agent()
# if not agent:
# msg = "Error: Agent not configured (missing HF_TOKEN)"
# print(f"β {msg}")
# result_messages.append(msg)
# else:
# print("β
Agent instance obtained successfully")
# # Process all tags in a single conversation with the agent
# try:
# # Create a comprehensive prompt for the agent
# user_prompt = f"""
# I need to add the following tags to the repository '{repo_name}': {", ".join(all_tags)}
# For each tag, please:
# 1. Check if the tag already exists on the repository using get_current_tags
# 2. If the tag doesn't exist, add it using add_new_tag
# 3. Provide a summary of what was done for each tag
# Please process all {len(all_tags)} tags: {", ".join(all_tags)}
# """
# print("π¬ Sending comprehensive prompt to agent...")
# print(f"π Prompt: {user_prompt}")
# # Let the agent handle the entire conversation
# conversation_result = []
# try:
# async for item in agent.run(user_prompt):
# # The agent yields different types of items
# item_str = str(item)
# conversation_result.append(item_str)
# # Log important events
# if (
# "tool_call" in item_str.lower()
# or "function" in item_str.lower()
# ):
# print(f"π§ Agent using tools: {item_str[:200]}...")
# elif "content" in item_str and len(item_str) < 500:
# print(f"π Agent response: {item_str}")
# # Extract the final response from the conversation
# full_response = " ".join(conversation_result)
# print(f"π Agent conversation completed successfully")
# # Try to extract meaningful results for each tag
# for tag in all_tags:
# tag_mentioned = tag.lower() in full_response.lower()
# if (
# "already exists" in full_response.lower()
# and tag_mentioned
# ):
# msg = f"Tag '{tag}': Already exists"
# elif (
# "pr" in full_response.lower()
# or "pull request" in full_response.lower()
# ):
# if tag_mentioned:
# msg = f"Tag '{tag}': PR created successfully"
# else:
# msg = (
# f"Tag '{tag}': Processed "
# "(PR may have been created)"
# )
# elif "success" in full_response.lower() and tag_mentioned:
# msg = f"Tag '{tag}': Successfully processed"
# elif "error" in full_response.lower() and tag_mentioned:
# msg = f"Tag '{tag}': Error during processing"
# else:
# msg = f"Tag '{tag}': Processed by agent"
# print(f"β
Result for tag '{tag}': {msg}")
# result_messages.append(msg)
# except Exception as agent_error:
# print(f"β οΈ Agent streaming failed: {str(agent_error)}")
# print("π Falling back to direct MCP tool calls...")
# # Import the MCP server functions directly as fallback
# try:
# import sys
# import importlib.util
# # Load the MCP server module
# spec = importlib.util.spec_from_file_location(
# "mcp_server", "./mcp_server.py"
# )
# mcp_module = importlib.util.module_from_spec(spec) # type: ignore
# spec.loader.exec_module(mcp_module) # type: ignore
# # Use the MCP tools directly for each tag
# for tag in all_tags:
# try:
# print(
# f"π§ Directly calling get_current_tags for '{tag}'"
# )
# current_tags_result = mcp_module.get_current_tags(
# repo_name
# )
# print(
# f"π Current tags result: {current_tags_result}"
# )
# # Parse the JSON result
# import json
# tags_data = json.loads(current_tags_result)
# if tags_data.get("status") == "success":
# current_tags = tags_data.get("current_tags", [])
# if tag in current_tags:
# msg = f"Tag '{tag}': Already exists"
# print(f"β
{msg}")
# else:
# print(
# f"π§ Directly calling add_new_tag for '{tag}'"
# )
# add_result = mcp_module.add_new_tag(
# repo_name, tag
# )
# print(f"π Add tag result: {add_result}")
# add_data = json.loads(add_result)
# if add_data.get("status") == "success":
# pr_url = add_data.get("pr_url", "")
# msg = f"Tag '{tag}': PR created - {pr_url}"
# elif (
# add_data.get("status")
# == "already_exists"
# ):
# msg = f"Tag '{tag}': Already exists"
# else:
# msg = f"Tag '{tag}': {add_data.get('message', 'Processed')}"
# print(f"β
{msg}")
# else:
# error_msg = tags_data.get(
# "error", "Unknown error"
# )
# msg = f"Tag '{tag}': Error - {error_msg}"
# print(f"β {msg}")
# result_messages.append(msg)
# except Exception as direct_error:
# error_msg = f"Tag '{tag}': Direct call error - {str(direct_error)}"
# print(f"β {error_msg}")
# result_messages.append(error_msg)
# except Exception as fallback_error:
# error_msg = (
# f"Fallback approach failed: {str(fallback_error)}"
# )
# print(f"β {error_msg}")
# result_messages.append(error_msg)
# except Exception as e:
# error_msg = f"Error during agent processing: {str(e)}"
# print(f"β {error_msg}")
# result_messages.append(error_msg)
# # Store the interaction
# base_url = "https://huggingface.co"
# discussion_url = f"{base_url}/{repo_name}/discussions/{discussion_num}"
# interaction = {
# "timestamp": datetime.now().isoformat(),
# "repo": repo_name,
# "discussion_title": discussion_title,
# "discussion_num": discussion_num,
# "discussion_url": discussion_url,
# "original_comment": comment_content,
# "comment_author": comment_author,
# "detected_tags": all_tags,
# "results": result_messages,
# }
# tag_operations_store.append(interaction)
# final_result = " | ".join(result_messages)
# print(f"πΎ Stored interaction and returning result: {final_result}")
# return final_result
# except Exception as e:
# error_msg = f"β Fatal error in process_webhook_comment: {str(e)}"
# print(error_msg)
# return error_msg
@app.post("/webhook")
async def webhook_handler(request: Request, background_tasks: BackgroundTasks):
"""
Handle incoming webhooks from Hugging Face Hub
Following the pattern from: https://raw.githubusercontent.com/huggingface/hub-docs/refs/heads/main/docs/hub/webhooks-guide-discussion-bot.md
"""
print("π Webhook received!")
# Step 1: Validate webhook secret (security)
webhook_secret = request.headers.get("X-Webhook-Secret")
if webhook_secret != WEBHOOK_SECRET:
print("β Invalid webhook secret")
return {"error": "Invalid webhook secret"}, 400
# Step 2: Parse webhook data
try:
webhook_data = await request.json()
print(f"π₯ Webhook data: {json.dumps(webhook_data, indent=2)}")
except Exception as e:
print(f"β Error parsing webhook data: {str(e)}")
return {"error": "invalid JSON"}, 400
# Step 3: Validate event structure
event = webhook_data.get("event", {})
if not event:
print("β No event data in webhook")
return {"error": "missing event data"}, 400
scope = event.get("scope")
action = event.get("action")
print(f"π Event details - scope: {scope}, action: {action}")
# Step 4: Check if this is a discussion comment creation
# Following the webhook guide pattern:
if (
action == "create" and
scope == "discussion.comment"
):
print("β
Valid discussion comment creation event")
# Process in background to return quickly to Hub
background_tasks.add_task(process_webhook_comment, webhook_data)
return {
"status": "accepted",
"message": "Comment processing started",
"timestamp": datetime.now().isoformat()
}
else:
print(f"βΉοΈ Ignoring event: action={event.get('action')}, scope={event.get('scope')}")
return {
"status": "ignored",
"reason": "Not a discussion comment creation"
}
async def process_webhook_comment(webhook_data: Dict[str, Any]):
"""
Process webhook comment to detect and add tags
Integrates with our MCP client for Hub interactions
"""
print("π·οΈ Starting process_webhook_comment...")
try:
# Extract comment and repository information
comment_content = webhook_data["comment"]["content"]
discussion_title = webhook_data["discussion"]["title"]
repo_name = webhook_data["repo"]["name"]
discussion_num = webhook_data["discussion"]["num"]
comment_author = webhook_data["comment"]["author"].get("id", "unknown")
print(f"π Comment from {comment_author}: {comment_content}")
print(f"π° Discussion: {discussion_title}")
print(f"π¦ Repository: {repo_name}")
except Exception as e:
print(f"β Error parsing webhook data: {str(e)}")
return {"error": "invalid JSON"}, 400
# Extract potential tags from comment and title
comment_tags = extract_tags_from_text(comment_content)
title_tags = extract_tags_from_text(discussion_title)
all_tags = list(set(comment_tags + title_tags))
print(f"π Found tags: {all_tags}")
# Store operation for monitoring
operation = {
"timestamp": datetime.now().isoformat(),
"repo_name": repo_name,
"discussion_num": discussion_num,
"comment_author": comment_author,
"extracted_tags": all_tags,
"comment_preview": comment_content[:100] + "..." if len(comment_content) > 100 else comment_content,
"status": "processing"
}
tag_operations_store.append(operation)
if not all_tags:
operation["status"] = "no_tags"
operation["message"] = "No recognizable tags found"
print("β No tags found to process")
return
# Get MCP agent for tag processing
agent = await get_agent()
if not agent:
operation["status"] = "error"
operation["message"] = "Agent not configured (missing HF_TOKEN)"
print("β No agent available")
return
# Process each extracted tag
operation["results"] = []
for tag in all_tags:
try:
print(f"π€ Processing tag '{tag}' for repo '{repo_name}'")
# Create prompt for agent to handle tag processing
prompt = f"""
Analyze the repository '{repo_name}' and determine if the tag '{tag}' should be added.
First, check the current tags using get_current_tags.
If '{tag}' is not already present and it's a valid tag, add it using add_new_tag.
Repository: {repo_name}
Tag to process: {tag}
Provide a clear summary of what was done.
"""
response = await agent.run(prompt) # type: ignore
print(f"π€ Agent response for '{tag}': {response}")
# Parse response and store result
tag_result = {
"tag": tag,
"response": response,
"timestamp": datetime.now().isoformat()
}
operation["results"].append(tag_result)
except Exception as e:
error_msg = f"β Error processing tag '{tag}': {str(e)}"
print(error_msg)
operation["results"].append({
"tag": tag,
"error": str(e),
"timestamp": datetime.now().isoformat()
})
operation["status"] = "completed"
print(f"β
Completed processing {len(all_tags)} tags")
@app.get("/")
async def root():
"""Root endpoint with basic information"""
return {
"name": "HF Tagging Bot",
"status": "running",
"description": "Webhook listener for automatic model tagging",
"endpoints": {
"webhook": "/webhook",
"health": "/health",
"operations": "/operations"
}
}
@app.get("/health")
async def health_check():
"""Health check endpoint for monitoring"""
agent = await get_agent()
return {
"status": "healthy",
"timestamp": datetime.now().isoformat(),
"components": {
"webhook_secret": "configured" if WEBHOOK_SECRET else "missing",
"hf_token": "configured" if HF_TOKEN else "missing",
"mcp_agent": "ready" if agent else "not_ready"
}
}
@app.get("/operations")
async def get_operations():
"""Get recent tag operations for monitoring"""
# Return last 50 operations
recent_ops = tag_operations_store[-50:] if tag_operations_store else []
return {
"total_operations": len(tag_operations_store),
"recent_operations": recent_ops
}
# async def simulate_webhook(
# repo_name: str, discussion_title: str, comment_content: str
# ) -> str:
# """Simulate webhook for testing"""
# if not all([repo_name, discussion_title, comment_content]):
# return "Please fill in all fields."
#
# mock_payload = {
# "event": {"action": "create", "scope": "discussion"},
# "comment": {
# "content": comment_content,
# "author": {"id": "test-user-id"},
# "id": "mock-comment-id",
# "hidden": False,
# },
# "discussion": {
# "title": discussion_title,
# "num": len(tag_operations_store) + 1,
# "id": "mock-discussion-id",
# "status": "open",
# "isPullRequest": False,
# },
# "repo": {
# "name": repo_name,
# "type": "model",
# "private": False,
# },
# }
#
# response = await process_webhook_comment(mock_payload)
# return f"β
Processed! Results: {response}"
def create_gradio_app():
"""Create Gradio interface"""
with gr.Blocks(
title="HF Tagging Bot",
# theme=gr.themes.Soft()
) as demo:
gr.Markdown("# π·οΈ HF Tagging Bot Dashboard")
gr.Markdown("*Automatically adds tags to models when mentioned in discussions*")
gr.Markdown("""
## How it works:
- Monitors HuggingFace Hub discussions
- Detects tag mentions in comments (e.g., "tag: pytorch",
"#transformers")
- Automatically adds recognized tags to the model repository
- Supports common ML tags like: pytorch, tensorflow,
text-generation, etc.
""")
with gr.Column():
sim_repo = gr.Textbox(
label="Repository",
value="burtenshaw/play-mcp-repo-bot",
placeholder="username/model-name",
)
sim_title = gr.Textbox(
label="Discussion Title",
value="Add pytorch tag",
placeholder="Discussion title",
)
sim_comment = gr.Textbox(
label="Comment",
lines=3,
value="This model should have tags: pytorch, text-generation",
placeholder="Comment mentioning tags...",
)
sim_btn = gr.Button("π·οΈ Test Tag Detection")
with gr.Column():
sim_result = gr.Textbox(label="Result", lines=8)
# sim_btn.click(
# fn=simulate_webhook,
# inputs=[sim_repo, sim_title, sim_comment],
# outputs=sim_result,
# )
gr.Markdown(f"""
## Recognized Tags:
{", ".join(sorted(RECOGNIZED_TAGS))}
""")
return demo
@app.get("/")
async def welcome() -> dict:
return { "message": "Hello World"}
@app.get("/gradio2")
async def welcome_gradio2() -> dict:
return { "message": "Hello gradio2"}
# Mount Gradio app
gradio_app = create_gradio_app()
app = gr.mount_gradio_app(app, gradio_app, path="/gradio")
if __name__ == "__main__":
print("π Starting HF Tagging Bot...")
print("π Dashboard: http://localhost:7860/gradio")
print("π Webhook: http://localhost:7860/webhook")
#
# QUICK-AND-DIRTY TEST WITHOUT uvicorn
#
uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True)
#
# gradio_app = create_gradio_app()
# gradio_app.launch()
# EOF
|