Spaces:
Sleeping
Sleeping
Vela
commited on
Commit
·
726f5db
1
Parent(s):
c086c76
created project
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import data
|
3 |
+
import models
|
4 |
+
|
5 |
+
def main():
|
6 |
+
st.title("CSV Sentiment Analysis")
|
7 |
+
|
8 |
+
uploaded_file = st.file_uploader("Upload CSV or Excel file", type=["csv", "xlsx"])
|
9 |
+
classifier = models.load_model()
|
10 |
+
df = data.read_data(uploaded_file)
|
11 |
+
|
12 |
+
if uploaded_file:
|
13 |
+
column = list(df.columns)
|
14 |
+
column_with_empty = [""] + column
|
15 |
+
text_to_analyze = st.selectbox("Select text column", column_with_empty)
|
16 |
+
|
17 |
+
|
18 |
+
|
19 |
+
if text_to_analyze in df.columns:
|
20 |
+
text_column = text_to_analyze
|
21 |
+
|
22 |
+
if text_column:
|
23 |
+
|
24 |
+
df = models.analyze_sentiments(df, text_column, classifier)
|
25 |
+
|
26 |
+
data.visualize_data(df, st)
|
27 |
+
|
28 |
+
st.subheader("Processed Data Preview")
|
29 |
+
st.dataframe(df.head())
|
30 |
+
|
31 |
+
if __name__ == "__main__":
|
32 |
+
main()
|
data.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
|
4 |
+
def read_data(file):
|
5 |
+
try:
|
6 |
+
if file.name.endswith(".csv"):
|
7 |
+
data_frame = pd.read_csv(file)
|
8 |
+
elif file.name.endswith(".xlsx"):
|
9 |
+
data_frame = pd.read_excel(file)
|
10 |
+
return data_frame
|
11 |
+
except Exception as e:
|
12 |
+
return f"Unable to read the file : {file}. Error : {e}"
|
13 |
+
|
14 |
+
|
15 |
+
def visualize_data(df,st):
|
16 |
+
sentiment_counts = df['sentiment'].value_counts()
|
17 |
+
fig, ax = plt.subplots()
|
18 |
+
ax.pie(sentiment_counts, labels=sentiment_counts.index, autopct='%1.1f%%')
|
19 |
+
ax.axis('equal')
|
20 |
+
st.pyplot(fig)
|
models.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
|
3 |
+
def load_model():
|
4 |
+
return pipeline("text-classification", model="tabularisai/multilingual-sentiment-analysis")
|
5 |
+
|
6 |
+
def analyze_sentiments(df, text_column, classifier):
|
7 |
+
if text_column not in df.columns:
|
8 |
+
raise ValueError(f"Column '{text_column}' not found in DataFrame.")
|
9 |
+
sentiments = []
|
10 |
+
for text in df[text_column]:
|
11 |
+
try:
|
12 |
+
sentiment = classifier(str(text))[0]['label']
|
13 |
+
sentiments.append(sentiment)
|
14 |
+
except Exception as e:
|
15 |
+
print(f"Error processing text: {text}. Error: {e}")
|
16 |
+
sentiments.append('UNKNOWN')
|
17 |
+
df['sentiment'] = sentiments
|
18 |
+
return df
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch --index-url https://download.pytorch.org/whl/cpu
|
2 |
+
transformers
|
3 |
+
streamlit
|
4 |
+
pandas
|
5 |
+
matplotlib
|
6 |
+
openpyxl
|