Spaces:
Runtime error
Runtime error
Update annotation.py
Browse files- annotation.py +66 -51
annotation.py
CHANGED
|
@@ -1,51 +1,66 @@
|
|
| 1 |
-
import re
|
| 2 |
-
import os
|
| 3 |
-
import json
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
"
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
"
|
| 29 |
-
"
|
| 30 |
-
"
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 {}
|