Skip to content

Car-ing is sharing, an auto dealership company for car sales and rental, is taking their services to the next level thanks to Large Language Models (LLMs).

As their newly recruited AI and NLP developer, you've been asked to prototype a chatbot app with multiple functionalities that not only assist customers but also provide support to human agents in the company.

The solution should receive textual prompts and use a variety of pre-trained Hugging Face LLMs to respond to a series of tasks, e.g. classifying the sentiment in a car’s text review, answering a customer question, summarizing or translating text, etc.

# Import necessary packages
import pandas as pd
# Start your code here!

# Importing pipeline
from transformers import pipeline

# Task - 1 (Sentiment analysis of car review classification)
sent_analysis_classifier = pipeline(task="sentiment-analysis", model='distilbert-base-uncased-finetuned-sst-2-english')

# Loading Dataframe and extracting reviews
df = pd.read_csv('data/car_reviews.csv', delimiter=';')
reviews = df['Review'].tolist()
labels = df['Class'].tolist()

# Use pipeline to get predicted labels.
predicted_labels = sent_analysis_classifier(reviews)
print(predicted_labels)

# Mapping labels to predictions list
predictions = [1 if plabel['label'] == 'POSITIVE' else 0 for plabel in predicted_labels]
print(predictions)

# Extracting real labels from the dataset
real_labels = [1 if label == 'POSITIVE' else 0 for label in labels]
print(real_labels)
import evaluate
accuarcy = evaluate.load('accuracy')
f1 = evaluate.load('f1')

accuracy_result = accuarcy.compute(references=real_labels, predictions=predictions)['accuracy']
f1_result = f1.compute(references=real_labels, predictions=predictions)['f1']
print("Accuracy: ", accuracy_result)
print("F1 score: ", f1_result)

# Task 2 - Translation EN to ES
translator = pipeline(task="translation_en_to_es", model="Helsinki-NLP/opus-mt-en-es")

# Extracting first two sentences
list_sentences = reviews[0].split('.')[:2]
first_two_sentences = ('.').join(list_sentences) + '.'
print("First two sentences: ", first_two_sentences)

# Translating the sentences
translated_review = translator(first_two_sentences)[0]['translation_text']
print("Translated text: ", translated_review)

# Get reference translations from the file
with open('data/reference_translations.txt', 'r') as file:
    lines = file.readlines()
    ref_translations = lines[0].strip()

print("Reference text: ", ref_translations)

# Loading and computing BLEU Score Metric
bleu = evaluate.load('bleu')
bleu_score = bleu.compute(predictions=[translated_review], references=[ref_translations])
print(bleu_score['bleu'])
# Task 4 - Text Summarization
summarizer = pipeline(task="summarization", model="facebook/bart-large-cnn")

original_text = reviews[-1]
summarized_text = summarizer(original_text, max_length=52)[0]['summary_text']

# print("Original text: ", original_text)
print("Summarized text: ", summarized_text)

# Analyzing the summary text using Toxicity
toxicity_metric = evaluate.load("toxicity")
toxicity_results = toxicity_metric.compute(predictions=summarized_text,aggregation="maximum")
print(toxicity_results)

# Analyzing the summary text using Regard
regard_metric = evaluate.load('regard')
regard_results = regard_metric.compute(data=summarized_text, aggregation="maximum")
print(regard_results)

# Task 3 - Question Answering
qa = pipeline(task='question-answering', model="deepset/minilm-uncased-squad2")
context = reviews[1]
question = "What did he like about the brand?"

answer = qa(context=context, question=question)['answer']
print('Answer: ', answer)