-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
165 lines (129 loc) · 5.28 KB
/
Copy pathapp.py
File metadata and controls
165 lines (129 loc) · 5.28 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
from flask import Flask, render_template, request, send_file, flash, redirect, url_for
import pandas as pd
import os
from werkzeug.utils import secure_filename
from model_predictor import ExoplanetPredictor
from pathlib import Path
import json
app = Flask(__name__)
app.secret_key = 'your-secret-key-change-this-in-production'
# Configuration
UPLOAD_FOLDER = './uploads'
RESULTS_FOLDER = './results'
ALLOWED_EXTENSIONS = {'csv'}
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['RESULTS_FOLDER'] = RESULTS_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
# Create necessary directories
Path(UPLOAD_FOLDER).mkdir(parents=True, exist_ok=True)
Path(RESULTS_FOLDER).mkdir(parents=True, exist_ok=True)
# Initialize predictor
try:
predictor = ExoplanetPredictor()
model_loaded = True
except Exception as e:
print(f"Warning: Could not load model: {e}")
model_loaded = False
def allowed_file(filename):
"""Check if file has allowed extension."""
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def index():
"""Home page with upload form."""
return render_template('index.html', model_loaded=model_loaded)
@app.route('/upload', methods=['POST'])
def upload_file():
"""Handle file upload and make predictions."""
if not model_loaded:
flash('Model not loaded! Please train the model first.', 'error')
return redirect(url_for('index'))
# Check if file was uploaded
if 'file' not in request.files:
flash('No file uploaded', 'error')
return redirect(url_for('index'))
file = request.files['file']
# Check if filename is empty
if file.filename == '':
flash('No file selected', 'error')
return redirect(url_for('index'))
# Validate file type
if not allowed_file(file.filename):
flash('Invalid file type. Please upload a CSV file.', 'error')
return redirect(url_for('index'))
try:
# Save uploaded file
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
# Load and validate CSV
df = pd.read_csv(filepath)
# Check if required features are present
required_features = predictor.feature_columns
missing_features = set(required_features) - set(df.columns)
if missing_features:
flash(f'Missing required columns: {", ".join(missing_features)}', 'error')
os.remove(filepath)
return redirect(url_for('index'))
# Make predictions
results = predictor.predict_from_csv(filepath)
# Save results
result_filename = f"predictions_{filename}"
result_filepath = os.path.join(app.config['RESULTS_FOLDER'], result_filename)
results.to_csv(result_filepath, index=False)
# Get summary statistics
summary = {
'total_samples': len(results),
'predictions': results['prediction'].value_counts().to_dict(),
'avg_confidence': float(results['confidence'].mean()),
'min_confidence': float(results['confidence'].min()),
'max_confidence': float(results['confidence'].max())
}
# Clean up uploaded file
os.remove(filepath)
return render_template('results.html',
summary=summary,
results=results.head(50).to_html(classes='table table-striped', index=False),
results_data=results.head(50),
result_filename=result_filename,
total_rows=len(results))
except Exception as e:
flash(f'Error processing file: {str(e)}', 'error')
if os.path.exists(filepath):
os.remove(filepath)
return redirect(url_for('index'))
@app.route('/download/<filename>')
def download_file(filename):
"""Download prediction results."""
filepath = os.path.join(app.config['RESULTS_FOLDER'], filename)
if os.path.exists(filepath):
return send_file(filepath, as_attachment=True)
else:
flash('File not found', 'error')
return redirect(url_for('index'))
@app.route('/api/predict', methods=['POST'])
def api_predict():
"""API endpoint for predictions (JSON)."""
if not model_loaded:
return {'error': 'Model not loaded'}, 500
try:
data = request.get_json()
if not data:
return {'error': 'No data provided'}, 400
# Make prediction
result = predictor.predict(data)
return result, 200
except Exception as e:
return {'error': str(e)}, 500
@app.route('/model-info')
def model_info():
"""Display model information."""
if not model_loaded:
flash('Model not loaded', 'error')
return redirect(url_for('index'))
# Load metadata
metadata_path = os.path.join(predictor.model_dir, 'model_metadata.json')
with open(metadata_path, 'r') as f:
metadata = json.load(f)
return render_template('model_info.html', metadata=metadata)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)