-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
281 lines (237 loc) · 10.4 KB
/
Copy pathapp.py
File metadata and controls
281 lines (237 loc) · 10.4 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import os
import pandas as pd
from flask import Flask, request, jsonify, render_template, send_file
from flask_cors import CORS
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
import joblib
import numpy as np
from datetime import datetime
import json
app = Flask(__name__)
CORS(app) # Enable CORS for frontend integration
# File paths
MODEL_PATH = 'model.pkl'
REAL_TIME_PREDICTIONS_PATH = 'data/real_time_predictions.csv'
BATCH_PREDICTIONS_PATH = 'data/batch_predictions.csv'
ONLINE_DATA_PATH = 'data/online_data.csv'
MODEL_METRICS_PATH = 'data/model_metrics.json'
REQUIRED_FEATURES = ['Pregnancies', "Glucose", "BloodPressure","SkinThickness","Insulin","BMI","DiabetesPedigreeFunction","Age"]
def fetch_and_save_data():
'''Fetch the dataset from an online API'''
url = f'https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv'
columns = REQUIRED_FEATURES + ['Outcome']
data = pd.read_csv(url, header=None, names = columns)
os.makedirs("data", exist_ok=True)
data.to_csv(ONLINE_DATA_PATH, index=False)
print("Dataset downloaded and saved into data folder")
return data
def train_and_save_model():
'''Training the model and save it to a file'''
if not os.path.exists(ONLINE_DATA_PATH):
data = fetch_and_save_data()
else:
data = pd.read_csv(ONLINE_DATA_PATH)
X = data.drop(columns = ['Outcome'])
y = data['Outcome']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size =0.2, random_state=42)
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
# Evaluate the model
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
report = classification_report(y_test, y_pred, output_dict=True)
# Save model metrics
metrics = {
'accuracy': accuracy,
'classification_report': report,
'training_date': datetime.now().isoformat(),
'feature_names': REQUIRED_FEATURES
}
os.makedirs("data", exist_ok=True)
with open(MODEL_METRICS_PATH, 'w') as f:
json.dump(metrics, f, indent=2)
print(f"Model accuracy: {accuracy:.4f}")
# Save the model
joblib.dump(model, MODEL_PATH)
print(f"Model is saved to {MODEL_PATH}")
return model, metrics
def load_model():
'''Load the trained model'''
if not os.path.exists(MODEL_PATH):
print("Model not found Training an new model!!!")
model, metrics = train_and_save_model()
return model
return joblib.load(MODEL_PATH)
model = load_model()
def validate_input(data, required_features):
'''validate input data for missing features'''
missing_features = [feature for feature in required_features if feature not in data]
if missing_features:
raise ValueError(f"Missing feature:{','.join(missing_features)}")
# Frontend route
@app.route('/')
def index():
'''Serve the main dashboard'''
return render_template('index.html')
@app.route('/api/health')
def health_check():
'''Health check endpoint'''
return jsonify({
'status': 'healthy',
'model_loaded': model is not None,
'timestamp': datetime.now().isoformat()
})
@app.route('/api/model-info')
def model_info():
'''Get model information and metrics'''
try:
if os.path.exists(MODEL_METRICS_PATH):
with open(MODEL_METRICS_PATH, 'r') as f:
metrics = json.load(f)
else:
metrics = {'message': 'No metrics available'}
return jsonify({
'required_features': REQUIRED_FEATURES,
'model_type': 'Random Forest Classifier',
'metrics': metrics
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/predict', methods=['POST'])
def predict():
'''Real time prediction endpoint for a specific usecase'''
try:
data = request.get_json()
validate_input(data, REQUIRED_FEATURES)
# Convert input into array
input_data = np.array([data[feature] for feature in REQUIRED_FEATURES]).reshape(1, -1)
# Make prediction and get probability
prediction = model.predict(input_data)
probability = model.predict_proba(input_data)
# Save this prediction into a file
record = {
**data,
"Prediction": int(prediction[0]),
"Probability_No_Diabetes": float(probability[0][0]),
"Probability_Diabetes": float(probability[0][1]),
"Timestamp": datetime.now().isoformat()
}
os.makedirs("data", exist_ok=True)
file_exists = os.path.isfile(REAL_TIME_PREDICTIONS_PATH)
df = pd.DataFrame([record])
df.to_csv(REAL_TIME_PREDICTIONS_PATH, mode='a', index=False, header=not file_exists)
return jsonify({
'prediction': int(prediction[0]),
'prediction_label': 'Diabetes' if prediction[0] == 1 else 'No Diabetes',
'probabilities': {
'no_diabetes': float(probability[0][0]),
'diabetes': float(probability[0][1])
},
'confidence': float(max(probability[0]))
})
except Exception as e:
return jsonify({"error": str(e)}), 400
@app.route('/api/batch-predict', methods=['POST'])
def batch_predict():
'''Batch prediction endpoint'''
try:
# Check if file is provided
if 'file' not in request.files:
return jsonify({'error':'no files uploaded by user'}), 400
file = request.files['file']
batch_data = pd.read_csv(file)
# Validating input data
missing_features = [feature for feature in REQUIRED_FEATURES if feature not in batch_data.columns]
if missing_features:
return jsonify({'error':f"Missing features in batch file: {','.join(missing_features)}"}), 400
# Make predictions
X = batch_data[REQUIRED_FEATURES]
predictions = model.predict(X)
probabilities = model.predict_proba(X)
# Add predictions and probabilities to batch data
batch_data['Prediction'] = predictions
batch_data['Prediction_Label'] = ['Diabetes' if p == 1 else 'No Diabetes' for p in predictions]
batch_data['Probability_No_Diabetes'] = probabilities[:, 0]
batch_data['Probability_Diabetes'] = probabilities[:, 1]
batch_data['Timestamp'] = datetime.now().isoformat()
os.makedirs("data", exist_ok=True)
batch_data.to_csv(BATCH_PREDICTIONS_PATH, index=False)
return jsonify({
'message': 'Batch predictions completed successfully',
'total_predictions': len(batch_data),
'diabetes_cases': int(sum(predictions)),
'output_file': BATCH_PREDICTIONS_PATH
})
except Exception as e:
return jsonify({'error': str(e)}), 400
@app.route('/api/predictions/history')
def get_prediction_history():
'''Get prediction history'''
try:
history_data = []
# Get real-time predictions
if os.path.exists(REAL_TIME_PREDICTIONS_PATH):
rt_df = pd.read_csv(REAL_TIME_PREDICTIONS_PATH)
rt_df['Type'] = 'Real-time'
history_data.append(rt_df)
# Get batch predictions
if os.path.exists(BATCH_PREDICTIONS_PATH):
batch_df = pd.read_csv(BATCH_PREDICTIONS_PATH)
batch_df['Type'] = 'Batch'
history_data.append(batch_df)
if history_data:
combined_df = pd.concat(history_data, ignore_index=True)
# Get recent 50 predictions
recent_predictions = combined_df.tail(50).to_dict('records')
return jsonify({
'predictions': recent_predictions,
'total_count': len(combined_df)
})
else:
return jsonify({'predictions': [], 'total_count': 0})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/statistics')
def get_statistics():
'''Get prediction statistics'''
try:
stats = {}
# Real-time predictions stats
if os.path.exists(REAL_TIME_PREDICTIONS_PATH):
rt_df = pd.read_csv(REAL_TIME_PREDICTIONS_PATH)
stats['realtime'] = {
'total': len(rt_df),
'diabetes_cases': int(rt_df['Prediction'].sum()) if 'Prediction' in rt_df.columns else 0,
'no_diabetes_cases': int(len(rt_df) - rt_df['Prediction'].sum()) if 'Prediction' in rt_df.columns else 0
}
else:
stats['realtime'] = {'total': 0, 'diabetes_cases': 0, 'no_diabetes_cases': 0}
# Batch predictions stats
if os.path.exists(BATCH_PREDICTIONS_PATH):
batch_df = pd.read_csv(BATCH_PREDICTIONS_PATH)
stats['batch'] = {
'total': len(batch_df),
'diabetes_cases': int(batch_df['Prediction'].sum()) if 'Prediction' in batch_df.columns else 0,
'no_diabetes_cases': int(len(batch_df) - batch_df['Prediction'].sum()) if 'Prediction' in batch_df.columns else 0
}
else:
stats['batch'] = {'total': 0, 'diabetes_cases': 0, 'no_diabetes_cases': 0}
return jsonify(stats)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/download/<file_type>')
def download_file(file_type):
'''Download prediction files'''
try:
if file_type == 'realtime' and os.path.exists(REAL_TIME_PREDICTIONS_PATH):
return send_file(REAL_TIME_PREDICTIONS_PATH, as_attachment=True)
elif file_type == 'batch' and os.path.exists(BATCH_PREDICTIONS_PATH):
return send_file(BATCH_PREDICTIONS_PATH, as_attachment=True)
else:
return jsonify({'error': 'File not found'}), 404
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ =='__main__':
app.run(debug=True, host='0.0.0.0', port=5000)