Spaces:
Sleeping
Sleeping
Update slapp.py
Browse files
slapp.py
CHANGED
|
@@ -1,18 +1,26 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
|
|
|
|
|
|
| 3 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Load the saved model
|
| 6 |
def load_model():
|
| 7 |
try:
|
| 8 |
-
|
|
|
|
| 9 |
return model
|
| 10 |
except Exception as e:
|
| 11 |
raise RuntimeError(f"Error loading model: {e}")
|
| 12 |
|
| 13 |
model = load_model()
|
| 14 |
-
app = FastAPI()
|
| 15 |
|
|
|
|
| 16 |
@app.post("/predict")
|
| 17 |
async def predict_transaction(data: dict):
|
| 18 |
try:
|
|
@@ -24,3 +32,69 @@ async def predict_transaction(data: dict):
|
|
| 24 |
return {"prediction": result}
|
| 25 |
except Exception as e:
|
| 26 |
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import threading
|
| 2 |
+
import uvicorn
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import requests
|
| 5 |
import pandas as pd
|
| 6 |
+
import pickle
|
| 7 |
+
from fastapi import FastAPI
|
| 8 |
+
|
| 9 |
+
# Initialize FastAPI app
|
| 10 |
+
app = FastAPI()
|
| 11 |
|
| 12 |
# Load the saved model
|
| 13 |
def load_model():
|
| 14 |
try:
|
| 15 |
+
with open('model.pkl', 'rb') as file:
|
| 16 |
+
model = pickle.load(file)
|
| 17 |
return model
|
| 18 |
except Exception as e:
|
| 19 |
raise RuntimeError(f"Error loading model: {e}")
|
| 20 |
|
| 21 |
model = load_model()
|
|
|
|
| 22 |
|
| 23 |
+
# Define the FastAPI endpoint
|
| 24 |
@app.post("/predict")
|
| 25 |
async def predict_transaction(data: dict):
|
| 26 |
try:
|
|
|
|
| 32 |
return {"prediction": result}
|
| 33 |
except Exception as e:
|
| 34 |
return {"error": str(e)}
|
| 35 |
+
|
| 36 |
+
# Function to run the FastAPI server
|
| 37 |
+
def run_fastapi():
|
| 38 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 39 |
+
|
| 40 |
+
# Start the FastAPI server in a separate thread
|
| 41 |
+
thread = threading.Thread(target=run_fastapi, daemon=True)
|
| 42 |
+
thread.start()
|
| 43 |
+
|
| 44 |
+
# Streamlit app interface
|
| 45 |
+
st.title("Fraud Detection API")
|
| 46 |
+
st.markdown("Welcome to the Fraud Detection API! Please enter the transaction details below:")
|
| 47 |
+
|
| 48 |
+
# Tabs for input sections
|
| 49 |
+
tab1, tab2, tab3 = st.tabs(["Basic Info", "Features (V1 - V14)", "Features (V15 - V28)"])
|
| 50 |
+
|
| 51 |
+
# Horizontal layout for Basic Info
|
| 52 |
+
with tab1:
|
| 53 |
+
st.header("Basic Information")
|
| 54 |
+
col1, col2 = st.columns(2)
|
| 55 |
+
with col1:
|
| 56 |
+
time = st.number_input("Time", min_value=0.0, step=0.1)
|
| 57 |
+
with col2:
|
| 58 |
+
amount = st.number_input("Amount", min_value=0.0, step=0.1)
|
| 59 |
+
|
| 60 |
+
# Horizontal layout for Features V1 - V14
|
| 61 |
+
with tab2:
|
| 62 |
+
st.header("Features (V1 - V14)")
|
| 63 |
+
cols = st.columns(7)
|
| 64 |
+
v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14 = [
|
| 65 |
+
st.number_input(f"V{i+1}", step=0.01) for i in range(14)
|
| 66 |
+
]
|
| 67 |
+
|
| 68 |
+
# Horizontal layout for Features V15 - V28
|
| 69 |
+
with tab3:
|
| 70 |
+
st.header("Features (V15 - V28)")
|
| 71 |
+
cols = st.columns(7)
|
| 72 |
+
v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28 = [
|
| 73 |
+
st.number_input(f"V{i+15}", step=0.01) for i in range(14)
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
# Button to make predictions
|
| 77 |
+
if st.button("Predict"):
|
| 78 |
+
# Create a dictionary from the input data
|
| 79 |
+
transaction_data = {
|
| 80 |
+
'Time': time,
|
| 81 |
+
'V1': v1, 'V2': v2, 'V3': v3, 'V4': v4, 'V5': v5, 'V6': v6,
|
| 82 |
+
'V7': v7, 'V8': v8, 'V9': v9, 'V10': v10, 'V11': v11, 'V12': v12,
|
| 83 |
+
'V13': v13, 'V14': v14, 'V15': v15, 'V16': v16, 'V17': v17, 'V18': v18,
|
| 84 |
+
'V19': v19, 'V20': v20, 'V21': v21, 'V22': v22, 'V23': v23, 'V24': v24,
|
| 85 |
+
'V25': v25, 'V26': v26, 'V27': v27, 'V28': v28, 'Amount': amount
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
# Send a POST request to the FastAPI server
|
| 89 |
+
try:
|
| 90 |
+
response = requests.post("http://localhost:8000/predict", json=transaction_data)
|
| 91 |
+
if response.status_code == 200:
|
| 92 |
+
result = response.json().get("prediction")
|
| 93 |
+
if result == "Acceptable transaction":
|
| 94 |
+
st.success("✅ " + result)
|
| 95 |
+
else:
|
| 96 |
+
st.error("🚨 " + result)
|
| 97 |
+
else:
|
| 98 |
+
st.error("Error: " + response.text)
|
| 99 |
+
except Exception as e:
|
| 100 |
+
st.error(f"Error connecting to API: {e}")
|