Spaces:
Sleeping
Sleeping
created main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, Header, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import Optional, Any, Dict, List
|
| 4 |
+
import httpx
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# --- Configuration ---
|
| 10 |
+
# Set these in your Space's "Settings" -> "Variables and secrets"
|
| 11 |
+
DISCORD_WEBHOOK_URL = os.environ.get("DISCORD_WEBHOOK_URL")
|
| 12 |
+
# Optional: Secure your webhook so only HF can trigger it
|
| 13 |
+
WEBHOOK_SECRET = os.environ.get("WEBHOOK_SECRET")
|
| 14 |
+
|
| 15 |
+
# --- Pydantic Models for HF Payload ---
|
| 16 |
+
class HFRepo(BaseModel):
|
| 17 |
+
type: str
|
| 18 |
+
name: str
|
| 19 |
+
id: str
|
| 20 |
+
url: Dict[str, str]
|
| 21 |
+
|
| 22 |
+
class HFEvent(BaseModel):
|
| 23 |
+
action: str
|
| 24 |
+
scope: str
|
| 25 |
+
|
| 26 |
+
class HFPayload(BaseModel):
|
| 27 |
+
repo: HFRepo
|
| 28 |
+
event: HFEvent
|
| 29 |
+
discussion: Optional[Dict[str, Any]] = None
|
| 30 |
+
comment: Optional[Dict[str, Any]] = None
|
| 31 |
+
webhook: Optional[Dict[str, Any]] = None
|
| 32 |
+
|
| 33 |
+
@app.get("/")
|
| 34 |
+
def home():
|
| 35 |
+
return {"status": "Webhook listener is running"}
|
| 36 |
+
|
| 37 |
+
@app.post("/webhook")
|
| 38 |
+
async def handle_webhook(
|
| 39 |
+
payload: HFPayload,
|
| 40 |
+
x_webhook_secret: Optional[str] = Header(None)
|
| 41 |
+
):
|
| 42 |
+
# 1. Security Check
|
| 43 |
+
if WEBHOOK_SECRET and x_webhook_secret != WEBHOOK_SECRET:
|
| 44 |
+
raise HTTPException(status_code=401, detail="Invalid Secret")
|
| 45 |
+
|
| 46 |
+
# 2. Format for Discord
|
| 47 |
+
embed = build_discord_embed(payload)
|
| 48 |
+
|
| 49 |
+
# 3. Send to Discord
|
| 50 |
+
if DISCORD_WEBHOOK_URL:
|
| 51 |
+
async with httpx.AsyncClient() as client:
|
| 52 |
+
resp = await client.post(
|
| 53 |
+
DISCORD_WEBHOOK_URL,
|
| 54 |
+
json={"embeds": [embed]}
|
| 55 |
+
)
|
| 56 |
+
print(f"Sent to Discord: {resp.status_code}")
|
| 57 |
+
|
| 58 |
+
return {"message": "Processed"}
|
| 59 |
+
|
| 60 |
+
def build_discord_embed(data: HFPayload):
|
| 61 |
+
repo_name = data.repo.name
|
| 62 |
+
repo_url = data.repo.url.get('web', '')
|
| 63 |
+
event_scope = data.event.scope
|
| 64 |
+
event_action = data.event.action
|
| 65 |
+
|
| 66 |
+
# Default Color (Grey)
|
| 67 |
+
color = 0x95a5a6
|
| 68 |
+
title = f"Update on {repo_name}"
|
| 69 |
+
description = f"**Action:** {event_action}\n**Scope:** {event_scope}"
|
| 70 |
+
|
| 71 |
+
# Customize based on event type
|
| 72 |
+
if event_scope.startswith("repo.content"):
|
| 73 |
+
# Code/Model update (Blue)
|
| 74 |
+
color = 0x3498db
|
| 75 |
+
title = f"New Commit to {repo_name}"
|
| 76 |
+
description = "Model/Dataset files have been updated."
|
| 77 |
+
|
| 78 |
+
elif event_scope.startswith("discussion"):
|
| 79 |
+
# Discussion/PR (Green)
|
| 80 |
+
color = 0x2ecc71
|
| 81 |
+
disc_title = data.discussion.get('title', 'No Title') if data.discussion else "Unknown"
|
| 82 |
+
title = f"Discussion Update: {disc_title}"
|
| 83 |
+
|
| 84 |
+
if data.comment:
|
| 85 |
+
description = f"**New Comment** on {repo_name}:\n> {data.comment.get('content', '')[:200]}..."
|
| 86 |
+
else:
|
| 87 |
+
description = f"Discussion status: **{event_action}**"
|
| 88 |
+
|
| 89 |
+
return {
|
| 90 |
+
"title": title,
|
| 91 |
+
"description": description,
|
| 92 |
+
"url": repo_url,
|
| 93 |
+
"color": color,
|
| 94 |
+
"footer": {
|
| 95 |
+
"text": "Hugging Face Webhook"
|
| 96 |
+
}
|
| 97 |
+
}
|