-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
107 lines (93 loc) · 3.74 KB
/
app.py
File metadata and controls
107 lines (93 loc) · 3.74 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Used to trigger the entire pipeline
import sys
import os
import certifi
ca=certifi.where()
from dotenv import load_dotenv
load_dotenv()
mongo_db_url=os.getenv("MONGO_DB_URL")
print(mongo_db_url)
import pymongo
from networksecurity.exception.exception import NetworkSecurtiyException
from networksecurity.logging.logger import logging
from networksecurity.pipeline.training_pipeline import TrainingPipeline
from networksecurity.utils.ml_utils.model.estimator import NetworkModel
"""
FastAPI is a modern, fast (high-performance) web framework for
building APIs with Python.
"""
from fastapi.middleware.cors import CORSMiddleware
from fastapi import FastAPI,File,UploadFile,Request
"""
FastAPI -->The main class used to create the API app
CORSMiddleware --> To handle Cross-Origin Resource Sharing (CORS) issues
File, UploadFile --> To handle file uploads
Request --> To handle incoming HTTP requests
"""
from uvicorn import run as app_run
from fastapi.responses import Response
from starlette.responses import RedirectResponse
import pandas as pd
from networksecurity.utils.main_utils.utils import load_object
client=pymongo.MongoClient(mongo_db_url,tlsCAFile=ca)
from networksecurity.constant.training_pipeline import DATA_INGESTION_COLLECTION_NAME
from networksecurity.constant.training_pipeline import DATA_INGESTION_DATABASE_NAME
database=client[DATA_INGESTION_DATABASE_NAME]
collection=database[DATA_INGESTION_COLLECTION_NAME]
app=FastAPI()
origins=["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
from fastapi.templating import Jinja2Templates
# Jinja2Templates is used to render HTML templates using the
# Jinja2 templating engine.
templates=Jinja2Templates(directory="./templates")
@app.get("/",tags=["authentication"])
# It groups this endpoint under the tag Authentication in the API docs.
async def index():
return RedirectResponse(url="/docs")
# When someone hits the / endpoint, instead of showing something, it redirects them to /docs.
@app.get("/train")
async def train_route():
try:
train_pipeline=TrainingPipeline()
train_pipeline.run_pipeline()
return Response("Training successful!!")
except Exception as e:
raise NetworkSecurtiyException(e,sys)
if __name__=="__main__":
app.run(app,host="localhost",port=8000)
@app.post("/predict")
async def predict_route(request:Request,file:UploadFile=File(...)):
# File(...) indicates that this parameter is required.
try:
df=pd.read_csv(file.file)
preprocessor=load_object("final_model/preprocessor.pkl")
final_model=load_object("final_model/model.pkl")
network_model=NetworkModel(preprocessor=preprocessor,model=final_model)
print(df.iloc[0])
y_pred=network_model.predict(df)
print(y_pred)
df["predicted_column"]=y_pred
print(df['predicted_column'])
df.to_csv("prediction_output/output.csv")
# Saves the DataFrame with predictions to a CSV file.
# We can also update this csv to mongodb if needed
table_html=df.to_html(classes="table table-striped")
# Converts the DataFrame to an HTML table with Bootstrap classes for styling.
return templates.TemplateResponse("table.html",{"request":request,"table":table_html})
# Renders the table.html template, passing the request and the generated HTML table.
except Exception as e:
raise NetworkSecurtiyException(e,sys)
"""
uvicorn app:app --reload
The command to run the FastAPI application using Uvicorn.
--reload flag enables auto-reloading of the server on code changes.
Uvicorn is an ASGI server that runs FastAPI applications.
app:app specifies the module (app.py) and the FastAPI instance (app) to run.
"""