Spaces:
Sleeping
Sleeping
Update slapp.py
Browse files
slapp.py
CHANGED
|
@@ -1,60 +1,40 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
st.markdown("Welcome to the Fraud Detection API! Please enter the transaction details below:")
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
with tab1:
|
| 13 |
-
st.header("Basic Information")
|
| 14 |
-
col1, col2 = st.columns(2)
|
| 15 |
-
with col1:
|
| 16 |
-
time = st.number_input("Time", min_value=0.0, step=0.1)
|
| 17 |
-
with col2:
|
| 18 |
-
amount = st.number_input("Amount", min_value=0.0, step=0.1)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28 = [
|
| 33 |
-
st.number_input(f"V{i+15}", step=0.01) for i in range(14)
|
| 34 |
-
]
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
transaction_data = {
|
| 40 |
-
'Time': time,
|
| 41 |
-
'V1': v1, 'V2': v2, 'V3': v3, 'V4': v4, 'V5': v5, 'V6': v6,
|
| 42 |
-
'V7': v7, 'V8': v8, 'V9': v9, 'V10': v10, 'V11': v11, 'V12': v12,
|
| 43 |
-
'V13': v13, 'V14': v14, 'V15': v15, 'V16': v16, 'V17': v17, 'V18': v18,
|
| 44 |
-
'V19': v19, 'V20': v20, 'V21': v21, 'V22': v22, 'V23': v23, 'V24': v24,
|
| 45 |
-
'V25': v25, 'V26': v26, 'V27': v27, 'V28': v28, 'Amount': amount
|
| 46 |
-
}
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
if response.status_code == 200:
|
| 52 |
-
result = response.json().get("prediction")
|
| 53 |
-
if result == "Acceptable transaction":
|
| 54 |
-
st.success("✅ " + result)
|
| 55 |
-
else:
|
| 56 |
-
st.error("🚨 " + result)
|
| 57 |
-
else:
|
| 58 |
-
st.error("Error: " + response.text)
|
| 59 |
-
except Exception as e:
|
| 60 |
-
st.error(f"Error connecting to API: {e}")
|
|
|
|
| 1 |
+
import threading
|
| 2 |
+
import uvicorn
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import pickle
|
| 5 |
+
from fastapi import FastAPI
|
| 6 |
|
| 7 |
+
# Initialize FastAPI app
|
| 8 |
+
app = FastAPI()
|
|
|
|
| 9 |
|
| 10 |
+
# Load the saved model
|
| 11 |
+
def load_model():
|
| 12 |
+
try:
|
| 13 |
+
with open('model.pkl', 'rb') as file:
|
| 14 |
+
model = pickle.load(file)
|
| 15 |
+
return model
|
| 16 |
+
except Exception as e:
|
| 17 |
+
raise RuntimeError(f"Error loading model: {e}")
|
| 18 |
|
| 19 |
+
model = load_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Define the FastAPI endpoint
|
| 22 |
+
@app.post("/predict")
|
| 23 |
+
async def predict_transaction(data: dict):
|
| 24 |
+
try:
|
| 25 |
+
# Convert the input data to a DataFrame
|
| 26 |
+
transaction_data = pd.DataFrame([data])
|
| 27 |
+
prediction = model.predict(transaction_data)
|
| 28 |
|
| 29 |
+
result = "Fraudulent transaction" if prediction[0] == 1 else "Acceptable transaction"
|
| 30 |
+
return {"prediction": result}
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Function to run the FastAPI server
|
| 35 |
+
def run_fastapi():
|
| 36 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Start the FastAPI server in a separate thread
|
| 39 |
+
thread = threading.Thread(target=run_fastapi, daemon=True)
|
| 40 |
+
thread.start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|