Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
4 |
+
from transformers import pipeline
|
5 |
+
from bertopic import BERTopic
|
6 |
+
|
7 |
+
# Emotion classification pipeline (can use AraBERT or any emotion classifier)
|
8 |
+
emotion_classifier = pipeline("text-classification", model="arpanghoshal/bert-base-uncased-emotion")
|
9 |
+
|
10 |
+
# Function to process CSV file and return emotion and topic model
|
11 |
+
def process_file(uploaded_file):
|
12 |
+
# Load CSV
|
13 |
+
df = pd.read_csv(uploaded_file)
|
14 |
+
|
15 |
+
# Display basic info about the CSV
|
16 |
+
st.write("CSV Loaded Successfully!")
|
17 |
+
st.write(f"Data Preview: {df.head()}")
|
18 |
+
|
19 |
+
# Preprocess the text: assuming the CSV has a 'text' column
|
20 |
+
texts = df['text'].dropna().tolist() # Modify this according to your column name
|
21 |
+
|
22 |
+
# Emotion Classification: Classify emotions for each text
|
23 |
+
emotions = [emotion_classifier(text)[0]['label'] for text in texts]
|
24 |
+
df['emotion'] = emotions
|
25 |
+
|
26 |
+
# Topic Modeling using BERTopic (install bertopic first if not installed)
|
27 |
+
topic_model = BERTopic()
|
28 |
+
topics, _ = topic_model.fit_transform(texts)
|
29 |
+
df['topic'] = topics
|
30 |
+
|
31 |
+
# Display the results
|
32 |
+
st.write("Emotions classified for each entry:")
|
33 |
+
st.write(df[['text', 'emotion', 'topic']])
|
34 |
+
|
35 |
+
return df
|
36 |
+
|
37 |
+
# Streamlit App
|
38 |
+
st.title("Topic Modeling & Emotion Classification")
|
39 |
+
st.write("Upload a CSV file to perform topic modeling and emotion classification on the text.")
|
40 |
+
|
41 |
+
# File upload widget
|
42 |
+
uploaded_file = st.file_uploader("Choose a CSV file", type=["csv"])
|
43 |
+
|
44 |
+
if uploaded_file is not None:
|
45 |
+
# Process the file
|
46 |
+
result_df = process_file(uploaded_file)
|