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)