File size: 8,295 Bytes
41d63c9
cd93ea5
41d63c9
 
 
 
 
 
 
 
 
 
 
df20a13
41d63c9
 
 
 
 
 
 
 
 
 
 
766b9f5
 
 
 
 
 
 
 
 
 
 
41d63c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd93ea5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41d63c9
 
 
 
 
 
 
 
 
 
 
 
 
df20a13
41d63c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd64b4c
75ff8df
 
bd64b4c
75ff8df
41d63c9
 
cd93ea5
 
41d63c9
 
cd93ea5
766b9f5
cd93ea5
 
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
import gradio as gr
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import JSONResponse
import os
import json
from datetime import datetime
from threading import Lock
from datasets import Dataset
from huggingface_hub import HfApi
import pandas as pd

# Configuration
DATASET_REPO = "assafvayner/webhook-messages"
BATCH_SIZE = 100
ALLOWED_SCOPES = {"repo", "repo.content"}

# In-memory storage
webhook_messages = []
message_lock = Lock()
batch_counter = 0
latest_batch_file = None

# HuggingFace API client
hf_api = HfApi(token=os.environ.get("HF_TOKEN"))

# Ensure dataset repo exists on startup
try:
    hf_api.create_repo(
        repo_id=DATASET_REPO,
        repo_type="dataset",
        exist_ok=True
    )
    print(f"βœ… Dataset repository ready: {DATASET_REPO}")
except Exception as e:
    print(f"⚠️  Warning: Could not create/verify dataset repo: {str(e)}")


def save_batch_to_dataset(messages, batch_num):
    """Save a batch of webhook messages to the HuggingFace dataset as a parquet file."""
    global latest_batch_file
    try:
        # Create DataFrame from messages
        df = pd.DataFrame(messages)

        # Create filename with timestamp and batch number
        timestamp = datetime.utcnow().strftime("%Y%m%d_%H%M%S")
        filename = f"batch_{batch_num:06d}_{timestamp}.parquet"

        # Convert to HuggingFace Dataset
        dataset = Dataset.from_pandas(df)

        # Upload to the dataset repo
        dataset.to_parquet(f"/tmp/{filename}")

        hf_api.upload_file(
            path_or_fileobj=f"/tmp/{filename}",
            path_in_repo=f"data/{filename}",
            repo_id=DATASET_REPO,
            repo_type="dataset",
        )

        print(f"βœ… Saved batch {batch_num} with {len(messages)} messages to {DATASET_REPO}")

        # Update latest batch file info
        latest_batch_file = f"data/{filename}"

        # Clean up temp file
        os.remove(f"/tmp/{filename}")

        return True
    except Exception as e:
        print(f"❌ Error saving batch {batch_num}: {str(e)}")
        return False


def process_webhook(payload: dict, event_type: str):
    """Process and store webhook payload if it matches allowed scopes."""
    global batch_counter

    # Extract scope from payload
    scope = payload.get("event", {}).get("scope")

    # Filter by scope
    if scope not in ALLOWED_SCOPES:
        return False

    # Create message entry
    message = {
        "timestamp": datetime.utcnow().isoformat(),
        "event_type": event_type,
        "scope": scope,
        "payload": json.dumps(payload)  # Store full payload as JSON string
    }

    with message_lock:
        webhook_messages.append(message)
        current_count = len(webhook_messages)

        # Check if we need to save a batch
        if current_count >= BATCH_SIZE:
            batch_counter += 1
            messages_to_save = webhook_messages.copy()
            webhook_messages.clear()

            # Save in background (non-blocking)
            save_batch_to_dataset(messages_to_save, batch_counter)

    return True


# Create FastAPI app first
app = FastAPI()


# Add webhook endpoints BEFORE mounting Gradio
@app.post("/webhooks/hub")
async def webhook_endpoint(request: Request):
    """
    Webhook endpoint for HuggingFace Hub events.

    Supports all webhook events documented at:
    https://huggingface.co/docs/hub/webhooks
    """
    try:
        # Get the event type from headers
        event_type = request.headers.get("X-Event-Type", "unknown")

        # Parse JSON payload
        payload = await request.json()

        # Process the webhook
        processed = process_webhook(payload, event_type)

        if processed:
            return JSONResponse(
                content={
                    "status": "success",
                    "message": "Webhook received and queued",
                    "scope": payload.get("event", {}).get("scope")
                },
                status_code=200
            )
        else:
            return JSONResponse(
                content={
                    "status": "ignored",
                    "message": "Webhook scope not in allowed list",
                    "scope": payload.get("event", {}).get("scope")
                },
                status_code=200
            )

    except Exception as e:
        print(f"Error processing webhook: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))


@app.get("/webhooks/health")
async def health_check():
    """Health check endpoint."""
    with message_lock:
        return {
            "status": "healthy",
            "messages_in_memory": len(webhook_messages),
            "batches_saved": batch_counter,
            "allowed_scopes": list(ALLOWED_SCOPES)
        }


# Create Gradio interface
with gr.Blocks(title="HuggingFace Webhook Processor") as demo:
    gr.Markdown("""
    # πŸŒ– HuggingFace Webhook Processor

    This app receives HuggingFace Hub webhooks and stores them for analysis.

    ## Webhook Endpoint

    Send POST requests to: `/webhooks/hub`

    ## Configuration
    - **Filtered Scopes**: `repo`, `repo.content`
    - **Batch Size**: 100 messages
    - **Dataset**: `assafvayner/webhook-messages`

    ## Status
    """)

    with gr.Row():
        with gr.Column():
            status_text = gr.Textbox(
                label="Current Status",
                value="Waiting for webhooks...",
                interactive=False
            )

            message_count = gr.Number(
                label="Messages in Memory",
                value=0,
                interactive=False
            )

        with gr.Column():
            batch_count = gr.Number(
                label="Batches Saved",
                value=0,
                interactive=False
            )

            latest_batch = gr.Textbox(
                label="Latest Batch File",
                value="No batches saved yet",
                interactive=False
            )

    def get_status():
        with message_lock:
            batch_file = latest_batch_file if latest_batch_file else "No batches saved yet"
            return (
                f"Active - Ready to receive webhooks",
                len(webhook_messages),
                batch_counter,
                batch_file
            )

    def get_recent_messages():
        with message_lock:
            if not webhook_messages:
                return "No messages in memory yet"

            # Get first 10 messages (or fewer if less than 10)
            messages_to_show = webhook_messages[:10]

            # Format messages nicely
            output = []
            for i, msg in enumerate(messages_to_show, 1):
                output.append(f"### Message {i}")
                output.append(f"**Timestamp:** {msg['timestamp']}")
                output.append(f"**Event Type:** {msg['event_type']}")
                output.append(f"**Scope:** {msg['scope']}")
                output.append(f"**Payload:**")

                # Parse and pretty-print JSON
                try:
                    payload = json.loads(msg['payload'])
                    output.append(f"```json\n{json.dumps(payload, indent=2)}\n```")
                except:
                    output.append(f"```\n{msg['payload']}\n```")

                output.append("\n---\n")

            return "\n".join(output)

    refresh_btn = gr.Button("πŸ”„ Refresh Status")
    refresh_btn.click(
        fn=get_status,
        outputs=[status_text, message_count, batch_count, latest_batch]
    )

    with gr.Accordion("πŸ“‹ Recent Messages (First 10)", open=False):
        recent_messages = gr.Markdown(
            value="Click 'Refresh Messages' to load recent messages"
        )

        refresh_messages_btn = gr.Button("πŸ”„ Refresh Messages")
        refresh_messages_btn.click(
            fn=get_recent_messages,
            outputs=[recent_messages]
        )

    # Load initial status on page load
    demo.load(
        fn=get_status,
        outputs=[status_text, message_count, batch_count, latest_batch]
    )


# Mount Gradio on our FastAPI app
app = gr.mount_gradio_app(app, demo, path="/")


# Launch the app
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)