Feiiisal commited on
Commit
8d2abde
β€’
1 Parent(s): 7a44e89

Updated files

Browse files
main.py CHANGED
@@ -7,17 +7,17 @@ import os
7
  import pickle
8
 
9
  # setup
10
- FASTAPI_INCOME_CLASSIFICATION = os.path.abspath('.')
11
 
12
  # Load the pipeline using pickle
13
- pipeline_path = os.path.join(FASTAPI_INCOME_CLASSIFICATION, 'rfc_pipeline.pkl')
14
  with open(pipeline_path, 'rb') as file:
15
- rfc_pipeline = pickle.load(file)
16
 
17
  # Load the encoder using pickle
18
- encoder_path = os.path.join(FASTAPI_INCOME_CLASSIFICATION, 'encoder.pkl')
19
- with open(encoder_path, 'rb') as file:
20
- encoder = pickle.load(file)
21
 
22
  app = FastAPI(
23
  title= 'Income Classification FastAPI',
@@ -71,21 +71,24 @@ def home():
71
  @app.post('/classify', response_model=IncomePredictionOutput)
72
  def income_classification(income: IncomePredictionInput):
73
  try:
74
- df = pd.DataFrame([income.model_dump()])
75
-
76
- # Make predictions
77
- prediction = rfc_pipeline.predict(df)
78
- output = rfc_pipeline.predict_proba(df)
79
 
80
- prediction_result = "Income over $50K" if prediction[0] == 1 else "Income under $50K"
81
- return {"income_prediction": prediction_result, "prediction_probability": output[0][1]}
 
82
 
 
 
83
 
84
  except Exception as e:
85
- # Return error message and details if an exception occurs
86
  error_detail = str(e)
87
  raise HTTPException(status_code=500, detail=f"Error during classification: {error_detail}")
88
 
89
 
90
  if __name__ == '__main__':
91
- uvicorn.run('main:app', reload=True)
 
 
7
  import pickle
8
 
9
  # setup
10
+ SRC = os.path.abspath('./SRC/Assets')
11
 
12
  # Load the pipeline using pickle
13
+ pipeline_path = os.path.join(SRC, 'pipeline.pkl')
14
  with open(pipeline_path, 'rb') as file:
15
+ pipeline = pickle.load(file)
16
 
17
  # Load the encoder using pickle
18
+ model_path = os.path.join(SRC, 'rfc_model.pkl')
19
+ with open(model_path, 'rb') as file:
20
+ model = pickle.load(file)
21
 
22
  app = FastAPI(
23
  title= 'Income Classification FastAPI',
 
71
  @app.post('/classify', response_model=IncomePredictionOutput)
72
  def income_classification(income: IncomePredictionInput):
73
  try:
74
+ # Convert input data to DataFrame
75
+ input_df = pd.DataFrame([dict(income)])
76
+
77
+ # Preprocess the input data through the pipeline
78
+ input_df_transformed = pipeline.transform(input_df)
79
 
80
+ # Make predictions
81
+ prediction = model.predict(input_df_transformed)
82
+ probability = model.predict_proba(input_df_transformed).max(axis=1)[0]
83
 
84
+ prediction_result = "Above Limit" if prediction[0] == 1 else "Below Limit"
85
+ return {"income_prediction": prediction_result, "prediction_probability": probability}
86
 
87
  except Exception as e:
 
88
  error_detail = str(e)
89
  raise HTTPException(status_code=500, detail=f"Error during classification: {error_detail}")
90
 
91
 
92
  if __name__ == '__main__':
93
+ uvicorn.run('main:app', reload=True)
94
+
encoder.pkl β†’ pipeline.pkl RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:98baac762b1becb7d5b699f21576a27ebe7555a83826444f23d540dcfc7d01d1
3
- size 270
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dd28c23cc70beba35906a28c1b6937630ffb3bb4a7c5e8cb8276f66a33eb60e4
3
+ size 4811
requirements.txt CHANGED
@@ -1,5 +1,6 @@
1
- fastapi
2
- uvicorn
3
- pandas
4
- pydantic
5
- scikit-learn==1.3.0
 
 
1
+ fastapi==0.108.0
2
+ uvicorn==0.25.0
3
+ pandas==2.1.4
4
+ pydantic==2.5.3
5
+ pydantic_core==2.14.6
6
+ scikit-learn==1.3.2
rfc_pipeline.pkl β†’ rfc_model.pkl RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:de6be7713061b3b93f04b7e4c3239216dc6f51b4b18ead0b60fce19c0cbe594e
3
- size 223631035
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cd6ad029aa941d54353a585b399b77c15380a06cfa80a2c81faf9697effe0aac
3
+ size 267723561