Spaces:
Runtime error
Runtime error
| 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}") | |
| def health_check(): | |
| # Return the current status of the app (whether the model is loaded or not) | |
| return {'status': f'{status}'} | |
| 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]} | |