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.3 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
  1. from flask import Flask, render_template, request, jsonify
  2. import os
  3. import numpy as np
  4. import pandas as pd
  5. from src.datascience.pipeline.prediction_pipeline import PredictionPipeline
  6. app = Flask(__name__) # initializing a flask app
  7. @app.route('/',methods=['GET']) # route to display the home page
  8. def homePage():
  9. return render_template("index.html")
  10. @app.route('/train',methods=['GET']) # route to train the pipeline
  11. def training():
  12. try:
  13. os.system("python main.py")
  14. return "<h1 style='text-align: center; color: green; font-family: Arial;'>✅ Training Successful!</h1><p style='text-align: center;'><a href='/' style='color: #667eea; text-decoration: none;'>← Back to Prediction</a></p>"
  15. except Exception as e:
  16. return f"<h1 style='text-align: center; color: red; font-family: Arial;'>❌ Training Failed!</h1><p style='text-align: center; color: #666;'>Error: {str(e)}</p><p style='text-align: center;'><a href='/' style='color: #667eea; text-decoration: none;'>← Back to Prediction</a></p>"
  17. @app.route('/predict',methods=['POST','GET']) # route to show the predictions in a web UI
  18. def index():
  19. if request.method == 'POST':
  20. try:
  21. # reading the inputs given by the user
  22. fixed_acidity = float(request.form['fixed_acidity'])
  23. volatile_acidity = float(request.form['volatile_acidity'])
  24. citric_acid = float(request.form['citric_acid'])
  25. residual_sugar = float(request.form['residual_sugar'])
  26. chlorides = float(request.form['chlorides'])
  27. free_sulfur_dioxide = float(request.form['free_sulfur_dioxide'])
  28. total_sulfur_dioxide = float(request.form['total_sulfur_dioxide'])
  29. density = float(request.form['density'])
  30. pH = float(request.form['pH'])
  31. sulphates = float(request.form['sulphates'])
  32. alcohol = float(request.form['alcohol'])
  33. # Validate input ranges (basic validation)
  34. validation_rules = {
  35. 'fixed_acidity': (3.8, 15.9),
  36. 'volatile_acidity': (0.08, 1.58),
  37. 'citric_acid': (0.0, 1.66),
  38. 'residual_sugar': (0.6, 65.8),
  39. 'chlorides': (0.009, 0.611),
  40. 'free_sulfur_dioxide': (1.0, 289.0),
  41. 'total_sulfur_dioxide': (6.0, 440.0),
  42. 'density': (0.98711, 1.03898),
  43. 'pH': (2.72, 4.01),
  44. 'sulphates': (0.22, 2.0),
  45. 'alcohol': (8.0, 15.0)
  46. }
  47. # Check if values are within expected ranges
  48. inputs = {
  49. 'fixed_acidity': fixed_acidity,
  50. 'volatile_acidity': volatile_acidity,
  51. 'citric_acid': citric_acid,
  52. 'residual_sugar': residual_sugar,
  53. 'chlorides': chlorides,
  54. 'free_sulfur_dioxide': free_sulfur_dioxide,
  55. 'total_sulfur_dioxide': total_sulfur_dioxide,
  56. 'density': density,
  57. 'pH': pH,
  58. 'sulphates': sulphates,
  59. 'alcohol': alcohol
  60. }
  61. for field, value in inputs.items():
  62. min_val, max_val = validation_rules[field]
  63. if not (min_val <= value <= max_val):
  64. return render_template('index.html', error=f"⚠️ {field.replace('_', ' ').title()} should be between {min_val} and {max_val}")
  65. data = [fixed_acidity, volatile_acidity, citric_acid, residual_sugar, chlorides,
  66. free_sulfur_dioxide, total_sulfur_dioxide, density, pH, sulphates, alcohol]
  67. data = np.array(data).reshape(1, 11)
  68. obj = PredictionPipeline()
  69. predict = obj.predict(data)
  70. # Round the prediction to 2 decimal places for better display
  71. prediction_value = round(float(predict[0]), 2)
  72. return render_template('results.html', prediction=prediction_value)
  73. except ValueError as ve:
  74. return render_template('index.html', error="⚠️ Please enter valid numeric values for all fields")
  75. except Exception as e:
  76. print('The Exception message is: ', e)
  77. return render_template('index.html', error=f"⚠️ Prediction failed: {str(e)}")
  78. else:
  79. return render_template('index.html')
  80. if __name__ == "__main__":
  81. app.run(host="0.0.0.0", port=8080, debug=True)
Tip!

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

Comments

Loading...