Spaces:
Sleeping
Sleeping
Download app.py from Divb30/network-intrusion-detection: direct link, hf CLI and curl.
- Browser
- Download file 2.71 kB
-
https://huggingface.co/spaces/Divb30/network-intrusion-detection/resolve/main/app.py
- Command line
-
hf download hf://spaces/Divb30/network-intrusion-detection/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Divb30/network-intrusion-detection/resolve/main/app.py
2.71 kB
| import sys | |
| import os | |
| import certifi | |
| ca = certifi.where() | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| mongo_db_url = os.getenv("MONGODB_URL_KEY") | |
| print(mongo_db_url) | |
| import pymongo | |
| from networksecurity.exception.exception import NetworkSecurityException | |
| from networksecurity.logging.logger import logging | |
| from networksecurity.pipeline.training_pipeline import TrainingPipeline | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi import FastAPI, File, UploadFile,Request | |
| 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 | |
| from networksecurity.utils.ml_utils.model.estimator import NetworkModel | |
| 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 | |
| templates = Jinja2Templates(directory="./templates") | |
| async def index(): | |
| return RedirectResponse(url="/docs") | |
| async def train_route(): | |
| try: | |
| train_pipeline=TrainingPipeline() | |
| train_pipeline.run_pipeline() | |
| return Response("Training is successful") | |
| except Exception as e: | |
| raise NetworkSecurityException(e,sys) | |
| async def predict_route(request: Request, file: UploadFile = File(...)): | |
| 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) | |
| y_pred = network_model.predict(df) | |
| df['predicted_column'] = y_pred | |
| df['predicted_column'] = df['predicted_column'].replace(-1, 0) | |
| os.makedirs("prediction_output", exist_ok=True) | |
| df.to_csv("prediction_output/output.csv", index=False) | |
| table_html = df.to_html(classes="table table-striped") | |
| return templates.TemplateResponse("table.html", { | |
| "request": request, | |
| "table": table_html | |
| }) | |
| except Exception as e: | |
| raise NetworkSecurityException(e, sys) | |
| if __name__=="__main__": | |
| app_run(app,host="0.0.0.0",port=8000) | |