LLM Question answer models for yes or no answers

541 Views Asked by At

imagine I have the following dataset:

import pandas as pd

# Positive and negative sentences
positive_sentences = [
    "I love this product!",
    "The weather is beautiful today.",
    "The team did an excellent job.",
    "She is a very talented musician."
]

negative_sentences = [
    "I am not satisfied with the service.",
    "The food was terrible at that restaurant.",
    "The movie was a complete disappointment.",
    "He made a lot of mistakes in the project."
]

# Combine positive and negative sentences
sentences = positive_sentences + negative_sentences

# Create a DataFrame with a "snippet" column
df = pd.DataFrame({'snippet': sentences})

# Display the DataFrame
print(df)

I want to use a LLM model that answers the following question. Is the following sentence positive, negative or neutral?

This is what I have tried so far:

## installing the libraries:
import pandas as pd
from transformers import pipeline, AutoModelForQuestionAnswering, AutoTokenizer
from transformers import RobertaTokenizer
from transformers import AutoTokenizer, RobertaForQuestionAnswering
import torch
from transformers import BertForQuestionAnswering, BertTokenizer

# Setting up the model and tokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForQuestionAnswering.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad')

# Create a question answering pipeline
question_answerer = pipeline("question-answering", model=model, tokenizer=tokenizer)

for index, row in df.iterrows():
    article = row["snippet"]  
    prompt = f"Is the following sentence positive, negative or neutral? {article}"

    result = question_answerer(question=prompt, context=article)

    # Check if "answer" key is in the result
    if "answer" in result:
        main_theme = result["answer"]
        print(f"Article {index+1} main theme: {main_theme}")
    else:
        print(f"Article {index+1} main theme not found in the result.")

0

There are 0 best solutions below