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.9 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
  1. from flask import Flask, render_template, request,Response,render_template_string, jsonify
  2. import os
  3. import time
  4. import subprocess
  5. import logging
  6. from reviewprediction.pipeline.prediction import PredictionPipeline, CustomData
  7. app = Flask(__name__) # initializing a Flask app
  8. main_py_timestamps = [] # List to store the timestamps when main.py was triggered
  9. @app.route('/', methods=['GET']) # route to display the home page
  10. def homePage():
  11. return render_template("index.html")
  12. @app.route('/train', methods=['POST','GET']) # route to train the pipeline
  13. def training():
  14. if request.method == 'GET':
  15. if os.path.exists('artifacts\model_trainer\model.joblib'):
  16. return render_template_string("""
  17. <script>
  18. if (confirm("Model is already trained. Do you still want to proceed and train the model again?")) {
  19. window.location.href = "/info";
  20. } else {
  21. window.location.href = "/"; // Go back to the home page
  22. }
  23. </script>
  24. """)
  25. else:
  26. return render_template('info.html')
  27. else:
  28. return "Invalid request method."
  29. @app.route('/info')
  30. def info():
  31. return render_template('info.html')
  32. @app.route('/proceed_training', methods=['GET'])
  33. def proceed_training():
  34. if request.method == 'GET':
  35. os.system("python main.py")
  36. return "Model trained successfully!"
  37. else:
  38. return "Invalid request method."
  39. @app.route('/predict', methods=['POST', 'GET']) # route to show the predictions in a web UI
  40. def predict():
  41. if os.path.exists('artifacts\model_trainer\model.joblib'):
  42. try:
  43. data = CustomData(
  44. order_item_id=int(request.form.get('order_item_id')),
  45. price=float(request.form.get('price')),
  46. freight_value=float(request.form.get('freight_value')),
  47. product_category_name=str(request.form.get('product_category_name')),
  48. product_name_length=float(request.form.get('product_name_length')),
  49. product_description_length=float(request.form.get('product_description_length')),
  50. product_photos_qty=float(request.form.get('product_photos_qty')),
  51. product_weight_g=float(request.form.get('product_weight_g')),
  52. product_length_cm=float(request.form.get('product_length_cm')),
  53. product_height_cm=float(request.form.get('product_height_cm')),
  54. product_width_cm=float(request.form.get('product_width_cm')),
  55. seller_zip_code_prefix=int(request.form.get('seller_zip_code_prefix')),
  56. seller_city=str(request.form.get('seller_city')),
  57. seller_state=str(request.form.get('seller_state')),
  58. order_status=str(request.form.get('order_status')),
  59. customer_zip_code_prefix=int(request.form.get('customer_zip_code_prefix')),
  60. customer_city=str(request.form.get('customer_city')),
  61. customer_state=str(request.form.get('customer_state')),
  62. review_id=str(request.form.get('review_id')),
  63. review_comment_title=str(request.form.get('review_comment_title')),
  64. review_comment_message=str(request.form.get('review_comment_message')),
  65. review_creation_date=str(request.form.get('review_creation_date')),
  66. review_answer_timestamp=str(request.form.get('review_answer_timestamp')),
  67. payment_sequential=int(request.form.get('payment_sequential')),
  68. payment_type=str(request.form.get('payment_type')),
  69. payment_installments=int(request.form.get('payment_installments')),
  70. payment_value=float(request.form.get('payment_value')),
  71. purchase_delivery_difference=int(request.form.get('purchase-delivery difference')),
  72. estimated_actual_delivery_difference=int(request.form.get('estimated-actual delivery difference')),
  73. price_category=str(request.form.get('price_category')),
  74. purchase_delivery_diff_per_price=float(request.form.get('purchase_delivery_diff_per_price')),
  75. review_availability=int(request.form.get('review_availability'))
  76. )
  77. final_data = data.data_transformer()
  78. obj = PredictionPipeline()
  79. pred = obj.predict(final_data)
  80. sentiment = 'The review is positive!' if pred == 1 else 'The review is negative!'
  81. return render_template('result.html', prediction=sentiment)
  82. except Exception as e:
  83. logging.exception("An error occurred while processing the request:")
  84. return 'Something went wrong while processing the request. Please try again later.'
  85. else:
  86. return "Please Train the model and proceed to get your prediction"
  87. if __name__ == "__main__":
  88. 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...