Skip to content

Commit

Permalink
added link for predictAPI + integrated model
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhamTiwary914 committed Feb 12, 2024
1 parent 906221b commit 0a97701
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 87 deletions.
4 changes: 2 additions & 2 deletions api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const predictRouter = require('./routes/predict/predict')
app.use(webRouter);
//path[POST]: <host>/esp/
app.use(sensorRouter);
//path[POST]: <host>/predict/
app.use(predictRouter);
//path[POST]: http://127.0.0.1:8023/temp/
app.use(predictRouter)



Expand Down
5 changes: 5 additions & 0 deletions api/db/schema/HourStats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const mongoose = require('mongoose')




34 changes: 0 additions & 34 deletions api/db/schema/WeekStat.js

This file was deleted.

72 changes: 72 additions & 0 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^1.6.7",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.3.2",
Expand Down
Binary file modified api/predictAPI/__pycache__/main.cpython-311.pyc
Binary file not shown.
33 changes: 27 additions & 6 deletions api/predictAPI/main.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import pandas as pd
import joblib
import warnings

warnings.filterwarnings("ignore")
model = joblib.load('./model/new_model.pkl');

app = FastAPI();
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["GET", "POST", "PUT", "DELETE"],
allow_headers=["*"],
)



class ReqBody(BaseModel):
item: str
temp: float

app = FastAPI();



def predictor(foodItem: str, temp: float):
testFd = pd.DataFrame([[foodItem, temp]], columns=["Food Item", "Temp.(in degree C)"])
preds = model.predict(testFd)
return preds[0];


@app.post('/temp/')
async def postReq(request: ReqBody):
model = joblib.load('./model/fsp_model.pkl');
prediction = model.predict([[request.temp]])[0];
print(prediction)

preds = predictor(request.item, request.temp)
return {
"success": True,
"output": ""
"output": preds
}


Expand Down
2 changes: 2 additions & 0 deletions api/predictAPI/model/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Input: [[Food Item: String, Temperature: Float]]
Output: [Prediction(Days): Int]
Binary file added api/predictAPI/model/new_model.pkl
Binary file not shown.
Binary file added api/predictAPI/model/preprocessor.joblib
Binary file not shown.
Binary file added api/predictAPI/model/preprocessor.pkl
Binary file not shown.
Binary file added api/predictAPI/model/tempModel.joblib
Binary file not shown.
12 changes: 7 additions & 5 deletions api/predictAPI/test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import joblib
import warnings
warnings.filterwarnings("ignore")
import pandas as pd



model = joblib.load('./model/fsp_model.pkl');
prediction = model.predict([[2, 23,5]]);
print(prediction)


pred = model.predict(testFd)[0]
print(pred)


Binary file removed api/routes/predict/fsp_model.onnx
Binary file not shown.
41 changes: 9 additions & 32 deletions api/routes/predict/predict.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,16 @@
const express = require('express')
const predictionRouter = express.Router();
const { InferenceSession, Tensor } = require('onnxruntime-node');
const express = require('express')
const predictRouter = express.Router();
const axios = require('axios')

const predictPATH = 'http://127.0.0.1:8023/temp/'


async function predictSpoilage(temp, responder){
// Create a new InferenceSession
const session = new InferenceSession();

// Load the ONNX model
await session.loadModel('./../../model/fsp_model.onnx');

// Prepare input data (replace inputData with your actual input data)
const inputData = new Float32Array([32.65]);

// Create an input Tensor
const inputTensor = new Tensor('float32', inputData, [1, inputData.length]);

// Run the model
const outputMap = await session.run([inputTensor]);

// Get the output tensor
const outputTensor = outputMap.values().next().value;

// Access the predictions
const predictions = outputTensor.data;
console.log(predictions);
}


predictionRouter.post('/predict/', (req,res)=>{
const temp = req.body.temp;
predictSpoilage(temp, res)
predictRouter.post('/temp/', (req,res)=>{
axios.post(predictPATH, req.body).then(result=>{
res.json(result.data)
})
})



module.exports = predictionRouter;
module.exports = predictRouter
3 changes: 3 additions & 0 deletions api/server.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off
start cmd /k "cd predictAPI && uvicorn main:app --reload --port 8023"
npm run server
2 changes: 2 additions & 0 deletions model/v2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Input: [[Food Item: String, Temperature: Float]]
Output: [Prediction(Days): Int]
Binary file added model/v2/tempModel.pkl
Binary file not shown.
9 changes: 1 addition & 8 deletions web/src/connect/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,7 @@ export default class Requests{
}


static async fetchDevice_currWeek(callback){

}


static async fetchDevice_currMonth(callback){

}



}
Expand Down

0 comments on commit 0a97701

Please sign in to comment.