{ "cells": [ { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "\n", "import tensorflow as tf\n", "import opendatasets as od\n", "import numpy as np\n", "from tensorflow.keras import layers\n", "from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline\n", "\n", "tf.get_logger().setLevel('ERROR')\n", "tokenizer = AutoTokenizer.from_pretrained(\"blanchefort/rubert-base-cased-sentiment-rurewiews\")" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "model = AutoModelForSequenceClassification.from_pretrained(\"blanchefort/rubert-base-cased-sentiment-rurewiews\")" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "pipe = pipeline(\"text-classification\", model=\"blanchefort/rubert-base-cased-sentiment-rurewiews\")" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'label': 'NEUTRAL', 'score': 0.5040543079376221}]" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pipe(\"Product is ok ok sounds also good but I am not feeling like 525 watts and weight also very less 525 watts means atleast 8kg or 10kg will be there and in paper they mentioned 5 modes are different on speaker what they given modes are different totally confused with this product\")" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Skipping, found downloaded files in \"restaurent_review\\reviews\" (use force=True to force download)\n" ] } ], "source": [ "import gradio as gr\n", "import pandas as pd\n", "import opendatasets as od\n", "\n", "od.download_kaggle_dataset('vigneshwarsofficial/reviews', data_dir='restaurent_review')\n", "prediction_data = pd.read_csv('restaurent_review/reviews/Restaurant_Reviews.tsv', delimiter='\\t')\n", "\n", "prediction_data.pop(\"Liked\")\n", "\n", "data = list(prediction_data[\"Review\"])\n", "results = pipe(data)\n" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Wow... Loved this place.',\n", " 'Crust is not good.',\n", " 'Not tasty and the texture was just nasty.',\n", " 'Stopped by during the late May bank holiday off Rick Steve recommendation and loved it.',\n", " 'The selection on the menu was great and so were the prices.']" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[:5]" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
countsentiment
0283Positive
1419Negative
2298Neutral
\n", "
" ], "text/plain": [ " count sentiment\n", "0 283 Positive\n", "1 419 Negative\n", "2 298 Neutral" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "positive_counter = 0 \n", "negative_counter = 0\n", "neutral_counter = 0\n", "for x in results:\n", " if x['label'] == 'POSITIVE':\n", " positive_counter = positive_counter + 1\n", " elif x['label'] == 'NEGATIVE':\n", " negative_counter = negative_counter + 1\n", " else:\n", " neutral_counter = neutral_counter + 1\n", "\n", "result_data = pd.DataFrame({\n", " 'count': [positive_counter, negative_counter, neutral_counter],\n", " 'sentiment': ['Positive', 'Negative', 'Neutral']\n", "})\n", "\n", "result_data" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7866\n", "Running on public URL: https://af39480688d32c192a.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "single_input_demo = gr.Interface.from_pipeline(pipe, title=\"Sentiment Analysis\")\n", "single_input_demo.launch(share=True)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7867\n", "Running on public URL: https://4ce90a546387218f07.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.express as plt\n", "\n", "def plotly_plot(): \n", " p = plt.bar(result_data, x='sentiment', y='count', title='Restaurent Review Analysis', color=\"count\")\n", " return p\n", "\n", "# show the results\n", "outputs = gr.Plot()\n", "\n", "demo = gr.Interface(fn=plotly_plot, inputs=None, outputs=outputs, title=\"Restaurant Customer Review Sentiment Analysis\")\n", "\n", "demo.launch(share=True)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" } }, "nbformat": 4, "nbformat_minor": 2 }