humbleakh commited on
Commit
3da706e
·
verified ·
1 Parent(s): c65db73

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+ import pandas as pd
5
+ from textblob import TextBlob
6
+ import matplotlib.pyplot as plt
7
+
8
+ # model_path = "C:\\Users\\abdul\\Documents\\genaiproj\\genai\\Models\\models--distilbert--distilbert-base-uncased-finetuned-sst-2-english"
9
+ analyzer = pipeline("text-classification", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
10
+ # analyzer = pipeline("text-classification", model=model_path)
11
+
12
+ # print(analyzer(["Nice to meet you!", "very expensive"]))
13
+
14
+ def sentiment_analysis(review):
15
+ sentiment = analyzer(review)
16
+ return sentiment[0]['label']
17
+
18
+ def plot_sentiment_distribution(df):
19
+ # Check if required columns are present
20
+ if 'Review' not in df.columns or 'Sentiment' not in df.columns:
21
+ raise ValueError("DataFrame must contain 'Review' and 'Sentiment' columns.")
22
+
23
+ # Count positive and negative sentiments
24
+ sentiment_counts = df['Sentiment'].value_counts()
25
+
26
+ # Create a bar chart
27
+ fig, ax = plt.subplots()
28
+ sentiment_counts.plot(kind='bar', ax=ax, color=['skyblue', 'salmon'])
29
+
30
+ # Set chart labels and title
31
+ ax.set_xlabel('Sentiment')
32
+ ax.set_ylabel('Count')
33
+ ax.set_title('Sentiment Distribution')
34
+
35
+ # Return the figure object
36
+ return fig
37
+
38
+ def analyze_reviews(file):
39
+ if not file.name.endswith('.xlsx'):
40
+ return "Invalid file type. Please upload an Excel file."
41
+
42
+ # Read the Excel file
43
+ df = pd.read_excel(file)
44
+
45
+ if 'Review' not in df.columns:
46
+ return "The Excel file must contain a column named 'Review'."
47
+
48
+ # Apply get_sentiment function to each review and create new column
49
+ df['Sentiment'] = df['Review'].apply(sentiment_analysis)
50
+ chart_object = plot_sentiment_distribution(df)
51
+ return df, chart_object
52
+
53
+ # Result = analyze_reviews("C:\\Users\\abdul\\Documents\\genaiproj\\genai\\Files\\app_reviews.xlsx")
54
+ # print(Result)
55
+
56
+ # Example usage
57
+ # file_path = 'path_to_your_excel_file.xlsx' # Update with your actual file path
58
+ # result_df = analyze_reviews(file_path)
59
+ # print(result_df)
60
+
61
+
62
+ gr.close_all()
63
+
64
+ # demo = gr.Interface(fn=summary, inputs="text", outputs="text")
65
+
66
+ demo = gr.Interface(
67
+ fn=analyze_reviews,
68
+ inputs=[gr.File(label="Input file to analyze")],
69
+ outputs=[gr.Dataframe(label="Sentiments"), gr.Plot(label="Sentiment Distribution")],
70
+ title="Sentiment Analyzer",
71
+ theme="soft",
72
+ description="Analyze the sentiment of any review in seconds!")
73
+
74
+ demo.launch(share=True)
75
+
76
+
77
+
78
+
79
+ # Example usage
80
+ # data = {'Review': ['Great product!', 'Not good', 'Excellent service', 'Bad experience'],
81
+ # 'Sentiment': ['Positive', 'Negative', 'Positive', 'Negative']}
82
+ # df = pd.DataFrame(data)
83
+ # fig = plot_sentiment_distribution(df)
84
+ # fig.show()