Spaces:
Paused
Paused
Delete api_service.py
Browse files- api_service.py +0 -92
api_service.py
DELETED
|
@@ -1,92 +0,0 @@
|
|
| 1 |
-
# filename: api_service.py
|
| 2 |
-
|
| 3 |
-
from fastapi import FastAPI, HTTPException
|
| 4 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
-
from pydantic import BaseModel
|
| 6 |
-
from typing import Dict, Any, Optional
|
| 7 |
-
|
| 8 |
-
# Import the core logic and model loading functions
|
| 9 |
-
from backend_pam import load_agent as load_backend_agent, PAM
|
| 10 |
-
from frontend_pam import load_frontend_agent, FrontendPAM
|
| 11 |
-
|
| 12 |
-
# --- Global Agent Variables ---
|
| 13 |
-
backend_agent: Optional[PAM] = None
|
| 14 |
-
frontend_agent: Optional[FrontendPAM] = None
|
| 15 |
-
|
| 16 |
-
# --- Data Model for API Request ---
|
| 17 |
-
class UserInput(BaseModel):
|
| 18 |
-
user_input: str
|
| 19 |
-
|
| 20 |
-
# --- FastAPI Initialization ---
|
| 21 |
-
app = FastAPI(
|
| 22 |
-
title="PAM Unified Inference Service (Technical & Chat)",
|
| 23 |
-
version="2.0.0",
|
| 24 |
-
docs_url=None,
|
| 25 |
-
redoc_url=None
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
# --- CORS Setup ---
|
| 29 |
-
origins = [
|
| 30 |
-
"https://www.uminur.app",
|
| 31 |
-
"https://api.uminur.app",
|
| 32 |
-
]
|
| 33 |
-
|
| 34 |
-
app.add_middleware(
|
| 35 |
-
CORSMiddleware,
|
| 36 |
-
allow_origins=origins,
|
| 37 |
-
allow_credentials=True,
|
| 38 |
-
allow_methods=["POST", "GET"],
|
| 39 |
-
allow_headers=["*"],
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
# --- Startup Event ---
|
| 43 |
-
@app.on_event("startup")
|
| 44 |
-
async def startup_event():
|
| 45 |
-
global backend_agent, frontend_agent
|
| 46 |
-
print("FastAPI: Starting up PAM agents...")
|
| 47 |
-
|
| 48 |
-
backend_agent = load_backend_agent()
|
| 49 |
-
frontend_agent = load_frontend_agent()
|
| 50 |
-
|
| 51 |
-
if not backend_agent or not frontend_agent:
|
| 52 |
-
print("FastAPI: ERROR — One or both agents failed to initialize.")
|
| 53 |
-
|
| 54 |
-
# --- Health Check ---
|
| 55 |
-
@app.get("/health", status_code=200, tags=["Status"])
|
| 56 |
-
def health_check():
|
| 57 |
-
backend_ok = backend_agent is not None
|
| 58 |
-
frontend_ok = frontend_agent is not None
|
| 59 |
-
|
| 60 |
-
if backend_ok and frontend_ok:
|
| 61 |
-
return {"status": "ok", "backend_ready": True, "frontend_ready": True}
|
| 62 |
-
|
| 63 |
-
raise HTTPException(
|
| 64 |
-
status_code=503,
|
| 65 |
-
detail=f"Service Unavailable: Backend Ready: {backend_ok}, Frontend Ready: {frontend_ok}"
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
-
# --- Technical Endpoint ---
|
| 69 |
-
@app.post("/ai/technical/", tags=["Technical"])
|
| 70 |
-
async def technical_endpoint(input_data: UserInput) -> Dict[str, Any]:
|
| 71 |
-
if backend_agent is None:
|
| 72 |
-
raise HTTPException(status_code=503, detail="Backend PAM is not initialized.")
|
| 73 |
-
|
| 74 |
-
try:
|
| 75 |
-
pam_reply = backend_agent.process_input(input_data.user_input)
|
| 76 |
-
return pam_reply
|
| 77 |
-
except Exception as e:
|
| 78 |
-
print(f"Error during technical inference: {e}")
|
| 79 |
-
raise HTTPException(status_code=500, detail="Internal Server Error (technical)")
|
| 80 |
-
|
| 81 |
-
# --- Chat Endpoint ---
|
| 82 |
-
@app.post("/ai/chat/", tags=["Chat"])
|
| 83 |
-
async def chat_endpoint(input_data: UserInput) -> Dict[str, Any]:
|
| 84 |
-
if frontend_agent is None:
|
| 85 |
-
raise HTTPException(status_code=503, detail="Frontend PAM is not initialized.")
|
| 86 |
-
|
| 87 |
-
try:
|
| 88 |
-
pam_reply = frontend_agent.respond(input_data.user_input)
|
| 89 |
-
return pam_reply
|
| 90 |
-
except Exception as e:
|
| 91 |
-
print(f"Error during chat inference: {e}")
|
| 92 |
-
raise HTTPException(status_code=500, detail="Internal Server Error (chat)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|