Newer
Older

Christopher Randolph Rhodes
committed
from typing import Dict
from fastapi import FastAPI, HTTPException

Christopher Randolph Rhodes
committed
from model_server.session import CouldNotFindModelError, Session
from model_server.workflow import infer_image_to_image
app = FastAPI(debug=True)
session = Session()
@app.on_event("startup")
def startup():
pass
@app.get('/')
def read_root():
@app.get('/models')
def list_active_models():

Christopher Randolph Rhodes
committed
return session.describe_loaded_models()

Christopher Randolph Rhodes
committed
@app.put('/models/load/')

Christopher Randolph Rhodes
committed
# def load_model(model_id: str, misc: Dict[str, str]) -> dict:
def load_model(model_id: str, misc: dict) -> dict:
if model_id in session.models.keys():
raise HTTPException(
status_code=409,
detail=f'Model with id {model_id} has already been loaded'
)

Christopher Randolph Rhodes
committed
try:
session.load_model(model_id, params=misc)
except CouldNotFindModelError:
raise HTTPException(
status_code=404,
detail=f'Could not find {model_id} in defined models'
)

Christopher Randolph Rhodes
committed
return session.describe_loaded_models()
@app.put('/i2i/infer/')
def infer_img(model_id: str, input_filename: str, channel: int = None) -> dict:

Christopher Randolph Rhodes
committed
if model_id not in session.describe_loaded_models().keys():
raise HTTPException(
status_code=409,
detail=f'Model {model_id} has not been loaded'
)
inpath = session.inbound.path / input_filename
if not inpath.exists():
raise HTTPException(
status_code=404,
detail=f'Could not find file:\n{inpath}'
)

Christopher Randolph Rhodes
committed
record = infer_image_to_image(

Christopher Randolph Rhodes
committed
session.models[model_id]['object'],
session.outbound.path,
channel=channel,
)

Christopher Randolph Rhodes
committed
session.record_workflow_run(record)
return record