Spaces:
Sleeping
Sleeping
Run uvicorn on module load and mount Gradio on FastAPI
Browse files- gradio_app.py +44 -42
gradio_app.py
CHANGED
|
@@ -35,46 +35,20 @@ async def lifespan(app: FastAPI):
|
|
| 35 |
yield
|
| 36 |
stop_scheduler()
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
gr.Markdown(
|
| 46 |
-
"""
|
| 47 |
-
# ⚡ BytePulse AI — Daily Tech Digest API
|
| 48 |
-
|
| 49 |
-
> Backend is **live and healthy!** 🚀
|
| 50 |
-
|
| 51 |
-
| Endpoint | Description |
|
| 52 |
-
|----------|-------------|
|
| 53 |
-
| [`/health`](./health) | Health check |
|
| 54 |
-
| [`/docs`](./docs) | Swagger API Docs |
|
| 55 |
-
| [`/api/v1`](./api/v1) | API Base |
|
| 56 |
-
"""
|
| 57 |
-
)
|
| 58 |
-
# Expose the GPU-decorated function explicitly to satisfy ZeroGPU detection
|
| 59 |
-
btn = gr.Button("Warm Up ZeroGPU")
|
| 60 |
-
out = gr.Textbox()
|
| 61 |
-
btn.click(fn=dummy_gpu_func, inputs=out, outputs=out)
|
| 62 |
-
|
| 63 |
-
# Access underlying FastAPI app of Gradio (which is run by the Space runner)
|
| 64 |
-
app = demo.app
|
| 65 |
-
|
| 66 |
-
# Configure FastAPI properties on the Gradio app
|
| 67 |
-
app.title = settings.APP_NAME
|
| 68 |
-
app.version = settings.VERSION
|
| 69 |
-
app.description = "Enterprise API for BytePulse AI Daily Tech Digest powered by 8 Scrapers, Gemini 2.5 Flash, ChromaDB, and FCM."
|
| 70 |
-
app.router.lifespan_context = lifespan
|
| 71 |
|
| 72 |
# Attach Rate Limiter to app state
|
| 73 |
-
|
| 74 |
-
|
| 75 |
|
| 76 |
# App Secret Authentication Middleware
|
| 77 |
-
@
|
| 78 |
async def app_secret_security_middleware(request: Request, call_next):
|
| 79 |
path = request.url.path
|
| 80 |
if path in ["/", "/health", "/docs", "/openapi.json", "/redoc", "/gradio"]:
|
|
@@ -93,7 +67,7 @@ async def app_secret_security_middleware(request: Request, call_next):
|
|
| 93 |
return await call_next(request)
|
| 94 |
|
| 95 |
# Global Error Handler
|
| 96 |
-
@
|
| 97 |
async def global_exception_handler(request: Request, exc: Exception):
|
| 98 |
logger.error(f"Global Error caught on {request.url.path}: {exc}", exc_info=True)
|
| 99 |
return JSONResponse(
|
|
@@ -106,7 +80,7 @@ async def global_exception_handler(request: Request, exc: Exception):
|
|
| 106 |
)
|
| 107 |
|
| 108 |
# Enable CORS Security Middleware
|
| 109 |
-
|
| 110 |
CORSMiddleware,
|
| 111 |
allow_origins=["*"],
|
| 112 |
allow_credentials=True,
|
|
@@ -115,14 +89,42 @@ app.add_middleware(
|
|
| 115 |
)
|
| 116 |
|
| 117 |
# Root & Health Endpoints
|
| 118 |
-
@
|
| 119 |
@limiter.limit("60/minute")
|
| 120 |
def health_check(request: Request):
|
| 121 |
return {"status": "healthy", "service": settings.APP_NAME, "security": "rate_limited_enabled"}
|
| 122 |
|
| 123 |
# Include API v1 Router
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
#
|
| 127 |
-
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
yield
|
| 36 |
stop_scheduler()
|
| 37 |
|
| 38 |
+
# Initialize FastAPI application
|
| 39 |
+
fastapi_app = FastAPI(
|
| 40 |
+
title=settings.APP_NAME,
|
| 41 |
+
version=settings.VERSION,
|
| 42 |
+
description="Enterprise API for BytePulse AI Daily Tech Digest powered by 8 Scrapers, Gemini 2.5 Flash, ChromaDB, and FCM.",
|
| 43 |
+
lifespan=lifespan
|
| 44 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Attach Rate Limiter to app state
|
| 47 |
+
fastapi_app.state.limiter = limiter
|
| 48 |
+
fastapi_app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
| 49 |
|
| 50 |
# App Secret Authentication Middleware
|
| 51 |
+
@fastapi_app.middleware("http")
|
| 52 |
async def app_secret_security_middleware(request: Request, call_next):
|
| 53 |
path = request.url.path
|
| 54 |
if path in ["/", "/health", "/docs", "/openapi.json", "/redoc", "/gradio"]:
|
|
|
|
| 67 |
return await call_next(request)
|
| 68 |
|
| 69 |
# Global Error Handler
|
| 70 |
+
@fastapi_app.exception_handler(Exception)
|
| 71 |
async def global_exception_handler(request: Request, exc: Exception):
|
| 72 |
logger.error(f"Global Error caught on {request.url.path}: {exc}", exc_info=True)
|
| 73 |
return JSONResponse(
|
|
|
|
| 80 |
)
|
| 81 |
|
| 82 |
# Enable CORS Security Middleware
|
| 83 |
+
fastapi_app.add_middleware(
|
| 84 |
CORSMiddleware,
|
| 85 |
allow_origins=["*"],
|
| 86 |
allow_credentials=True,
|
|
|
|
| 89 |
)
|
| 90 |
|
| 91 |
# Root & Health Endpoints
|
| 92 |
+
@fastapi_app.get("/health")
|
| 93 |
@limiter.limit("60/minute")
|
| 94 |
def health_check(request: Request):
|
| 95 |
return {"status": "healthy", "service": settings.APP_NAME, "security": "rate_limited_enabled"}
|
| 96 |
|
| 97 |
# Include API v1 Router
|
| 98 |
+
fastapi_app.include_router(api_router, prefix="/api/v1")
|
| 99 |
+
|
| 100 |
+
# Dummy function decorated with GPU to satisfy Hugging Face ZeroGPU requirement
|
| 101 |
+
@spaces.GPU
|
| 102 |
+
def dummy_gpu_func(name):
|
| 103 |
+
return f"ZeroGPU active for {name}"
|
| 104 |
+
|
| 105 |
+
# Minimal Gradio landing page
|
| 106 |
+
with gr.Blocks(title="BytePulse AI Backend", theme=gr.themes.Soft()) as demo:
|
| 107 |
+
gr.Markdown(
|
| 108 |
+
"""
|
| 109 |
+
# ⚡ BytePulse AI — Daily Tech Digest API
|
| 110 |
+
|
| 111 |
+
> Backend is **live and healthy!** 🚀
|
| 112 |
+
|
| 113 |
+
| Endpoint | Description |
|
| 114 |
+
|----------|-------------|
|
| 115 |
+
| [`/health`](./health) | Health check |
|
| 116 |
+
| [`/docs`](./docs) | Swagger API Docs |
|
| 117 |
+
| [`/api/v1`](./api/v1) | API Base |
|
| 118 |
+
"""
|
| 119 |
+
)
|
| 120 |
+
btn = gr.Button("Warm Up ZeroGPU")
|
| 121 |
+
out = gr.Textbox()
|
| 122 |
+
btn.click(fn=dummy_gpu_func, inputs=out, outputs=out)
|
| 123 |
|
| 124 |
+
# Mount Gradio inside FastAPI under /gradio path so that root routes belong to FastAPI
|
| 125 |
+
app = gr.mount_gradio_app(fastapi_app, demo, path="/gradio")
|
| 126 |
|
| 127 |
+
# Run uvicorn server directly at module load time (without __name__ == '__main__')
|
| 128 |
+
# This blocks the importer thread and keeps the Space container running on port 7860!
|
| 129 |
+
import uvicorn
|
| 130 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|