KurtDu commited on
Commit
8c71012
·
verified ·
1 Parent(s): 41a07e4

Update app.py

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