|
|
|
|
|
""" |
|
|
AgGPT-20 Target Trainer |
|
|
Simple single-file training script that creates individual .feather models |
|
|
|
|
|
Usage: |
|
|
python target_train.py [file_path] |
|
|
|
|
|
If no file path is provided, it will use extra_corpora/example.txt by default. |
|
|
""" |
|
|
|
|
|
import os |
|
|
import glob |
|
|
from train import AgGPTTrainer, MiniModelTrainer |
|
|
from feather import FeatherManager |
|
|
|
|
|
def get_next_target_model_number(models_dir="models"): |
|
|
"""Find the next available target_AgGPT_X.feather number""" |
|
|
if not os.path.exists(models_dir): |
|
|
os.makedirs(models_dir) |
|
|
|
|
|
|
|
|
pattern = os.path.join(models_dir, "target_AgGPT_*.feather") |
|
|
existing_files = glob.glob(pattern) |
|
|
|
|
|
if not existing_files: |
|
|
return 1 |
|
|
|
|
|
|
|
|
numbers = [] |
|
|
for file_path in existing_files: |
|
|
filename = os.path.basename(file_path) |
|
|
|
|
|
try: |
|
|
number_str = filename.split('_')[2].split('.')[0] |
|
|
numbers.append(int(number_str)) |
|
|
except (IndexError, ValueError): |
|
|
continue |
|
|
|
|
|
return max(numbers) + 1 if numbers else 1 |
|
|
|
|
|
def train_target_model(file_path="extra_corpora/example.txt", models_dir="models"): |
|
|
"""Train a single target model from a text file""" |
|
|
print("AgGPT-20 Target Trainer") |
|
|
print("=" * 40) |
|
|
|
|
|
|
|
|
if not os.path.exists(file_path): |
|
|
print(f"Error: Training file '{file_path}' not found!") |
|
|
print(f"Please create the file or specify a different path.") |
|
|
return False |
|
|
|
|
|
|
|
|
file_size = os.path.getsize(file_path) |
|
|
if file_size == 0: |
|
|
print(f"Error: Training file '{file_path}' is empty!") |
|
|
return False |
|
|
|
|
|
print(f"Training file: {file_path}") |
|
|
print(f"File size: {file_size} bytes") |
|
|
|
|
|
|
|
|
if not os.path.exists(models_dir): |
|
|
os.makedirs(models_dir) |
|
|
|
|
|
|
|
|
feather_manager = FeatherManager(models_dir) |
|
|
trainer = AgGPTTrainer(models_dir) |
|
|
mini_trainer = MiniModelTrainer(feather_manager) |
|
|
|
|
|
|
|
|
print(f"Loading training data from {file_path}...") |
|
|
try: |
|
|
training_pairs = trainer.load_training_data(file_path) |
|
|
except Exception as e: |
|
|
print(f"Error loading training data: {e}") |
|
|
return False |
|
|
|
|
|
if not training_pairs: |
|
|
print("No valid training pairs found in the file!") |
|
|
print("Please check the file format:") |
|
|
print("user: [your message]") |
|
|
print("<pad>") |
|
|
print("ai: [response]") |
|
|
print("<eos>") |
|
|
return False |
|
|
|
|
|
print(f"Found {len(training_pairs)} training pairs") |
|
|
|
|
|
|
|
|
print("Training mini-model...") |
|
|
try: |
|
|
mini_model = mini_trainer.train_mini_model(training_pairs) |
|
|
except Exception as e: |
|
|
print(f"Error during training: {e}") |
|
|
return False |
|
|
|
|
|
if not mini_model: |
|
|
print("Failed to create mini-model!") |
|
|
return False |
|
|
|
|
|
|
|
|
model_number = get_next_target_model_number(models_dir) |
|
|
model_filename = f"target_AgGPT_{model_number}.feather" |
|
|
|
|
|
|
|
|
print(f"Saving model as {model_filename}...") |
|
|
try: |
|
|
feather_manager.save_mini_model(mini_model, model_number, model_filename) |
|
|
model_path = os.path.join(models_dir, model_filename) |
|
|
print(f"โ
Successfully created: {model_path}") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"Error saving model: {e}") |
|
|
return False |
|
|
|
|
|
|
|
|
print("\nModel Statistics:") |
|
|
print(f" Training samples: {mini_model.get('training_samples', 0)}") |
|
|
print(f" Patterns: {len(mini_model.get('patterns', []))}") |
|
|
print(f" Responses: {len(mini_model.get('responses', []))}") |
|
|
print(f" Confidence: {mini_model.get('confidence', 0.0):.3f}") |
|
|
print(f" Keywords: {len(mini_model.get('keywords', []))}") |
|
|
|
|
|
print(f"\n๐ Target model training complete!") |
|
|
print(f"The model will be automatically discovered by chat.py") |
|
|
print("Run 'python chat.py' to test your new model!") |
|
|
|
|
|
return True |
|
|
|
|
|
def main(): |
|
|
import sys |
|
|
|
|
|
|
|
|
if len(sys.argv) > 1: |
|
|
file_path = sys.argv[1] |
|
|
else: |
|
|
file_path = "extra_corpora/example.txt" |
|
|
|
|
|
success = train_target_model(file_path) |
|
|
|
|
|
if not success: |
|
|
print("\nโ Training failed!") |
|
|
sys.exit(1) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|