tharu22 commited on
Commit
0e992ac
·
1 Parent(s): d56c01c
Files changed (2) hide show
  1. app.py +52 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import torch
4
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
+ import matplotlib.pyplot as plt
6
+
7
+ # Load model and tokenizer
8
+ model_name = "tabularisai/multilingual-sentiment-analysis"
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
11
+
12
+ def predict_sentiment(texts):
13
+ inputs = tokenizer(texts, return_tensors="pt", truncation=True, padding=True, max_length=512)
14
+ with torch.no_grad():
15
+ outputs = model(**inputs)
16
+ probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
17
+ sentiment_map = {0: "Very Negative", 1: "Negative", 2: "Neutral", 3: "Positive", 4: "Very Positive"}
18
+ return [sentiment_map[p] for p in torch.argmax(probabilities, dim=-1).tolist()]
19
+
20
+ # Streamlit UI
21
+ st.title("Sentiment Analysis App")
22
+ st.write("Upload an Excel file containing text data, and we'll analyze its sentiment.")
23
+
24
+ uploaded_file = st.file_uploader("Upload Excel File", type=["xlsx", "xls"])
25
+
26
+ if uploaded_file is not None:
27
+ df = pd.read_excel(uploaded_file)
28
+ st.write("Preview of Uploaded Data:")
29
+ st.dataframe(df.head())
30
+
31
+ text_column = st.selectbox("Select the column containing text", df.columns)
32
+
33
+ if st.button("Analyze Sentiment"):
34
+ df["Sentiment"] = predict_sentiment(df[text_column].astype(str).tolist())
35
+
36
+ # Display results
37
+ st.write("Sentiment Analysis Results:")
38
+ st.dataframe(df[[text_column, "Sentiment"]])
39
+
40
+ # Pie chart
41
+ sentiment_counts = df["Sentiment"].value_counts()
42
+ fig, ax = plt.subplots()
43
+ ax.pie(sentiment_counts, labels=sentiment_counts.index, autopct='%1.1f%%', colors=["red", "yellow", "pink", "lightgreen", "green"])
44
+ ax.set_title("Sentiment Distribution")
45
+ st.pyplot(fig)
46
+
47
+ # Table display for sentiment analysis results
48
+ st.write("Detailed Sentiment Table:")
49
+ st.table(df[[text_column, "Sentiment"]])
50
+
51
+ # Download option
52
+ st.download_button("Download Results", df.to_csv(index=False).encode('utf-8'), "sentiment_results.csv", "text/csv")
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ torch --index-url https://download.pytorch.org/whl/cpu
2
+ transformers
3
+ streamlit
4
+ pandas
5
+ matplotlib