first commit

This commit is contained in:
heyethereum
2024-08-13 02:03:37 +08:00
commit 9284abbae2
16 changed files with 1357274 additions and 0 deletions

23
app/dockerfile Normal file
View File

@@ -0,0 +1,23 @@
# Use an official Python runtime as a parent image
FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /app
# Copy the requirements file into the container at /app
COPY requirements.txt .
# Install any dependencies specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Copy the model file into the container
COPY random_forest_model.pkl /app/
# Copy the rest of the working directory contents into the container at /app
COPY . .
# Expose the port the app runs on
EXPOSE 8000
# Run the FastAPI application using Uvicorn
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]

66
app/main.py Normal file
View File

@@ -0,0 +1,66 @@
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
import pandas as pd
# Initialize the FastAPI app
app = FastAPI()
# Load the trained model
model = joblib.load('random_forest_model.pkl')
# Define the input data structure using Pydantic
class InputData(BaseModel):
domain: int
subdomain: int
top_level_domain: int
query: int
fragment: int
redirect: int
path: int
redirect_chain: int
hsts_header: int
ssl_stripping: int
hostname_embedding: int
javascript_check: int
shortening_service: int
has_ip_address: int
tracking_descriptions: int
url_encoding: int
has_executable: int
tls: int
contents: int
# Define a mapping from numerical predictions to class labels
class_mapping = {
0: "Benign",
1: "Defacement",
2: "Malware",
3: "Phishing"
}
# Define a prediction endpoint
@app.post("/predict")
def predict(data: InputData):
# Convert input data to a dictionary and wrap it in a list
input_data = data.dict()
input_df = pd.DataFrame([input_data], columns=[
'domain', 'subdomain', 'top_level_domain', 'query',
'fragment', 'redirect', 'path', 'redirect_chain',
'hsts_header', 'ssl_stripping', 'hostname_embedding',
'javascript_check', 'shortening_service', 'has_ip_address',
'tracking_descriptions', 'url_encoding', 'has_executable',
'tls', 'contents'
])
# Make a prediction using the loaded model
prediction = model.predict(input_df)[0]
# Map the prediction to the class label
prediction_label = class_mapping.get(prediction, "Unknown")
# Return the class label as the prediction
return prediction_label
# Running the FastAPI app
# uvicorn main:app --reload (Use this command to run the FastAPI app)

BIN
app/random_forest_model.pkl Normal file

Binary file not shown.

5
app/requirements.txt Normal file
View File

@@ -0,0 +1,5 @@
fastapi==0.112.0
uvicorn==0.30.5
pandas==2.1.3
scikit-learn==1.3.2
joblib==1.4.2