-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
48 lines (37 loc) · 1.4 KB
/
app.py
File metadata and controls
48 lines (37 loc) · 1.4 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
from fastapi import FastAPI
import pandas as pd
import joblib
import uvicorn
# Initialize FastAPI app
app = FastAPI()
# Load dataset (Assuming 'data/smart_meter_grid_south_africa.csv' exists)
df = pd.read_csv("data/smart_meter_grid_south_africa.csv")
# Ensure 'Timestamp' column is correctly formatted
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
# Extract time-based features
df['Hour'] = df['Timestamp'].dt.hour
df['Day'] = df['Timestamp'].dt.day
df['Month'] = df['Timestamp'].dt.month
df['DayOfWeek'] = df['Timestamp'].dt.dayofweek
# Load trained fraud detection model
model = joblib.load("fraud_detection_model.pkl") # Ensure this file exists
# Define features used in the model
features = [
'Hour', 'Day', 'Month', 'DayOfWeek',
'Energy Consumption (kWh)', 'Voltage (kV)',
'Frequency (Hz)', 'Power Factor'
]
# Function to detect fraudulent smart meters
def detect_fraud(df):
df['Fraud_Prediction'] = model.predict(df[features])
# Filter high-risk (fraudulent) meters
fraudsters = df[df['Fraud_Prediction'] == 1]
return fraudsters[['Meter ID', 'Province', 'City', 'Latitude', 'Longitude']].to_dict(orient='records')
# API Endpoint: Return list of fraudulent smart meters
@app.get("/fraudsters")
def get_fraudsters():
fraud_list = detect_fraud(df)
return {"fraudsters": fraud_list}
# Run API Server
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)