Spaces:
Sleeping
Sleeping
Commit ·
766b9f5
1
Parent(s): 3ff68b3
Use Gradio's internal FastAPI app for webhook endpoints
Browse filesRestructure to use demo.fastapi() instead of separate FastAPI app. This ensures proper integration with HuggingFace Spaces Gradio SDK and prevents exit code 0 issues.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from fastapi import
|
| 3 |
from fastapi.responses import JSONResponse
|
| 4 |
import os
|
| 5 |
import json
|
|
@@ -9,9 +9,6 @@ from datasets import Dataset
|
|
| 9 |
from huggingface_hub import HfApi
|
| 10 |
import pandas as pd
|
| 11 |
|
| 12 |
-
# Create FastAPI app
|
| 13 |
-
app = FastAPI()
|
| 14 |
-
|
| 15 |
# Configuration
|
| 16 |
DATASET_REPO = "assafvayner/webhook-messages"
|
| 17 |
BATCH_SIZE = 100
|
|
@@ -26,6 +23,17 @@ latest_batch_file = None
|
|
| 26 |
# HuggingFace API client
|
| 27 |
hf_api = HfApi(token=os.environ.get("HF_TOKEN"))
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def save_batch_to_dataset(messages, batch_num):
|
| 31 |
"""Save a batch of webhook messages to the HuggingFace dataset as a parquet file."""
|
|
@@ -209,8 +217,8 @@ with gr.Blocks(title="HuggingFace Webhook Processor") as demo:
|
|
| 209 |
timer.tick(fn=get_status, outputs=[status_text, message_count, batch_count, latest_batch])
|
| 210 |
|
| 211 |
|
| 212 |
-
# Add webhook
|
| 213 |
-
@
|
| 214 |
async def webhook_endpoint(request: Request):
|
| 215 |
"""
|
| 216 |
Webhook endpoint for HuggingFace Hub events.
|
|
@@ -252,7 +260,7 @@ async def webhook_endpoint(request: Request):
|
|
| 252 |
raise HTTPException(status_code=500, detail=str(e))
|
| 253 |
|
| 254 |
|
| 255 |
-
@
|
| 256 |
async def health_check():
|
| 257 |
"""Health check endpoint."""
|
| 258 |
with message_lock:
|
|
@@ -264,17 +272,6 @@ async def health_check():
|
|
| 264 |
}
|
| 265 |
|
| 266 |
|
| 267 |
-
#
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
repo_id=DATASET_REPO,
|
| 271 |
-
repo_type="dataset",
|
| 272 |
-
exist_ok=True
|
| 273 |
-
)
|
| 274 |
-
print(f"✅ Dataset repository ready: {DATASET_REPO}")
|
| 275 |
-
except Exception as e:
|
| 276 |
-
print(f"⚠️ Warning: Could not create/verify dataset repo: {str(e)}")
|
| 277 |
-
|
| 278 |
-
# Mount Gradio app on FastAPI
|
| 279 |
-
# HuggingFace Spaces will automatically serve this app
|
| 280 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from fastapi import Request, HTTPException
|
| 3 |
from fastapi.responses import JSONResponse
|
| 4 |
import os
|
| 5 |
import json
|
|
|
|
| 9 |
from huggingface_hub import HfApi
|
| 10 |
import pandas as pd
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
# Configuration
|
| 13 |
DATASET_REPO = "assafvayner/webhook-messages"
|
| 14 |
BATCH_SIZE = 100
|
|
|
|
| 23 |
# HuggingFace API client
|
| 24 |
hf_api = HfApi(token=os.environ.get("HF_TOKEN"))
|
| 25 |
|
| 26 |
+
# Ensure dataset repo exists on startup
|
| 27 |
+
try:
|
| 28 |
+
hf_api.create_repo(
|
| 29 |
+
repo_id=DATASET_REPO,
|
| 30 |
+
repo_type="dataset",
|
| 31 |
+
exist_ok=True
|
| 32 |
+
)
|
| 33 |
+
print(f"✅ Dataset repository ready: {DATASET_REPO}")
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"⚠️ Warning: Could not create/verify dataset repo: {str(e)}")
|
| 36 |
+
|
| 37 |
|
| 38 |
def save_batch_to_dataset(messages, batch_num):
|
| 39 |
"""Save a batch of webhook messages to the HuggingFace dataset as a parquet file."""
|
|
|
|
| 217 |
timer.tick(fn=get_status, outputs=[status_text, message_count, batch_count, latest_batch])
|
| 218 |
|
| 219 |
|
| 220 |
+
# Add webhook endpoints to Gradio's internal FastAPI app
|
| 221 |
+
@demo.fastapi().post("/webhooks/hub")
|
| 222 |
async def webhook_endpoint(request: Request):
|
| 223 |
"""
|
| 224 |
Webhook endpoint for HuggingFace Hub events.
|
|
|
|
| 260 |
raise HTTPException(status_code=500, detail=str(e))
|
| 261 |
|
| 262 |
|
| 263 |
+
@demo.fastapi().get("/webhooks/health")
|
| 264 |
async def health_check():
|
| 265 |
"""Health check endpoint."""
|
| 266 |
with message_lock:
|
|
|
|
| 272 |
}
|
| 273 |
|
| 274 |
|
| 275 |
+
# Launch the Gradio app (HuggingFace Spaces will handle this automatically)
|
| 276 |
+
if __name__ == "__main__":
|
| 277 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|