mfarre HF staff commited on
Commit
0be0bad
·
1 Parent(s): 9c021ca

adding auth

Browse files
Files changed (1) hide show
  1. app.py +35 -1
app.py CHANGED
@@ -10,7 +10,37 @@ from pathlib import Path
10
  import time
11
  import torch
12
  import spaces
13
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  from video_highlight_detector import (
16
  load_model,
@@ -179,5 +209,9 @@ if __name__ == "__main__":
179
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
180
  zero = torch.Tensor([0]).to(device)
181
 
 
 
 
 
182
  app = create_ui("video_spec.json")
183
  app.launch()
 
10
  import time
11
  import torch
12
  import spaces
13
+ import os
14
+ from huggingface_hub import HfApi, login
15
+ from dotenv import load_dotenv
16
+
17
+ def setup_hf_auth():
18
+ """
19
+ Set up Hugging Face authentication using a token stored as a secret.
20
+ Returns authenticated HfApi instance if successful.
21
+ """
22
+ try:
23
+ # First try to get token from environment variable
24
+ hf_token = os.getenv('HF_TOKEN')
25
+
26
+ if not hf_token:
27
+ raise ValueError("HF_TOKEN not found in environment variables or .env file")
28
+
29
+ # Login to Hugging Face
30
+ login(token=hf_token)
31
+
32
+ # Initialize API client
33
+ api = HfApi()
34
+
35
+ # Verify authentication by trying to get user info
36
+ user_info = api.whoami()
37
+ print(f"Successfully authenticated as: {user_info.name}")
38
+
39
+ return api
40
+
41
+ except Exception as e:
42
+ print(f"Authentication failed: {str(e)}")
43
+ return None
44
 
45
  from video_highlight_detector import (
46
  load_model,
 
209
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
210
  zero = torch.Tensor([0]).to(device)
211
 
212
+ hf_api = setup_hf_auth()
213
+ if not hf_api:
214
+ raise RuntimeError("Failed to authenticate with Hugging Face")
215
+
216
  app = create_ui("video_spec.json")
217
  app.launch()