Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
import pandas as pd
|
3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
4 |
import matplotlib.pyplot as plt
|
|
|
5 |
|
6 |
# Load model and tokenizer
|
7 |
model_name = "ac0hik/Sentiment_Analysis_French"
|
@@ -11,48 +12,63 @@ model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
|
11 |
# Create sentiment analysis pipeline
|
12 |
classifier = pipeline('sentiment-analysis', model=model, tokenizer=tokenizer)
|
13 |
|
|
|
|
|
14 |
# Function to process a single text input
|
|
|
15 |
def analyze_text(text):
|
16 |
result = classifier(text)[0]
|
17 |
return result
|
18 |
|
19 |
-
# Function to process a CSV file
|
|
|
20 |
def analyze_csv(file):
|
21 |
-
df = pd.read_csv(file)
|
22 |
texts = df['text'].tolist()
|
23 |
-
results = classifier(texts)
|
24 |
-
|
25 |
-
sentiments = [result['label'] for result in results]
|
26 |
-
df['sentiment'] = sentiments
|
27 |
|
28 |
-
|
29 |
-
neg_count =
|
30 |
-
neu_count = sentiments.count('neutral')
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
# Gradio interface
|
43 |
with gr.Blocks() as demo:
|
44 |
-
gr.
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
53 |
|
54 |
text_button.click(analyze_text, inputs=text_input, outputs=text_output)
|
55 |
csv_button.click(analyze_csv, inputs=csv_input, outputs=[csv_output, csv_chart])
|
56 |
|
57 |
# Launch the Gradio app
|
58 |
-
demo.launch()
|
|
|
2 |
import pandas as pd
|
3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
4 |
import matplotlib.pyplot as plt
|
5 |
+
import spaces
|
6 |
|
7 |
# Load model and tokenizer
|
8 |
model_name = "ac0hik/Sentiment_Analysis_French"
|
|
|
12 |
# Create sentiment analysis pipeline
|
13 |
classifier = pipeline('sentiment-analysis', model=model, tokenizer=tokenizer)
|
14 |
|
15 |
+
|
16 |
+
|
17 |
# Function to process a single text input
|
18 |
+
@spaces.GPU
|
19 |
def analyze_text(text):
|
20 |
result = classifier(text)[0]
|
21 |
return result
|
22 |
|
23 |
+
# Function to process a CSV file and update results live
|
24 |
+
@spaces.GPU
|
25 |
def analyze_csv(file):
|
26 |
+
df = pd.read_csv(file.name,nrows=2)
|
27 |
texts = df['text'].tolist()
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
results = []
|
30 |
+
pos_count = neg_count = neu_count = 0
|
|
|
31 |
|
32 |
+
for text in texts:
|
33 |
+
result = classifier(text)[0]
|
34 |
+
results.append({'text': text, 'sentiment': result['label']})
|
35 |
+
|
36 |
+
if result['label'] == 'positive':
|
37 |
+
pos_count += 1
|
38 |
+
elif result['label'] == 'negative':
|
39 |
+
neg_count += 1
|
40 |
+
else:
|
41 |
+
neu_count += 1
|
42 |
+
|
43 |
+
# Create a pie chart
|
44 |
+
labels = 'Positive', 'Negative', 'Neutral'
|
45 |
+
sizes = [pos_count, neg_count, neu_count]
|
46 |
+
colors = ['#ff9999','#66b3ff','#99ff99']
|
47 |
+
fig, ax = plt.subplots()
|
48 |
+
wedges, texts, autotexts = ax.pie(sizes, labels=labels, colors=colors, autopct=lambda pct: "{:.1f}%\n({:d})".format(pct, int(pct/100.*sum(sizes))), startangle=90)
|
49 |
+
ax.axis('equal')
|
50 |
+
|
51 |
+
# Update results live
|
52 |
+
yield pd.DataFrame(results), fig
|
53 |
|
54 |
# Gradio interface
|
55 |
with gr.Blocks() as demo:
|
56 |
+
with gr.Row():
|
57 |
+
with gr.Column():
|
58 |
+
gr.Markdown("## Sentiment Analysis")
|
59 |
+
text_input = gr.Textbox(label="Enter Text")
|
60 |
+
text_output = gr.JSON(label="Sentiment Analysis Result")
|
61 |
+
text_button = gr.Button("Analyze Text")
|
62 |
+
|
63 |
+
csv_input = gr.File(label="Upload CSV", file_types=['csv'])
|
64 |
+
csv_output = gr.Dataframe(label="Sentiment Analysis Results")
|
65 |
+
csv_button = gr.Button("Analyze CSV")
|
66 |
+
|
67 |
+
with gr.Column():
|
68 |
+
csv_chart = gr.Plot(label="Sentiment Distribution")
|
69 |
|
70 |
text_button.click(analyze_text, inputs=text_input, outputs=text_output)
|
71 |
csv_button.click(analyze_csv, inputs=csv_input, outputs=[csv_output, csv_chart])
|
72 |
|
73 |
# Launch the Gradio app
|
74 |
+
demo.launch()
|