-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
57 lines (46 loc) · 1.87 KB
/
Copy pathmain.py
File metadata and controls
57 lines (46 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
FastAPI Server Entrypoint
Demonstrates modern AI engineering serving integrations specifically requested in the Tredence JD (Async/FastAPI/Deployment).
"""
import logging
import torch
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from src.model import SelfPruningNet
from src.config import config
logger = logging.getLogger("TredenceServer")
app = FastAPI(
title="Self-Pruning Network API",
description="Web inference layer for the dynamic prunable PyTorch mechanism developed for the Tredence case study.",
version="1.0.0"
)
# Initialize global architecture (In reality, we load the trained .pth state_dict here)
ml_backend = SelfPruningNet()
ml_backend.eval()
class InferencePayload(BaseModel):
image_tensor: list[float]
@app.get("/health")
async def health_check():
"""Confirms traffic functionality."""
return {"status": "up", "framework": "PyTorch + FastAPI"}
@app.post("/api/v1/predict")
async def execute_prediction(payload: InferencePayload):
"""Executes a single structural forward pass asynchronously."""
if len(payload.image_tensor) != config.input_size:
raise HTTPException(
status_code=400,
detail=f"Mismatched context window: Expected inputs length of {config.input_size}"
)
tensor_input = torch.tensor(payload.image_tensor, dtype=torch.float32).unsqueeze(0)
try:
with torch.no_grad():
raw_logits = ml_backend(tensor_input)
prob_matrix = torch.softmax(raw_logits, dim=1)
prediction = torch.argmax(prob_matrix, dim=1).item()
return {
"prediction_class": prediction,
"confidence_scores": prob_matrix.tolist()[0]
}
except Exception as e:
logger.error(f"Inference failure: {e}")
raise HTTPException(status_code=500, detail="Matrix transform error!")