Fancy-yousa commited on
Commit
a96fef4
·
verified ·
1 Parent(s): 089b51b

Delete Webapp/app1.py

Browse files
Files changed (1) hide show
  1. Webapp/app1.py +0 -160
Webapp/app1.py DELETED
@@ -1,160 +0,0 @@
1
- import os
2
- import pickle
3
- from flask import Flask, jsonify, request, render_template
4
-
5
- # ===============================
6
- # 基本路径配置
7
- # ===============================
8
- PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
9
- RESULT_DIR = os.path.join(PROJECT_ROOT, "results")
10
- DATASET_DIR = os.path.join(PROJECT_ROOT, "datasets")
11
-
12
- os.makedirs(RESULT_DIR, exist_ok=True)
13
- os.makedirs(DATASET_DIR, exist_ok=True)
14
-
15
- # ===============================
16
- # Flask App
17
- # ===============================
18
- app = Flask(__name__)
19
-
20
- # ===============================
21
- # 内存缓存
22
- # ===============================
23
- RESULT_CACHE = {} # {dataset_name: results}
24
-
25
-
26
- # ===============================
27
- # PKL IO 工具
28
- # ===============================
29
- def save_result_pkl(dataset, results):
30
- path = os.path.join(RESULT_DIR, f"{dataset}.pkl")
31
- with open(path, "wb") as f:
32
- pickle.dump(results, f)
33
-
34
-
35
- def load_result_pkl(dataset):
36
- path = os.path.join(RESULT_DIR, f"{dataset}.pkl")
37
- if not os.path.exists(path):
38
- return None
39
- with open(path, "rb") as f:
40
- return pickle.load(f)
41
-
42
-
43
- def list_available_datasets():
44
- datasets = set()
45
-
46
- for f in os.listdir(RESULT_DIR):
47
- if f.endswith(".pkl"):
48
- datasets.add(f.replace(".pkl", ""))
49
-
50
- # 默认展示数据集
51
- datasets.add("Authorship")
52
-
53
- return sorted(datasets)
54
-
55
-
56
- # ===============================
57
- # ⚠️ 你自己的 Agent 入口
58
- # ===============================
59
- def run_agent_for_dataset(dataset):
60
- """
61
- ⚠️ 用你自己的 router / agent 替换这里
62
- 必须返回 List[Dict]
63
- """
64
- # ---------------------------
65
- # 示例(占位)
66
- # ---------------------------
67
- return [
68
- {
69
- "algorithm": "UCRFS",
70
- "num_features": 15,
71
- "mean_f1": 0.9233716475,
72
- "mean_auc": 0.9816098520,
73
- "time": 5.75,
74
- "score": 0.9408431088,
75
- },
76
- {
77
- "algorithm": "JMIM",
78
- "num_features": 15,
79
- "mean_f1": 0.918,
80
- "mean_auc": 0.979,
81
- "time": 7.32,
82
- "score": 0.932,
83
- },
84
- ]
85
-
86
-
87
- # ===============================
88
- # 页面
89
- # ===============================
90
- @app.route("/")
91
- def index():
92
- return render_template(
93
- "index.html",
94
- datasets=list_available_datasets(),
95
- default_dataset="Authorship",
96
- )
97
-
98
-
99
- # ===============================
100
- # API:获取结果
101
- # ===============================
102
- @app.route("/api/results")
103
- def get_results():
104
- dataset = request.args.get("dataset", "Authorship")
105
-
106
- # ① 内存缓存
107
- if dataset in RESULT_CACHE:
108
- rank_results = jsonify(RESULT_CACHE[dataset])
109
-
110
- # ② 磁盘 pkl
111
- results = load_result_pkl(dataset)
112
- if results is not None:
113
- RESULT_CACHE[dataset] = results
114
- rank_results = jsonify(results)
115
-
116
- # ③ Agent 实时运行
117
- results = run_agent_for_dataset(dataset)
118
-
119
- # ④ 存储
120
- save_result_pkl(dataset, results)
121
- RESULT_CACHE[dataset] = results
122
-
123
- rank_results = jsonify(results)
124
- leaderboard = rank_results(rank_results)
125
- return leaderboard
126
- # @app.route("/api/results")
127
- # def get_results():
128
- dataset = request.args.get("dataset", "Authorship")
129
-
130
- print(f"[DEBUG] request dataset = {dataset}")
131
-
132
- if dataset in RESULT_CACHE:
133
- print("[DEBUG] hit memory cache")
134
- return jsonify(RESULT_CACHE[dataset])
135
-
136
- results = load_result_pkl(dataset)
137
- if results is not None:
138
- print("[DEBUG] hit pkl cache")
139
- RESULT_CACHE[dataset] = results
140
- return jsonify(results)
141
-
142
- print("[DEBUG] run agent")
143
- results = run_agent_for_dataset(dataset)
144
- print("[DEBUG] agent results =", results)
145
-
146
- save_result_pkl(dataset, results)
147
- RESULT_CACHE[dataset] = results
148
- return jsonify(results)
149
-
150
-
151
- # ===============================
152
- # API:数据集列表
153
- # ===============================
154
- @app.route("/api/datasets")
155
- def api_datasets():
156
- return jsonify(list_available_datasets())
157
-
158
-
159
- if __name__ == "__main__":
160
- app.run(debug=True)