Spaces:
Runtime error
Runtime error
File size: 1,342 Bytes
d7a8003 3d61956 d7a8003 3d61956 d7a8003 3d61956 d7a8003 3d61956 d7a8003 3d61956 d7a8003 3d61956 d7a8003 3d61956 d7a8003 |
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 40 41 42 43 44 45 46 47 48 49 |
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import joblib
import numpy as np
app = FastAPI()
class InputData(BaseModel):
input1: float
input2: float
input3: float
input4: float
input5: float
input6: float
input7: float
# Load the model and handle potential errors gracefully
try:
model = joblib.load('random_forest_model.joblib')
status = 'Loaded'
print(f"Model {status}")
except Exception as e:
status = f"not loaded: {e}"
print(f"Model {status}")
@app.get('/')
def health_check():
# Return the current status of the app (whether the model is loaded or not)
return {'status': f'{status}'}
@app.post('/predict')
def predict(input: InputData):
# Ensure the model is loaded before making predictions
if status != 'Loaded':
raise HTTPException(status_code=500, detail="Model not loaded. Please check the server logs.")
# Prepare the input data for prediction
data = np.array([[input.input1, input.input2,
input.input3, input.input4,
input.input5, input.input6,
input.input7]])
# Make prediction using the loaded model
prediction = model.predict(data).tolist()
# Return the prediction in JSON format
return {'prediction': prediction[0]}
|