Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

app.py 2.0 KB

You have to be logged in to leave a comment. Sign In
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
56
57
58
59
60
61
62
  1. from fastapi import FastAPI
  2. import uvicorn
  3. import pandas as pd
  4. from pydantic import BaseModel
  5. import sys
  6. import os
  7. from src.pipeline.predict_pipeline import PredictPipeline, CustomData
  8. from src.exception import CustomException
  9. from src.logger import logging
  10. from src.pipeline.validation_pipeline import CustomDataModel
  11. # Iniiate FastAPI
  12. app = FastAPI()
  13. predictor = PredictPipeline()
  14. @app.get("/")
  15. def home():
  16. logging.info("Recieved a request at / endpoint.")
  17. return {"message": "MLOps best practices\n\n Source code: https://www.github.com/karan842/mlops-best-practices"}
  18. @app.post("/")
  19. def home():
  20. logging.error("Wrong method selected.")
  21. return {"message":"Wrong method selected1 please use GET method."}
  22. @app.post("/predict")
  23. async def predict_custom_data(custom_data: CustomDataModel):
  24. try:
  25. # convert the received Pydantic model to dict
  26. custom_data_dict = custom_data.dict()
  27. # Create a CustomData instance using the data from Pydantic model
  28. custom_data_instance = CustomData(**custom_data_dict)
  29. # Get the data as a dataframe from the CustomData instance
  30. custom_data_df = custom_data_instance.get_data_as_data_frame()
  31. # Make predictions using the PredictPipeline
  32. preds = predictor.predict(custom_data_df)
  33. # Coverting to the desired output format
  34. prediction_result = int(preds.item())
  35. if prediction_result == 1:
  36. return {'Churn Prediction': "Yes"}
  37. else:
  38. return {"Churn Prediction": "No"}
  39. logging.info("Prediction successful.")
  40. # return {"prediction": prediction_result}
  41. except Exception as e:
  42. logging.error("Something went wrong on /predict endpoint.")
  43. return {"Error:": str(e)}
  44. @app.get("/predict")
  45. def predict_custom_data():
  46. logging.error("Wrong method selected.")
  47. return {"message":"Wrong method selected! please use POST method."}
  48. if __name__ == '__main__':
  49. uvicorn.run(app, host='0.0.0.0', port=4040)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...