fromslm_builderimportSLMBuilder# Initialize builder
builder=SLMBuilder(project_name="qa-from-csv",base_model="google/flan-t5-small")# Load data from CSV
builder.load_data_from_csv("qa_data.csv")# Train model
builder.train(epochs=3,learning_rate=2e-5,method="lora",batch_size=8)# Evaluate
metrics=builder.evaluate()print(f"Accuracy: {metrics['accuracy']:.2%}")print(f"F1 Score: {metrics['f1']:.3f}")# Test the model
questions=["What is Python?","How do I install packages?","What is pip?"]forquestioninquestions:answer=builder.generate(question)print(f"Q: {question}")print(f"A: {answer}\n")# Export
builder.export_model("./output",format="huggingface")
Expected Output
1
2
3
4
5
6
7
8
9
10
Training: 100%|██████████| 300/300 [02:45<00:00, 1.81it/s]
Evaluating: 100%|██████████| 50/50 [00:15<00:00, 3.25it/s]
Accuracy: 92.50%
F1 Score: 0.934
Q: What is Python?
A: Python is a high-level programming language...
✅ Model exported to ./output
JSON/JSONL Data Loading
Load training data from JSON or JSONL files.
JSON Format
1
2
3
4
5
6
7
8
9
10
[{"question":"What is machine learning?","answer":"Machine learning is a subset of AI..."},{"question":"What is deep learning?","answer":"Deep learning uses neural networks..."}]
JSONL Format
{"question": "What is machine learning?", "answer": "Machine learning is..."}
{"question": "What is deep learning?", "answer": "Deep learning uses..."}
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fromslm_builderimportSLMBuilderbuilder=SLMBuilder(project_name="qa-from-json",base_model="google/flan-t5-small")# Load from JSON
builder.load_data_from_json("data.json")# Or load from JSONL
builder.load_data_from_jsonl("data.jsonl")# Train and evaluate
builder.train(epochs=3,method="lora")metrics=builder.evaluate()print(f"✅ Training complete! Accuracy: {metrics['accuracy']:.2%}")
fromslm_builderimportSLMBuilderimportpandasaspddefpreprocess_data(df):"""Custom preprocessing function."""# Convert to lowercase
df['question']=df['question'].str.lower()df['answer']=df['answer'].str.lower()# Remove duplicates
df=df.drop_duplicates(subset=['question'])# Filter by length
df=df[df['question'].str.len()>10]df=df[df['answer'].str.len()>20]returndf# Load and preprocess
builder=SLMBuilder(project_name="custom-preprocessing",base_model="google/flan-t5-small")# Load raw data
raw_data=pd.read_csv("raw_data.csv")# Apply preprocessing
processed_data=preprocess_data(raw_data)# Save and load
processed_data.to_csv("processed_data.csv",index=False)builder.load_data_from_csv("processed_data.csv")# Train
builder.train(epochs=3,method="lora")
Train-Test Split Configuration
Configure how your data is split for training and testing.
fromslm_builderimportSLMBuilderbuilder=SLMBuilder(project_name="batch-inference",base_model="google/flan-t5-small")# Load and train
builder.load_data_from_csv("data.csv")builder.train(epochs=3,method="lora")# Prepare batch of questions
questions=["What is Python?","How do I create a list?","What is a dictionary?","How do I read a file?","What is a function?"]# Batch inference
responses=builder.generate_batch(questions,batch_size=8,max_length=128)# Display results
forq,ainzip(questions,responses):print(f"Q: {q}")print(f"A: {a}\n")
Model Saving and Loading
Save trained models and load them later.
Saving
1
2
3
4
5
6
7
8
9
10
fromslm_builderimportSLMBuilder# Train model
builder=SLMBuilder(project_name="saved-model")builder.load_data_from_csv("data.csv")builder.train(epochs=3,method="lora")# Save model
builder.save_model("./my-model")print("✅ Model saved to ./my-model")
Loading
1
2
3
4
5
6
7
8
9
10
11
12
fromslm_builderimportSLMBuilder# Load saved model
builder=SLMBuilder.load_model("./my-model")# Use immediately
response=builder.generate("What is Python?")print(response)# Continue training if needed
builder.load_data_from_csv("new_data.csv")builder.train(epochs=2,method="lora")
Configuration File
Use configuration files for reproducible experiments.