Jurk06 commited on
Commit
6123868
·
verified ·
1 Parent(s): 57894cc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -27
app.py CHANGED
@@ -1,34 +1,25 @@
1
- from sklearn.datasets import load_iris
2
- from sklearn.model_selection import train_test_split
3
- from sklearn.ensemble import RandomForestClassifier
4
- from sklearn.metrics import accuracy_score
5
 
6
- # Load dataset (for demonstration)
7
- data = load_iris()
8
- X, y = data.data, data.target
9
 
10
- # Split the dataset into training and testing sets
11
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
12
 
13
- # Initialize RandomForestClassifier
14
- classifier = RandomForestClassifier(n_estimators=100, random_state=42)
15
 
16
- # Train the classifier
17
- classifier.fit(X_train, y_train)
 
 
 
 
 
 
 
 
18
 
 
 
19
 
20
- # Make predictions on the test set
21
- y_pred = classifier.predict(X_test)
22
 
23
- # Calculate accuracy
24
- accuracy = accuracy_score(y_test, y_pred)
25
- print("Accuracy:", accuracy)
26
-
27
-
28
- # prompt: save the model object in pickle file
29
-
30
- import pickle
31
-
32
- # Save the trained model to a pickle file
33
- with open('model.pkl', 'wb') as f:
34
- pickle.dump(classifier, f)
 
1
+ # app.py
 
 
 
2
 
3
+ from flask import Flask, request, jsonify
4
+ import joblib
 
5
 
6
+ app = Flask(__name__)
 
7
 
8
+ # Load trained model
9
+ model = joblib.load('model.pkl')
10
 
11
+ @app.route('/predict', methods=['POST'])
12
+ def predict():
13
+ # Get input data
14
+ data = request.json['data']
15
+
16
+ # Make predictions
17
+ predictions = model.predict(data)
18
+
19
+ # Return predictions
20
+ return jsonify({'predictions': predictions.tolist()})
21
 
22
+ if __name__ == '__main__':
23
+ app.run(debug=True)
24
 
 
 
25