Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import torch
|
4 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
5 |
+
|
6 |
+
# Load model and tokenizer globally for efficiency
|
7 |
+
model_name = "tabularisai/multilingual-sentiment-analysis"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
10 |
+
|
11 |
+
|
12 |
+
def predict_sentiment(texts):
|
13 |
+
"""
|
14 |
+
Predict sentiment for a list of texts
|
15 |
+
"""
|
16 |
+
inputs = tokenizer(texts, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
17 |
+
with torch.no_grad():
|
18 |
+
outputs = model(**inputs)
|
19 |
+
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
20 |
+
sentiment_map = {
|
21 |
+
0: "Very Negative",
|
22 |
+
1: "Negative",
|
23 |
+
2: "Neutral",
|
24 |
+
3: "Positive",
|
25 |
+
4: "Very Positive"
|
26 |
+
}
|
27 |
+
return [sentiment_map[p] for p in torch.argmax(probabilities, dim=-1).tolist()]
|
28 |
+
|
29 |
+
|
30 |
+
def process_file(file_obj):
|
31 |
+
"""
|
32 |
+
Process the input file and add sentiment analysis results
|
33 |
+
"""
|
34 |
+
try:
|
35 |
+
# Read the file based on its extension
|
36 |
+
file_path = file_obj.name
|
37 |
+
if file_path.endswith('.csv'):
|
38 |
+
df = pd.read_csv(file_path)
|
39 |
+
elif file_path.endswith(('.xlsx', '.xls')):
|
40 |
+
df = pd.read_excel(file_path)
|
41 |
+
else:
|
42 |
+
raise ValueError("Unsupported file format. Please upload a CSV or Excel file.")
|
43 |
+
|
44 |
+
# Verify that 'Reviews' column exists
|
45 |
+
if 'Reviews' not in df.columns:
|
46 |
+
raise ValueError("Input file must contain a 'Reviews' column.")
|
47 |
+
|
48 |
+
# Perform sentiment analysis
|
49 |
+
reviews = df['Reviews'].fillna("") # Handle any missing values
|
50 |
+
sentiments = predict_sentiment(reviews.tolist())
|
51 |
+
|
52 |
+
# Add results to the dataframe
|
53 |
+
df['Sentiment'] = sentiments
|
54 |
+
|
55 |
+
# Save the results to a new Excel file
|
56 |
+
output_path = "output_with_sentiment.xlsx"
|
57 |
+
df.to_excel(output_path, index=False)
|
58 |
+
|
59 |
+
return df, output_path
|
60 |
+
|
61 |
+
except Exception as e:
|
62 |
+
raise gr.Error(str(e))
|
63 |
+
|
64 |
+
|
65 |
+
# Create Gradio interface
|
66 |
+
with gr.Blocks() as interface:
|
67 |
+
gr.Markdown("# Review Sentiment Analysis")
|
68 |
+
gr.Markdown("Upload an Excel or CSV file with a 'Reviews' column to analyze sentiment.")
|
69 |
+
|
70 |
+
with gr.Row():
|
71 |
+
file_input = gr.File(
|
72 |
+
label="Upload File (CSV or Excel)",
|
73 |
+
file_types=[".csv", ".xlsx", ".xls"]
|
74 |
+
)
|
75 |
+
|
76 |
+
with gr.Row():
|
77 |
+
analyze_btn = gr.Button("Analyze Sentiments")
|
78 |
+
|
79 |
+
with gr.Row():
|
80 |
+
output_df = gr.Dataframe(label="Results Preview")
|
81 |
+
output_file = gr.File(label="Download Results")
|
82 |
+
|
83 |
+
analyze_btn.click(
|
84 |
+
fn=process_file,
|
85 |
+
inputs=[file_input],
|
86 |
+
outputs=[output_df, output_file]
|
87 |
+
)
|
88 |
+
|
89 |
+
# Launch the interface
|
90 |
+
interface.launch()
|