mbabazif commited on
Commit
aaf7735
1 Parent(s): 1ac3f75
Files changed (1) hide show
  1. main.py +75 -0
main.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ import joblib
4
+ import pandas as pd
5
+ from fastapi import FastAPI,HTTPException
6
+ import uvicorn
7
+
8
+ # Create a FastAPI instance
9
+ app = FastAPI()
10
+
11
+ # Load the entire pipeline
12
+ rfc_pipeline = joblib.load('./rfc_pipeline.joblib')
13
+ encoder = joblib.load('./encoder.joblib')
14
+
15
+ # Define a FastAPI instance ML model input schema
16
+ class IncomePredictionInput(BaseModel):
17
+ age: int
18
+ gender: object
19
+ education: object
20
+ worker_class: object
21
+ marital_status: object
22
+ race: object
23
+ is_hispanic: object
24
+ employment_commitment: object
25
+ employment_stat: int
26
+ wage_per_hour: int
27
+ working_week_per_year: int
28
+ industry_code: int
29
+ industry_code_main: object
30
+ occupation_code: int
31
+ occupation_code_main: object
32
+ total_employed: int
33
+ household_summary: object
34
+ vet_benefit: int
35
+ tax_status: object
36
+ gains: int
37
+ losses: int
38
+ stocks_status: int
39
+ citizenship: object
40
+ importance_of_record: float
41
+
42
+ class IncomePredictionOutput(BaseModel):
43
+ income_prediction: str
44
+ prediction_probability: float
45
+ # Defining the root endpoint for the API
46
+ @app.get("/")
47
+ def index():
48
+ explanation = {
49
+ 'message': "Welcome to the Income Iniquality Prediction App",
50
+ 'description': "This API allows you to predict Income Iniquality based on Personal data.",
51
+ }
52
+ return explanation
53
+
54
+
55
+ @app.post('/classify', response_model=IncomePredictionOutput)
56
+ def income_classification(income: IncomePredictionInput):
57
+ try:
58
+ df = pd.DataFrame([income.model_dump()])
59
+
60
+ # Make predictions
61
+ prediction = rfc_pipeline.predict(df)
62
+ output = rfc_pipeline.predict_proba(df)
63
+
64
+ prediction_result = "Income over $50K" if prediction[0] == 1 else "Income under $50K"
65
+ return {"income_prediction": prediction_result, "prediction_probability": output[0][1]}
66
+
67
+
68
+ except Exception as e:
69
+ # Return error message and details if an exception occurs
70
+ error_detail = str(e)
71
+ raise HTTPException(status_code=500, detail=f"Error during classification: {error_detail}")
72
+
73
+
74
+ if __name__ == '__main__':
75
+ uvicorn.run('main:app', reload=True)