|
|
from fastapi import FastAPI, HTTPException |
|
|
from pydantic import BaseModel |
|
|
import joblib |
|
|
import numpy as np |
|
|
|
|
|
|
|
|
model = joblib.load("linear_regression_model.pkl") |
|
|
|
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
|
|
|
class PredictionInput(BaseModel): |
|
|
feature1: float |
|
|
|
|
|
|
|
|
@app.post("/predict") |
|
|
def predict(input_data: PredictionInput): |
|
|
try: |
|
|
|
|
|
input_features = np.array([[input_data.feature1]]) |
|
|
prediction = model.predict(input_features) |
|
|
return {"prediction": prediction.tolist()} |
|
|
except Exception as e: |
|
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|
|
|
|
|
@app.get("/") |
|
|
def greet_json(): |
|
|
return {"message": "Welcome to the Linear Regression API!"} |
|
|
|