-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
281 lines (232 loc) · 8.01 KB
/
Copy pathapp.py
File metadata and controls
281 lines (232 loc) · 8.01 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
"""
Flask Web Application for Code Inspector
Provides a web interface for code evaluation.
"""
import os
from flask import Flask, render_template, request, jsonify, send_file, redirect, url_for
from werkzeug.utils import secure_filename
import json
from datetime import datetime
from main import CodeInspector
# Initialize Flask app
app = Flask(__name__)
app.config['SECRET_KEY'] = 'codeinspector-secret-key-change-in-production'
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
# Create necessary directories
os.makedirs('uploads', exist_ok=True)
os.makedirs('reports', exist_ok=True)
# Initialize Code Inspector
inspector = CodeInspector()
# Store results temporarily (in production, use database)
evaluation_cache = {}
@app.route('/')
def index():
"""Home page."""
return render_template('index.html')
@app.route('/evaluate', methods=['POST'])
def evaluate():
"""Evaluate code submission."""
try:
# Get form data
evaluation_type = request.form.get('evaluation_type', 'code')
language = request.form.get('language', 'python')
requirements = request.form.get('requirements', '')
# Get reference code
if 'reference_file' in request.files and request.files['reference_file'].filename:
reference_file = request.files['reference_file']
reference_code = reference_file.read().decode('utf-8')
else:
reference_code = request.form.get('reference_code', '')
if not reference_code:
return jsonify({
'error': 'Reference code is required',
'status': 'failed'
}), 400
# Get student code based on evaluation type
if evaluation_type == 'github':
github_url = request.form.get('github_url', '')
if not github_url:
return jsonify({
'error': 'GitHub URL is required',
'status': 'failed'
}), 400
# Evaluate GitHub project
results = inspector.evaluate_github_project(
github_url,
reference_code,
requirements if requirements else None,
language
)
student_info = {
'github_url': github_url,
'evaluation_type': 'GitHub Repository'
}
else: # code evaluation
if 'student_file' in request.files and request.files['student_file'].filename:
student_file = request.files['student_file']
student_code = student_file.read().decode('utf-8')
student_info = {
'filename': student_file.filename,
'evaluation_type': 'File Upload'
}
else:
student_code = request.form.get('student_code', '')
student_info = {
'evaluation_type': 'Direct Input'
}
if not student_code:
return jsonify({
'error': 'Student code is required',
'status': 'failed'
}), 400
# Evaluate code
results = inspector.evaluate_code(
student_code,
reference_code,
requirements if requirements else None,
language
)
# Check for errors
if results.get('status') == 'failed':
return jsonify(results), 400
# Generate evaluation ID
eval_id = datetime.now().strftime('%Y%m%d%H%M%S')
evaluation_cache[eval_id] = {
'results': results,
'student_info': student_info,
'requirements': requirements,
'timestamp': datetime.now().isoformat()
}
# Return results
return jsonify({
'status': 'success',
'eval_id': eval_id,
'results': results
})
except Exception as e:
return jsonify({
'error': str(e),
'status': 'failed'
}), 500
@app.route('/report/<eval_id>')
def view_report(eval_id):
"""View evaluation report."""
if eval_id not in evaluation_cache:
return "Report not found", 404
data = evaluation_cache[eval_id]
return render_template(
'report.html',
results=data['results'],
student_info=data['student_info'],
requirements=data.get('requirements')
)
@app.route('/download/<eval_id>/<format>')
def download_report(eval_id, format):
"""Download evaluation report."""
if eval_id not in evaluation_cache:
return "Report not found", 404
if format not in ['text', 'html', 'json']:
return "Invalid format", 400
data = evaluation_cache[eval_id]
# Generate report
if format == 'text':
content = inspector.report_generator.generate_text_report(
data['results'],
data['student_info'],
data.get('requirements')
)
mimetype = 'text/plain'
extension = 'txt'
elif format == 'html':
content = inspector.report_generator.generate_html_report(
data['results'],
data['student_info'],
data.get('requirements')
)
mimetype = 'text/html'
extension = 'html'
else: # json
content = inspector.report_generator.generate_json_report(
data['results'],
data['student_info'],
data.get('requirements')
)
mimetype = 'application/json'
extension = 'json'
# Save temporarily
filename = f"evaluation_report_{eval_id}.{extension}"
filepath = os.path.join('reports', filename)
with open(filepath, 'w', encoding='utf-8') as f:
f.write(content)
return send_file(
filepath,
mimetype=mimetype,
as_attachment=True,
download_name=filename
)
@app.route('/batch-evaluate', methods=['POST'])
def batch_evaluate():
"""Batch evaluation endpoint."""
try:
# Get reference code
reference_code = request.form.get('reference_code', '')
requirements = request.form.get('requirements', '')
language = request.form.get('language', 'python')
if not reference_code:
return jsonify({
'error': 'Reference code is required',
'status': 'failed'
}), 400
# Get student codes from uploaded files
student_codes = []
files = request.files.getlist('student_files')
for file in files:
if file and file.filename:
code = file.read().decode('utf-8')
student_codes.append(code)
if not student_codes:
return jsonify({
'error': 'No student files provided',
'status': 'failed'
}), 400
# Batch evaluate
results = inspector.batch_evaluate(
student_codes,
reference_code,
requirements if requirements else None,
language
)
# Store results
eval_id = datetime.now().strftime('%Y%m%d%H%M%S')
evaluation_cache[eval_id] = {
'results': results,
'batch': True,
'timestamp': datetime.now().isoformat()
}
return jsonify({
'status': 'success',
'eval_id': eval_id,
'results': results,
'total': len(results)
})
except Exception as e:
return jsonify({
'error': str(e),
'status': 'failed'
}), 500
@app.route('/api/health')
def health_check():
"""Health check endpoint."""
return jsonify({
'status': 'healthy',
'timestamp': datetime.now().isoformat()
})
if __name__ == '__main__':
print("="*60)
print("Code Inspector Web Interface")
print("="*60)
print("Starting Flask server...")
print("Open your browser and navigate to: http://localhost:5000")
print("="*60)
app.run(debug=True, host='0.0.0.0', port=5000)