aether-raider commited on
Commit
bcfeaf3
·
1 Parent(s): aa7e5d8

added hf token

Browse files
Files changed (1) hide show
  1. backend/hf_logging.py +26 -13
backend/hf_logging.py CHANGED
@@ -2,6 +2,7 @@
2
 
3
  import json
4
  import io
 
5
  from datetime import datetime
6
  from typing import Dict, Any
7
 
@@ -9,8 +10,9 @@ from huggingface_hub import HfApi, CommitOperationAdd
9
 
10
  from .config import RATINGS_DATASET_ID
11
 
12
- # Hugging Face API for logging
13
- hf_api = HfApi()
 
14
 
15
 
16
  def push_session_to_hub(session_id: str, export_data: Dict[str, Any]):
@@ -53,16 +55,27 @@ def push_session_to_hub(session_id: str, export_data: Dict[str, Any]):
53
  path_in_repo = f"sessions/{session_id}.json"
54
 
55
  # Create a git commit adding/updating that file
56
- hf_api.create_commit(
57
- repo_id=RATINGS_DATASET_ID,
58
- repo_type="dataset",
59
- operations=[
60
- CommitOperationAdd(
61
- path_in_repo=path_in_repo,
62
- path_or_fileobj=fileobj,
63
- )
64
- ],
65
- commit_message=f"Add MOS ratings for session {session_id}",
66
- )
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  print(f"[LOG] Pushed session {session_id} to {RATINGS_DATASET_ID}/{path_in_repo}")
 
2
 
3
  import json
4
  import io
5
+ import os
6
  from datetime import datetime
7
  from typing import Dict, Any
8
 
 
10
 
11
  from .config import RATINGS_DATASET_ID
12
 
13
+ # Hugging Face API for logging - get token from environment or use default auth
14
+ hf_token = os.getenv("HF_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN")
15
+ hf_api = HfApi(token=hf_token)
16
 
17
 
18
  def push_session_to_hub(session_id: str, export_data: Dict[str, Any]):
 
55
  path_in_repo = f"sessions/{session_id}.json"
56
 
57
  # Create a git commit adding/updating that file
58
+ try:
59
+ hf_api.create_commit(
60
+ repo_id=RATINGS_DATASET_ID,
61
+ repo_type="dataset",
62
+ operations=[
63
+ CommitOperationAdd(
64
+ path_in_repo=path_in_repo,
65
+ path_or_fileobj=fileobj,
66
+ )
67
+ ],
68
+ commit_message=f"Add MOS ratings for session {session_id}",
69
+ token=hf_token, # Explicitly pass token
70
+ )
71
+ except Exception as e:
72
+ print(f"[ERROR] Failed to push to Hub: {e}")
73
+ # If no token or authentication fails, save locally as fallback
74
+ fallback_path = f"./session_exports/{session_id}.json"
75
+ os.makedirs("./session_exports", exist_ok=True)
76
+ with open(fallback_path, "w") as f:
77
+ json.dump(export_data, f, indent=2, default=str, ensure_ascii=False)
78
+ print(f"[INFO] Saved session data locally to {fallback_path}")
79
+ return
80
 
81
  print(f"[LOG] Pushed session {session_id} to {RATINGS_DATASET_ID}/{path_in_repo}")