LazyBoss commited on
Commit
10a9072
·
verified ·
1 Parent(s): c949ec8

Update slapp.py

Browse files
Files changed (1) hide show
  1. slapp.py +33 -53
slapp.py CHANGED
@@ -1,60 +1,40 @@
1
- import streamlit as st
2
- import requests
 
 
 
3
 
4
- # Streamlit app interface
5
- st.title("Fraud Detection API")
6
- st.markdown("Welcome to the Fraud Detection API! Please enter the transaction details below:")
7
 
8
- # Tabs for input sections
9
- tab1, tab2, tab3 = st.tabs(["Basic Info", "Features (V1 - V14)", "Features (V15 - V28)"])
 
 
 
 
 
 
10
 
11
- # Horizontal layout for Basic Info
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
- # Horizontal layout for Features V1 - V14
21
- with tab2:
22
- st.header("Features (V1 - V14)")
23
- cols = st.columns(7)
24
- v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14 = [
25
- st.number_input(f"V{i+1}", step=0.01) for i in range(14)
26
- ]
27
 
28
- # Horizontal layout for Features V15 - V28
29
- with tab3:
30
- st.header("Features (V15 - V28)")
31
- cols = st.columns(7)
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
- # Button to make predictions
37
- if st.button("Predict"):
38
- # Create a dictionary from the input data
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
- # Send a POST request to the FastAPI server
49
- try:
50
- response = requests.post("http://localhost:8000/predict", json=transaction_data)
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()