-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_29.py
More file actions
105 lines (81 loc) · 2.77 KB
/
05_29.py
File metadata and controls
105 lines (81 loc) · 2.77 KB
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
'''
lsof -i:5000 #search for all hosts with that port 5000
kill -9 <PID> #this ends the hosts running in the background with port 5000
'''
import logging
# Logging Configuration
logging.basicConfig(filename='err.log', level=logging.DEBUG)
logging.debug('This is a debug msg')
###########################################
# Flask Server Code
###########################################
from flask import Flask, request
import requests
import threading
import time
import os
app = Flask(__name__)
shutdown_event = threading.Event()
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
return 'This is a GET request'
elif request.method == 'POST':
return 'This is a POST request'
def shutdown_server():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
print('Not running with the Werkzeug Server')
else:
func()
print('Server shutting down...')
@app.route('/shutdown', methods=['POST'])
def shutdown():
shutdown_event.set()
shutdown_server()
return 'Server shutdown initiated'
def send_data():
time.sleep(1) # Give the server a moment to start
resp = requests.post('http://127.0.0.1:5000/')
if resp.request.method == 'POST':
print('This was a POST method call')
# Send shutdown request
resp = requests.post('http://127.0.0.1:5000/shutdown')
if resp.status_code == 200:
print('Server shutdown initiated')
def run_app():
app.run()
if __name__ == '__main__':
# Start Flask server in a separate thread
flask_thread = threading.Thread(target=run_app)
flask_thread.start()
# Send POST request
send_data()
# Wait for shutdown event
shutdown_event.wait()
print('Flask server has been shutdown')
###########################################
# Scikit-Learn Code
###########################################
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
# Load Iris dataset
iris = load_iris()
X, y = iris.data, iris.target
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Train a Decision Tree classifier on the training set
clf = DecisionTreeClassifier(random_state=42)
clf.fit(X_train, y_train)
# Make predictions on the testing set
y_pred = clf.predict(X_test)
# Calculate the accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.2f}')
# Optional: Print feature importances
feature_importances = clf.feature_importances_
print("Feature importances:")
for feature, importance in zip(iris.feature_names, feature_importances):
print(f'{feature}: {importance:.2f}')