import uvicorn import pandas as pd from fastapi import FastAPI from pydantic import BaseModel import joblib # Initialize FastAPI app = FastAPI() # Define the request body format for predictions class PredictionFeatures(BaseModel): experience_level_encoded: float company_size_encoded: float employment_type_PT: int job_title_Data_Engineer: int job_title_Data_Manager: int job_title_Data_Scientist: int job_title_Machine_Learning_Engineer: int # Global variable to store the loaded model model = None # Download the model def download_model(): global model model = joblib.load('lin_regress.sav') # Download the model immediately when the script runs download_model() # API Root endpoint @app.get("/") async def index(): return {"message": "Welcome to the Data Science Income API. Use the /predict feature to predict your income."} # Prediction endpoint @app.post("/predict") async def predict(features: PredictionFeatures): # Create input DataFrame for prediction input_data = pd.DataFrame([{ "experience_level_encoded": features.experience_level_encoded, "company_size_encoded": features.company_size_encoded, "employment_type_PT": features.employment_type_PT, "job_title_Data Engineer": features.job_title_Data_Engineer, "job_title_Data Manager": features.job_title_Data_Manager, "job_title_Data Scientist": features.job_title_Data_Scientist, "job_title_Machine Learning Engineer": features.job_title_Machine_Learning_Engineer }]) # Predict using the loaded model prediction = model.predict(input_data)[0] return { "Salary (USD)": prediction } if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=8000)