import torch import gradio as gr import pandas as pd import matplotlib.pyplot as plt from transformers import pipeline analyzer = pipeline("text-classification",model="distilbert/distilbert-base-uncased-finetuned-sst-2-english") # model_path = ("../.venv/Models/models--distilbert--distilbert-base-uncased-finetuned-sst-2-english/snapshots/714eb0fa89d2f80546fda750413ed43d93601a13") # analyzer = pipeline("text-classification",model=model_path) # print(analyzer(["This production is good", "This product was quite expensive"])) def sentiment_analyzer(review): sentiment = analyzer(review) return sentiment[0]['label'] def sentiment_bar_chart(df): sentiment_counts = df['Sentiment'].value_counts() # Create a bar chart fig, ax = plt.subplots() sentiment_counts.plot(kind='pie', ax=ax, autopct='%1.1f%%') ax.set_title('Review Sentiment Counts') ax.set_xlabel('Sentiment') ax.set_ylabel('Count') # ax.set_xticklabels(['Positive', 'Negative'], rotation=0) # Return the figure object return fig def read_reviews_and_analyze_sentiment(file_object): df = pd.read_excel(file_object) if 'Review' not in df.columns: raise ValueError("Excel file must contain a 'Review' column.") df['Sentiment'] = df['Review'].apply(sentiment_analyzer) chart_object = sentiment_bar_chart(df) return df, chart_object # result = read_reviews_and_analyze_sentiment("../Files/Sentiment_Analysis_Reviews.xlsx") # print(result) # df = read_reviews_and_analyze_sentiment('path_to_your_excel_file.xlsx') # print(df) demo = gr.Interface(fn=read_reviews_and_analyze_sentiment, # inputs=[gr.Textbox(label="Input you text/review comment for analysis", lines =4)], # outputs=[gr.Textbox(label="Sentiment", lines=1)], inputs=[gr.File(file_types=[".xlsx"], label="Upload your review comment file")], outputs=[gr.Dataframe(label="Sentiments"), gr.Plot(label="Sentiment Analysis")], title="Sentiment Analyzer", description="THIS APPLICATION CAN BE USED TO ANALYZE THE SENTIMENT BASED ON FILE UPLAODED.") demo.launch() # Assuming you have a dataframe `df` with appropriate data # fig = sentiment_bar_chart(df) # fig.show() # This line is just to visualize the plot in a local environment