Skip to content

Commit

Permalink
model for deep learning and tailwind css modified
Browse files Browse the repository at this point in the history
  • Loading branch information
tedoaba committed Oct 26, 2024
1 parent 891cf47 commit c6f73ca
Show file tree
Hide file tree
Showing 23 changed files with 479 additions and 385 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ mlruns
mlartifacts/
mlruns/

node_modules/


# Distribution / packaging
.Python
Expand Down
43 changes: 12 additions & 31 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
# Use official Python image as the base image
# Use the official Python image as a base image
FROM python:3.10

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV FLASK_APP=app

# Copy and install system dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpq-dev \
curl \
&& rm -rf /var/lib/apt/lists/*

# Install Node.js and npm for TailwindCSS
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
apt-get install -y nodejs && \
apt-get clean && rm -rf /var/lib/apt/lists/*

# Set the working directory
# Set the working directory in the container
WORKDIR /app

# Copy project dependencies first to improve caching
# Copy the requirements file into the container
COPY requirements.txt .

RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir -r requirements.txt
# Copy Node dependencies and install them
COPY app/package.json .
RUN npm install
# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the project files
COPY app/. .
# Copy the entire application into the container
COPY . .

# Build CSS with Tailwind
RUN npm run create-css
# Set environment variables
ENV FLASK_APP=app
ENV FLASK_ENV=development

# Expose port 5000 for the Flask app
# Expose the port the app runs on
EXPOSE 5000

# Run the Flask app
# Command to run the application
CMD ["flask", "run", "--host=0.0.0.0"]
4 changes: 2 additions & 2 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from dotenv import load_dotenv
from flask import Flask

from app import pages, transactions, data, predict
from app import pages, transactions, data, xgb_model
from app.models import db
from app.database import init_db_command
from app.dashboard import (
Expand Down Expand Up @@ -35,7 +35,7 @@ def create_app():
app.register_blueprint(pages.bp)
app.register_blueprint(transactions.bp)
app.register_blueprint(data.bp)
app.register_blueprint(predict.bp)
app.register_blueprint(xgb_model.bp)

# Initialize Dash apps and pass the `db` object for querying the database
create_summary_dash_app(app, db)
Expand Down
4 changes: 2 additions & 2 deletions app/dashboard.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pandas as pd
import plotly.express as px
from dash import Dash, dcc, html, Input, Output
from sqlalchemy import create_engine, func
from sqlalchemy import func
from app.models import Features

def create_summary_dash_app(flask_app, db):
Expand Down Expand Up @@ -101,7 +101,7 @@ def update_device_browser_analysis(_):
)
device_browser_df = pd.DataFrame(device_browser_data, columns=['device_id', 'browser', 'fraud_cases'])

fig = px.bar(device_browser_df, x='device_id', y='fraud_cases', color='browser', title='Fraud by Device and Browser')
fig = px.bar(device_browser_df, x='fraud_cases', y='device_id', color='browser', title='Fraud by Device and Browser')
return fig

return dash_app
73 changes: 1 addition & 72 deletions app/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,13 @@
import pandas as pd
import matplotlib.pyplot as plt
from dotenv import load_dotenv
from flask import Blueprint, render_template, jsonify
from flask import Blueprint, render_template
from sqlalchemy import create_engine
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
from sklearn.preprocessing import LabelEncoder
from imblearn.over_sampling import SMOTE
from xgboost import XGBClassifier
from sklearn import preprocessing
import mlflow
from mlflow import pyfunc
import xgboost as xgb
from dash import Dash, dcc, html, Input, Output
from flask import Blueprint
from dash import Dash
import dash_core_components as dcc
import dash_html_components as html

load_dotenv()

bp = Blueprint("data", __name__)

# Load the model
model = xgb.Booster()
model = model.load_model("model.xgb")

@bp.route("/data")
def data():
# Retrieve database URL from environment variables
Expand Down Expand Up @@ -59,56 +41,3 @@ def data():
features = df.to_dict(orient='records')

return render_template('data/data.html', features=features)

@bp.route('/result')
def result():
database_url = os.getenv('DATABASE_URL')
if not database_url:
raise RuntimeError("DATABASE_URL environment variable not set.")

engine = create_engine(database_url)

# Fetch data from database
data = pd.read_sql("SELECT * FROM transactions ORDER BY ctid DESC LIMIT 1", engine)

ID = data['user_id']

df = data

# Initialize a LabelEncoder object
label_encoder = LabelEncoder()

# Iterate through each column in the dataframe
for column in df.columns:
# Check if the column datatype is not numeric
if df[column].dtype not in ['int64', 'float64']:
# Fit label encoder and transform values
df[column] = label_encoder.fit_transform(df[column])

# Get the numeric columns
numeric_columns = df.select_dtypes(include=['number']).columns

# Create the StandardScaler
transform = preprocessing.StandardScaler()

# Fit the scaler on the data (calculate mean and standard deviation)
transform.fit(df[numeric_columns])

# Transform the data using the fitted transform and reassign it to X
df[numeric_columns] = transform.transform(df[numeric_columns])

df = xgb.DMatrix(df)

prediction = None

if model is None:
print("Model is not loaded. Please check the loading process.")
else:
prediction = model.predict(df)


# Make prediction
#prediction = model.predict(df)
print("Prediction: ", prediction)

return render_template('data/result.html', prediction=prediction, ID=ID)
Binary file removed app/model.xgb
Binary file not shown.
2 changes: 1 addition & 1 deletion app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Features(db.Model):
sex = db.Column(db.String)
age = db.Column(db.Integer)
ip_address = db.Column(db.String)
class_ = db.Column('class', db.Integer) # Fraud class: 0 = non-fraud, 1 = fraud
class_ = db.Column('class', db.Integer)
lower_bound_ip_address = db.Column(db.BigInteger)
upper_bound_ip_address = db.Column(db.BigInteger)
country = db.Column(db.String)
36 changes: 0 additions & 36 deletions app/predict.py

This file was deleted.

44 changes: 31 additions & 13 deletions app/schema.sql
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
DROP TABLE IF EXISTS transactions;

CREATE TABLE transactions (
user_id INT PRIMARY KEY, -- user_id as the unique identifier
signup_time TIMESTAMP NOT NULL, -- Signup time as timestamp
purchase_time TIMESTAMP NOT NULL, -- Purchase time as timestamp
purchase_value FLOAT NOT NULL, -- Purchase value as a float
device_id VARCHAR NOT NULL, -- Device ID as a string
source VARCHAR NOT NULL, -- Source as a string (e.g., app, web)
browser VARCHAR NOT NULL, -- Browser as a string
sex VARCHAR NOT NULL, -- Sex as a string
age INT NOT NULL, -- Age as integer
ip_address FLOAT NOT NULL, -- IP address as a float
lower_bound_ip_address FLOAT NOT NULL, -- Lower bound of IP address
upper_bound_ip_address FLOAT NOT NULL, -- Upper bound of IP address
country VARCHAR NOT NULL -- Country as a string
user_id INT PRIMARY KEY,
signup_time TIMESTAMP NOT NULL,
purchase_time TIMESTAMP NOT NULL,
purchase_value FLOAT NOT NULL,
device_id VARCHAR NOT NULL,
source VARCHAR NOT NULL,
browser VARCHAR NOT NULL,
sex VARCHAR NOT NULL,
age INT NOT NULL,
ip_address FLOAT NOT NULL,
lower_bound_ip_address FLOAT NOT NULL,
upper_bound_ip_address FLOAT NOT NULL,
country VARCHAR NOT NULL

DROP TABLE IF EXISTS features;

CREATE TABLE features (
user_id SERIAL PRIMARY KEY,
signup_time TIMESTAMP,
purchase_time TIMESTAMP,
purchase_value FLOAT,
device_id VARCHAR,
source VARCHAR,
browser VARCHAR,
sex VARCHAR,
age INTEGER,
ip_address VARCHAR,
class INTEGER,
lower_bound_ip_address BIGINT,
upper_bound_ip_address BIGINT,
country VARCHAR
);
Loading

0 comments on commit c6f73ca

Please sign in to comment.