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
63
64
65
66
67
68
69
  1. from flask import Flask, render_template, request, jsonify
  2. import os
  3. import numpy as np
  4. import yaml
  5. import joblib
  6. webapp_root = "webapp"
  7. params_path = "params.yaml"
  8. static_dir = os.path.join(webapp_root, "static")
  9. template_dir = os.path.join(webapp_root, "templates")
  10. app = Flask(__name__, static_folder=static_dir,template_folder=template_dir)
  11. class NotANumber(Exception):
  12. def __init__(self, message="Values entered are not Numerical"):
  13. self.message = message
  14. super().__init__(self.message)
  15. def read_params(config_path):
  16. with open(config_path) as yaml_file:
  17. config = yaml.safe_load(yaml_file)
  18. return config
  19. def predict(data):
  20. config = read_params(params_path)
  21. model_dir_path = config["model_webapp_dir"]
  22. model = joblib.load(model_dir_path)
  23. prediction = model.predict(data).tolist()[0]
  24. return prediction
  25. def validate_input(dict_request):
  26. for _, val in dict_request.items():
  27. try:
  28. val=float(val)
  29. except Exception as e:
  30. raise NotANumber
  31. return True
  32. def form_response(dict_request):
  33. try:
  34. if validate_input(dict_request):
  35. data = dict_request.values()
  36. data = [list(map(float, data))]
  37. response = predict(data)
  38. return response
  39. except NotANumber as e:
  40. response = str(e)
  41. return response
  42. @app.route("/", methods=["GET", "POST"])
  43. def index():
  44. if request.method == "POST":
  45. try:
  46. if request.form:
  47. dict_req = dict(request.form)
  48. response = form_response(dict_req)
  49. return render_template("index.html", response=response)
  50. except Exception as e:
  51. print(e)
  52. error = {"error": "Something went wrong!! Try again later!"}
  53. error = {"error": e}
  54. return render_template("404.html", error=error)
  55. else:
  56. return render_template("index.html")
  57. if __name__ == "__main__":
  58. app.run(host='0.0.0.0', port=5000, debug=True)
Tip!

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

Comments

Loading...