Rename agent_ai.py to main.py
Browse files- agent_ai.py → main.py +32 -13
agent_ai.py → main.py
RENAMED
|
@@ -1,16 +1,18 @@
|
|
| 1 |
-
#
|
| 2 |
import os
|
|
|
|
| 3 |
from transformers import PreTrainedTokenizerFast, GPTNeoXForCausalLM, GPTNeoXConfig
|
|
|
|
| 4 |
|
| 5 |
# -------------------------------
|
| 6 |
# Paramètres du modèle (>2M)
|
| 7 |
# -------------------------------
|
| 8 |
model_config = {
|
| 9 |
"vocab_size": 50257,
|
| 10 |
-
"n_embd": 512,
|
| 11 |
-
"n_layer": 12,
|
| 12 |
-
"n_head": 8,
|
| 13 |
-
"block_size": 128,
|
| 14 |
"dropout": 0.1,
|
| 15 |
"model_type": "gpt_neox"
|
| 16 |
}
|
|
@@ -22,13 +24,11 @@ paths = {
|
|
| 22 |
"model_save": "./agent_model",
|
| 23 |
"tokenizer_save": "./agent_tokenizer"
|
| 24 |
}
|
| 25 |
-
|
| 26 |
-
# Crée les dossiers s'ils n'existent pas
|
| 27 |
os.makedirs(paths["model_save"], exist_ok=True)
|
| 28 |
os.makedirs(paths["tokenizer_save"], exist_ok=True)
|
| 29 |
|
| 30 |
# -------------------------------
|
| 31 |
-
# Tokenizer minimal compatible Xenova
|
| 32 |
# -------------------------------
|
| 33 |
tokenizer = PreTrainedTokenizerFast(
|
| 34 |
tokenizer_file=None,
|
|
@@ -45,14 +45,18 @@ tokenizer.save_pretrained(paths["tokenizer_save"])
|
|
| 45 |
config = GPTNeoXConfig(
|
| 46 |
vocab_size=model_config["vocab_size"],
|
| 47 |
hidden_size=model_config["n_embd"],
|
| 48 |
-
|
| 49 |
num_attention_heads=model_config["n_head"],
|
| 50 |
max_position_embeddings=model_config["block_size"],
|
| 51 |
dropout_rate=model_config["dropout"],
|
| 52 |
)
|
| 53 |
|
| 54 |
model = GPTNeoXForCausalLM(config)
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# -------------------------------
|
| 58 |
# Test rapide
|
|
@@ -62,6 +66,21 @@ inputs = tokenizer(prompt, return_tensors="pt")
|
|
| 62 |
output = model.generate(**inputs, max_length=50)
|
| 63 |
print(tokenizer.decode(output[0], skip_special_tokens=True))
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# main.py — Gopu Agent-AI pour Friendli
|
| 2 |
import os
|
| 3 |
+
import torch
|
| 4 |
from transformers import PreTrainedTokenizerFast, GPTNeoXForCausalLM, GPTNeoXConfig
|
| 5 |
+
from huggingface_hub import login, upload_folder
|
| 6 |
|
| 7 |
# -------------------------------
|
| 8 |
# Paramètres du modèle (>2M)
|
| 9 |
# -------------------------------
|
| 10 |
model_config = {
|
| 11 |
"vocab_size": 50257,
|
| 12 |
+
"n_embd": 512,
|
| 13 |
+
"n_layer": 12,
|
| 14 |
+
"n_head": 8,
|
| 15 |
+
"block_size": 128,
|
| 16 |
"dropout": 0.1,
|
| 17 |
"model_type": "gpt_neox"
|
| 18 |
}
|
|
|
|
| 24 |
"model_save": "./agent_model",
|
| 25 |
"tokenizer_save": "./agent_tokenizer"
|
| 26 |
}
|
|
|
|
|
|
|
| 27 |
os.makedirs(paths["model_save"], exist_ok=True)
|
| 28 |
os.makedirs(paths["tokenizer_save"], exist_ok=True)
|
| 29 |
|
| 30 |
# -------------------------------
|
| 31 |
+
# Tokenizer minimal compatible Xenova + Friendli
|
| 32 |
# -------------------------------
|
| 33 |
tokenizer = PreTrainedTokenizerFast(
|
| 34 |
tokenizer_file=None,
|
|
|
|
| 45 |
config = GPTNeoXConfig(
|
| 46 |
vocab_size=model_config["vocab_size"],
|
| 47 |
hidden_size=model_config["n_embd"],
|
| 48 |
+
num_hidden_layers=model_config["n_layer"],
|
| 49 |
num_attention_heads=model_config["n_head"],
|
| 50 |
max_position_embeddings=model_config["block_size"],
|
| 51 |
dropout_rate=model_config["dropout"],
|
| 52 |
)
|
| 53 |
|
| 54 |
model = GPTNeoXForCausalLM(config)
|
| 55 |
+
|
| 56 |
+
# -------------------------------
|
| 57 |
+
# Sauvegarde en safetensors (recommandé pour Friendli)
|
| 58 |
+
# -------------------------------
|
| 59 |
+
model.save_pretrained(paths["model_save"], safe_serialization=True)
|
| 60 |
|
| 61 |
# -------------------------------
|
| 62 |
# Test rapide
|
|
|
|
| 66 |
output = model.generate(**inputs, max_length=50)
|
| 67 |
print(tokenizer.decode(output[0], skip_special_tokens=True))
|
| 68 |
|
| 69 |
+
# -------------------------------
|
| 70 |
+
# Push vers Hugging Face Hub
|
| 71 |
+
# -------------------------------
|
| 72 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 73 |
+
REPO_ID = "Mauricio-100/agent-ai" # Change si tu veux publier ailleurs
|
| 74 |
+
|
| 75 |
+
if HF_TOKEN:
|
| 76 |
+
print("🔐 Connexion à Hugging Face Hub...")
|
| 77 |
+
login(token=HF_TOKEN)
|
| 78 |
+
|
| 79 |
+
print("📤 Upload du modèle...")
|
| 80 |
+
upload_folder(folder_path=paths["model_save"], repo_id=REPO_ID, repo_type="model")
|
| 81 |
+
upload_folder(folder_path=paths["tokenizer_save"], repo_id=REPO_ID, repo_type="model")
|
| 82 |
+
print(f"✅ Modèle poussé sur https://huggingface.co/{REPO_ID}")
|
| 83 |
+
print("🚀 Tu peux maintenant activer Friendli Endpoints depuis l’onglet [Deploy] du modèle.")
|
| 84 |
+
else:
|
| 85 |
+
print("⚠️ Aucun HF_TOKEN trouvé. Ajoute-le dans les Secrets ou les variables d’environnement.")
|
| 86 |
+
|