Hwilner commited on
Commit
370ca4d
·
verified ·
1 Parent(s): 6cbc100

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ import pandas as pd
4
+ import matplotlib.pyplot as plt
5
+
6
+ # Use a pipeline as a high-level helper
7
+ from transformers import pipeline
8
+
9
+ analyzer = pipeline("text-classification",
10
+ model="dicta-il/dictalm2.0")
11
+
12
+ # analyzer = pipeline("text-classification",
13
+ # model=model_path)
14
+
15
+
16
+
17
+ # print(analyzer(["This production is good", "This product was quite expensive"]))
18
+
19
+ def sentiment_analyzer(review):
20
+ sentiment = analyzer(review)
21
+ return sentiment[0]['label']
22
+
23
+ def sentiment_bar_chart(df):
24
+ sentiment_counts = df['Sentiment'].value_counts()
25
+
26
+ # Create a bar chart
27
+ fig, ax = plt.subplots()
28
+ sentiment_counts.plot(kind='pie', ax=ax, autopct='%1.1f%%', color=['green', 'red'])
29
+ ax.set_title('Review Sentiment Counts')
30
+ ax.set_xlabel('Sentiment')
31
+ ax.set_ylabel('Count')
32
+ # ax.set_xticklabels(['Positive', 'Negative'], rotation=0)
33
+
34
+ # Return the figure object
35
+ return fig
36
+
37
+
38
+ def read_reviews_and_analyze_sentiment(file_object):
39
+ # Load the Excel file into a DataFrame
40
+ df = pd.read_excel(file_object)
41
+
42
+ # Check if 'Review' column is in the DataFrame
43
+ if 'Reviews' not in df.columns:
44
+ raise ValueError("Excel file must contain a 'Review' column.")
45
+
46
+ # Apply the get_sentiment function to each review in the DataFrame
47
+ df['Sentiment'] = df['Reviews'].apply(sentiment_analyzer)
48
+ chart_object = sentiment_bar_chart(df)
49
+ return df, chart_object
50
+
51
+ # result = read_reviews_and_analyze_sentiment("../Files/Prod-review.xlsx")
52
+ # print(result)
53
+ # Example usage:
54
+ # df = read_reviews_and_analyze_sentiment('path_to_your_excel_file.xlsx')
55
+ # print(df)
56
+
57
+
58
+ demo = gr.Interface(fn=read_reviews_and_analyze_sentiment,
59
+ inputs=[gr.File(file_types=["xlsx"], label="Upload your review comment file")],
60
+ outputs=[gr.Dataframe(label="Sentiments"), gr.Plot(label="Sentiment Analysis")],
61
+ title="Sentiment Analyzer",
62
+ description="THIS APPLICATION WILL BE USED TO ANALYZE THE SENTIMENT BASED ON FILE UPLAODED.")
63
+ demo.launch()