LazyBoss commited on
Commit
07eb06b
·
verified ·
1 Parent(s): 098b147

Update slapp.py

Browse files
Files changed (1) hide show
  1. slapp.py +12 -96
slapp.py CHANGED
@@ -1,110 +1,26 @@
1
- import streamlit as st
2
  import pickle
3
  import pandas as pd
4
 
5
  # Load the saved model
6
- @st.cache_resource # Cache the model loading to avoid reloading on each run
7
  def load_model():
8
  try:
9
  model = pickle.load(open('model.pkl', 'rb'))
10
  return model
11
  except Exception as e:
12
- st.error(f"Error loading model: {e}")
13
- return None
14
 
15
  model = load_model()
 
16
 
17
- # Title of the app
18
- st.title("Fraud Detection API")
19
- st.markdown("Welcome to the Fraud Detection API! Please enter the transaction details below:")
20
-
21
- # Tabs for input sections
22
- tab1, tab2, tab3 = st.tabs(["Basic Info", "Features (V1 - V14)", "Features (V15 - V28)"])
23
-
24
- # Horizontal layout for Basic Info
25
- with tab1:
26
- st.header("Basic Information")
27
- col1, col2 = st.columns(2)
28
- with col1:
29
- time = st.number_input("Time", min_value=0.0, step=0.1)
30
- with col2:
31
- amount = st.number_input("Amount", min_value=0.0, step=0.1)
32
-
33
- # Horizontal layout for Features V1 - V14
34
- with tab2:
35
- st.header("Features (V1 - V14)")
36
- cols = st.columns(7)
37
- with cols[0]:
38
- v1 = st.number_input("V1", step=0.01)
39
- v2 = st.number_input("V2", step=0.01)
40
- with cols[1]:
41
- v3 = st.number_input("V3", step=0.01)
42
- v4 = st.number_input("V4", step=0.01)
43
- with cols[2]:
44
- v5 = st.number_input("V5", step=0.01)
45
- v6 = st.number_input("V6", step=0.01)
46
- with cols[3]:
47
- v7 = st.number_input("V7", step=0.01)
48
- v8 = st.number_input("V8", step=0.01)
49
- with cols[4]:
50
- v9 = st.number_input("V9", step=0.01)
51
- v10 = st.number_input("V10", step=0.01)
52
- with cols[5]:
53
- v11 = st.number_input("V11", step=0.01)
54
- v12 = st.number_input("V12", step=0.01)
55
- with cols[6]:
56
- v13 = st.number_input("V13", step=0.01)
57
- v14 = st.number_input("V14", step=0.01)
58
-
59
- # Horizontal layout for Features V15 - V28
60
- with tab3:
61
- st.header("Features (V15 - V28)")
62
- cols = st.columns(7)
63
- with cols[0]:
64
- v15 = st.number_input("V15", step=0.01)
65
- v16 = st.number_input("V16", step=0.01)
66
- with cols[1]:
67
- v17 = st.number_input("V17", step=0.01)
68
- v18 = st.number_input("V18", step=0.01)
69
- with cols[2]:
70
- v19 = st.number_input("V19", step=0.01)
71
- v20 = st.number_input("V20", step=0.01)
72
- with cols[3]:
73
- v21 = st.number_input("V21", step=0.01)
74
- v22 = st.number_input("V22", step=0.01)
75
- with cols[4]:
76
- v23 = st.number_input("V23", step=0.01)
77
- v24 = st.number_input("V24", step=0.01)
78
- with cols[5]:
79
- v25 = st.number_input("V25", step=0.01)
80
- v26 = st.number_input("V26", step=0.01)
81
- with cols[6]:
82
- v27 = st.number_input("V27", step=0.01)
83
- v28 = st.number_input("V28", step=0.01)
84
-
85
- # Button to make predictions
86
- if st.button("Predict"):
87
- if model:
88
- # Create a DataFrame from the input data
89
- transaction_data = pd.DataFrame({
90
- 'Time': [time],
91
- 'V1': [v1], 'V2': [v2], 'V3': [v3], 'V4': [v4], 'V5': [v5], 'V6': [v6],
92
- 'V7': [v7], 'V8': [v8], 'V9': [v9], 'V10': [v10], 'V11': [v11], 'V12': [v12],
93
- 'V13': [v13], 'V14': [v14], 'V15': [v15], 'V16': [v16], 'V17': [v17], 'V18': [v18],
94
- 'V19': [v19], 'V20': [v20], 'V21': [v21], 'V22': [v22], 'V23': [v23], 'V24': [v24],
95
- 'V25': [v25], 'V26': [v26], 'V27': [v27], 'V28': [v28], 'Amount': [amount]
96
- })
97
-
98
- # Perform prediction
99
  prediction = model.predict(transaction_data)
100
 
101
- # Display results
102
- if prediction[0] == 0:
103
- st.success("✅ Acceptable transaction")
104
- else:
105
- st.error("🚨 Fraudulent transaction")
106
- else:
107
- st.error("Model not loaded.")
108
-
109
-
110
-
 
1
+ from fastapi import FastAPI
2
  import pickle
3
  import pandas as pd
4
 
5
  # Load the saved model
 
6
  def load_model():
7
  try:
8
  model = pickle.load(open('model.pkl', 'rb'))
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:
19
+ # Convert the input data to a DataFrame
20
+ transaction_data = pd.DataFrame([data])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  prediction = model.predict(transaction_data)
22
 
23
+ result = "Fraudulent transaction" if prediction[0] == 1 else "Acceptable transaction"
24
+ return {"prediction": result}
25
+ except Exception as e:
26
+ return {"error": str(e)}