CostPrediction/demo_standalone/server.py

49 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from pathlib import Path
from flask import Flask, jsonify, request, send_from_directory
from flask_cors import CORS
from demo_service import DemoModelService
BASE_DIR = Path(__file__).resolve().parent
STATIC_DIR = BASE_DIR / "frontend"
DATASET_PATH = BASE_DIR / "data" / "demo_equipment_costs.csv"
def create_app():
app = Flask(__name__, static_folder=None)
CORS(app)
@app.get("/api/demo/algorithms")
def demo_algorithms():
service = DemoModelService(DATASET_PATH)
return jsonify({"algorithms": service.get_algorithms()})
@app.get("/api/demo/dataset")
def demo_dataset():
service = DemoModelService(DATASET_PATH)
return jsonify(service.get_dataset_summary())
@app.post("/api/demo/run")
def demo_run():
payload = request.get_json(silent=True) or {}
service = DemoModelService(DATASET_PATH)
return jsonify(service.run_demo(payload.get("algorithms")))
@app.get("/")
@app.get("/<path:path>")
def frontend(path=""):
file_path = STATIC_DIR / path
if path and file_path.exists() and file_path.is_file():
return send_from_directory(STATIC_DIR, path)
return send_from_directory(STATIC_DIR, "index.html")
return app
if __name__ == "__main__":
app = create_app()
print("算法演示服务已启动http://127.0.0.1:5001/algorithm-demo")
app.run(host="127.0.0.1", port=5001, debug=False)