Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| from transformers import pipeline | |
| # Use a pipeline with a suitable model for sentiment analysis | |
| model_name = "distilbert/distilbert-base-uncased-finetuned-sst-2-english" | |
| analyzer = pipeline("text-classification", model=model_name) | |
| def sentiment_analyzer(review): | |
| try: | |
| sentiment = analyzer(review) | |
| return sentiment[0]['label'] | |
| except Exception as e: | |
| print(f"Error in sentiment_analyzer: {e}") | |
| return f"Error: {e}" | |
| def sentiment_bar_chart(df): | |
| sentiment_counts = df['Sentiment'].value_counts() | |
| fig, ax = plt.subplots() | |
| sentiment_counts.plot(kind='pie', ax=ax, autopct='%1.1f%%', colors=['green', 'red']) | |
| ax.set_title('Review Sentiment Counts') | |
| ax.set_xlabel('Sentiment') | |
| ax.set_ylabel('Count') | |
| return fig | |
| def read_reviews_and_analyze_sentiment(file_object): | |
| try: | |
| df = pd.read_excel(file_object) | |
| if 'Reviews' not in df.columns: | |
| raise ValueError("Excel file must contain a 'Reviews' column.") | |
| df['Sentiment'] = df['Reviews'].apply(sentiment_analyzer) | |
| chart_object = sentiment_bar_chart(df) | |
| return df, chart_object | |
| except Exception as e: | |
| print(f"Error in read_reviews_and_analyze_sentiment: {e}") | |
| return f"Error: {e}", None | |
| gr.close_all() | |
| demo = gr.Interface(fn=read_reviews_and_analyze_sentiment, | |
| 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 will be used to analyze the sentiment based on the uploaded file.") | |
| demo.launch() | |