| |
| from transformers import pipeline |
| import gradio as gr |
| import pandas as pd |
| import matplotlib.pyplot as plt |
|
|
| analyser = pipeline("text-classification", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english") |
|
|
| |
|
|
| |
|
|
| |
|
|
|
|
| def sentiment_analysis(text_to_review): |
| sentiment = analyser(text_to_review) |
| return sentiment[0]['label'] |
|
|
| |
|
|
| def plot_sentiment_pie(df): |
| |
| sentiment_counts = df['Sentiment'].value_counts() |
| |
| |
| fig, ax = plt.subplots(figsize=(6, 6)) |
| ax.pie(sentiment_counts.values, labels=sentiment_counts.index, autopct='%1.1f%%') |
| ax.set_title('Sentiment Distribution') |
| |
| |
| return fig |
|
|
| def read_excel_and_get_sentiment(file): |
| try: |
| df = pd.read_excel(file) |
| if 'Review' not in df.columns: |
| raise KeyError("'Review' column not found in the Excel file.") |
| df['Sentiment'] = df['Review'].apply(sentiment_analysis) |
|
|
| chart_object = plot_sentiment_pie(df) |
|
|
| return df, chart_object |
| except FileNotFoundError: |
| print(f"Error: {file} not found.") |
| raise |
| except Exception as e: |
| print(f"Error: {e}") |
| raise |
|
|
| gr.close_all() |
|
|
| demo = gr.Interface(fn=read_excel_and_get_sentiment, |
| inputs=[gr.File(file_types= ['xlsx'],label="upload your review comment excel file.")], |
| outputs=[gr.DataFrame(label="Reviewed text"), gr.Plot(label="Sentiment Analysis")], |
| title="@IT AI Enthusiast (https://www.youtube.com/@itaienthusiast/) - Sentiment Analysis", |
| description="THIS APPLICATION WILL BE USED TO ANALYZER THE SENTIMENT BASED ON THE COMMENT PROVIDER.", |
| theme=gr.themes.Soft(), |
| concurrency_limit=16) |
| demo.launch() |
|
|
|
|