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 1.5 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
  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. def read_params(config_path):
  12. with open(config_path) as yaml_file:
  13. config = yaml.safe_load(yaml_file)
  14. return config
  15. def predict(data):
  16. config = read_params(params_path)
  17. model_dir_path = config["model_dir"]
  18. model = joblib.load(model_dir_path)
  19. prediction = model.predict(data).tolist()[0]
  20. return prediction
  21. def form_response(dict_request):
  22. data = dict_request.values()
  23. data = [list(map(float, data))]
  24. response = predict(data)
  25. return response
  26. @app.route("/", methods=["GET", "POST"])
  27. def index():
  28. if request.method == "POST":
  29. try:
  30. if request.form:
  31. dict_req = dict(request.form)
  32. response = form_response(dict_req)
  33. return render_template("index.html", response=response)
  34. except Exception as e:
  35. print(e)
  36. error = {"error": "Something went wrong!! Try again later!"}
  37. error = {"error": e}
  38. return render_template("404.html", error=error)
  39. else:
  40. return render_template("index.html")
  41. if __name__ == "__main__":
  42. 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...