Spaces:
Sleeping
Sleeping
| import asyncio | |
| import time | |
| from fastapi import FastAPI, Request | |
| from fastapi.responses import JSONResponse | |
| from starlette.status import HTTP_504_GATEWAY_TIMEOUT | |
| from neural_searcher import NeuralSearcher | |
| from huggingface_hub import login | |
| from config import HUGGING_FACE_API_KEY,COLLECTION_NAME | |
| import os | |
| login(HUGGING_FACE_API_KEY) | |
| app = FastAPI() | |
| neural_searcher = NeuralSearcher(collection_name=COLLECTION_NAME) | |
| REQUEST_TIMEOUT_ERROR = 30 | |
| async def search(q: str): | |
| data = await neural_searcher.search(text=q) | |
| return data | |
| async def timeout_middleware(request: Request, call_next): | |
| try: | |
| start_time = time.time() | |
| return await asyncio.wait_for(call_next(request), timeout=REQUEST_TIMEOUT_ERROR) | |
| except asyncio.TimeoutError: | |
| process_time = time.time() - start_time | |
| return JSONResponse({'detail': 'Request processing time excedeed limit', | |
| 'processing_time': process_time}, | |
| status_code=HTTP_504_GATEWAY_TIMEOUT) |