Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,25 @@
|
|
1 |
-
|
2 |
-
from sklearn.model_selection import train_test_split
|
3 |
-
from sklearn.ensemble import RandomForestClassifier
|
4 |
-
from sklearn.metrics import accuracy_score
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
X, y = data.data, data.target
|
9 |
|
10 |
-
|
11 |
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|