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