PowerMachine commited on
Commit
865b11f
·
verified ·
1 Parent(s): ceff207

Upload cnn_bigru/scripts/push_to_hf.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. cnn_bigru/scripts/push_to_hf.py +105 -0
cnn_bigru/scripts/push_to_hf.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """push_to_hf.py — Envia scripts em lote ao HF novo repositório 'CNN-BiGRU'.
2
+
3
+ Executa:
4
+ 1. Cria repositório 'PowerMachine/CNN-BiGRU' (se não existir)
5
+ 2. Envia todos os scripts .py, README.md, requirements.txt
6
+ 3. Remove HF_TOKEN do ambiente após o uso
7
+
8
+ Usage:
9
+ HF_TOKEN=hf_xxx python push_to_hf.py
10
+ """
11
+ from __future__ import annotations
12
+
13
+ import os
14
+ import sys
15
+ import logging
16
+ from pathlib import Path
17
+
18
+ logging.basicConfig(level=logging.INFO, format="%(asctime)s | %(levelname)s | %(message)s")
19
+ logger = logging.getLogger("push_hf")
20
+
21
+ REPO_ID = "PowerMachine/CNN-BiGRU"
22
+ # Path: scripts/push_to_hf.py -> parent=scripts -> parent=cnn_bigru
23
+ PROJECT_DIR = Path(__file__).resolve().parent.parent
24
+ # PROJECT_DIR.parent = /home/z/my-project (raiz do projeto)
25
+ REPO_ROOT = PROJECT_DIR.parent
26
+
27
+
28
+ def main():
29
+ # Pega HF_TOKEN da variável de ambiente
30
+ hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
31
+ if not hf_token:
32
+ logger.error("HF_TOKEN não definido no ambiente. Abortando.")
33
+ return 1
34
+
35
+ try:
36
+ from huggingface_hub import HfApi, create_repo
37
+ except ImportError:
38
+ logger.error("huggingface_hub não instalado. Execute: pip install huggingface_hub")
39
+ return 1
40
+
41
+ api = HfApi(token=hf_token)
42
+
43
+ # 1. Cria repositório (se não existir)
44
+ try:
45
+ repo_url = create_repo(
46
+ repo_id=REPO_ID,
47
+ token=hf_token,
48
+ repo_type="model",
49
+ exist_ok=True,
50
+ private=False,
51
+ )
52
+ logger.info("Repositório criado/acessado: %s", repo_url)
53
+ except Exception as e:
54
+ logger.error("Falha ao criar repositório: %s", e)
55
+ return 1
56
+
57
+ # 2. Coleta todos os arquivos para upload
58
+ files_to_upload = []
59
+ for path in PROJECT_DIR.rglob("*"):
60
+ if path.is_file():
61
+ # Pula arquivos de cache
62
+ if "__pycache__" in path.parts or ".pyc" in path.name:
63
+ continue
64
+ files_to_upload.append(path)
65
+
66
+ logger.info("Total de arquivos para upload: %d (de %s)", len(files_to_upload), PROJECT_DIR)
67
+
68
+ # 3. Upload em lote
69
+ n_ok = 0
70
+ n_fail = 0
71
+ for filepath in files_to_upload:
72
+ # Caminho relativo dentro do repo (preserva cnn_bigru/ prefix)
73
+ rel_path = filepath.relative_to(REPO_ROOT)
74
+ try:
75
+ api.upload_file(
76
+ path_or_fileobj=str(filepath),
77
+ path_in_repo=str(rel_path),
78
+ repo_id=REPO_ID,
79
+ repo_type="model",
80
+ token=hf_token,
81
+ )
82
+ logger.info(" OK %s", rel_path)
83
+ n_ok += 1
84
+ except Exception as e:
85
+ logger.error(" FAIL %s: %s", rel_path, e)
86
+ n_fail += 1
87
+
88
+ logger.info("=" * 60)
89
+ logger.info("Upload concluído: %d OK, %d falhas", n_ok, n_fail)
90
+ logger.info("Repo: https://huggingface.co/%s", REPO_ID)
91
+ logger.info("=" * 60)
92
+
93
+ # 4. Remove HF_TOKEN do ambiente (requisito do usuário)
94
+ if "HF_TOKEN" in os.environ:
95
+ del os.environ["HF_TOKEN"]
96
+ logger.info("HF_TOKEN removido do ambiente")
97
+ if "HUGGING_FACE_HUB_TOKEN" in os.environ:
98
+ del os.environ["HUGGING_FACE_HUB_TOKEN"]
99
+ logger.info("HUGGING_FACE_HUB_TOKEN removido do ambiente")
100
+
101
+ return 0 if n_fail == 0 else 1
102
+
103
+
104
+ if __name__ == "__main__":
105
+ sys.exit(main())