File size: 896 Bytes
324ecd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import json
import os
import urllib.request
import modal

app = modal.App('beamboot-hf-identity')

@app.function(image=modal.Image.debian_slim(python_version='3.12'), secrets=[modal.Secret.from_name('huggingface-secret')])
def identity():
    token = (os.environ.get('HF_TOKEN') or os.environ.get('HUGGING_FACE_HUB_TOKEN') or os.environ.get('HUGGINGFACE_TOKEN') or os.environ.get('HUGGINGFACEHUB_API_TOKEN'))
    if not token: raise RuntimeError('No recognized HF token variable')
    request = urllib.request.Request('https://huggingface.co/api/whoami-v2', headers={'Authorization': 'Bearer ' + token})
    with urllib.request.urlopen(request, timeout=20) as response:
        data = json.load(response)
    return {'name': data.get('name'), 'type': data.get('type'), 'role': data.get('auth', {}).get('accessToken', {}).get('role')}

@app.local_entrypoint()
def main(): print(identity.remote())