from transformers import pipeline
model_names = ["model1", "model2", "model3"]
pipelines = [pipeline("sentiment-analysis", model=model_name) for model_name in model_names]
def ensemble_predict(text):
predictions = [pipeline(text)[0] for pipeline in pipelines]
labels = [pred["label"] for pred in predictions]
scores = [pred["score"] for pred in predictions]
# Majority voting
final_label = max(set(labels), key=labels.count)
final_score = sum(scores) / len(scores)
return {"label": final_label, "score": final_score}
text = "The company reported strong quarterly earnings, exceeding market expectations."
result = ensemble_predict(text)
print(result)