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)