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 4.2 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  1. from distutils.command.config import config
  2. from urllib import response
  3. from flask import Flask,render_template,jsonify,request
  4. import os
  5. import joblib
  6. import numpy as np
  7. import yaml
  8. import pandas as pd
  9. import logging
  10. # from gevent.pywsgi import WSGIServer
  11. logging.basicConfig(filename="deployment_logs.logs", format='%(asctime)s %(message)s',level=logging.INFO)
  12. params_path="params.yaml"
  13. logging.info("params.yaml loaded successfully")
  14. webapp_root="webapp"
  15. static_dir=os.path.join(webapp_root,"static")
  16. logging.info("staticdir linked successfully")
  17. template_dir=os.path.join(webapp_root,"templates")
  18. logging.info("template_dir linking successful")
  19. app=Flask(__name__,static_folder=static_dir,template_folder=template_dir)
  20. # port=5000
  21. # app_server = gevent.pywsgi.WSGIServer(('', port), app)
  22. # app_server.serve_forever()
  23. def read_params(config_path):
  24. try:
  25. with open(config_path) as yaml_file:
  26. config=yaml.safe_load(yaml_file)
  27. return config
  28. except Exception as e:
  29. logging.info("The following error message is :",str())
  30. def predict(data):
  31. try:
  32. config=read_params(params_path)
  33. model_dir_path=config["webapp_model_dir"]
  34. model=joblib.load(model_dir_path)
  35. prediction=model.predict(data)
  36. print(prediction)
  37. return prediction
  38. except Exception as e:
  39. logging.info("the following file has an error",str(e))
  40. def api_response(request):
  41. try:
  42. data=np.array([list(request.json.values())])
  43. response=predict(data)
  44. response={"response":response}
  45. return jsonify(response)
  46. except Exception as e:
  47. print(e)
  48. error={"something went wrong try again!!"}
  49. return error
  50. config=read_params(params_path)
  51. raw_data=config["raw_data"]["raw"]
  52. data1=pd.read_csv(raw_data)
  53. # print(data1.head())
  54. logging.info("csv read successful")
  55. @app.route("/",methods=["GET","POST"])
  56. def index():
  57. sex=sorted(data1["sex"].unique())
  58. smoker=sorted(data1["smoker"].unique())
  59. region=sorted(data1["region"].unique())
  60. if request.method=="POST":
  61. try:
  62. if request.form:
  63. # data=dict(request.form)
  64. # data=[list(map(float,data))]
  65. # response=predict(data)
  66. # return render_template("index.html",response=response)
  67. error={"error":"Please Select the correct dropdown value"}
  68. age=int(request.form.get("age"))
  69. sex=(request.form.get("sex"))
  70. if(sex=="female"):
  71. sex=0
  72. elif(sex=="male"):
  73. sex=1
  74. else:
  75. return render_template("404.html",error=error)
  76. bmi=float(request.form.get("bmi"))
  77. children=request.form.get("children")
  78. smoker=request.form.get("smoker")
  79. if(smoker=="no"):
  80. smoker=0
  81. elif smoker=="yes":
  82. smoker=1
  83. else:
  84. return render_template("404.html",error=error)
  85. region=request.form.get("region")
  86. if region=="northeast":
  87. region=0
  88. elif region=="northwest":
  89. region=1
  90. elif region=="southeast":
  91. region=2
  92. elif region=="southwest":
  93. region=3
  94. else:
  95. return render_template("404.html",error=error)
  96. # data=dict(request.form)
  97. # data=[list(map(float,data))]
  98. response=predict(pd.DataFrame([[age, sex, bmi, children, smoker, region]], columns=['age', 'sex', 'bmi', 'children', 'smoker', 'region']))
  99. # response=predict(data)
  100. return render_template("index.html",response=str(response[0]))
  101. elif request.json:
  102. response=api_response(request)
  103. return jsonify(response)
  104. except Exception as e:
  105. error = {"error":e}
  106. return render_template("404.html", error=error)
  107. else:
  108. return render_template("index.html",sex=sex,smoker=smoker,region=region)
  109. logging.info("application running succesfully")
  110. if __name__=="__main__":
  111. app.run(port=5000,debug=True)
Tip!

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

Comments

Loading...