amogaddy commited on
Commit
40cba1f
Β·
verified Β·
1 Parent(s): c522400

Upload upload_hf.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. upload_hf.py +168 -0
upload_hf.py ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ upload_hf.py
3
+ ============
4
+ Carica su HuggingFace solo i file necessari del progetto GenerAI.
5
+
6
+ Uso:
7
+ python upload_hf.py
8
+ python upload_hf.py --repo amogaddy/GenerAI --model-weights
9
+ """
10
+
11
+ import argparse
12
+ import os
13
+ import shutil
14
+ import tempfile
15
+ from pathlib import Path
16
+
17
+ # Carica .env se presente
18
+ if Path(".env").exists():
19
+ for line in Path(".env").read_text(encoding="utf-8").splitlines():
20
+ if "=" in line and not line.startswith("#"):
21
+ k, v = line.split("=", 1)
22
+ os.environ.setdefault(k.strip(), v.strip())
23
+
24
+ # File del codice da includere
25
+ SOURCE_FILES = [
26
+ "app.py",
27
+ "brain.py",
28
+ "knowledge_base.py",
29
+ "scraper.py",
30
+ "errors.py",
31
+ "seed_italian.py",
32
+ "export_dataset.py",
33
+ "finetune.py",
34
+ "upload_hf.py",
35
+ "requirements.txt",
36
+ "requirements-train.txt",
37
+ ]
38
+
39
+ DEFAULT_REPO = "amogaddy/GenerAI"
40
+ MODEL_DIR = "./generai-finetuned"
41
+
42
+
43
+ def check_deps():
44
+ try:
45
+ from huggingface_hub import login, upload_folder, HfApi
46
+ return login, upload_folder, HfApi
47
+ except ImportError:
48
+ print("❌ huggingface_hub non installato.")
49
+ print(" pip install huggingface-hub")
50
+ exit(1)
51
+
52
+
53
+ def build_model_card(repo_id: str, include_weights: bool) -> str:
54
+ return f"""---
55
+ license: mit
56
+ base_model: Qwen/Qwen2.5-0.5B-Instruct
57
+ language:
58
+ - it
59
+ tags:
60
+ - italian
61
+ - generai
62
+ - fine-tuned
63
+ - rag
64
+ ---
65
+
66
+ # GenerAI πŸ€–
67
+
68
+ Assistente AI in italiano con memoria locale (ChromaDB) e ricerca web.
69
+
70
+ ## Come usarlo
71
+
72
+ ```bash
73
+ pip install -r requirements.txt
74
+ python app.py
75
+ ```
76
+
77
+ ## Fine-tuning
78
+
79
+ ```bash
80
+ pip install -r requirements-train.txt
81
+ python export_dataset.py
82
+ python finetune.py --dataset dataset.jsonl --hf-repo {repo_id}
83
+ ```
84
+
85
+ ## Licenza
86
+
87
+ MIT β€” fai quello che vuoi, basta lasciare il credito.
88
+ """
89
+
90
+
91
+ def main():
92
+ parser = argparse.ArgumentParser(description="Carica GenerAI su HuggingFace")
93
+ parser.add_argument("--repo", default=DEFAULT_REPO, help=f"repo_id HuggingFace (default: {DEFAULT_REPO})")
94
+ parser.add_argument("--model-weights", action="store_true", help="Includi anche i pesi del modello fine-tunato")
95
+ parser.add_argument("--no-login", action="store_true", help="Salta il login (usa token giΓ  salvato)")
96
+ args = parser.parse_args()
97
+
98
+ login, upload_folder, HfApi = check_deps()
99
+
100
+ # ── Login ──────────────────────────────────────────────────────────────────
101
+ if not args.no_login:
102
+ token = os.environ.get("HF_TOKEN", "")
103
+ if token:
104
+ print("Login HuggingFace con token da .env...")
105
+ login(token=token)
106
+ else:
107
+ print("Login HuggingFace...")
108
+ login()
109
+
110
+ # ── Crea cartella temporanea con solo i file necessari ─────────────────────
111
+ with tempfile.TemporaryDirectory() as tmp:
112
+ tmp_path = Path(tmp)
113
+ copied = []
114
+ missing = []
115
+
116
+ for fname in SOURCE_FILES:
117
+ src = Path(fname)
118
+ if src.exists():
119
+ shutil.copy2(src, tmp_path / src.name)
120
+ copied.append(fname)
121
+ else:
122
+ missing.append(fname)
123
+
124
+ # Genera README.md / model card
125
+ (tmp_path / "README.md").write_text(
126
+ build_model_card(args.repo, args.model_weights),
127
+ encoding="utf-8",
128
+ )
129
+ copied.append("README.md (generato)")
130
+
131
+ print(f"\nFile da caricare ({len(copied)}):")
132
+ for f in copied:
133
+ print(f" + {f}")
134
+ if missing:
135
+ print(f"\nFile non trovati (saltati):")
136
+ for f in missing:
137
+ print(f" - {f}")
138
+
139
+ # ── Upload codice ──────────────────────────────────────────────────────
140
+ print(f"\nUpload codice -> {args.repo} ...")
141
+ upload_folder(
142
+ folder_path=str(tmp_path),
143
+ repo_id=args.repo,
144
+ repo_type="model",
145
+ commit_message="Upload GenerAI β€” codice + grammatica italiana",
146
+ )
147
+ print("Codice caricato.")
148
+
149
+ # ── Upload pesi modello (opzionale) ────────────────────────────────────────
150
+ if args.model_weights:
151
+ if not Path(MODEL_DIR).exists():
152
+ print(f"\nCartella pesi non trovata: {MODEL_DIR}")
153
+ print(" Esegui prima: python finetune.py --dataset dataset.jsonl")
154
+ else:
155
+ print(f"\nUpload pesi modello -> {args.repo} ...")
156
+ upload_folder(
157
+ folder_path=MODEL_DIR,
158
+ repo_id=args.repo,
159
+ repo_type="model",
160
+ commit_message="Upload pesi modello fine-tunato GenerAI",
161
+ )
162
+ print("Pesi modello caricati.")
163
+
164
+ print(f"\nCompletato! -> https://huggingface.co/{args.repo}")
165
+
166
+
167
+ if __name__ == "__main__":
168
+ main()