Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconNLP with Transformers: Fundamentals and Core Applications
NLP with Transformers: Fundamentals and Core Applications

Project 3: Customer Feedback Analysis Using Sentiment Analysis

7. Step 5: Testing with Real Customer Feedback

Test your model with actual customer feedback to see how well it performs.

# Define a sample feedback
custom_feedback = "The product was delivered late and the quality was not as expected."

# Tokenize and predict
inputs = tokenizer(custom_feedback, return_tensors="pt", truncation=True, padding=True)
outputs = model(**inputs)
predicted_label = outputs.logits.argmax(-1).item()

# Interpret the result
sentiments = ['Negative', 'Positive']
print(f"Predicted Sentiment: {sentiments[predicted_label]}")

Let's break down this code:

1. Setting up the test input:

custom_feedback = "The product was delivered late and the quality was not as expected."

This line defines a sample customer feedback text to analyze.

2. Processing and prediction:

  • The feedback is tokenized using the BERT tokenizer, converting the text into a format the model can process
  • The tokenized input is passed to the model, which returns output logits (raw prediction scores)
  • argmax(-1).item() converts these logits into a single predicted label (0 or 1)

3. Interpreting results:

  • A list of sentiments (['Negative', 'Positive']) maps the numerical predictions to human-readable labels
  • The final line prints the predicted sentiment for the given feedback

This code represents the practical application of the sentiment analysis model, allowing it to analyze real customer feedback and determine whether the sentiment is positive or negative.

7. Step 5: Testing with Real Customer Feedback

Test your model with actual customer feedback to see how well it performs.

# Define a sample feedback
custom_feedback = "The product was delivered late and the quality was not as expected."

# Tokenize and predict
inputs = tokenizer(custom_feedback, return_tensors="pt", truncation=True, padding=True)
outputs = model(**inputs)
predicted_label = outputs.logits.argmax(-1).item()

# Interpret the result
sentiments = ['Negative', 'Positive']
print(f"Predicted Sentiment: {sentiments[predicted_label]}")

Let's break down this code:

1. Setting up the test input:

custom_feedback = "The product was delivered late and the quality was not as expected."

This line defines a sample customer feedback text to analyze.

2. Processing and prediction:

  • The feedback is tokenized using the BERT tokenizer, converting the text into a format the model can process
  • The tokenized input is passed to the model, which returns output logits (raw prediction scores)
  • argmax(-1).item() converts these logits into a single predicted label (0 or 1)

3. Interpreting results:

  • A list of sentiments (['Negative', 'Positive']) maps the numerical predictions to human-readable labels
  • The final line prints the predicted sentiment for the given feedback

This code represents the practical application of the sentiment analysis model, allowing it to analyze real customer feedback and determine whether the sentiment is positive or negative.

7. Step 5: Testing with Real Customer Feedback

Test your model with actual customer feedback to see how well it performs.

# Define a sample feedback
custom_feedback = "The product was delivered late and the quality was not as expected."

# Tokenize and predict
inputs = tokenizer(custom_feedback, return_tensors="pt", truncation=True, padding=True)
outputs = model(**inputs)
predicted_label = outputs.logits.argmax(-1).item()

# Interpret the result
sentiments = ['Negative', 'Positive']
print(f"Predicted Sentiment: {sentiments[predicted_label]}")

Let's break down this code:

1. Setting up the test input:

custom_feedback = "The product was delivered late and the quality was not as expected."

This line defines a sample customer feedback text to analyze.

2. Processing and prediction:

  • The feedback is tokenized using the BERT tokenizer, converting the text into a format the model can process
  • The tokenized input is passed to the model, which returns output logits (raw prediction scores)
  • argmax(-1).item() converts these logits into a single predicted label (0 or 1)

3. Interpreting results:

  • A list of sentiments (['Negative', 'Positive']) maps the numerical predictions to human-readable labels
  • The final line prints the predicted sentiment for the given feedback

This code represents the practical application of the sentiment analysis model, allowing it to analyze real customer feedback and determine whether the sentiment is positive or negative.

7. Step 5: Testing with Real Customer Feedback

Test your model with actual customer feedback to see how well it performs.

# Define a sample feedback
custom_feedback = "The product was delivered late and the quality was not as expected."

# Tokenize and predict
inputs = tokenizer(custom_feedback, return_tensors="pt", truncation=True, padding=True)
outputs = model(**inputs)
predicted_label = outputs.logits.argmax(-1).item()

# Interpret the result
sentiments = ['Negative', 'Positive']
print(f"Predicted Sentiment: {sentiments[predicted_label]}")

Let's break down this code:

1. Setting up the test input:

custom_feedback = "The product was delivered late and the quality was not as expected."

This line defines a sample customer feedback text to analyze.

2. Processing and prediction:

  • The feedback is tokenized using the BERT tokenizer, converting the text into a format the model can process
  • The tokenized input is passed to the model, which returns output logits (raw prediction scores)
  • argmax(-1).item() converts these logits into a single predicted label (0 or 1)

3. Interpreting results:

  • A list of sentiments (['Negative', 'Positive']) maps the numerical predictions to human-readable labels
  • The final line prints the predicted sentiment for the given feedback

This code represents the practical application of the sentiment analysis model, allowing it to analyze real customer feedback and determine whether the sentiment is positive or negative.