Create handler.py
Browse files- handler.py +36 -0
handler.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import asyncio
|
| 4 |
+
from ai_core import AICore
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# Initialize the AI core
|
| 9 |
+
ai_core = AICore()
|
| 10 |
+
|
| 11 |
+
class QueryRequest(BaseModel):
|
| 12 |
+
query: str
|
| 13 |
+
|
| 14 |
+
class QueryResponse(BaseModel):
|
| 15 |
+
insights: list
|
| 16 |
+
response: str
|
| 17 |
+
security_level: int
|
| 18 |
+
safety_checks: dict
|
| 19 |
+
health_status: dict
|
| 20 |
+
encrypted_query: str
|
| 21 |
+
|
| 22 |
+
@app.post("/query", response_model=QueryResponse)
|
| 23 |
+
async def handle_query(request: QueryRequest):
|
| 24 |
+
try:
|
| 25 |
+
response = await ai_core.generate_response(request.query)
|
| 26 |
+
return QueryResponse(**response)
|
| 27 |
+
except Exception as e:
|
| 28 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 29 |
+
|
| 30 |
+
@app.on_event("shutdown")
|
| 31 |
+
async def shutdown_event():
|
| 32 |
+
await ai_core.shutdown()
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
import uvicorn
|
| 36 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|