File size: 8,575 Bytes
4b4bf72
 
3f0f6de
4b4bf72
b3d1640
6bd6b44
3f0f6de
4b4bf72
3f0f6de
 
 
4b4bf72
3f0f6de
4ec5d16
3f0f6de
b3d1640
4ec5d16
 
 
 
 
 
 
79bbe0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ec5d16
 
 
 
 
 
6bd6b44
4ec5d16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f0f6de
79bbe0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ec5d16
 
 
 
 
 
 
6bd6b44
3f0f6de
 
 
 
 
 
4ec5d16
 
 
6bd6b44
4ec5d16
 
 
 
 
 
 
 
 
 
 
6bd6b44
4ec5d16
 
 
6bd6b44
 
4ec5d16
 
 
6bd6b44
 
 
 
 
 
 
 
 
 
3f0f6de
 
 
 
5fce9bd
3f0f6de
 
5fce9bd
4b4bf72
 
5fce9bd
4ec5d16
 
3f0f6de
 
 
 
 
 
 
 
 
4ec5d16
 
 
 
 
 
 
 
 
 
 
 
3f0f6de
4ec5d16
 
 
 
 
 
 
 
 
5fce9bd
4ec5d16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import streamlit as st
import pandas as pd
from transformers import AutoTokenizer, AutoModel, AutoModelForSequenceClassification, pipeline
from bertopic import BERTopic
import torch
import numpy as np
from collections import Counter

# Load AraBERT tokenizer and model for embeddings
bert_tokenizer = AutoTokenizer.from_pretrained("aubmindlab/bert-base-arabertv2")
bert_model = AutoModel.from_pretrained("aubmindlab/bert-base-arabertv2")

# Load AraBERT model for emotion classification
emotion_model = AutoModelForSequenceClassification.from_pretrained("CAMeL-Lab/bert-base-arabic-camelbert-msa-sentiment")
emotion_classifier = pipeline("text-classification", model=emotion_model, tokenizer=bert_tokenizer)

# Define emotion labels mapping
EMOTION_LABELS = {
    'LABEL_0': 'Negative',
    'LABEL_1': 'Positive',
    'LABEL_2': 'Neutral'
}

def chunk_text(text, max_length=512):
    """Split text into chunks of maximum token length."""
    tokens = bert_tokenizer.encode(text, add_special_tokens=False)
    chunks = []
    
    for i in range(0, len(tokens), max_length - 2):  # -2 to account for [CLS] and [SEP] tokens
        chunk = tokens[i:i + max_length - 2]
        # Add special tokens
        chunk = [bert_tokenizer.cls_token_id] + chunk + [bert_tokenizer.sep_token_id]
        chunks.append(chunk)
    
    return chunks

def get_embedding_for_text(text):
    """Get embedding for a single text."""
    chunks = chunk_text(text)
    chunk_embeddings = []
    
    for chunk in chunks:
        # Convert to tensor and add batch dimension
        input_ids = torch.tensor([chunk]).to(bert_model.device)
        attention_mask = torch.ones_like(input_ids)
        
        with torch.no_grad():
            outputs = bert_model(input_ids, attention_mask=attention_mask)
        
        # Get [CLS] token embedding for this chunk
        chunk_embedding = outputs.last_hidden_state[:, 0, :].cpu().numpy()
        chunk_embeddings.append(chunk_embedding[0])
    
    # Average embeddings from all chunks
    if chunk_embeddings:
        return np.mean(chunk_embeddings, axis=0)
    return np.zeros(bert_model.config.hidden_size)  # fallback

def generate_embeddings(texts):
    """Generate embeddings for a list of texts."""
    embeddings = []
    
    for text in texts:
        try:
            embedding = get_embedding_for_text(text)
            embeddings.append(embedding)
        except Exception as e:
            st.warning(f"Error processing text: {str(e)}")
            # Add zero embedding as fallback
            embeddings.append(np.zeros(bert_model.config.hidden_size))
    
    return np.array(embeddings)

def classify_emotion(text):
    """Classify emotion for a single text."""
    try:
        chunks = chunk_text(text)
        if not chunks:
            return "unknown"
        
        # Use first chunk for classification
        chunk_text = bert_tokenizer.decode(chunks[0])
        result = emotion_classifier(chunk_text)[0]
        return result['label']
    except Exception as e:
        st.warning(f"Error in emotion classification: {str(e)}")
        return "unknown"

def format_topics(topic_model, topic_counts):
    """Convert topic numbers to readable labels."""
    formatted_topics = []
    for topic_num, count in topic_counts:
        if topic_num == -1:
            topic_label = "Miscellaneous"
        else:
            # Get the top words for this topic
            words = topic_model.get_topic(topic_num)
            # Take the top 3 words to form a topic label
            topic_label = " | ".join([word for word, _ in words[:3]])
        
        formatted_topics.append({
            'topic': topic_label,
            'count': count
        })
    return formatted_topics

def format_emotions(emotion_counts):
    """Convert emotion labels to readable text."""
    formatted_emotions = []
    for label, count in emotion_counts:
        emotion = EMOTION_LABELS.get(label, label)
        formatted_emotions.append({
            'emotion': emotion,
            'count': count
        })
    return formatted_emotions

def process_and_summarize(uploaded_file, top_n=50):
    # Determine the file type
    if uploaded_file.name.endswith(".csv"):
        df = pd.read_csv(uploaded_file)
    elif uploaded_file.name.endswith(".xlsx"):
        df = pd.read_excel(uploaded_file)
    else:
        st.error("Unsupported file format.")
        return None, None

    # Validate required columns
    required_columns = ['country', 'poem']
    missing_columns = [col for col in required_columns if col not in df.columns]
    if missing_columns:
        st.error(f"Missing columns: {', '.join(missing_columns)}")
        return None, None

    # Parse and preprocess the file
    df['country'] = df['country'].str.strip()
    df = df.dropna(subset=['country', 'poem'])
    
    # Initialize BERTopic with specific parameters
    topic_model = BERTopic(
        language="arabic",
        calculate_probabilities=True,
        verbose=True
    )
    
    # Group by country
    summaries = []
    for country, group in df.groupby('country'):
        st.info(f"Processing poems for {country}...")

        texts = group['poem'].dropna().tolist()
        batch_size = 10
        all_emotions = []
        all_embeddings = []
        
        for i in range(0, len(texts), batch_size):
            batch_texts = texts[i:i + batch_size]
            
            st.info(f"Generating embeddings for batch {i//batch_size + 1}...")
            batch_embeddings = generate_embeddings(batch_texts)
            all_embeddings.extend(batch_embeddings)
            
            st.info(f"Classifying emotions for batch {i//batch_size + 1}...")
            batch_emotions = [classify_emotion(text) for text in batch_texts]
            all_emotions.extend(batch_emotions)

        try:
            embeddings = np.array(all_embeddings)
            
            st.info(f"Fitting topic model for {country}...")
            topics, _ = topic_model.fit_transform(texts, embeddings)
            
            # Format topics and emotions with readable labels
            top_topics = format_topics(topic_model, Counter(topics).most_common(top_n))
            top_emotions = format_emotions(Counter(all_emotions).most_common(top_n))

            summaries.append({
                'country': country,
                'total_poems': len(texts),
                'top_topics': top_topics,
                'top_emotions': top_emotions
            })
        except Exception as e:
            st.warning(f"Could not generate topics for {country}: {str(e)}")
            continue

    return summaries, topic_model

# Streamlit App Interface
st.title("Arabic Poem Topic Modeling & Emotion Classification")
st.write("Upload a CSV or Excel file containing Arabic poems with columns `country` and `poem`.")

uploaded_file = st.file_uploader("Choose a file", type=["csv", "xlsx"])

if uploaded_file is not None:
    try:
        top_n = st.number_input("Select the number of top topics/emotions to display:", 
                               min_value=1, max_value=100, value=10)

        summaries, topic_model = process_and_summarize(uploaded_file, top_n=top_n)
        if summaries is not None:
            st.success("Data successfully processed!")

            # Display summary for each country
            for summary in summaries:
                st.write(f"### {summary['country']}")
                st.write(f"Total Poems: {summary['total_poems']}")
                
                st.write(f"\nTop {top_n} Topics:")
                for topic in summary['top_topics']:
                    st.write(f"• {topic['topic']}: {topic['count']} poems")
                
                st.write(f"\nTop {top_n} Emotions:")
                for emotion in summary['top_emotions']:
                    st.write(f"• {emotion['emotion']}: {emotion['count']} poems")
                
                st.write("---")

            # Display overall topics in a more readable format
            st.write("### Global Topic Information:")
            topic_info = topic_model.get_topic_info()
            for _, row in topic_info.iterrows():
                if row['Topic'] == -1:
                    topic_name = "Miscellaneous"
                else:
                    words = topic_model.get_topic(row['Topic'])
                    topic_name = " | ".join([word for word, _ in words[:3]])
                st.write(f"• Topic {row['Topic']}: {topic_name} ({row['Count']} poems)")
                
    except Exception as e:
        st.error(f"Error: {str(e)}")