A Coding Guide to Sentiment Analysis of Customer Reviews Using IBMs Open Source AI Model Granite-3B and Hugging Face Transformers
www.marktechpost.com
In this tutorial, we will look into how to easily perform sentiment analysis on text data using IBMs open-source Granite 3B model integrated with Hugging Face Transformers. Sentiment analysis, a widely-used natural language processing (NLP) technique, helps quickly identify the emotions expressed in text. It makes it invaluable for businesses aiming to understand customer feedback and enhance their products and services. Now, lets walk you through installing the necessary libraries, loading the IBM Granite model, classifying sentiments, and visualizing your results, all effortlessly executable in Google Colab.!pip install transformers torch accelerateFirst, well install the essential librariestransformers, torch, and acceleraterequired for loading and running powerful NLP models seamlessly. Transformers provides pre-built NLP models, torch serves as the backend for deep learning tasks, and accelerate ensures efficient resource utilization on GPUs.import torchfrom transformers import AutoModelForCausalLM, AutoTokenizer, pipelineimport pandas as pdimport matplotlib.pyplot as pltThen, well import the required Python libraries. Well use torch for efficient tensor operations, transformers for loading pre-trained NLP models from Hugging Face, pandas for managing and processing data in structured formats, and matplotlib for visually interpreting your analysis results clearly and intuitively.model_id = "ibm-granite/granite-3.0-3b-a800m-instruct"tokenizer = AutoTokenizer.from_pretrained(model_id)model = AutoModelForCausalLM.from_pretrained( model_id, device_map='auto', torch_dtype=torch.bfloat16, trust_remote_code=True)generator = pipeline("text-generation", model=model, tokenizer=tokenizer)Here, well load IBMs open-source Granite 3B instruction-following model, specifically ibm-granite/granite-3.0-3b-a800m-instruct, using Hugging Faces AutoTokenizer and AutoModelForCausalLM. This compact, instruction-tuned model is optimized to handle tasks like sentiment classification directly within Colab, even under limited computational resources.def classify_sentiment(review): prompt = f"""Classify the sentiment of the following review as Positive, Negative, or Neutral.Review: "{review}"Sentiment:""" response = generator( prompt, max_new_tokens=5, do_sample=False, pad_token_id=tokenizer.eos_token_id ) sentiment = response[0]['generated_text'].split("Sentiment:")[-1].split("n")[0].strip() return sentimentNow well define the core function classify_sentiment. This function leverages the IBM Granite 3B model through an instruction-based prompt to classify the sentiment of any given review into Positive, Negative, or Neutral. The function formats the input review, invokes the model with precise instructions, and extracts the resulting sentiment from the generated text.import pandas as pdreviews = [ "I absolutely loved the service! Definitely coming back.", "The item arrived damaged, very disappointed.", "Average product. Nothing too exciting.", "Superb experience, exceeded all expectations!", "Not worth the money, poor quality."]reviews_df = pd.DataFrame(reviews, columns=['review'])Next, well create a simple DataFrame reviews_df using Pandas, containing a collection of example reviews. These sample reviews serve as input data for sentiment classification, enabling us to observe how effectively the IBM Granite model can determine customer sentiments in a practical scenario.reviews_df['sentiment'] = reviews_df['review'].apply(classify_sentiment)print(reviews_df)After defining the reviews, well apply the classify_sentiment function to each review in the DataFrame. This will generate a new column, sentiment, where the IBM Granite model classifies each review as Positive, Negative, or Neutral. By printing the updated reviews_df, we can see the original text and its corresponding sentiment classification.import matplotlib.pyplot as pltsentiment_counts = reviews_df['sentiment'].value_counts()plt.figure(figsize=(8, 6))sentiment_counts.plot.pie(autopct='%1.1f%%', explode=[0.05]*len(sentiment_counts), colors=['#66bb6a', '#ff7043', '#42a5f5'])plt.ylabel('')plt.title('Sentiment Distribution of Reviews')plt.show()Lastly, well visualize the sentiment distribution in a pie chart. This step provides a clear, intuitive overview of how the reviews are classified, making interpreting the models overall performance easier. Matplotlib lets us quickly see the proportion of Positive, Negative, and Neutral sentiments, bringing your sentiment analysis pipeline full circle.PlotIn conclusion, we have successfully implemented a powerful sentiment analysis pipeline using IBMs Granite 3B open-source model hosted on Hugging Face. You learned how to leverage pre-trained models to quickly classify text into positive, negative, or neutral sentiments, visualize insights effectively, and interpret your findings. This foundational approach allows you to easily adapt these skills to analyze datasets or explore other NLP tasks. IBMs Granite models combined with Hugging Face Transformers offer an efficient way to perform advanced NLP tasks.Here is the Colab Notebook. Also,dont forget to follow us onTwitterand join ourTelegram ChannelandLinkedIn Group. Dont Forget to join our80k+ ML SubReddit. Asif RazzaqWebsite| + postsBioAsif Razzaq is the CEO of Marktechpost Media Inc.. As a visionary entrepreneur and engineer, Asif is committed to harnessing the potential of Artificial Intelligence for social good. His most recent endeavor is the launch of an Artificial Intelligence Media Platform, Marktechpost, which stands out for its in-depth coverage of machine learning and deep learning news that is both technically sound and easily understandable by a wide audience. The platform boasts of over 2 million monthly views, illustrating its popularity among audiences.Asif Razzaqhttps://www.marktechpost.com/author/6flvq/AMD Releases Instella: A Series of Fully Open-Source State-of-the-Art 3B Parameter Language ModelAsif Razzaqhttps://www.marktechpost.com/author/6flvq/Alibaba Released Babel: An Open Multilingual Large Language Model LLM Serving Over 90% of Global SpeakersAsif Razzaqhttps://www.marktechpost.com/author/6flvq/A Step by Step Guide to Deploy Streamlit App Using Cloudflared, BeautifulSoup, Pandas, Plotly for Real-Time Cryptocurrency Web Scraping and VisualizationAsif Razzaqhttps://www.marktechpost.com/author/6flvq/Qwen Releases QwQ-32B: A 32B Reasoning Model that Achieves Significantly Enhanced Performance in Downstream Task Recommended Open-Source AI Platform: IntellAgent is a An Open-Source Multi-Agent Framework to Evaluate Complex Conversational AI System' (Promoted)
0 Kommentare
·0 Anteile
·72 Ansichten