This demo uses Hugging Face Transformers for
1) Sentiment classification of 2 iPhone reviews
2) Generating a customer service reply
This shows how Transformers can produce context-aware, human-like text that is useful for automated customer support.
#
# (Optional) Environment setup – run these in a notebook cell / terminal
#
# Install PyTorch CPU wheels
# !pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
#
# Install / upgrade Transformers
# !pip install --upgrade transformers
#
# Remove TensorFlow / Keras to avoid Keras 3 + Transformers conflicts
# !pip uninstall tensorflow keras -y
#
#
# Imports
#
from transformers import pipeline, set_seed
import pandas as pd
#
# Sample reviews
#
# review1: Negative customer review about an iPhone purchase
review1= '''Extremely disappointed with my recent iPhone purchase from Apple. The device constantly lags, and the battery life is abysmal, barely lasting through the day. Despite the hefty price tag, the performance is far from satisfactory. Customer support has been unhelpful, providing no viable solutions to address these persistent issues. This experience has left me regretting my decision to choose Apple, and I expected much better from a company known for its premium products.'''
# review2: Positive customer review about an iPhone purchase
review2 = '''I recently purchased an iPhone from Apple, and it has been an absolute delight! The device runs smoothly, and the battery life is impressive, easily lasting throughout the day. The price, though high, is justified by the excellent performance and top-notch customer support. I am thoroughly satisfied with my decision to choose Apple, and it reaffirms their reputation for delivering premium products. Highly recommended for anyone seeking a reliable and high-performance smartphone.'''
#
# Task 1: Sentiment Classification
# Analyze the reviews and classify sentiment (positive / negative).
#
sentiment_classifier = pipeline(
task="text-classification",
framework="pt" # Force PyTorch backend to avoid TensorFlow / Keras issues
)
# Classify sentiment for the negative review
outputs1 = sentiment_classifier(review1)
df1 = pd.DataFrame(outputs1)
print("Sentiment for review1 (negative review):")
print(df1, "\n")
# Classify sentiment for the positive review
outputs2 = sentiment_classifier(review2)
df2 = pd.DataFrame(outputs2)
print("Sentiment for review2 (positive review):")
print(df2, "\n")
#
# Task 2: Text Generation
# Generate a customer service response to the negative review.
#
set_seed(42) # For reproducible generation
# Use a text-generation pipeline with PyTorch
text_generator = pipeline(
task="text-generation",
framework="pt" # Again, force PyTorch
# You can also specify a model explicitly, e.g. model="gpt2"
)
# Base response opening from customer service
base_response = (
"Dear Customer, thank you for taking the time to share your feedback. "
"I’m very sorry to hear about your experience with your iPhone."
)
# Combine the original review and the response prompt
prompt = review1 + "\n\nCustomer service response:\n" + base_response
# Generate a reply
generated = text_generator(prompt, max_length=150, num_return_sequences=1)
print(generated[0]["generated_text"])
Output:
Extremely disappointed with my recent iPhone purchase from Apple. The device constantly lags, and the battery life is abysmal, barely lasting through the day. Despite the hefty price tag, the performance is far from satisfactory. Customer support has been unhelpful, providing no viable solutions to address these persistent issues. This experience has left me regretting my decision to choose Apple, and I expected much better from a company known for its premium products.
Customer service response:
Dear Customer, thank you for taking the time to share your feedback. I’m very sorry to hear about your experience with your iPhone. I would like to thank you for your patience, understanding, and understanding. I understand that we are unable to provide solutions for the iPhone that you purchased from Apple, and I will gladly accept your refund. We would like to sincerely apologize for the inconvenience and disappointment this has caused you. Thank you for your patience and understanding.
Quality assurance:
Dear Customer, I apologize for the inconvenience caused by our service. I understand that we are unable to provide solutions for the iPhone that you purchased from Apple, and I will gladly accept your refund. We would like to sincerely apologize for the inconvenience and disappointment this has caused you. Thank you for your patience and understanding.
Expert Advice:
You can get expert advice from Apple about performance and battery life. However, please note that you may have to pay additional shipping to get the app to work correctly.
What do I do?
I would like to take the time to discuss my issues with Apple. I've reviewed various options in the app, and believe that the best approach is to purchase the app from us and return it here before refunding you. You may also want to take the time to review the features available from Apple.