Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import joblib
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load the saved model (make sure it's in the same directory or provide the correct path)
|
| 7 |
+
model = joblib.load("linear_regression_model.pkl")
|
| 8 |
+
|
| 9 |
+
# Initialize the FastAPI app
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
# Define the input schema for predictions
|
| 13 |
+
class PredictionInput(BaseModel):
|
| 14 |
+
feature1: float
|
| 15 |
+
feature2: float
|
| 16 |
+
|
| 17 |
+
# Define the prediction endpoint
|
| 18 |
+
@app.post("/predict")
|
| 19 |
+
def predict(input_data: PredictionInput):
|
| 20 |
+
try:
|
| 21 |
+
# Convert input into model-compatible format (as a 2D array)
|
| 22 |
+
input_features = np.array([[input_data.feature1, input_data.feature2]])
|
| 23 |
+
prediction = model.predict(input_features)
|
| 24 |
+
return {"prediction": prediction.tolist()} # Return prediction as a list
|
| 25 |
+
except Exception as e:
|
| 26 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 27 |
+
|
| 28 |
+
# Basic greeting endpoint (optional)
|
| 29 |
+
@app.get("/")
|
| 30 |
+
def greet_json():
|
| 31 |
+
return {"message": "Welcome to the Linear Regression API!"}
|