Examples¶
This section provides example scripts to illustrate how to use various functionalities of the textpredict
package.
Simple Text Prediction¶
The following example demonstrates how to use the textpredict
package to perform simple sentiment analysis and emotion detection.
from datasets import load_dataset
import textpredict as tp
# Function to test simple prediction using default model
def text_simple_prediction():
# Sentiment analysis
texts = ["I love this product!", "I love this product!"]
model = tp.initialize(task="sentiment")
result = model.analyze(texts, return_probs=False)
print(f"Simple Prediction Result: {result}")
# Emotion detection
texts = ["I am happy today", "I am happy today"]
model = tp.initialize(task="emotion")
result = model.analyze(texts, return_probs=False)
print(f"Emotion Prediction Result: {result}")
if __name__ == "__main__":
text_simple_prediction()
Sequence Classification Training¶
The following example demonstrates how to use the SequenceClassificationTrainer
class to train a sequence classification model.
from datasets import load_dataset
import textpredict as tp
# Function to test sequence classification trainer
def sequence_classification_training():
dataset = load_dataset('imdb')
trainer = tp.SequenceClassificationTrainer(model_name='bert-base-uncased', output_dir='./model', config=None, device='cpu')
trainer.train_dataset = dataset['train']
trainer.val_dataset = dataset['test']
trainer.train()
trainer.save()
results = trainer.evaluate(dataset['test'])
print(f"Training Results: {results}")
if __name__ == "__main__":
sequence_classification_training()
Explainability¶
The following example demonstrates how to use the Explainability
class to get feature importance for a given text.
from textpredict import initialize, Explainability
# Initialize the predictor
predictor = initialize(task='sentiment', device='cpu', model_name='bert-base-uncased')
# Explainability instance
explain = Explainability(model_name='bert-base-uncased', task='sentiment')
# Get feature importance
importance = explain.feature_importance("This is a great product!")
print(importance)
Benchmarking¶
The following example demonstrates how to use the Benchmarking
class to benchmark a model.
from textpredict import Benchmarking
from datasets import load_dataset
def benchmark_model():
dataset = load_dataset('imdb', split='test')
benchmark = Benchmarking(model_name='bert-base-uncased')
metrics = benchmark.benchmark(dataset)
inference_time = benchmark.measure_inference_time(dataset)
memory_usage = benchmark.measure_memory_usage(dataset)
print(metrics, inference_time, memory_usage)
if __name__ == "__main__":
benchmark_model()