Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, Request | |
| from slowapi import Limiter, _rate_limit_exceeded_handler | |
| from fastapi.responses import FileResponse | |
| from slowapi.middleware import SlowAPIMiddleware | |
| from slowapi.errors import RateLimitExceeded | |
| from slowapi.util import get_remote_address | |
| from fastapi.responses import JSONResponse | |
| from features.text_classifier.routes import router as text_classifier_router | |
| from features.nepali_text_classifier.routes import ( | |
| router as nepali_text_classifier_router, | |
| ) | |
| from features.image_classifier.routes import router as image_classifier_router | |
| from features.image_edit_detector.routes import router as image_edit_detector_router | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from config import ACCESS_RATE | |
| import requests | |
| limiter = Limiter(key_func=get_remote_address, default_limits=[ACCESS_RATE]) | |
| app = FastAPI() | |
| # added the robots.txt | |
| # Set up SlowAPI | |
| app.state.limiter = limiter | |
| app.add_exception_handler( | |
| RateLimitExceeded, | |
| lambda request, exc: JSONResponse( | |
| status_code=429, | |
| content={ | |
| "status_code": 429, | |
| "error": "Rate limit exceeded", | |
| "message": "Too many requests. Chill for a bit and try again", | |
| }, | |
| ), | |
| ) | |
| # 1. Add CORS first | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # Allow all origins | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # 2. Then add SlowAPI rate limiting middleware | |
| app.add_middleware(SlowAPIMiddleware) | |
| # Include your routes | |
| app.include_router(text_classifier_router, prefix="/text") | |
| app.include_router(nepali_text_classifier_router, prefix="/NP") | |
| app.include_router(image_classifier_router, prefix="/AI-image") | |
| app.include_router(image_edit_detector_router, prefix="/detect") | |
| async def root(request: Request): | |
| return { | |
| "message": "API is working", | |
| "endpoints": [ | |
| "/text/analyse", | |
| "/text/upload", | |
| "/text/analyse-sentences", | |
| "/text/analyse-sentance-file", | |
| "/NP/analyse", | |
| "/NP/upload", | |
| "/NP/analyse-sentences", | |
| "/NP/file-sentences-analyse", | |
| "/AI-image/analyse", | |
| ], | |
| } | |