import shutil import glob import os import numpy as np import pandas as pd from fastapi import FastAPI,UploadFile,File from pydantic import BaseModel,Field from app.modelling import train from app.inference import predict dataset=None trained_model=None encoder=None transform=None class Item(BaseModel): Torque:float=Field(gt=0,default=24.25) Hydraulic_Pressure:float=Field(gt=0,default=121.86) Cutting:float=Field(gt=0,default=2.89) Coolant_Pressure:float=Field(gt=0,default=6.96) Spindle_Speed:float=Field(gt=0,default=20504.0) Coolant_Temperature:float=Field(gt=0,default=14.9) app=FastAPI() @app.get("/") def home(): return {"message":"Hello World!"} @app.post("/upload/") def upload_csv(file:UploadFile=File(...)): global dataset dataset=pd.read_csv(file.file) file.file.close() return {"filename": file.filename} @app.post("/train/") def training(): if dataset is not None: results=train(dataset) global trained_model trained_model=results["model"] global encoder encoder=results["encoder"] global transform transform=results["transform"] return {"Accuracy":results["Accuracy"], "F1_Score":results["F1_Score"]} else: return {"message":"First Upload Dataset"} @app.post("/predict/") def prediction(item:Item): if trained_model is not None: arr=[[item.Torque,item.Hydraulic_Pressure,item.Cutting,item.Coolant_Pressure,item.Spindle_Speed,item.Coolant_Temperature]] results=predict(trained_model,encoder,transform,arr) return results else: return {"message":"First Train Model"}