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

run_api.py 2.4 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
63
64
65
66
  1. import os
  2. import sys
  3. import requests
  4. import argparse
  5. from dotenv import load_dotenv
  6. load_dotenv()
  7. project_home_path = os.environ.get('PROJECT_HOME_PATH')
  8. sys.path.append(project_home_path)
  9. from src.pipeline.predict_pipeline import CustomData
  10. from src.logger import logging
  11. # Sample test cases
  12. BASE_URL = "http://localhost:4040"
  13. def predict_endpoint(test_data):
  14. try:
  15. response = requests.post(f"{BASE_URL}/predict",
  16. json=test_data)
  17. response_data = response.json()
  18. if response.status_code == 200:
  19. prediction = response_data.get("Churn Prediction")
  20. logging.info(f"Prediction result: {prediction}")
  21. print(f"Prediction result: {prediction}")
  22. else:
  23. logging.error(f"Failed to make a prediction. Error: {response_data}")
  24. print(f"Failed to make a prediction. Error: {response_data}")
  25. except requests.exceptions.RequestException as e:
  26. logging.error(f"Failed to make request to the server. Error: {e}")
  27. print(f"Failed to make a request to the server. Error: {e}")
  28. if __name__ == '__main__':
  29. '''
  30. Run this script in a terminal for testing
  31. '''
  32. parser = argparse.ArgumentParser(description="Test FastAPI predict endpoint.")
  33. parser.add_argument("--CreditScore", type=int, required=True)
  34. parser.add_argument("--Geography", type=str, required=True)
  35. parser.add_argument("--Gender", type=str, required=True)
  36. parser.add_argument("--Age", type=int, required=True)
  37. parser.add_argument("--Tenure", type=int, required=True)
  38. parser.add_argument("--Balance", type=float, required=True)
  39. parser.add_argument("--NumOfProducts", type=int, required=True)
  40. parser.add_argument("--HasCrCard", type=int, required=True)
  41. parser.add_argument("--IsActiveMember", type=int, required=True)
  42. parser.add_argument("--EstimatedSalary", type=float, required=True)
  43. args = parser.parse_args()
  44. test_data = {
  45. "CreditScore": args.CreditScore,
  46. "Geography": args.Geography,
  47. "Gender": args.Gender,
  48. "Age": args.Age,
  49. "Tenure": args.Tenure,
  50. "Balance": args.Balance,
  51. "NumOfProducts": args.NumOfProducts,
  52. "HasCrCard": args.HasCrCard,
  53. "IsActiveMember": args.IsActiveMember,
  54. "EstimatedSalary": args.EstimatedSalary
  55. }
  56. predict_endpoint(test_data)
Tip!

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

Comments

Loading...