Commit 8af5b7d4 authored by IT19974156_Nawagamuwa.N.G.K's avatar IT19974156_Nawagamuwa.N.G.K 🥇

main.py

parent f94e16b7
from fastapi import FastAPI, Request, Form
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from uvicorn import run
import numpy as np
import os
import joblib
import json
app = FastAPI()
origins = ["*"]
methods = ["*"]
headers = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins = origins,
allow_credentials = True,
allow_methods = methods,
allow_headers = headers
)
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
def getPredict(topredict):
kn_classifier2 = joblib.load('fishRecModel.pkl' , mmap_mode ='r')
prediction = kn_classifier2.predict((topredict))
return prediction
@app.get("/home",response_class=HTMLResponse)
async def homePage(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.post("/result",response_class=HTMLResponse)
async def resultPage(request: Request, NH3: str= Form(...),NO2: str= Form(...),NO3: str= Form(...),Temprature: str= Form(...),Oxygen: str= Form(...),PH: str= Form(...)):
to_predict_list =np.array([[NH3,NO2,NO3,Temprature,Oxygen,PH]])
prediction_r = getPredict(to_predict_list)
resultlist = prediction_r.tolist()
json_str = json.dumps(resultlist)
outcome = json_str.replace("[", "").replace("]", "")
return templates.TemplateResponse("result.html", {"request": request, "prediction":outcome})
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
run(app, host="0.0.0.0", port=port)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment