Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -270,6 +270,7 @@ def sample_audio_paths(audio_paths, k=5, max_count=1):
|
|
| 270 |
|
| 271 |
# return selected, count_data
|
| 272 |
# Keep count_data atomic
|
|
|
|
| 273 |
return selected
|
| 274 |
|
| 275 |
# ==============================================================================
|
|
@@ -466,7 +467,7 @@ def back_to_welcome():
|
|
| 466 |
# Retry Function Definitions
|
| 467 |
# ==============================================================================
|
| 468 |
|
| 469 |
-
#
|
| 470 |
def retry_with_timeout(max_retries=3, timeout=10, backoff=1):
|
| 471 |
def decorator(func):
|
| 472 |
@wraps(func)
|
|
@@ -495,12 +496,12 @@ def retry_with_timeout(max_retries=3, timeout=10, backoff=1):
|
|
| 495 |
return wrapper
|
| 496 |
return decorator
|
| 497 |
|
| 498 |
-
def save_with_retry(all_results, user_data
|
| 499 |
# 尝试上传到Hugging Face Hub
|
| 500 |
try:
|
| 501 |
# 使用线程安全的保存方式
|
| 502 |
with ThreadPoolExecutor(max_workers=1) as executor:
|
| 503 |
-
future = executor.submit(save_all_results_to_file, all_results, user_data
|
| 504 |
try:
|
| 505 |
future.result(timeout=30) # 设置30秒超时
|
| 506 |
return True
|
|
@@ -597,6 +598,7 @@ def update_count_with_retry(count_data, question_set, max_retries=3):
|
|
| 597 |
gr.update(), gr.update(),
|
| 598 |
) + (gr.update(),) * MAX_SUB_DIMS + (all_results, result_str)"""
|
| 599 |
|
|
|
|
| 600 |
def submit_question_and_advance(q_idx, d_idx, selections, final_choice, all_results, user_data):
|
| 601 |
try:
|
| 602 |
# 准备数据
|
|
@@ -633,7 +635,8 @@ def submit_question_and_advance(q_idx, d_idx, selections, final_choice, all_resu
|
|
| 633 |
|
| 634 |
# 尝试上传(带重试)
|
| 635 |
try:
|
| 636 |
-
success = save_with_retry(all_results, user_data, user_data.get("updated_count_data"))
|
|
|
|
| 637 |
except Exception as e:
|
| 638 |
print(f"上传过程中发生错误: {e}")
|
| 639 |
success = False
|
|
@@ -646,7 +649,7 @@ def submit_question_and_advance(q_idx, d_idx, selections, final_choice, all_resu
|
|
| 646 |
|
| 647 |
# 准备数据包
|
| 648 |
user_info_clean = {
|
| 649 |
-
k: v for k, v in user_data.items() if k not in ["question_set"
|
| 650 |
}
|
| 651 |
final_data_package = {
|
| 652 |
"user_info": user_info_clean,
|
|
@@ -663,10 +666,10 @@ def submit_question_and_advance(q_idx, d_idx, selections, final_choice, all_resu
|
|
| 663 |
|
| 664 |
# 更新count.json(剔除未完成的题目)
|
| 665 |
try:
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
)
|
| 670 |
except Exception as e:
|
| 671 |
print(f"更新count.json失败: {e}")
|
| 672 |
count_update_success = False
|
|
@@ -743,14 +746,14 @@ def submit_question_and_advance(q_idx, d_idx, selections, final_choice, all_resu
|
|
| 743 |
except Exception as e:
|
| 744 |
print(f"上传出错: {e}")"""
|
| 745 |
|
| 746 |
-
def save_all_results_to_file(all_results, user_data
|
| 747 |
repo_id = "intersteller2887/Turing-test-dataset"
|
| 748 |
username = user_data.get("username", "user")
|
| 749 |
timestamp = pd.Timestamp.now().strftime('%Y%m%d_%H%M%S')
|
| 750 |
submission_filename = f"submissions_{username}_{timestamp}.json"
|
| 751 |
|
| 752 |
user_info_clean = {
|
| 753 |
-
k: v for k, v in user_data.items() if k not in ["question_set"
|
| 754 |
}
|
| 755 |
|
| 756 |
final_data_package = {
|
|
@@ -775,19 +778,21 @@ def save_all_results_to_file(all_results, user_data, count_data=None):
|
|
| 775 |
commit_message=f"Add new submission from {username}"
|
| 776 |
)
|
| 777 |
|
| 778 |
-
|
| 779 |
with FileLock(COUNT_JSON_PATH + ".lock", timeout=5):
|
| 780 |
-
with open(COUNT_JSON_PATH, "
|
| 781 |
-
|
| 782 |
-
|
| 783 |
api.upload_file(
|
| 784 |
-
path_or_fileobj=
|
| 785 |
path_in_repo=COUNT_JSON_REPO_PATH,
|
| 786 |
repo_id=repo_id,
|
| 787 |
repo_type="dataset",
|
| 788 |
token=hf_token,
|
| 789 |
commit_message=f"Update count.json after submission by {username}"
|
| 790 |
)
|
|
|
|
|
|
|
| 791 |
|
| 792 |
# ==============================================================================
|
| 793 |
# Gradio 界面定义 (Gradio UI Definition)
|
|
|
|
| 270 |
|
| 271 |
# return selected, count_data
|
| 272 |
# Keep count_data atomic
|
| 273 |
+
|
| 274 |
return selected
|
| 275 |
|
| 276 |
# ==============================================================================
|
|
|
|
| 467 |
# Retry Function Definitions
|
| 468 |
# ==============================================================================
|
| 469 |
|
| 470 |
+
# Decorator function that allows to use ThreadPoolExecutor to retry a function with timeout
|
| 471 |
def retry_with_timeout(max_retries=3, timeout=10, backoff=1):
|
| 472 |
def decorator(func):
|
| 473 |
@wraps(func)
|
|
|
|
| 496 |
return wrapper
|
| 497 |
return decorator
|
| 498 |
|
| 499 |
+
def save_with_retry(all_results, user_data):
|
| 500 |
# 尝试上传到Hugging Face Hub
|
| 501 |
try:
|
| 502 |
# 使用线程安全的保存方式
|
| 503 |
with ThreadPoolExecutor(max_workers=1) as executor:
|
| 504 |
+
future = executor.submit(save_all_results_to_file, all_results, user_data)
|
| 505 |
try:
|
| 506 |
future.result(timeout=30) # 设置30秒超时
|
| 507 |
return True
|
|
|
|
| 598 |
gr.update(), gr.update(),
|
| 599 |
) + (gr.update(),) * MAX_SUB_DIMS + (all_results, result_str)"""
|
| 600 |
|
| 601 |
+
# user_data now no further contain "updated_count_data", which should be read/write with filelock and be directly accessed from working directory
|
| 602 |
def submit_question_and_advance(q_idx, d_idx, selections, final_choice, all_results, user_data):
|
| 603 |
try:
|
| 604 |
# 准备数据
|
|
|
|
| 635 |
|
| 636 |
# 尝试上传(带重试)
|
| 637 |
try:
|
| 638 |
+
# success = save_with_retry(all_results, user_data, user_data.get("updated_count_data"))
|
| 639 |
+
success = save_with_retry(all_results, user_data)
|
| 640 |
except Exception as e:
|
| 641 |
print(f"上传过程中发生错误: {e}")
|
| 642 |
success = False
|
|
|
|
| 649 |
|
| 650 |
# 准备数据包
|
| 651 |
user_info_clean = {
|
| 652 |
+
k: v for k, v in user_data.items() if k not in ["question_set"]
|
| 653 |
}
|
| 654 |
final_data_package = {
|
| 655 |
"user_info": user_info_clean,
|
|
|
|
| 666 |
|
| 667 |
# 更新count.json(剔除未完成的题目)
|
| 668 |
try:
|
| 669 |
+
with FileLock(COUNT_JSON_PATH + ".lock", timeout=5):
|
| 670 |
+
with open(COUNT_JSON_PATH, "r", encoding="utf-8") as f:
|
| 671 |
+
count_data = json.load(f, object_pairs_hook=collections.OrderedDict)
|
| 672 |
+
count_update_success = update_count_with_retry(count_data, user_data["question_set"])
|
| 673 |
except Exception as e:
|
| 674 |
print(f"更新count.json失败: {e}")
|
| 675 |
count_update_success = False
|
|
|
|
| 746 |
except Exception as e:
|
| 747 |
print(f"上传出错: {e}")"""
|
| 748 |
|
| 749 |
+
def save_all_results_to_file(all_results, user_data):
|
| 750 |
repo_id = "intersteller2887/Turing-test-dataset"
|
| 751 |
username = user_data.get("username", "user")
|
| 752 |
timestamp = pd.Timestamp.now().strftime('%Y%m%d_%H%M%S')
|
| 753 |
submission_filename = f"submissions_{username}_{timestamp}.json"
|
| 754 |
|
| 755 |
user_info_clean = {
|
| 756 |
+
k: v for k, v in user_data.items() if k not in ["question_set"]
|
| 757 |
}
|
| 758 |
|
| 759 |
final_data_package = {
|
|
|
|
| 778 |
commit_message=f"Add new submission from {username}"
|
| 779 |
)
|
| 780 |
|
| 781 |
+
try:
|
| 782 |
with FileLock(COUNT_JSON_PATH + ".lock", timeout=5):
|
| 783 |
+
with open(COUNT_JSON_PATH, "r", encoding="utf-8") as f:
|
| 784 |
+
count_data_str = f.read()
|
| 785 |
+
|
| 786 |
api.upload_file(
|
| 787 |
+
path_or_fileobj=bytes(count_data_str, "utf-8"),
|
| 788 |
path_in_repo=COUNT_JSON_REPO_PATH,
|
| 789 |
repo_id=repo_id,
|
| 790 |
repo_type="dataset",
|
| 791 |
token=hf_token,
|
| 792 |
commit_message=f"Update count.json after submission by {username}"
|
| 793 |
)
|
| 794 |
+
except Exception as e:
|
| 795 |
+
print(f"上传 count.json 失败: {e}")
|
| 796 |
|
| 797 |
# ==============================================================================
|
| 798 |
# Gradio 界面定义 (Gradio UI Definition)
|