Abduuu commited on
Commit
9b924e3
1 Parent(s): 771015e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ !pip install gradio
2
+ !pip install transformers
3
+
4
+ import gradio as gr # Gradio library to create an interactive interface
5
+ from transformers import pipeline # Transformers libraries which imports pipeline to use Hugging-Face models
6
+ import pandas as pd # Pandas library for data manipulation and analysis
7
+ import matplotlib.pylab as plt # Matplot library for the interactive visualizations
8
+
9
+ # Initialize the analyzers
10
+
11
+ # Loads a pretrained model for the Arabic language
12
+ arabic_analyzer = pipeline('sentiment-analysis', model='CAMeL-Lab/bert-base-arabic-camelbert-da-sentiment')
13
+
14
+ # Loads a pretrained model for the English language
15
+ english_analyzer = pipeline('sentiment-analysis', model='distilbert-base-uncased-finetuned-sst-2-english')
16
+
17
+
18
+ # Define function
19
+
20
+ def sentiment_analysis(language,file):
21
+ # Select the appropriate analyzer
22
+ if language == "Arabic":
23
+ analyzer = arabic_analyzer
24
+ else:
25
+ analyzer = english_analyzer
26
+
27
+ results = []
28
+
29
+ # Read the sentences from the uploaded file
30
+ with open (file.name,'r') as fn:
31
+ sentences = fn.readlines()
32
+
33
+ # Perform sentiment analysis on each sentence
34
+ for sentence in sentences:
35
+ result = analyzer(sentence)
36
+ result = result[0]
37
+
38
+ results.append({
39
+ "Sentence": sentence,
40
+ "Label": result['label'],
41
+ "Score": f"{result['score'] *100:.2f}%"
42
+ })
43
+ # Convert the results into a DataFrame
44
+ df = pd.DataFrame(results)
45
+ # Ensure every label is lower, if not this will cause a logic error
46
+ # English labels are Upper but Arabic labels are lower
47
+ df['Label'] = df['Label'].str.lower()
48
+ # Take the "Label" column values
49
+ label_value = df['Label'].value_counts()
50
+
51
+ # Pre-Define the plot parameters
52
+ labels = ['Positive','Neutral','Negative']
53
+ counts = [label_value.get('positive',0),
54
+ label_value.get('neutral',0),
55
+ label_value.get('negative',0)]
56
+ colors=['green','gray','red']
57
+
58
+ # Create a bar plot
59
+ plt.bar(labels,counts,color=colors)
60
+ plt.title('Sentiment-Analysis')
61
+ plt.xlabel("Labels")
62
+ plt.ylabel("No. of sentences")
63
+
64
+
65
+ return df,plt
66
+
67
+
68
+ # Set up the Gradio interface
69
+
70
+ demo = gr.Interface(
71
+ fn=sentiment_analysis,
72
+ inputs=[gr.Dropdown(choices=["Arabic","English"],
73
+ label="Select a Language",
74
+ value="Arabic"),
75
+ gr.File(label="Upload a file")],
76
+ outputs=[gr.DataFrame(label="Results"),
77
+ gr.Plot(label="Bar plot")],
78
+ title="Sentiment-Analysis",
79
+ description="Gradio interface that allows users to Choose what language the sentences will be and upload a text file containing the sentences to be analyzed, the sentences will be classified and results will be in a formatted table along with a plot sentiment distribution"
80
+ )
81
+
82
+ demo.launch(debug=True)