File size: 831 Bytes
5e8e534
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import requests

from omegaconf import OmegaConf

def gh_download(repo, path, token):
    paths = [path] if isinstance(path, str) else path
    result = None
    headers = {"Authorization": f"Bearer {token}", "Accept": "application/vnd.github.raw+json"}
    for path in paths:
        url = f"https://api.github.com/repos/{repo}/contents/{path}" 
        response = requests.get(url, headers=headers)
        if response.status_code != 200:
            raise Exception(f"Failed to download {path} from {repo}")
        
        if result is None:
            result = response.json()
        elif isinstance(result, list):
            result.extend(response.json())
        elif isinstance(result, dict):
            result.update(response.json())

    return result
    
OmegaConf.register_new_resolver("gh_download", gh_download)