assafvayner HF Staff Claude Sonnet 4.5 commited on
Commit
ebff9dd
·
1 Parent(s): 5e1f939

Fix FastAPI integration for Gradio 6.5.1

Browse files

- Create FastAPI app first and mount Gradio on it
- Use gr.mount_gradio_app() for proper integration
- Add uvicorn and fastapi to requirements
- Use

@app
decorators instead of

@demo
.fastapi_app

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

Files changed (2) hide show
  1. app.py +14 -4
  2. requirements.txt +2 -0
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from fastapi import Request, HTTPException
3
  from fastapi.responses import JSONResponse
4
  import os
5
  import json
@@ -9,6 +9,9 @@ from datasets import Dataset
9
  from huggingface_hub import HfApi
10
  import pandas as pd
11
 
 
 
 
12
  # Configuration
13
  DATASET_REPO = "assafvayner/webhook-messages"
14
  BATCH_SIZE = 10000
@@ -207,7 +210,7 @@ with gr.Blocks(title="HuggingFace Webhook Processor") as demo:
207
 
208
 
209
  # Add webhook endpoint to FastAPI
210
- @demo.fastapi_app.post("/webhooks/hub")
211
  async def webhook_endpoint(request: Request):
212
  """
213
  Webhook endpoint for HuggingFace Hub events.
@@ -249,7 +252,7 @@ async def webhook_endpoint(request: Request):
249
  raise HTTPException(status_code=500, detail=str(e))
250
 
251
 
252
- @demo.fastapi_app.get("/webhooks/health")
253
  async def health_check():
254
  """Health check endpoint."""
255
  with message_lock:
@@ -261,7 +264,13 @@ async def health_check():
261
  }
262
 
263
 
 
 
 
 
264
  if __name__ == "__main__":
 
 
265
  # Ensure dataset repo exists
266
  try:
267
  hf_api.create_repo(
@@ -273,4 +282,5 @@ if __name__ == "__main__":
273
  except Exception as e:
274
  print(f"⚠️ Warning: Could not create/verify dataset repo: {str(e)}")
275
 
276
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
1
  import gradio as gr
2
+ from fastapi import FastAPI, 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
+ # Create FastAPI app
13
+ app = FastAPI()
14
+
15
  # Configuration
16
  DATASET_REPO = "assafvayner/webhook-messages"
17
  BATCH_SIZE = 10000
 
210
 
211
 
212
  # Add webhook endpoint to FastAPI
213
+ @app.post("/webhooks/hub")
214
  async def webhook_endpoint(request: Request):
215
  """
216
  Webhook endpoint for HuggingFace Hub events.
 
252
  raise HTTPException(status_code=500, detail=str(e))
253
 
254
 
255
+ @app.get("/webhooks/health")
256
  async def health_check():
257
  """Health check endpoint."""
258
  with message_lock:
 
264
  }
265
 
266
 
267
+ # Mount Gradio app on FastAPI
268
+ app = gr.mount_gradio_app(app, demo, path="/")
269
+
270
+
271
  if __name__ == "__main__":
272
+ import uvicorn
273
+
274
  # Ensure dataset repo exists
275
  try:
276
  hf_api.create_repo(
 
282
  except Exception as e:
283
  print(f"⚠️ Warning: Could not create/verify dataset repo: {str(e)}")
284
 
285
+ # Launch FastAPI app with uvicorn
286
+ uvicorn.run(app, host="0.0.0.0", port=7860)
requirements.txt CHANGED
@@ -3,3 +3,5 @@ huggingface-hub
3
  datasets
4
  pandas
5
  pyarrow
 
 
 
3
  datasets
4
  pandas
5
  pyarrow
6
+ uvicorn
7
+ fastapi