Spaces:
Runtime error
Runtime error
Update annotation.py
Browse files- 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,
|
| 5 |
import config
|
| 6 |
|
| 7 |
-
#
|
| 8 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
-
REPO_ID = config.SAVE_REPO_ID
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
safe_username = re.sub(r'[\\/*?:"<>|]', "_", username)
|
| 15 |
-
return f"
|
|
|
|
| 16 |
|
| 17 |
def save_annotations(username_state, annotation_results_state, tasks):
|
| 18 |
-
"""
|
| 19 |
try:
|
| 20 |
-
|
| 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 |
-
#
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
api.upload_file(
|
| 43 |
-
path_or_fileobj=
|
| 44 |
-
path_in_repo=
|
| 45 |
repo_id=REPO_ID,
|
| 46 |
repo_type="dataset",
|
| 47 |
token=HF_TOKEN
|
| 48 |
)
|
| 49 |
|
| 50 |
-
return f"✅
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
-
return f"❌
|
|
|
|
| 54 |
|
| 55 |
def load_annotations(username):
|
| 56 |
-
"""
|
| 57 |
try:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 {}
|