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 3.7 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
127
128
129
130
131
132
133
  1. from flask import Flask, render_template, request, jsonify
  2. import os
  3. import numpy as np
  4. import pandas as pd
  5. from sentimentAnalysis.pipeline.prediction_adv import (
  6. PredictionPipeline as PredictionPipelineAdv,
  7. )
  8. from sentimentAnalysis.pipeline.prediction import PredictionPipeline
  9. from sentimentAnalysis.config.configuration import ConfigurationManager
  10. from sentimentAnalysis.utils.common import load_json
  11. from pathlib import Path
  12. app = Flask(__name__) # initializing a flask app
  13. @app.route("/", methods=["GET"]) # route to display the home page
  14. def homePage():
  15. return render_template("index.html")
  16. @app.route("/train", methods=["GET"]) # route to train the pipeline
  17. def training():
  18. os.system("python main.py")
  19. return "Training Successful!"
  20. @app.route(
  21. "/predict", methods=["POST", "GET"]
  22. ) # route to show the predictions in a web UI
  23. def index():
  24. if request.method == "POST":
  25. try:
  26. # reading the inputs given by the user
  27. text = request.form["text"]
  28. config = ConfigurationManager()
  29. config = config.get_prediction_config()
  30. data_path = config.data_path
  31. with open(data_path, "w") as f:
  32. f.write(text)
  33. obj = PredictionPipeline()
  34. obj.main()
  35. predition_path = config.prediction_file
  36. file = load_json(path=Path(predition_path))
  37. predict = file["prediction"]
  38. sentiment_color = "background-color: cyan;"
  39. return render_template(
  40. "result.html",
  41. sentiment=predict,
  42. sentiment_color="black",
  43. bg_style=sentiment_color,
  44. )
  45. except Exception as e:
  46. print("The Exception message is: ", e)
  47. return render_template("404.html")
  48. else:
  49. return render_template("index.html")
  50. @app.route(
  51. "/predict_adv", methods=["POST", "GET"]
  52. )
  53. def adv():
  54. if request.method == "POST":
  55. try:
  56. # reading the inputs given by the user
  57. text = request.form["text"]
  58. config = ConfigurationManager()
  59. config = config.get_prediction_config()
  60. data_path = config.data_path
  61. with open(data_path, "w") as f:
  62. f.write(text)
  63. obj = PredictionPipelineAdv()
  64. obj.main()
  65. predition_path = config.prediction_file
  66. file = load_json(path=Path(predition_path))
  67. predict = file["prediction"]
  68. sentiment_color = "background-color: cyan;"
  69. return render_template(
  70. "result.html",
  71. sentiment=predict,
  72. sentiment_color="black",
  73. bg_style=sentiment_color,
  74. )
  75. except Exception as e:
  76. print("The Exception message is: ", e)
  77. return render_template("404.html")
  78. else:
  79. return render_template("index_adv.html")
  80. # api call
  81. @app.route("/predict_data", methods=["POST", "GET"])
  82. def pred():
  83. try:
  84. text = request.args.get("text")
  85. config = ConfigurationManager()
  86. config = config.get_prediction_config()
  87. data_path = config.data_path
  88. with open(data_path, "w") as f:
  89. f.write(text)
  90. obj = PredictionPipeline()
  91. obj.main()
  92. predition_path = config.prediction_file
  93. file = load_json(path=Path(predition_path))
  94. predict = file["prediction"]
  95. return jsonify({"result": predict})
  96. except Exception as e:
  97. print("The Exception message is: ", e)
  98. return jsonify({"result": "error"})
  99. # error handle
  100. @app.errorhandler(404)
  101. def not_found(e):
  102. return render_template("404.html")
  103. # main
  104. # if __name__ == "__main__":
  105. # app.run(host="0.0.0.0", port=8080)
Tip!

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

Comments

Loading...