simikkk commited on
Commit
9ec4e30
·
verified ·
1 Parent(s): 56be63b

Create storage.py

Browse files
Files changed (1) hide show
  1. storage.py +54 -0
storage.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import secrets
4
+ from huggingface_hub import HfApi, hf_hub_download
5
+
6
+ HF_TOKEN = os.environ.get("HF_TOKEN")
7
+ DATASET_REPO_ID = os.environ.get("DATASET_REPO_ID") # nastav jako secret: "tvujusername/resumelens-data"
8
+
9
+ def _get_licenses() -> dict:
10
+ """Načte licenses.json z HF Dataset repo."""
11
+ api = HfApi(token=HF_TOKEN)
12
+ try:
13
+ path = api.hf_hub_download(
14
+ repo_id=DATASET_REPO_ID,
15
+ filename="licenses.json",
16
+ repo_type="dataset",
17
+ force_download=True
18
+ )
19
+ with open(path) as f:
20
+ return json.load(f)
21
+ except Exception:
22
+ return {}
23
+
24
+ def _save_licenses(licenses: dict):
25
+ """Uloží licenses.json zpět do HF Dataset repo."""
26
+ api = HfApi(token=HF_TOKEN)
27
+ api.upload_file(
28
+ path_or_fileobj=json.dumps(licenses, indent=2).encode(),
29
+ path_in_repo="licenses.json",
30
+ repo_id=DATASET_REPO_ID,
31
+ repo_type="dataset",
32
+ )
33
+
34
+ def create_license_key(email: str, plan: str) -> str:
35
+ """Vygeneruje nový klíč, uloží ho a vrátí."""
36
+ key = "rl_" + secrets.token_hex(16)
37
+ licenses = _get_licenses()
38
+ licenses[key] = {
39
+ "email": email,
40
+ "plan": plan,
41
+ "active": True
42
+ }
43
+ _save_licenses(licenses)
44
+ return key
45
+
46
+ def check_license_key(key: str) -> dict | None:
47
+ """Vrátí info o klíči nebo None."""
48
+ if not key or len(key) < 10:
49
+ return None
50
+ licenses = _get_licenses()
51
+ entry = licenses.get(key)
52
+ if entry and entry.get("active"):
53
+ return entry
54
+ return None