Ramesh572's picture
Update app.py
7913995
import tensorflow as tf
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
import opendatasets as od
import gradio as gr
import pandas as pd
import plotly.express as plt
tf.get_logger().setLevel("ERROR")
# create tokenizer from pre-trained model
tokenizer = AutoTokenizer.from_pretrained(
"blanchefort/rubert-base-cased-sentiment-rurewiews"
)
# Load the model
model = AutoModelForSequenceClassification.from_pretrained(
"blanchefort/rubert-base-cased-sentiment-rurewiews"
)
# Create a pipeline for the model
pipe = pipeline(
"text-classification", model="blanchefort/rubert-base-cased-sentiment-rurewiews"
)
# load review from open dataset
od.download_kaggle_dataset("vigneshwarsofficial/reviews", data_dir="restaurent_review")
prediction_data = pd.read_csv(
"restaurent_review/reviews/Restaurant_Reviews.tsv", delimiter="\t"
)
# popping irrelevant coloumn
prediction_data.pop("Liked")
# making a list
data = list(prediction_data["Review"])
# making prediction using pipe
results = pipe(data)
# Categorizing result
positive_counter = 0
negative_counter = 0
neutral_counter = 0
for x in results:
if x["label"] == "POSITIVE":
positive_counter = positive_counter + 1
elif x["label"] == "NEGATIVE":
negative_counter = negative_counter + 1
else:
neutral_counter = neutral_counter + 1
result_data = pd.DataFrame(
{
"count": [positive_counter, negative_counter, neutral_counter],
"sentiment": ["Positive", "Negative", "Neutral"],
}
)
# create bar chart interface on gradio
def plotly_plot():
p = plt.bar(
result_data,
x="sentiment",
y="count",
title="Restaurent Review Analysis",
color="count",
)
return p
# show the results
outputs = gr.Plot()
demo = gr.Interface(
fn=plotly_plot,
inputs=None,
outputs=outputs,
title="Restaurant Customer Review Sentiment Analysis",
)
demo.launch()