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