File size: 4,882 Bytes
94e0fc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import os
import sys
import pickle
import json
from flask import Flask, jsonify, request, render_template

# Add project root to sys.path to import leaderboard
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from leaderboard import rank_results
# ===============================
# 基本路径配置
# ===============================
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
RESULT_DIR = os.path.join(PROJECT_ROOT, "results")
DATASET_DIR = os.path.join(PROJECT_ROOT, "datasets")

os.makedirs(RESULT_DIR, exist_ok=True)
os.makedirs(DATASET_DIR, exist_ok=True)

# ===============================
# Flask App
# ===============================
app = Flask(__name__)

# ===============================
# 内存缓存
# ===============================
RESULT_CACHE = {}  # {dataset_name: results}


# ===============================
# PKL IO 工具
# ===============================
def save_result_json(dataset, results):
    path = os.path.join(RESULT_DIR, f"{dataset}.json")
    with open(path, "w", encoding='utf-8') as f:
        json.dump(results, f, indent=4)


def load_result_json(dataset):
    path = os.path.join(RESULT_DIR, f"{dataset}.json")
    if not os.path.exists(path):
        return None
    with open(path, 'r', encoding='utf-8') as f:
        return json.load(f)


def list_available_datasets():
    datasets = set()

    for f in os.listdir(RESULT_DIR):
        if f.endswith(".json"):
            datasets.add(f.replace(".json", ""))

    # 默认展示数据集
    datasets.add("Authorship")

    return sorted(datasets)


# ===============================
# ⚠️ 你自己的 Agent 入口
# ===============================
def run_agent_for_dataset(dataset):
    """
    ⚠️ 用你自己的 router / agent 替换这里
    必须返回 List[Dict]
    """
    # ---------------------------
    # 示例(占位)
    # ---------------------------
    return [
        
    ]
# {
#             "algorithm": "shibaile",
#             "num_features": 15,
#             "mean_f1": 0.9233716475,
#             "mean_auc": 0.9816098520,
#             "time": 5.75,
#             "score": 0.9408431088,
#         },
#         {
#             "algorithm": "JMchongxinzailai",
#             "num_features": 15,
#             "mean_f1": 0.918,
#             "mean_auc": 0.979,
#             "time": 7.32,
#             "score": 0.932,
#         },

# ===============================
# 页面
# ===============================
@app.route("/")
def index():
    return render_template(
        "index.html",
        datasets=list_available_datasets(),
        default_dataset="Authorship",
    )


# ===============================
# API:获取结果
# ===============================
@app.route("/api/results")
def get_results():
    dataset = request.args.get("dataset", "Authorship")

    # ① 内存缓存 (Disabled for debugging)
    # if dataset in RESULT_CACHE:
    #     print("------------------------------------------------------------------zoude cache")
    #     return jsonify(RESULT_CACHE[dataset])
    
    # ② 磁盘 pkl/json
    results = load_result_json(dataset)
    print(111,results)
    if results is not None:
        print("------------------------------------------------------------------zoude json\n",results)
        RESULT_CACHE[dataset] = results
        leaderboard = rank_results(results)
        return jsonify(leaderboard)

    # ③ Agent 实时运行
    results = run_agent_for_dataset(dataset)
    print(222,results)

    # ④ 存储
    if results and len(results) > 0:
        save_result_json(dataset, results)
        RESULT_CACHE[dataset] = results
    
    print("------------------------------------------------------------------zoude agent")
    leaderboard = rank_results(results)
    # 
    return jsonify(leaderboard)
    # print(333,leaderboard)
# @app.route("/api/results")
# def get_results():
    dataset = request.args.get("dataset", "Authorship")

    print(f"[DEBUG] request dataset = {dataset}")

    if dataset in RESULT_CACHE:
        print("[DEBUG] hit memory cache")
        return jsonify(RESULT_CACHE[dataset])

    results = load_result_pkl(dataset)
    if results is not None:
        print("[DEBUG] hit pkl cache")
        RESULT_CACHE[dataset] = results
        return jsonify(results)

    print("[DEBUG] run agent")
    results = run_agent_for_dataset(dataset)
    print("[DEBUG] agent results =", results)

    save_result_pkl(dataset, results)
    RESULT_CACHE[dataset] = results
    return jsonify(results)


# ===============================
# API:数据集列表
# ===============================
@app.route("/api/datasets")
def api_datasets():
    return jsonify(list_available_datasets())


if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.run(host="0.0.0.0", port=port, debug=True)