Spaces:
Sleeping
Sleeping
Update slapp.py
Browse files
slapp.py
CHANGED
|
@@ -1,45 +1,5 @@
|
|
| 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:
|
| 27 |
-
# Convert the input data to a DataFrame
|
| 28 |
-
transaction_data = pd.DataFrame([data])
|
| 29 |
-
prediction = model.predict(transaction_data)
|
| 30 |
-
|
| 31 |
-
result = "Fraudulent transaction" if prediction[0] == 1 else "Acceptable transaction"
|
| 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")
|
|
@@ -91,10 +51,8 @@ if st.button("Predict"):
|
|
| 91 |
if response.status_code == 200:
|
| 92 |
result = response.json().get("prediction")
|
| 93 |
if result == "Acceptable transaction":
|
| 94 |
-
print(result)
|
| 95 |
st.success("✅ " + result)
|
| 96 |
else:
|
| 97 |
-
print(result)
|
| 98 |
st.error("🚨 " + result)
|
| 99 |
else:
|
| 100 |
st.error("Error: " + response.text)
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Streamlit app interface
|
| 5 |
st.title("Fraud Detection API")
|
|
|
|
| 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)
|