sunnyzjx commited on
Commit
6bf2582
·
verified ·
1 Parent(s): b8d01e0

Update annotation.py

Browse files
Files changed (1) hide show
  1. annotation.py +66 -51
annotation.py CHANGED
@@ -1,51 +1,66 @@
1
- import re
2
- import os
3
- import json
4
-
5
-
6
- def get_user_annotation_file(username):
7
- """获取用户特定的标注文件路径"""
8
- os.makedirs("annotations", exist_ok=True)
9
- safe_username = re.sub(r'[\\/*?:"<>|]', "_", username)
10
- return f"annotations/annotation_results_{safe_username}.json"
11
-
12
-
13
- def save_annotations(username_state, annotation_results_state, tasks):
14
- """保存标注结果到用户特定的文件"""
15
- try:
16
- annotation_file = get_user_annotation_file(username_state)
17
- save_data = {
18
- "total_tasks": len(tasks),
19
- "completed_tasks": len(annotation_results_state),
20
- "username": username_state,
21
- "annotations": []
22
- }
23
- for task_id, choice in annotation_results_state.items():
24
- save_data["annotations"].append({
25
- "task_id": task_id,
26
- "text": tasks[task_id]["text"],
27
- "choice": choice,
28
- "audioA_id": f"audioA_{task_id}",
29
- "audioB_id": f"audioB_{task_id}",
30
- "username": username_state
31
- })
32
- with open(annotation_file, "w", encoding="utf-8") as f:
33
- json.dump(save_data, f, ensure_ascii=False, indent=2)
34
- return f"✅ 标注结果已保存到 {annotation_file}\n完成进度: {len(annotation_results_state)}/{len(tasks)}"
35
- except Exception as e:
36
- return f" 保存失败: {str(e)}"
37
-
38
-
39
- def load_annotations(username):
40
- """加载用户特定的标注结果"""
41
- try:
42
- annotation_file = get_user_annotation_file(username)
43
- if os.path.exists(annotation_file):
44
- with open(annotation_file, "r", encoding="utf-8") as f:
45
- save_data = json.load(f)
46
- annotation_results = {ann["task_id"]: ann["choice"] for ann in save_data["annotations"]}
47
- return annotation_results
48
- else:
49
- return {}
50
- except Exception as e:
51
- return {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import os
3
+ import json
4
+ from huggingface_hub import HfApi, HfFolder, Repository
5
+
6
+ # 初始化 Hub token
7
+ HF_TOKEN = os.getenv("HF_TOKEN") # 从 Space secret 里读取
8
+ REPO_ID = "sunnyzjx/annotation_results" # 你的 dataset repo
9
+
10
+ def get_user_annotation_file(username):
11
+ """获取用户特定的标注文件路径"""
12
+ os.makedirs("annotations", exist_ok=True)
13
+ safe_username = re.sub(r'[\\/*?:"<>|]', "_", username)
14
+ return f"annotations/annotation_results_{safe_username}.json"
15
+
16
+ def save_annotations(username_state, annotation_results_state, tasks):
17
+ """保存标注结果到本地并推送到 Hub"""
18
+ try:
19
+ annotation_file = get_user_annotation_file(username_state)
20
+ save_data = {
21
+ "total_tasks": len(tasks),
22
+ "completed_tasks": len(annotation_results_state),
23
+ "username": username_state,
24
+ "annotations": []
25
+ }
26
+ for task_id, choice in annotation_results_state.items():
27
+ save_data["annotations"].append({
28
+ "task_id": task_id,
29
+ "text": tasks[task_id]["text"],
30
+ "choice": choice,
31
+ "audioA_id": f"audioA_{task_id}",
32
+ "audioB_id": f"audioB_{task_id}",
33
+ "username": username_state
34
+ })
35
+ # 保存到本地
36
+ with open(annotation_file, "w", encoding="utf-8") as f:
37
+ json.dump(save_data, f, ensure_ascii=False, indent=2)
38
+
39
+ # 推送到 Hugging Face Hub
40
+ api = HfApi()
41
+ api.upload_file(
42
+ path_or_fileobj=annotation_file,
43
+ path_in_repo=os.path.basename(annotation_file),
44
+ repo_id=REPO_ID,
45
+ repo_type="dataset",
46
+ token=HF_TOKEN
47
+ )
48
+
49
+ return f"✅ 标注结果已保存并上传到 {REPO_ID}\n完成进度: {len(annotation_results_state)}/{len(tasks)}"
50
+
51
+ except Exception as e:
52
+ return f"❌ 保存失败: {str(e)}"
53
+
54
+ def load_annotations(username):
55
+ """加载用户特定的标注结果(仅本地版本,Hub 上的可选扩展)"""
56
+ try:
57
+ annotation_file = get_user_annotation_file(username)
58
+ if os.path.exists(annotation_file):
59
+ with open(annotation_file, "r", encoding="utf-8") as f:
60
+ save_data = json.load(f)
61
+ annotation_results = {ann["task_id"]: ann["choice"] for ann in save_data["annotations"]}
62
+ return annotation_results
63
+ else:
64
+ return {}
65
+ except Exception as e:
66
+ return {}