Spaces:
Sleeping
Sleeping
File size: 924 Bytes
10a9072 320d03d 10a9072 320d03d 10a9072 320d03d 10a9072 320d03d 10a9072 320d03d 10a9072 320d03d 10a9072 8cc39a4 320d03d 1c9ae0d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import threading
import uvicorn
import pandas as pd
import pickle
from fastapi import FastAPI
# Initialize FastAPI app
app = FastAPI()
# Load the saved model
def load_model():
try:
with open('model.pkl', 'rb') as file:
model = pickle.load(file)
return model
except Exception as e:
raise RuntimeError(f"Error loading model: {e}")
model = load_model()
# Define the FastAPI endpoint
@app.post("/predict")
async def predict_transaction(data: dict):
try:
# Convert the input data to a DataFrame
transaction_data = pd.DataFrame([data])
prediction = model.predict(transaction_data)
result = "Fraudulent transaction" if prediction[0] == 1 else "Acceptable transaction"
return {"prediction": result}
except Exception as e:
return {"error": str(e)}
# Function to run the FastAPI server
def run_fastapi():
uvicorn.run(app)
|