digopala commited on
Commit
5b1f64c
·
verified ·
1 Parent(s): f5b9543

Upload app.main.example.py

Browse files

Add gateway example with auth and routing

Files changed (1) hide show
  1. app.main.example.py +38 -0
app.main.example.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Depends, HTTPException, status
2
+ from fastapi.security import OAuth2PasswordBearer
3
+ from pydantic import BaseModel, Field
4
+ import httpx, os
5
+
6
+ app = FastAPI(title="Healthcare Inference Gateway")
7
+
8
+ oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
9
+
10
+ class InferenceRequest(BaseModel):
11
+ content_type: str = Field(..., regex="^(image/.*|text/.*|application/json)$")
12
+ payload: dict
13
+
14
+ def verify_token(token: str = Depends(oauth2_scheme)):
15
+ if not token:
16
+ raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token")
17
+ return token
18
+
19
+ @app.get("/health")
20
+ def health(): return {"status":"ok"}
21
+
22
+ @app.post("/infer")
23
+ async def infer(req: InferenceRequest, token: str = Depends(verify_token)):
24
+ # Conditional routing to preprocessor
25
+ normalized = req.payload
26
+ if req.content_type.startswith(("image/","text/")):
27
+ target = os.getenv("PREPROCESSOR_URL", "http://preprocessor-svc")
28
+ async with httpx.AsyncClient(timeout=10) as client:
29
+ resp = await client.post(f"{target}/preprocess", json=req.payload)
30
+ resp.raise_for_status()
31
+ normalized = resp.json()
32
+
33
+ # Forward to Triton (HTTP)
34
+ async with httpx.AsyncClient(timeout=15) as client:
35
+ triton = os.getenv("TRITON_URL", "http://triton-service:8000")
36
+ infer_resp = await client.post(f"{triton}/v2/models/<model>/infer", json=normalized)
37
+ infer_resp.raise_for_status()
38
+ return infer_resp.json()