Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,239 +1,211 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
# app = Flask(__name__)
|
11 |
-
# CORS(app)
|
12 |
-
# app.secret_key = 'supersecretkey'
|
13 |
-
|
14 |
-
# base_dir = os.path.dirname(os.path.abspath(__file__))
|
15 |
-
|
16 |
-
# DATA_DIR = os.path.join(base_dir, '/app/data')
|
17 |
-
# RESULTS_DIR = os.path.join(base_dir, '/app/results')
|
18 |
-
|
19 |
-
# # 实例化 EloRank 系统
|
20 |
-
# elo_rank_system = EloRank()
|
21 |
-
|
22 |
-
# # 初始化 Elo 排名的模型
|
23 |
-
# models = ['output_path_4o', 'output_path_miniomni', 'output_path_speechgpt', 'output_path_funaudio', 'output_path_4o_cascade', 'output_path_4o_llama_omni']
|
24 |
-
# for model in models:
|
25 |
-
# elo_rank_system.add_model(model)
|
26 |
-
|
27 |
-
# import os
|
28 |
-
|
29 |
-
# def print_directory_structure(start_path, indent=''):
|
30 |
-
# for item in os.listdir(start_path):
|
31 |
-
# item_path = os.path.join(start_path, item)
|
32 |
-
# if os.path.isdir(item_path):
|
33 |
-
# print(f"{indent}📁 {item}/")
|
34 |
-
# print_directory_structure(item_path, indent + ' ')
|
35 |
-
# else:
|
36 |
-
# print(f"{indent}📄 {item}")
|
37 |
-
|
38 |
-
|
39 |
-
# def load_test_data(task):
|
40 |
-
# """Load the JSON file corresponding to the selected task"""
|
41 |
-
# # 调用函数,打印当前目录结构
|
42 |
-
# with open('/app/test_text.txt', 'r') as file:
|
43 |
-
# content = file.read()
|
44 |
-
# print(content)
|
45 |
-
# # with open(os.path.join(DATA_DIR, f"{task}.json"), "r", encoding='utf-8') as f:
|
46 |
-
# # test_data = json.load(f)
|
47 |
-
|
48 |
-
# try:
|
49 |
-
# with open(os.path.join(DATA_DIR, f"{task}.json"), "r", encoding='utf-8') as f:
|
50 |
-
# test_data = json.load(f)
|
51 |
-
# except FileNotFoundError:
|
52 |
-
# return jsonify({"message": "Test data file not found"}), 400
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
# item['input_path'] = f"/app/static/audio{item['input_path']}"
|
58 |
-
# item['output_path_4o'] = f"/app/static/audio{item['output_path_4o']}"
|
59 |
-
# item['output_path_miniomni'] = f"/app/static/audio{item['output_path_miniomni']}"
|
60 |
-
# item['output_path_speechgpt'] = f"/app/static/audio{item['output_path_speechgpt']}"
|
61 |
-
# item['output_path_funaudio'] = f"/app/static/audio{item['output_path_funaudio']}"
|
62 |
-
# item['output_path_4o_cascade'] = f"/app/static/audio{item['output_path_4o_cascade']}"
|
63 |
-
# item['output_path_4o_llama_omni'] = f"/app/static/audio{item['output_path_4o_llama_omni']}"
|
64 |
-
|
65 |
-
# return test_data
|
66 |
-
|
67 |
-
|
68 |
-
# def save_result(task, username, result_data, session_id):
|
69 |
-
# """Save user's result in a separate file"""
|
70 |
-
# file_path = os.path.join(RESULTS_DIR, f"{task}_{username}_{session_id}.jsonl")
|
71 |
-
# # 获取所有模型的 Elo 分数
|
72 |
-
# elo_scores = {model: elo_rank_system.get_rating(model) for model in models}
|
73 |
-
|
74 |
-
# # 添加 Elo 分数和时间戳到结果数据
|
75 |
-
# result_data['elo_scores'] = elo_scores
|
76 |
-
# result_data['timestamp'] = datetime.now().isoformat()
|
77 |
-
# with open(file_path, "a", encoding='utf-8') as f:
|
78 |
-
# f.write(json.dumps(result_data) + "\n")
|
79 |
-
|
80 |
-
# @app.route('/start_test', methods=['POST'])
|
81 |
-
# def start_test():
|
82 |
-
# """Initiate the test for a user with the selected task"""
|
83 |
-
# data = request.json
|
84 |
-
# task = data['task']
|
85 |
-
# username = data['username']
|
86 |
-
|
87 |
-
# # Load the test data
|
88 |
-
# test_data = load_test_data(task)
|
89 |
-
# if not test_data:
|
90 |
-
# return jsonify({"message": "No test data available"}), 400
|
91 |
-
|
92 |
-
# # Shuffle test data for the user
|
93 |
-
# random.shuffle(test_data)
|
94 |
-
|
95 |
-
# # Generate a unique session ID (for example using uuid)
|
96 |
-
# session_id = str(uuid.uuid4())
|
97 |
-
|
98 |
-
# # Store in session
|
99 |
-
# session['task'] = task
|
100 |
-
# session['username'] = username
|
101 |
-
# session['test_data'] = test_data
|
102 |
-
# session['current_index'] = 0
|
103 |
-
# session['session_id'] = session_id # Store the session ID in the session
|
104 |
-
|
105 |
-
# task_description = test_data[0].get('task_description', '')
|
106 |
-
|
107 |
-
# print(session)
|
108 |
-
|
109 |
-
|
110 |
-
# return jsonify({"message": "Test started", "total_tests": len(test_data), "task_description": task_description})
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
# @app.route('/next_test', methods=['GET'])
|
115 |
-
# def next_test():
|
116 |
-
# """Serve the next test item"""
|
117 |
-
# # if 'current_index' not in session or 'test_data' not in session:
|
118 |
-
# # return jsonify({"message": "No active test found"}), 400
|
119 |
-
|
120 |
-
# if 'current_index' not in session or 'test_data' not in session:
|
121 |
-
# # 转换 session 为字典避免序列化错误
|
122 |
-
# print("Debug Session:", dict(session)) # 调试信息
|
123 |
-
# return jsonify({"message": "Session data missing"}), 400
|
124 |
-
|
125 |
-
|
126 |
-
# current_index = session['current_index']
|
127 |
-
# test_data = session['test_data']
|
128 |
-
|
129 |
-
# if current_index >= len(test_data):
|
130 |
-
# # Return the "Test completed" message when all tests are done
|
131 |
-
# return jsonify({"message": "Test completed"}), 200
|
132 |
-
|
133 |
-
# # 使用 EloRank 的 sample_next_match 来选择两款模型
|
134 |
-
# selected_models = elo_rank_system.sample_next_match()
|
135 |
-
|
136 |
-
# if not selected_models or len(selected_models) != 2:
|
137 |
-
# return jsonify({"message": "Error selecting models"}), 500
|
138 |
-
|
139 |
-
# # Serve test data with the two selected models
|
140 |
-
# current_test = test_data[current_index]
|
141 |
-
# session['selected_models'] = selected_models
|
142 |
-
# session['current_index'] += 1
|
143 |
-
|
144 |
-
# return jsonify({
|
145 |
-
# "text": current_test["text"],
|
146 |
-
# "input_path": current_test["input_path"],
|
147 |
-
# "model_a": selected_models[0],
|
148 |
-
# "model_b": selected_models[1],
|
149 |
-
# "audio_a": current_test[selected_models[0]],
|
150 |
-
# "audio_b": current_test[selected_models[1]]
|
151 |
-
# })
|
152 |
|
153 |
-
|
154 |
-
# def submit_result():
|
155 |
-
# """Submit the user's result and save it"""
|
156 |
-
# data = request.json
|
157 |
-
# chosen_model = data['chosen_model']
|
158 |
|
159 |
-
|
160 |
-
|
161 |
-
# current_index = session.get('current_index') - 1 # Subtract since we increment after serving
|
162 |
-
# session_id = session.get('session_id') # Get the session ID
|
163 |
|
164 |
-
#
|
165 |
-
|
166 |
|
167 |
-
#
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
|
172 |
-
|
173 |
-
# "name": username,
|
174 |
-
# "chosen_model": chosen_model,
|
175 |
-
# "model_a": model_a,
|
176 |
-
# "model_b": model_b,
|
177 |
-
# "result": {
|
178 |
-
# model_a: 1 if chosen_model == 'A' else 0,
|
179 |
-
# model_b: 1 if chosen_model == 'B' else 0
|
180 |
-
# }
|
181 |
-
# }
|
182 |
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
|
|
|
|
|
|
|
|
187 |
|
188 |
-
# # 更新 Elo 排名系统
|
189 |
-
# if chosen_model == 'A':
|
190 |
-
# elo_rank_system.record_match(model_a, model_b)
|
191 |
-
# else:
|
192 |
-
# elo_rank_system.record_match(model_b, model_a)
|
193 |
|
194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
|
202 |
-
|
203 |
-
#
|
204 |
-
|
205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
|
207 |
-
# if __name__ == '__main__':
|
208 |
-
# if not os.path.exists(RESULTS_DIR):
|
209 |
-
# os.makedirs(RESULTS_DIR)
|
210 |
-
# # 允许局域网访问
|
211 |
-
# app.run(host="0.0.0.0", debug=True, port=8080)
|
212 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
|
|
|
|
|
214 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import random
|
4 |
+
import uuid
|
5 |
+
from flask import Flask, request, jsonify, session, render_template
|
6 |
+
from flask_cors import CORS
|
7 |
+
from datetime import datetime
|
8 |
+
from elo_rank import EloRank
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
app = Flask(__name__)
|
11 |
+
CORS(app)
|
12 |
+
app.secret_key = 'supersecretkey'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
base_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
DATA_DIR = os.path.join(base_dir, '/app/data')
|
17 |
+
RESULTS_DIR = os.path.join(base_dir, '/app/results')
|
|
|
|
|
18 |
|
19 |
+
# 实例化 EloRank 系统
|
20 |
+
elo_rank_system = EloRank()
|
21 |
|
22 |
+
# 初始化 Elo 排名的模型
|
23 |
+
models = ['output_path_4o', 'output_path_miniomni', 'output_path_speechgpt', 'output_path_funaudio', 'output_path_4o_cascade', 'output_path_4o_llama_omni']
|
24 |
+
for model in models:
|
25 |
+
elo_rank_system.add_model(model)
|
26 |
|
27 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
def print_directory_structure(start_path, indent=''):
|
30 |
+
for item in os.listdir(start_path):
|
31 |
+
item_path = os.path.join(start_path, item)
|
32 |
+
if os.path.isdir(item_path):
|
33 |
+
print(f"{indent}📁 {item}/")
|
34 |
+
print_directory_structure(item_path, indent + ' ')
|
35 |
+
else:
|
36 |
+
print(f"{indent}📄 {item}")
|
37 |
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
def load_test_data(task):
|
40 |
+
"""Load the JSON file corresponding to the selected task"""
|
41 |
+
# 调用函数,打印当前目录结构
|
42 |
+
with open('/app/test_text.txt', 'r') as file:
|
43 |
+
content = file.read()
|
44 |
+
print(content)
|
45 |
+
# with open(os.path.join(DATA_DIR, f"{task}.json"), "r", encoding='utf-8') as f:
|
46 |
+
# test_data = json.load(f)
|
47 |
|
48 |
+
try:
|
49 |
+
with open(os.path.join(DATA_DIR, f"{task}.json"), "r", encoding='utf-8') as f:
|
50 |
+
test_data = json.load(f)
|
51 |
+
except FileNotFoundError:
|
52 |
+
return jsonify({"message": "Test data file not found"}), 400
|
53 |
|
54 |
+
|
55 |
+
# 更新音频路径,将它们指向 Flask 静态文件夹
|
56 |
+
for item in test_data:
|
57 |
+
item['input_path'] = f"/app/static/audio{item['input_path']}"
|
58 |
+
item['output_path_4o'] = f"/app/static/audio{item['output_path_4o']}"
|
59 |
+
item['output_path_miniomni'] = f"/app/static/audio{item['output_path_miniomni']}"
|
60 |
+
item['output_path_speechgpt'] = f"/app/static/audio{item['output_path_speechgpt']}"
|
61 |
+
item['output_path_funaudio'] = f"/app/static/audio{item['output_path_funaudio']}"
|
62 |
+
item['output_path_4o_cascade'] = f"/app/static/audio{item['output_path_4o_cascade']}"
|
63 |
+
item['output_path_4o_llama_omni'] = f"/app/static/audio{item['output_path_4o_llama_omni']}"
|
64 |
+
|
65 |
+
return test_data
|
66 |
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
def save_result(task, username, result_data, session_id):
|
69 |
+
"""Save user's result in a separate file"""
|
70 |
+
file_path = os.path.join(RESULTS_DIR, f"{task}_{username}_{session_id}.jsonl")
|
71 |
+
# 获取所有模型的 Elo 分数
|
72 |
+
elo_scores = {model: elo_rank_system.get_rating(model) for model in models}
|
73 |
+
|
74 |
+
# 添加 Elo 分数和时间戳到结果数据
|
75 |
+
result_data['elo_scores'] = elo_scores
|
76 |
+
result_data['timestamp'] = datetime.now().isoformat()
|
77 |
+
with open(file_path, "a", encoding='utf-8') as f:
|
78 |
+
f.write(json.dumps(result_data) + "\n")
|
79 |
+
|
80 |
+
@app.route('/start_test', methods=['POST'])
|
81 |
+
def start_test():
|
82 |
+
"""Initiate the test for a user with the selected task"""
|
83 |
+
data = request.json
|
84 |
+
task = data['task']
|
85 |
+
username = data['username']
|
86 |
+
|
87 |
+
# Load the test data
|
88 |
+
test_data = load_test_data(task)
|
89 |
+
if not test_data:
|
90 |
+
return jsonify({"message": "No test data available"}), 400
|
91 |
+
|
92 |
+
# Shuffle test data for the user
|
93 |
+
random.shuffle(test_data)
|
94 |
|
95 |
+
# Generate a unique session ID (for example using uuid)
|
96 |
+
session_id = str(uuid.uuid4())
|
97 |
|
98 |
+
# Store in session
|
99 |
+
session['task'] = task
|
100 |
+
session['username'] = username
|
101 |
+
session['test_data'] = test_data
|
102 |
+
session['current_index'] = 0
|
103 |
+
session['session_id'] = session_id # Store the session ID in the session
|
104 |
+
|
105 |
+
task_description = test_data[0].get('task_description', '')
|
106 |
|
107 |
+
print(session)
|
108 |
+
|
109 |
+
|
110 |
+
return jsonify({"message": "Test started", "total_tests": len(test_data), "task_description": task_description})
|
111 |
+
|
112 |
+
|
113 |
+
|
114 |
+
@app.route('/next_test', methods=['GET'])
|
115 |
+
def next_test():
|
116 |
+
"""Serve the next test item"""
|
117 |
+
# if 'current_index' not in session or 'test_data' not in session:
|
118 |
+
# return jsonify({"message": "No active test found"}), 400
|
119 |
+
|
120 |
+
if 'current_index' not in session or 'test_data' not in session:
|
121 |
+
# 转换 session 为字典避免序列化错误
|
122 |
+
print("Debug Session:", dict(session)) # 调试信息
|
123 |
+
return jsonify({"message": "Session data missing"}), 400
|
124 |
+
|
125 |
+
|
126 |
+
current_index = session['current_index']
|
127 |
+
test_data = session['test_data']
|
128 |
+
|
129 |
+
if current_index >= len(test_data):
|
130 |
+
# Return the "Test completed" message when all tests are done
|
131 |
+
return jsonify({"message": "Test completed"}), 200
|
132 |
+
|
133 |
+
# 使用 EloRank 的 sample_next_match 来选择两款模型
|
134 |
+
selected_models = elo_rank_system.sample_next_match()
|
135 |
+
|
136 |
+
if not selected_models or len(selected_models) != 2:
|
137 |
+
return jsonify({"message": "Error selecting models"}), 500
|
138 |
+
|
139 |
+
# Serve test data with the two selected models
|
140 |
+
current_test = test_data[current_index]
|
141 |
+
session['selected_models'] = selected_models
|
142 |
+
session['current_index'] += 1
|
143 |
+
|
144 |
+
return jsonify({
|
145 |
+
"text": current_test["text"],
|
146 |
+
"input_path": current_test["input_path"],
|
147 |
+
"model_a": selected_models[0],
|
148 |
+
"model_b": selected_models[1],
|
149 |
+
"audio_a": current_test[selected_models[0]],
|
150 |
+
"audio_b": current_test[selected_models[1]]
|
151 |
+
})
|
152 |
+
|
153 |
+
@app.route('/submit_result', methods=['POST'])
|
154 |
+
def submit_result():
|
155 |
+
"""Submit the user's result and save it"""
|
156 |
+
data = request.json
|
157 |
+
chosen_model = data['chosen_model']
|
158 |
+
|
159 |
+
username = session.get('username')
|
160 |
+
task = session.get('task')
|
161 |
+
current_index = session.get('current_index') - 1 # Subtract since we increment after serving
|
162 |
+
session_id = session.get('session_id') # Get the session ID
|
163 |
+
|
164 |
+
if not username or not task or current_index < 0:
|
165 |
+
return jsonify({"message": "No active test found"}), 400
|
166 |
+
|
167 |
+
# Retrieve the selected models
|
168 |
+
selected_models = session['selected_models']
|
169 |
+
model_a = selected_models[0]
|
170 |
+
model_b = selected_models[1]
|
171 |
+
|
172 |
+
result = {
|
173 |
+
"name": username,
|
174 |
+
"chosen_model": chosen_model,
|
175 |
+
"model_a": model_a,
|
176 |
+
"model_b": model_b,
|
177 |
+
"result": {
|
178 |
+
model_a: 1 if chosen_model == 'A' else 0,
|
179 |
+
model_b: 1 if chosen_model == 'B' else 0
|
180 |
+
}
|
181 |
+
}
|
182 |
+
|
183 |
+
# Save the result for the current test using session_id to avoid filename conflict
|
184 |
+
test_data = session['test_data'][current_index]
|
185 |
+
result_data = {**test_data, **result}
|
186 |
+
save_result(task, username, result_data, session_id)
|
187 |
+
|
188 |
+
# 更新 Elo 排名系统
|
189 |
+
if chosen_model == 'A':
|
190 |
+
elo_rank_system.record_match(model_a, model_b)
|
191 |
+
else:
|
192 |
+
elo_rank_system.record_match(model_b, model_a)
|
193 |
+
|
194 |
+
return jsonify({"message": "Result submitted", "model_a": model_a, "model_b": model_b, "chosen_model": chosen_model})
|
195 |
+
|
196 |
+
@app.route('/end_test', methods=['GET'])
|
197 |
+
def end_test():
|
198 |
+
"""End the test session"""
|
199 |
+
session.clear()
|
200 |
+
return jsonify({"message": "Test completed"})
|
201 |
+
|
202 |
+
# 渲染index.html页面
|
203 |
+
@app.route('/')
|
204 |
+
def index():
|
205 |
+
return render_template('index.html')
|
206 |
+
|
207 |
+
if __name__ == '__main__':
|
208 |
+
if not os.path.exists(RESULTS_DIR):
|
209 |
+
os.makedirs(RESULTS_DIR)
|
210 |
+
# 允许局域网访问
|
211 |
+
app.run(host="0.0.0.0", debug=True, port=8080)
|