rishi002 commited on
Commit
788a979
·
verified ·
1 Parent(s): 2cc5f9e

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +48 -0
  2. requirements.txt +5 -0
  3. scaler.pkl +3 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ import joblib
5
+
6
+ app = Flask(__name__)
7
+
8
+ # Load the saved model and scaler
9
+ model = tf.keras.models.load_model('aqi_model.h5')
10
+ scaler = joblib.load('scaler.pkl')
11
+
12
+ @app.route('/predict', methods=['POST'])
13
+ def predict():
14
+ try:
15
+ # Get the input features from the JSON request
16
+ data = request.get_json()
17
+ features = [
18
+ data['PM10'],
19
+ data['PM2.5'],
20
+ data['NO2'],
21
+ data['O3'],
22
+ data['CO'],
23
+ data['SO2'],
24
+ data['NH3']
25
+ ]
26
+
27
+ # Convert to numpy array and reshape for a single prediction
28
+ features_array = np.array(features).reshape(1, -1)
29
+
30
+ # Scale the input features using the loaded scaler
31
+ features_scaled = scaler.transform(features_array)
32
+
33
+ # Make prediction using the loaded model
34
+ prediction = model.predict(features_scaled)
35
+ predicted_aqi = prediction[0][0]
36
+
37
+ # Convert the result to a standard Python float
38
+ predicted_aqi = float(predicted_aqi)
39
+
40
+ # Return the predicted AQI
41
+ return jsonify({'predicted_aqi': predicted_aqi})
42
+
43
+ except Exception as e:
44
+ return jsonify({'error': str(e)}), 400
45
+
46
+ if __name__ == "__main__":
47
+ app.run(host='0.0.0.0', port=8080)
48
+
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ Flask==2.3.2
2
+ numpy==1.23.5
3
+ tensorflow==2.17.0
4
+ scikit-learn==1.0.2
5
+ joblib==1.2.0
scaler.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3a08b0155563bdc6a5b681384119c8b5c2cbe1a468308845e9daf0ce168c6079
3
+ size 1135