Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,8 @@ from functools import wraps
|
|
| 10 |
from filelock import FileLock
|
| 11 |
from datasets import load_dataset, Audio
|
| 12 |
from huggingface_hub import HfApi
|
|
|
|
|
|
|
| 13 |
|
| 14 |
dataset = load_dataset("intersteller2887/Turing-test-dataset", split="train")
|
| 15 |
dataset = dataset.cast_column("audio", Audio(decode=False)) # Prevent calling 'torchcodec' from newer version of 'datasets'
|
|
@@ -376,44 +378,42 @@ def retry_with_timeout(max_retries=3, timeout=10, backoff=1):
|
|
| 376 |
last_exception = None
|
| 377 |
for attempt in range(max_retries):
|
| 378 |
try:
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
signal.alarm(timeout)
|
| 388 |
-
|
| 389 |
-
try:
|
| 390 |
-
result = func(*args, **kwargs)
|
| 391 |
-
signal.alarm(0) # Cancel the clocker
|
| 392 |
-
return result
|
| 393 |
-
except TimeoutError:
|
| 394 |
-
signal.alarm(0)
|
| 395 |
-
raise
|
| 396 |
except Exception as e:
|
| 397 |
last_exception = e
|
| 398 |
print(f"Attempt {attempt + 1} failed: {str(e)}")
|
| 399 |
if attempt < max_retries - 1:
|
| 400 |
time.sleep(backoff * (attempt + 1))
|
| 401 |
-
|
| 402 |
-
signal.alarm(0) # Guarantee that the clocker will be canceled
|
| 403 |
-
|
| 404 |
print(f"All {max_retries} attempts failed")
|
| 405 |
if last_exception:
|
| 406 |
raise last_exception
|
| 407 |
-
raise Exception("
|
| 408 |
return wrapper
|
| 409 |
return decorator
|
| 410 |
|
| 411 |
def save_with_retry(all_results, user_data, count_data):
|
|
|
|
| 412 |
try:
|
| 413 |
-
|
| 414 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
except Exception as e:
|
| 416 |
-
print(f"
|
| 417 |
return False
|
| 418 |
|
| 419 |
def save_locally_with_retry(data, filename, max_retries=3):
|
|
@@ -423,7 +423,7 @@ def save_locally_with_retry(data, filename, max_retries=3):
|
|
| 423 |
json.dump(data, f, indent=4, ensure_ascii=False)
|
| 424 |
return True
|
| 425 |
except Exception as e:
|
| 426 |
-
print(f"
|
| 427 |
if attempt < max_retries - 1:
|
| 428 |
time.sleep(1)
|
| 429 |
return False
|
|
@@ -503,7 +503,7 @@ def update_count_with_retry(count_data, question_set, max_retries=3):
|
|
| 503 |
|
| 504 |
def submit_question_and_advance(q_idx, d_idx, selections, final_choice, all_results, user_data):
|
| 505 |
try:
|
| 506 |
-
#
|
| 507 |
cleaned_selections = {}
|
| 508 |
for dim_title, sub_scores in selections.items():
|
| 509 |
cleaned_selections["final_choice"] = final_choice
|
|
@@ -525,6 +525,7 @@ def submit_question_and_advance(q_idx, d_idx, selections, final_choice, all_resu
|
|
| 525 |
init_q_updates = init_test_question(user_data, q_idx)
|
| 526 |
return init_q_updates + (all_results, gr.update(value=""))
|
| 527 |
else:
|
|
|
|
| 528 |
result_str = "### 测试全部完成!\n\n你的提交结果概览:\n"
|
| 529 |
for res in all_results:
|
| 530 |
result_str += f"##### 最终判断: **{res['selections'].get('final_choice', '未选择')}**\n"
|
|
@@ -535,13 +536,19 @@ def submit_question_and_advance(q_idx, d_idx, selections, final_choice, all_resu
|
|
| 535 |
result_str += f" - *{sub_dim[:20]}...*: {score}/5\n"
|
| 536 |
|
| 537 |
# 尝试上传(带重试)
|
| 538 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 539 |
|
| 540 |
if not success:
|
|
|
|
| 541 |
username = user_data.get("username", "anonymous")
|
| 542 |
timestamp = pd.Timestamp.now().strftime('%Y%m%d_%H%M%S')
|
| 543 |
local_filename = f"submission_{username}_{timestamp}.json"
|
| 544 |
-
|
|
|
|
| 545 |
user_info_clean = {
|
| 546 |
k: v for k, v in user_data.items() if k not in ["question_set", "updated_count_data"]
|
| 547 |
}
|
|
@@ -559,10 +566,14 @@ def submit_question_and_advance(q_idx, d_idx, selections, final_choice, all_resu
|
|
| 559 |
result_str += "\n\n❌ 上传失败且无法保存到本地文件,请联系管理员"
|
| 560 |
|
| 561 |
# 更新count.json(剔除未完成的题目)
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 566 |
|
| 567 |
if not count_update_success:
|
| 568 |
result_str += "\n\n⚠️ 无法更新题目计数,请联系管理员"
|
|
@@ -636,7 +647,6 @@ def submit_question_and_advance(q_idx, d_idx, selections, final_choice, all_resu
|
|
| 636 |
except Exception as e:
|
| 637 |
print(f"上传出错: {e}")"""
|
| 638 |
|
| 639 |
-
@retry_with_timeout(max_retries=3, timeout=10)
|
| 640 |
def save_all_results_to_file(all_results, user_data, count_data=None):
|
| 641 |
repo_id = "intersteller2887/Turing-test-dataset"
|
| 642 |
username = user_data.get("username", "user")
|
|
@@ -659,15 +669,14 @@ def save_all_results_to_file(all_results, user_data, count_data=None):
|
|
| 659 |
|
| 660 |
api = HfApi()
|
| 661 |
|
| 662 |
-
#
|
| 663 |
api.upload_file(
|
| 664 |
path_or_fileobj=bytes(json_string, "utf-8"),
|
| 665 |
path_in_repo=f"submissions/{submission_filename}",
|
| 666 |
repo_id=repo_id,
|
| 667 |
repo_type="dataset",
|
| 668 |
token=hf_token,
|
| 669 |
-
commit_message=f"Add new submission from {username}"
|
| 670 |
-
timeout=30
|
| 671 |
)
|
| 672 |
|
| 673 |
if count_data:
|
|
@@ -681,8 +690,7 @@ def save_all_results_to_file(all_results, user_data, count_data=None):
|
|
| 681 |
repo_id=repo_id,
|
| 682 |
repo_type="dataset",
|
| 683 |
token=hf_token,
|
| 684 |
-
commit_message=f"Update count.json after submission by {username}"
|
| 685 |
-
timeout=30
|
| 686 |
)
|
| 687 |
|
| 688 |
def toggle_reference_view(current):
|
|
|
|
| 10 |
from filelock import FileLock
|
| 11 |
from datasets import load_dataset, Audio
|
| 12 |
from huggingface_hub import HfApi
|
| 13 |
+
from multiprocessing import TimeoutError
|
| 14 |
+
from concurrent.futures import ThreadPoolExecutor, TimeoutError as FutureTimeoutError
|
| 15 |
|
| 16 |
dataset = load_dataset("intersteller2887/Turing-test-dataset", split="train")
|
| 17 |
dataset = dataset.cast_column("audio", Audio(decode=False)) # Prevent calling 'torchcodec' from newer version of 'datasets'
|
|
|
|
| 378 |
last_exception = None
|
| 379 |
for attempt in range(max_retries):
|
| 380 |
try:
|
| 381 |
+
with ThreadPoolExecutor(max_workers=1) as executor:
|
| 382 |
+
future = executor.submit(func, *args, **kwargs)
|
| 383 |
+
try:
|
| 384 |
+
result = future.result(timeout=timeout)
|
| 385 |
+
return result
|
| 386 |
+
except FutureTimeoutError:
|
| 387 |
+
future.cancel()
|
| 388 |
+
raise TimeoutError(f"Operation timed out after {timeout} seconds")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
except Exception as e:
|
| 390 |
last_exception = e
|
| 391 |
print(f"Attempt {attempt + 1} failed: {str(e)}")
|
| 392 |
if attempt < max_retries - 1:
|
| 393 |
time.sleep(backoff * (attempt + 1))
|
| 394 |
+
|
|
|
|
|
|
|
| 395 |
print(f"All {max_retries} attempts failed")
|
| 396 |
if last_exception:
|
| 397 |
raise last_exception
|
| 398 |
+
raise Exception("Unknown error occurred")
|
| 399 |
return wrapper
|
| 400 |
return decorator
|
| 401 |
|
| 402 |
def save_with_retry(all_results, user_data, count_data):
|
| 403 |
+
# 尝试上传到Hugging Face Hub
|
| 404 |
try:
|
| 405 |
+
# 使用线程安全的保存方式
|
| 406 |
+
with ThreadPoolExecutor(max_workers=1) as executor:
|
| 407 |
+
future = executor.submit(save_all_results_to_file, all_results, user_data, count_data)
|
| 408 |
+
try:
|
| 409 |
+
future.result(timeout=30) # 设置30秒超时
|
| 410 |
+
return True
|
| 411 |
+
except FutureTimeoutError:
|
| 412 |
+
future.cancel()
|
| 413 |
+
print("上传超时")
|
| 414 |
+
return False
|
| 415 |
except Exception as e:
|
| 416 |
+
print(f"上传到Hub失败: {e}")
|
| 417 |
return False
|
| 418 |
|
| 419 |
def save_locally_with_retry(data, filename, max_retries=3):
|
|
|
|
| 423 |
json.dump(data, f, indent=4, ensure_ascii=False)
|
| 424 |
return True
|
| 425 |
except Exception as e:
|
| 426 |
+
print(f"本地保存尝试 {attempt + 1} 失败: {e}")
|
| 427 |
if attempt < max_retries - 1:
|
| 428 |
time.sleep(1)
|
| 429 |
return False
|
|
|
|
| 503 |
|
| 504 |
def submit_question_and_advance(q_idx, d_idx, selections, final_choice, all_results, user_data):
|
| 505 |
try:
|
| 506 |
+
# 准备数据
|
| 507 |
cleaned_selections = {}
|
| 508 |
for dim_title, sub_scores in selections.items():
|
| 509 |
cleaned_selections["final_choice"] = final_choice
|
|
|
|
| 525 |
init_q_updates = init_test_question(user_data, q_idx)
|
| 526 |
return init_q_updates + (all_results, gr.update(value=""))
|
| 527 |
else:
|
| 528 |
+
# 准备完整结果数据
|
| 529 |
result_str = "### 测试全部完成!\n\n你的提交结果概览:\n"
|
| 530 |
for res in all_results:
|
| 531 |
result_str += f"##### 最终判断: **{res['selections'].get('final_choice', '未选择')}**\n"
|
|
|
|
| 536 |
result_str += f" - *{sub_dim[:20]}...*: {score}/5\n"
|
| 537 |
|
| 538 |
# 尝试上传(带重试)
|
| 539 |
+
try:
|
| 540 |
+
success = save_with_retry(all_results, user_data, user_data.get("updated_count_data"))
|
| 541 |
+
except Exception as e:
|
| 542 |
+
print(f"上传过程中发生错误: {e}")
|
| 543 |
+
success = False
|
| 544 |
|
| 545 |
if not success:
|
| 546 |
+
# 上传失败,保存到本地
|
| 547 |
username = user_data.get("username", "anonymous")
|
| 548 |
timestamp = pd.Timestamp.now().strftime('%Y%m%d_%H%M%S')
|
| 549 |
local_filename = f"submission_{username}_{timestamp}.json"
|
| 550 |
+
|
| 551 |
+
# 准备数据包
|
| 552 |
user_info_clean = {
|
| 553 |
k: v for k, v in user_data.items() if k not in ["question_set", "updated_count_data"]
|
| 554 |
}
|
|
|
|
| 566 |
result_str += "\n\n❌ 上传失败且无法保存到本地文件,请联系管理员"
|
| 567 |
|
| 568 |
# 更新count.json(剔除未完成的题目)
|
| 569 |
+
try:
|
| 570 |
+
count_update_success = update_count_with_retry(
|
| 571 |
+
user_data.get("updated_count_data", {}),
|
| 572 |
+
user_data["question_set"]
|
| 573 |
+
)
|
| 574 |
+
except Exception as e:
|
| 575 |
+
print(f"更新count.json失败: {e}")
|
| 576 |
+
count_update_success = False
|
| 577 |
|
| 578 |
if not count_update_success:
|
| 579 |
result_str += "\n\n⚠️ 无法更新题目计数,请联系管理员"
|
|
|
|
| 647 |
except Exception as e:
|
| 648 |
print(f"上传出错: {e}")"""
|
| 649 |
|
|
|
|
| 650 |
def save_all_results_to_file(all_results, user_data, count_data=None):
|
| 651 |
repo_id = "intersteller2887/Turing-test-dataset"
|
| 652 |
username = user_data.get("username", "user")
|
|
|
|
| 669 |
|
| 670 |
api = HfApi()
|
| 671 |
|
| 672 |
+
# 上传提交文件(不再使用装饰器,直接调用)
|
| 673 |
api.upload_file(
|
| 674 |
path_or_fileobj=bytes(json_string, "utf-8"),
|
| 675 |
path_in_repo=f"submissions/{submission_filename}",
|
| 676 |
repo_id=repo_id,
|
| 677 |
repo_type="dataset",
|
| 678 |
token=hf_token,
|
| 679 |
+
commit_message=f"Add new submission from {username}"
|
|
|
|
| 680 |
)
|
| 681 |
|
| 682 |
if count_data:
|
|
|
|
| 690 |
repo_id=repo_id,
|
| 691 |
repo_type="dataset",
|
| 692 |
token=hf_token,
|
| 693 |
+
commit_message=f"Update count.json after submission by {username}"
|
|
|
|
| 694 |
)
|
| 695 |
|
| 696 |
def toggle_reference_view(current):
|