import torch import gradio as gr import pandas as pd import matplotlib.pyplot as plt # Use a pipeline as a high-level helper from transformers import pipeline # model_path = "../models/models--distilbert--distilbert-base-uncased-finetuned-sst-2-english/snapshots/714eb0fa89d2f80546fda750413ed43d93601a13" # analyzer = pipeline("text-classification", model=model_path) analyzer = pipeline("text-classification", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english") # print(analyzer(["This product is good", "This product was quite expensive"])) def sentiment_analyzer(review): sentiment = analyzer(review) return sentiment[0]['label'] def generate_sentiment_bar_chart(df): # Validate DataFrame if not {'Review', 'Sentiment'}.issubset(df.columns): raise ValueError("DataFrame must contain 'Review' and 'Sentiment' columns.") # Count occurrences of each sentiment sentiment_counts = df['Sentiment'].value_counts() # Create bar chart fig, ax = plt.subplots(figsize=(8, 5)) sentiment_counts.plot(kind='bar', color=['green', 'red'], edgecolor='black', ax=ax) # Customize plot ax.set_title("Sentiment Distribution", fontsize=14) ax.set_xlabel("Sentiment", fontsize=12) ax.set_ylabel("Count", fontsize=12) ax.grid(axis='y', linestyle='--', alpha=0.7) plt.xticks(rotation=45) # Adjust layout plt.tight_layout() return fig def read_review_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' colum.") df['Sentiment'] = df['Review'].apply(sentiment_analyzer) chat_object = generate_sentiment_bar_chart(df) return df, chat_object # file = '../files/product_review.xlsx' # result = read_review_and_analyze_sentiment(file) # print(result) gr.close_all() # demo = gr.Interface(fn=summary, inputs="text", outputs="text") demo = gr.Interface(fn=read_review_and_analyze_sentiment, inputs=[gr.File(file_types=[".xlsx"],label="Input your review comment")], outputs=[gr.Dataframe(label="Sentiment"), gr.Plot(label="Sentiment Analysis")], title="GenAI Project 3: Sentiment Analyzer", description="This application is use to analyze the sentiment based on the File uploaded.") demo.launch()