sunnyzjx commited on
Commit
2dfaa0b
·
verified ·
1 Parent(s): c94938b

Update annotation.py

Browse files
Files changed (1) hide show
  1. annotation.py +38 -28
annotation.py CHANGED
@@ -1,23 +1,26 @@
1
  import re
2
  import os
3
  import json
4
- from huggingface_hub import HfApi, HfFolder, Repository
5
  import config
6
 
7
- # 初始化 Hub token
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
- REPO_ID = config.SAVE_REPO_ID
10
 
11
- def get_user_annotation_file(username):
12
- """获取用户特定的标注文件路径"""
13
- os.makedirs("annotations", exist_ok=True)
 
 
14
  safe_username = re.sub(r'[\\/*?:"<>|]', "_", username)
15
- return f"annotations/annotation_results_{safe_username}.json"
 
16
 
17
  def save_annotations(username_state, annotation_results_state, tasks):
18
- """保存标注结果到本地并推送到 Hub"""
19
  try:
20
- annotation_file = get_user_annotation_file(username_state)
21
  save_data = {
22
  "total_tasks": len(tasks),
23
  "completed_tasks": len(annotation_results_state),
@@ -33,35 +36,42 @@ def save_annotations(username_state, annotation_results_state, tasks):
33
  "audioB_id": f"audioB_{task_id}",
34
  "username": username_state
35
  })
36
- # 保存到本地
37
- with open(annotation_file, "w", encoding="utf-8") as f:
38
- json.dump(save_data, f, ensure_ascii=False, indent=2)
39
 
40
- # 推送到 Hugging Face Hub
41
- api = HfApi()
 
 
 
42
  api.upload_file(
43
- path_or_fileobj=annotation_file,
44
- path_in_repo=os.path.basename(annotation_file),
45
  repo_id=REPO_ID,
46
  repo_type="dataset",
47
  token=HF_TOKEN
48
  )
49
 
50
- return f"✅ 标注结果已保存并上传到 {REPO_ID}\n完成进度: {len(annotation_results_state)}/{len(tasks)}"
51
 
52
  except Exception as e:
53
- return f"❌ 保存失败: {str(e)}"
 
54
 
55
  def load_annotations(username):
56
- """加载用户特定的标注结果(仅本地版本,Hub 上的可选扩展)"""
57
  try:
58
- annotation_file = get_user_annotation_file(username)
59
- if os.path.exists(annotation_file):
60
- with open(annotation_file, "r", encoding="utf-8") as f:
61
- save_data = json.load(f)
62
- annotation_results = {ann["task_id"]: ann["choice"] for ann in save_data["annotations"]}
63
- return annotation_results
64
- else:
65
- return {}
66
- except Exception as e:
 
 
 
 
 
 
67
  return {}
 
1
  import re
2
  import os
3
  import json
4
+ from huggingface_hub import HfApi, hf_hub_download
5
  import config
6
 
7
+ # Space Secret 中读取 Token
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
+ REPO_ID = config.SAVE_REPO_ID # 在 config.py 里配置你的 dataset repo,比如 "sunnyzjx/annotation_results"
10
 
11
+ api = HfApi()
12
+
13
+
14
+ def get_user_annotation_filename(username: str) -> str:
15
+ """生成用户标注文件名"""
16
  safe_username = re.sub(r'[\\/*?:"<>|]', "_", username)
17
+ return f"annotation_results_{safe_username}.json"
18
+
19
 
20
  def save_annotations(username_state, annotation_results_state, tasks):
21
+ """直接推送标注结果到 Hugging Face Hub"""
22
  try:
23
+ # 组织数据
24
  save_data = {
25
  "total_tasks": len(tasks),
26
  "completed_tasks": len(annotation_results_state),
 
36
  "audioB_id": f"audioB_{task_id}",
37
  "username": username_state
38
  })
 
 
 
39
 
40
+ # 临时保存到内存/字符串
41
+ save_str = json.dumps(save_data, ensure_ascii=False, indent=2)
42
+ filename = get_user_annotation_filename(username_state)
43
+
44
+ # 上传到 Hub
45
  api.upload_file(
46
+ path_or_fileobj=save_str.encode("utf-8"),
47
+ path_in_repo=filename,
48
  repo_id=REPO_ID,
49
  repo_type="dataset",
50
  token=HF_TOKEN
51
  )
52
 
53
+ return f"✅ 标注结果已上传到 {REPO_ID}/{filename}\n完成进度: {len(annotation_results_state)}/{len(tasks)}"
54
 
55
  except Exception as e:
56
+ return f"❌ 上传失败: {str(e)}"
57
+
58
 
59
  def load_annotations(username):
60
+ """从 Hugging Face Hub 加载用户特定的标注结果"""
61
  try:
62
+ filename = get_user_annotation_filename(username)
63
+ # 下载用户的标注文件
64
+ local_path = hf_hub_download(
65
+ repo_id=REPO_ID,
66
+ filename=filename,
67
+ repo_type="dataset",
68
+ token=HF_TOKEN,
69
+ force_download=True # 确保拿到最新版本
70
+ )
71
+ with open(local_path, "r", encoding="utf-8") as f:
72
+ save_data = json.load(f)
73
+ annotation_results = {ann["task_id"]: ann["choice"] for ann in save_data.get("annotations", [])}
74
+ return annotation_results
75
+ except Exception:
76
+ # 用户还没有标注文件的情况
77
  return {}