File size: 1,267 Bytes
2a08dd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
from huggingface_hub import WebhooksServer, WebhookPayload

# Optional: custom UI for your Space landing page
with gr.Blocks() as ui:
    gr.Markdown("# ๐Ÿ› ๏ธ MCP-Webhooks-CodeFix Webhook Receiver")
    gr.Markdown(
        "This Space listens to Hugging Face Hub webhooks and displays the incoming payloads."
    )
    gr.Markdown("๐ŸŽฏ Set up a webhook in your Hugging Face account to point here.")

# Read secret from env or define directly
webhook_secret = os.getenv("WEBHOOK_SECRET", None)

# Launch the webhook server with optional security
app = WebhooksServer(ui=ui, webhook_secret=webhook_secret)

@app.add_webhook("/on_push")
async def on_repository_push(payload: WebhookPayload):
    # Example: handle repo pushes
    repo_name = payload.repo.name
    event = payload.event.action
    return {"msg": f"Received {event} in repo `{repo_name}`"}

@app.add_webhook("/on_discussion")
async def on_discussion_event(payload: WebhookPayload):
    # Example: handle discussion events
    discussion = payload.discussion
    title = discussion.title if discussion else "N/A"
    return {"msg": f"Discussion event on '{title}'"}

if __name__ == "__main__":
    # Starts the Gradio / FastAPI app with webhook endpoints
    app.launch()