Spaces:
Runtime error
Runtime error
Dan Walsh commited on
Commit ·
c6df052
1
Parent(s): d7a2753
Add root endpoint with API information and documentation links
Browse files
main.py
CHANGED
|
@@ -1,11 +1,18 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# Import the router
|
| 6 |
from app.api.routes import router as api_router
|
| 7 |
|
| 8 |
-
app = FastAPI(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Configure CORS
|
| 11 |
app.add_middleware(
|
|
@@ -22,10 +29,48 @@ app.add_middleware(
|
|
| 22 |
# Include the router
|
| 23 |
app.include_router(api_router)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
@app.get("/health")
|
| 26 |
async def health_check():
|
| 27 |
return {"status": "healthy"}
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
if __name__ == "__main__":
|
| 30 |
import uvicorn
|
| 31 |
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8000)))
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from fastapi.responses import JSONResponse, RedirectResponse
|
| 4 |
import os
|
| 5 |
|
| 6 |
# Import the router
|
| 7 |
from app.api.routes import router as api_router
|
| 8 |
|
| 9 |
+
app = FastAPI(
|
| 10 |
+
title="AI Content Summariser API",
|
| 11 |
+
description="An API for summarizing text content and web pages using NLP models",
|
| 12 |
+
version="1.0.0",
|
| 13 |
+
docs_url="/docs",
|
| 14 |
+
redoc_url="/redoc",
|
| 15 |
+
)
|
| 16 |
|
| 17 |
# Configure CORS
|
| 18 |
app.add_middleware(
|
|
|
|
| 29 |
# Include the router
|
| 30 |
app.include_router(api_router)
|
| 31 |
|
| 32 |
+
@app.get("/", include_in_schema=True)
|
| 33 |
+
async def root():
|
| 34 |
+
"""
|
| 35 |
+
Root endpoint that provides information about the API and available endpoints.
|
| 36 |
+
"""
|
| 37 |
+
return {
|
| 38 |
+
"name": "AI Content Summariser API",
|
| 39 |
+
"version": "1.0.0",
|
| 40 |
+
"description": "API for summarizing text content and web pages using NLP models",
|
| 41 |
+
"endpoints": {
|
| 42 |
+
"documentation": "/docs",
|
| 43 |
+
"alternative_docs": "/redoc",
|
| 44 |
+
"health_check": "/health",
|
| 45 |
+
"api_endpoints": {
|
| 46 |
+
"summarise_text": "/api/summarise",
|
| 47 |
+
"summarise_url": "/api/summarise-url",
|
| 48 |
+
"status": "/api/status"
|
| 49 |
+
}
|
| 50 |
+
},
|
| 51 |
+
"github_repository": "https://github.com/dang-w/ai-content-summariser-api",
|
| 52 |
+
"frontend_application": "https://ai-content-summariser.vercel.app"
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
@app.get("/docs-redirect")
|
| 56 |
+
async def docs_redirect():
|
| 57 |
+
"""
|
| 58 |
+
Redirects to the API documentation.
|
| 59 |
+
"""
|
| 60 |
+
return RedirectResponse(url="/docs")
|
| 61 |
+
|
| 62 |
@app.get("/health")
|
| 63 |
async def health_check():
|
| 64 |
return {"status": "healthy"}
|
| 65 |
|
| 66 |
+
# Global exception handler for better error responses
|
| 67 |
+
@app.exception_handler(Exception)
|
| 68 |
+
async def global_exception_handler(request: Request, exc: Exception):
|
| 69 |
+
return JSONResponse(
|
| 70 |
+
status_code=500,
|
| 71 |
+
content={"detail": f"An unexpected error occurred: {str(exc)}"}
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
if __name__ == "__main__":
|
| 75 |
import uvicorn
|
| 76 |
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8000)))
|