sks01dev commited on
Commit
a2cb5ab
·
1 Parent(s): a6b5f1d

FIXED: Resolved AttributeError by converting Pydantic model to list of dicts for pipeline

Browse files
Files changed (1) hide show
  1. Week 5/predict.py +7 -6
Week 5/predict.py CHANGED
@@ -19,13 +19,15 @@ class Customer(BaseModel):
19
  "json_schema_extra": {
20
  "examples": [
21
  {
 
22
  "lead_source": "paid_ads",
23
- "annual_income": 79276.0,
24
  "number_of_courses_viewed": 2,
25
  }
26
  ]
27
  }
28
  }
 
29
 
30
  # response data
31
  class PredictResponse(BaseModel):
@@ -40,15 +42,14 @@ with open("model.bin", "rb") as f_in:
40
 
41
  # Helper function to get prediction from the loaded model
42
  def predict_single(customer_dict: dict) -> float:
43
- return pipeline.predict_proba(customer_dict)[0, 1]
44
 
45
  # Define the prediction endpoint
46
  @app.post("/predict", response_model=PredictResponse)
47
  def predict(customer: Customer):
48
- prob = predict_single(customer)
49
  return PredictResponse(convert_probability=prob, converted=(prob >= 0.5))
50
 
51
  # Run the app for local development
52
- # The '__main__' block is for local development and will NOT run on Hugging Face Spaces
53
- # if __name__ == "__main__":
54
- # uvicorn.run("predict:app", host="0.0.0.0", port=9696)
 
19
  "json_schema_extra": {
20
  "examples": [
21
  {
22
+ # This dictionary below is the sample that will appear in the Swagger UI
23
  "lead_source": "paid_ads",
24
+ "annual_income": 79276.0, # Note: Use a float (79276.0) for consistency
25
  "number_of_courses_viewed": 2,
26
  }
27
  ]
28
  }
29
  }
30
+
31
 
32
  # response data
33
  class PredictResponse(BaseModel):
 
42
 
43
  # Helper function to get prediction from the loaded model
44
  def predict_single(customer_dict: dict) -> float:
45
+ return pipeline.predict_proba([customer_dict])[0, 1]
46
 
47
  # Define the prediction endpoint
48
  @app.post("/predict", response_model=PredictResponse)
49
  def predict(customer: Customer):
50
+ prob = predict_single(customer.model_dump())
51
  return PredictResponse(convert_probability=prob, converted=(prob >= 0.5))
52
 
53
  # Run the app for local development
54
+ if __name__ == "__main__":
55
+ uvicorn.run("predict:app", host="0.0.0.0", port=9696)