|
import gradio as gr |
|
import pandas as pd |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline |
|
import matplotlib.pyplot as plt |
|
|
|
|
|
model_name = "ac0hik/Sentiment_Analysis_French" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForSequenceClassification.from_pretrained(model_name) |
|
|
|
|
|
classifier = pipeline('sentiment-analysis', model=model, tokenizer=tokenizer) |
|
|
|
|
|
def analyze_text(text): |
|
result = classifier(text)[0] |
|
return result |
|
|
|
|
|
def analyze_csv(file): |
|
df = pd.read_csv(file) |
|
texts = df['text'].tolist() |
|
results = classifier(texts) |
|
|
|
sentiments = [result['label'] for result in results] |
|
df['sentiment'] = sentiments |
|
|
|
pos_count = sentiments.count('positive') |
|
neg_count = sentiments.count('negative') |
|
neu_count = sentiments.count('neutral') |
|
|
|
|
|
labels = 'Positive', 'Negative', 'Neutral' |
|
sizes = [pos_count, neg_count, neu_count] |
|
colors = ['#ff9999','#66b3ff','#99ff99'] |
|
fig, ax = plt.subplots() |
|
ax.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90) |
|
ax.axis('equal') |
|
|
|
return df, fig |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("## Sentiment Analysis") |
|
text_input = gr.Textbox(label="Enter Text") |
|
text_output = gr.JSON(label="Sentiment Analysis Result") |
|
text_button = gr.Button("Analyze Text") |
|
|
|
csv_input = gr.File(label="Upload CSV", file_types=['csv']) |
|
csv_output = gr.Dataframe(label="Sentiment Analysis Results") |
|
csv_chart = gr.Plot(label="Sentiment Distribution") |
|
csv_button = gr.Button("Analyze CSV") |
|
|
|
text_button.click(analyze_text, inputs=text_input, outputs=text_output) |
|
csv_button.click(analyze_csv, inputs=csv_input, outputs=[csv_output, csv_chart]) |
|
|
|
|
|
demo.launch() |
|
|