File size: 3,059 Bytes
a736df0 be49472 ea299c1 be49472 ea299c1 be49472 248d588 be49472 ea299c1 3f9e70e dd0ce30 91d04a1 dd0ce30 be49472 dd0ce30 ea299c1 dd0ce30 ea299c1 dd0ce30 ea299c1 dd0ce30 ea299c1 dd0ce30 ea299c1 dd0ce30 ea299c1 dd0ce30 be49472 ea299c1 dd0ce30 ea299c1 dd0ce30 ea299c1 dd0ce30 ea299c1 dd0ce30 be49472 dd0ce30 be49472 ea299c1 |
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 80 81 82 83 84 85 86 87 88 89 90 91 |
import gradio as gr
import pandas as pd
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
import matplotlib.pyplot as plt
import spaces
# Load model and tokenizer
model_name = "ac0hik/Sentiment_Analysis_French"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
# Create sentiment analysis pipeline
classifier = pipeline('sentiment-analysis', model=model, tokenizer=tokenizer)
# Function to process a single text input
@spaces.GPU
def analyze_text(text):
result = classifier(text)[0]
return result
# Function to process a CSV file and update results live
@spaces.GPU
# Function to process a single text input
def analyze_text(text):
result = classifier(text)[0]
return result
# Function to process a CSV file and update results live
def analyze_csv(file):
df = pd.read_csv(file.name)
texts = df['text'].tolist()
results = []
pos_count = neg_count = neu_count = 0
for text in texts:
result = classifier(text)[0]
results.append({'text': text, 'sentiment': result['label']})
if result['label'] == 'positive':
pos_count += 1
elif result['label'] == 'negative':
neg_count += 1
else:
neu_count += 1
# Create a pie chart
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')
# Update results live
yield pd.DataFrame(results), fig, pos_count, neg_count, neu_count
# Function to save the DataFrame to a CSV file
def save_csv(df):
file_path = "/mnt/data/sentiment_analysis_results.csv"
df.to_csv(file_path, index=False)
return file_path
# Gradio interface
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
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_button = gr.Button("Analyze CSV")
with gr.Column():
gr.Markdown("## csv Result")
pos_count_output = gr.Number(label="Positive Count", value=0)
neg_count_output = gr.Number(label="Negative Count", value=0)
neu_count_output = gr.Number(label="Neutral Count", value=0)
csv_chart = gr.Plot(label="Sentiment Distribution")
text_button.click(analyze_text, inputs=text_input, outputs=text_output)
csv_button.click(analyze_csv, inputs=csv_input, outputs=[csv_output, csv_chart, pos_count_output, neg_count_output, neu_count_output])
# Launch the Gradio app
demo.launch() |