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.

Before you start

In order to complete the project you may wish to install some Hugging Face libraries such as transformers and evaluate.

!pip install transformers
!pip install evaluate==0.4.0
!pip install datasets==2.10.0
!pip install sentencepiece==0.1.97

from transformers import logging
logging.set_verbosity(logging.WARNING)
Hidden output
# Start your code here!
from transformers import pipeline
def parse_csv(file_path):
    data = []
    with open(file_path, 'r') as file:
        for line in file:
            cleaned_line = line.strip()
            row = cleaned_line.split(';')
            data.append(row)
    return data
reviews = parse_csv('data/car_reviews.csv')
# Remove the first row (header row) in-place
del reviews[0]
inputs = [item[0] for item in reviews]
labels = [1 if item[1] == 'POSITIVE' else 0 for item in reviews]
classifier = pipeline(task='text-classification', model='distilbert-base-uncased-finetuned-sst-2-english')
Hidden output
predicted_labels = classifier(inputs)
predictions = [1 if item['label'] == 'POSITIVE' else 0 for item in predicted_labels]
accuracy = evaluate.load("accuracy")
f1 = evaluate.load("f1")
Hidden output
accuracy_score = accuracy.compute(references=labels, predictions=predictions)
f1_score = f1.compute(references=labels, predictions=predictions)

accuracy_result = accuracy_score['accuracy']
f1_result = f1_score['f1']
translator = pipeline(task='translation_en_to_es', model='Helsinki-NLP/opus-mt-en-es')
Hidden output
sentences_to_translate = [f'{sentence}.' for sentence in inputs[0].split('. ')[:2]]
output = translator(sentences_to_translate, max_length=30, clean_up_tokenization_spaces=True)
translated_sentences = [item['translation_text'] for item in output]
translated_review = ' '.join(translated_sentences)
with open("data/reference_translations.txt", 'r') as file:
    lines = file.readlines()
    references = [line.strip() for line in lines]