Esvanth commited on
Commit
78264fe
Β·
verified Β·
1 Parent(s): 568591d

Add app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ MindScan β€” Flask Backend
3
+ NCI H9DAI Research Project 2026
4
+
5
+ Loads all 12 models (3 classical + XLM-RoBERTa per dataset)
6
+ and serves predictions via a single /predict endpoint.
7
+
8
+ Run: python app.py
9
+ Open: http://localhost:5000
10
+ """
11
+
12
+ from flask import Flask, request, jsonify, render_template
13
+ import os, time
14
+
15
+ # Import our prediction module
16
+ from predict import load_all_models, predict_all, models_loaded
17
+
18
+ app = Flask(__name__)
19
+
20
+ # ─────────────────────────────────────────────────────────────────
21
+ # Load models once at startup β€” not per request
22
+ # ─────────────────────────────────────────────────────────────────
23
+ print("\n" + "="*55)
24
+ print(" MindScan β€” Starting up")
25
+ print("="*55)
26
+ print(" Loading models... (XLM-RoBERTa takes ~30s on CPU)")
27
+
28
+ start = time.time()
29
+ load_all_models()
30
+ elapsed = time.time() - start
31
+
32
+ print(f" βœ… All models loaded in {elapsed:.1f}s")
33
+ print(f" 🌐 Open: http://localhost:5000")
34
+ print("="*55 + "\n")
35
+
36
+
37
+ # ─────────────────────────────────────────────────────────────────
38
+ # ROUTES
39
+ # ─────────────────────────────────────────────────────────────────
40
+
41
+ @app.route('/')
42
+ def index():
43
+ """Serve the main UI."""
44
+ return render_template('index.html')
45
+
46
+
47
+ @app.route('/predict', methods=['POST'])
48
+ def predict():
49
+ """
50
+ POST /predict
51
+ Body: { "text": "your text here" }
52
+ Returns: full prediction JSON from all 12 models
53
+ """
54
+ data = request.get_json()
55
+
56
+ if not data or 'text' not in data:
57
+ return jsonify({'error': 'Missing "text" field in request body'}), 400
58
+
59
+ text = data['text'].strip()
60
+
61
+ if not text:
62
+ return jsonify({'error': 'Text cannot be empty'}), 400
63
+
64
+ if len(text) > 5000:
65
+ return jsonify({'error': 'Text too long (max 5000 characters)'}), 400
66
+
67
+ if not models_loaded():
68
+ return jsonify({'error': 'Models not loaded yet β€” try again in a moment'}), 503
69
+
70
+ try:
71
+ t0 = time.time()
72
+ result = predict_all(text)
73
+ result['processing_time_ms'] = round((time.time() - t0) * 1000)
74
+ return jsonify(result)
75
+ except Exception as e:
76
+ print(f"Prediction error: {e}")
77
+ return jsonify({'error': f'Prediction failed: {str(e)}'}), 500
78
+
79
+
80
+ @app.route('/health')
81
+ def health():
82
+ """Quick health check endpoint."""
83
+ return jsonify({
84
+ 'status': 'ok',
85
+ 'models_ready': models_loaded()
86
+ })
87
+
88
+
89
+ # ─────────────────────────────────────────────────────────────────
90
+ # START
91
+ # ─────────────────────────────────────────────────────────────────
92
+ if __name__ == '__main__':
93
+ app.run(debug=False, host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))