File size: 3,336 Bytes
016c645 | 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 | """
MindScan β Flask Backend
NCI H9DAI Research Project 2026
Loads all 12 models (3 classical + XLM-RoBERTa per dataset)
and serves predictions via a single /predict endpoint.
Run: python app.py
Open: http://localhost:5000
"""
from flask import Flask, request, jsonify, render_template
import os, time
# Import our prediction module
from predict import load_all_models, predict_all, models_loaded
app = Flask(__name__)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Load models once at startup β not per request
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
print("\n" + "="*55)
print(" MindScan β Starting up")
print("="*55)
print(" Loading models... (XLM-RoBERTa takes ~30s on CPU)")
start = time.time()
load_all_models()
elapsed = time.time() - start
print(f" β
All models loaded in {elapsed:.1f}s")
print(f" π Open: http://localhost:5000")
print("="*55 + "\n")
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# ROUTES
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.route('/')
def index():
"""Serve the main UI."""
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
"""
POST /predict
Body: { "text": "your text here" }
Returns: full prediction JSON from all 12 models
"""
data = request.get_json()
if not data or 'text' not in data:
return jsonify({'error': 'Missing "text" field in request body'}), 400
text = data['text'].strip()
if not text:
return jsonify({'error': 'Text cannot be empty'}), 400
if len(text) > 5000:
return jsonify({'error': 'Text too long (max 5000 characters)'}), 400
if not models_loaded():
return jsonify({'error': 'Models not loaded yet β try again in a moment'}), 503
try:
t0 = time.time()
result = predict_all(text)
result['processing_time_ms'] = round((time.time() - t0) * 1000)
return jsonify(result)
except Exception as e:
print(f"Prediction error: {e}")
return jsonify({'error': f'Prediction failed: {str(e)}'}), 500
@app.route('/health')
def health():
"""Quick health check endpoint."""
return jsonify({
'status': 'ok',
'models_ready': models_loaded()
})
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# START
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port=5000)
|