routes.py 689 B

12345678910111213141516171819202122232425
  1. from flask import Blueprint, request, jsonify
  2. from .model import predict
  3. import pandas as pd
  4. # 创建蓝图 (Blueprint),用于分离路由
  5. bp = Blueprint('routes', __name__)
  6. # 路由:预测
  7. @bp.route('/predict', methods=['POST'])
  8. def predict_route():
  9. try:
  10. # 从请求中获取数据
  11. data = request.get_json()
  12. # 将数据转为 pandas DataFrame,确保数据列名一致
  13. input_data = pd.DataFrame([data])
  14. # 调用模型进行预测
  15. predictions = predict(input_data)
  16. # 返回预测结果
  17. return jsonify({'predictions': predictions}), 200
  18. except Exception as e:
  19. return jsonify({'error': str(e)}), 400