Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pickle | |
| import pandas as pd | |
| # Load the saved model | |
| # Cache the model loading to avoid reloading on each run | |
| def load_model(): | |
| try: | |
| model = pickle.load(open('model.pkl', 'rb')) | |
| return model | |
| except Exception as e: | |
| st.error(f"Error loading model: {e}") | |
| return None | |
| model = load_model() | |
| # Title of the app | |
| st.title("Fraud Detection API") | |
| st.markdown("Welcome to the Fraud Detection API! Please enter the transaction details below:") | |
| # Tabs for input sections | |
| tab1, tab2, tab3 = st.tabs(["Basic Info", "Features (V1 - V14)", "Features (V15 - V28)"]) | |
| # Horizontal layout for Basic Info | |
| with tab1: | |
| st.header("Basic Information") | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| time = st.number_input("Time", min_value=0.0, step=0.1) | |
| with col2: | |
| amount = st.number_input("Amount", min_value=0.0, step=0.1) | |
| # Horizontal layout for Features V1 - V14 | |
| with tab2: | |
| st.header("Features (V1 - V14)") | |
| cols = st.columns(7) | |
| with cols[0]: | |
| v1 = st.number_input("V1", step=0.01) | |
| v2 = st.number_input("V2", step=0.01) | |
| with cols[1]: | |
| v3 = st.number_input("V3", step=0.01) | |
| v4 = st.number_input("V4", step=0.01) | |
| with cols[2]: | |
| v5 = st.number_input("V5", step=0.01) | |
| v6 = st.number_input("V6", step=0.01) | |
| with cols[3]: | |
| v7 = st.number_input("V7", step=0.01) | |
| v8 = st.number_input("V8", step=0.01) | |
| with cols[4]: | |
| v9 = st.number_input("V9", step=0.01) | |
| v10 = st.number_input("V10", step=0.01) | |
| with cols[5]: | |
| v11 = st.number_input("V11", step=0.01) | |
| v12 = st.number_input("V12", step=0.01) | |
| with cols[6]: | |
| v13 = st.number_input("V13", step=0.01) | |
| v14 = st.number_input("V14", step=0.01) | |
| # Horizontal layout for Features V15 - V28 | |
| with tab3: | |
| st.header("Features (V15 - V28)") | |
| cols = st.columns(7) | |
| with cols[0]: | |
| v15 = st.number_input("V15", step=0.01) | |
| v16 = st.number_input("V16", step=0.01) | |
| with cols[1]: | |
| v17 = st.number_input("V17", step=0.01) | |
| v18 = st.number_input("V18", step=0.01) | |
| with cols[2]: | |
| v19 = st.number_input("V19", step=0.01) | |
| v20 = st.number_input("V20", step=0.01) | |
| with cols[3]: | |
| v21 = st.number_input("V21", step=0.01) | |
| v22 = st.number_input("V22", step=0.01) | |
| with cols[4]: | |
| v23 = st.number_input("V23", step=0.01) | |
| v24 = st.number_input("V24", step=0.01) | |
| with cols[5]: | |
| v25 = st.number_input("V25", step=0.01) | |
| v26 = st.number_input("V26", step=0.01) | |
| with cols[6]: | |
| v27 = st.number_input("V27", step=0.01) | |
| v28 = st.number_input("V28", step=0.01) | |
| # Button to make predictions | |
| if st.button("Predict"): | |
| if model: | |
| # Create a DataFrame from the input data | |
| transaction_data = pd.DataFrame({ | |
| 'Time': [time], | |
| 'V1': [v1], 'V2': [v2], 'V3': [v3], 'V4': [v4], 'V5': [v5], 'V6': [v6], | |
| 'V7': [v7], 'V8': [v8], 'V9': [v9], 'V10': [v10], 'V11': [v11], 'V12': [v12], | |
| 'V13': [v13], 'V14': [v14], 'V15': [v15], 'V16': [v16], 'V17': [v17], 'V18': [v18], | |
| 'V19': [v19], 'V20': [v20], 'V21': [v21], 'V22': [v22], 'V23': [v23], 'V24': [v24], | |
| 'V25': [v25], 'V26': [v26], 'V27': [v27], 'V28': [v28], 'Amount': [amount] | |
| }) | |
| # Perform prediction | |
| prediction = model.predict(transaction_data) | |
| # Display results | |
| if prediction[0] == 0: | |
| st.success("✅ Acceptable transaction") | |
| else: | |
| st.error("🚨 Fraudulent transaction") | |
| else: | |
| st.error("Model not loaded.") | |