Spaces:
Sleeping
Sleeping
File size: 1,688 Bytes
e781781 |
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 50 51 52 53 54 55 |
# app.py
from fastapi import FastAPI, Request
from pydantic import BaseModel
import pickle
import numpy as np
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
# Enable CORS for all origins, methods, and headers to avoid CORS issues when making requests from React, Axios, etc.
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
# Load the trained model
with open('best_model.pkl', 'rb') as f:
model = pickle.load(f)
# Input schema for FastAPI
class AlgaeInput(BaseModel):
Light: float
Nitrate: float
Iron: float
Phosphate: float
Temperature: float
pH: float
CO2: float
# Root endpoint to check if the API is running
@app.get("/")
def greet_json():
return {"Hello": "World!, the prediction is at /predict"}
# Prediction endpoint to accept input data and return the predicted algae quantity
@app.post("/predict")
async def predict_algae(input_data: AlgaeInput):
try:
# Convert input data to the correct format
input_array = np.array([[input_data.Light, input_data.Nitrate, input_data.Iron,
input_data.Phosphate, input_data.Temperature,
input_data.pH, input_data.CO2]])
# Perform prediction
prediction = model.predict(input_array)
# Return the prediction as a JSON response
return {"predicted_population": prediction[0]}
except Exception as e:
# Return an error message if prediction fails
return {"error": str(e)}
|