Spaces:
Running
Running
File size: 8,620 Bytes
d5ba1b1 1bd5399 d5ba1b1 7837ce3 d5ba1b1 7837ce3 d5ba1b1 |
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 232 |
import re
import pandas as pd
import spacy
from langdetect import detect_langs
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.decomposition import LatentDirichletAllocation
from sklearn.feature_extraction.text import ENGLISH_STOP_WORDS
from spacy.lang.fr.stop_words import STOP_WORDS as FRENCH_STOP_WORDS
from sklearn.cluster import KMeans
from sklearn.manifold import TSNE
import numpy as np
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoConfig
import streamlit as st
from datetime import datetime
# Lighter model
MODEL ="cardiffnlp/twitter-xlm-roberta-base-sentiment"
# Cache model loading with fallback for quantization
@st.cache_resource
def load_model():
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {device}")
tokenizer = AutoTokenizer.from_pretrained(MODEL, use_fast=True)
model = AutoModelForSequenceClassification.from_pretrained(MODEL).to(device)
# Attempt quantization with fallback
try:
# Set quantization engine explicitly (fbgemm for x86, qnnpack for ARM)
torch.backends.quantized.engine = 'fbgemm' if torch.cuda.is_available() else 'qnnpack'
model = torch.quantization.quantize_dynamic(model, {torch.nn.Linear}, dtype=torch.qint8)
print("Model quantized successfully.")
except RuntimeError as e:
print(f"Quantization failed: {e}. Using non-quantized model.")
config = AutoConfig.from_pretrained(MODEL)
return tokenizer, model, config, device
tokenizer, model, config, device = load_model()
nlp_fr = spacy.load("fr_core_news_sm")
nlp_en = spacy.load("en_core_web_sm")
custom_stop_words = list(ENGLISH_STOP_WORDS.union(FRENCH_STOP_WORDS))
def preprocess(text):
if text is None:
return ""
if not isinstance(text, str):
try:
text = str(text)
except:
return ""
new_text = []
for t in text.split(" "):
t = '@user' if t.startswith('@') and len(t) > 1 else t
t = 'http' if t.startswith('http') else t
new_text.append(t)
return " ".join(new_text)
def clean_message(text):
if not isinstance(text, str):
return ""
text = text.lower()
text = text.replace("<media omitted>", "").replace("this message was deleted", "").replace("null", "")
text = re.sub(r"http\S+|www\S+|https\S+", "", text, flags=re.MULTILINE)
text = re.sub(r"[^a-zA-ZÀ-ÿ0-9\s]", "", text)
return text.strip()
def lemmatize_text(text, lang):
if lang == 'fr':
doc = nlp_fr(text)
else:
doc = nlp_en(text)
return " ".join([token.lemma_ for token in doc if not token.is_punct])
def preprocess(data):
pattern = r"^(?P<Date>\d{1,2}/\d{1,2}/\d{2,4}),\s+(?P<Time>[\d:]+(?:\S*\s?[AP]M)?)\s+-\s+(?:(?P<Sender>.*?):\s+)?(?P<Message>.*)$"
filtered_messages, valid_dates = [], []
for line in data.strip().split("\n"):
match = re.match(pattern, line)
if match:
entry = match.groupdict()
sender = entry.get("Sender")
if sender and sender.strip().lower() != "system":
filtered_messages.append(f"{sender.strip()}: {entry['Message']}")
valid_dates.append(f"{entry['Date']}, {entry['Time'].replace(' ', ' ')}")
print("-_____--------------__________----------_____________----------______________")
def convert_to_target_format(date_str):
try:
# Attempt to parse the original date string
dt = datetime.strptime(date_str, '%d/%m/%Y, %H:%M')
except ValueError:
# Return the original date string if parsing fails
return date_str
# Extract components without leading zeros
month = dt.month
day = dt.day
year_short = dt.strftime('%y') # Last two digits of the year
# Convert to 12-hour format and determine AM/PM
hour_12 = dt.hour % 12
if hour_12 == 0:
hour_12 = 12 # Adjust 0 (from 12 AM/PM) to 12
hour_str = str(hour_12)
# Format minute with leading zero if necessary
minute_str = f"{dt.minute:02d}"
# Get AM/PM designation
am_pm = dt.strftime('%p')
# Construct the formatted date string with Unicode narrow space
return f"{month}/{day}/{year_short}, {hour_str}:{minute_str}\u202f{am_pm}"
converted_dates = [convert_to_target_format(date) for date in valid_dates]
df = pd.DataFrame({'user_message': filtered_messages, 'message_date': converted_dates})
df['message_date'] = pd.to_datetime(df['message_date'], format='%m/%d/%y, %I:%M %p', errors='coerce')
df.rename(columns={'message_date': 'date'}, inplace=True)
users, messages = [], []
msg_pattern = r"^(.*?):\s(.*)$"
for message in df["user_message"]:
match = re.match(msg_pattern, message)
if match:
users.append(match.group(1))
messages.append(match.group(2))
else:
users.append("group_notification")
messages.append(message)
df["user"] = users
df["message"] = messages
df = df[df["user"] != "group_notification"].reset_index(drop=True)
df["unfiltered_messages"] = df["message"]
df["message"] = df["message"].apply(clean_message)
# Extract time-based features
df['year'] = pd.to_numeric(df['date'].dt.year, downcast='integer')
df['month'] = df['date'].dt.month_name()
df['day'] = pd.to_numeric(df['date'].dt.day, downcast='integer')
df['hour'] = pd.to_numeric(df['date'].dt.hour, downcast='integer')
df['day_of_week'] = df['date'].dt.day_name()
# Lemmatize messages for topic modeling
lemmatized_messages = []
for message in df["message"]:
try:
lang = detect_langs(message)
lemmatized_messages.append(lemmatize_text(message, lang))
except:
lemmatized_messages.append("")
df["lemmatized_message"] = lemmatized_messages
df = df[df["message"].notnull() & (df["message"] != "")].copy()
df.drop(columns=["user_message"], inplace=True)
# Perform topic modeling
vectorizer = CountVectorizer(max_df=0.95, min_df=2, stop_words=custom_stop_words)
dtm = vectorizer.fit_transform(df['lemmatized_message'])
# Apply LDA
lda = LatentDirichletAllocation(n_components=5, random_state=42)
lda.fit(dtm)
# Assign topics to messages
topic_results = lda.transform(dtm)
df = df.iloc[:topic_results.shape[0]].copy()
df['topic'] = topic_results.argmax(axis=1)
# Store topics for visualization
topics = []
for topic in lda.components_:
topics.append([vectorizer.get_feature_names_out()[i] for i in topic.argsort()[-10:]])
print("Top words for each topic-----------------------------------------------------:")
print(topics)
return df, topics
def preprocess_for_clustering(df, n_clusters=5):
df = df[df["lemmatized_message"].notnull() & (df["lemmatized_message"].str.strip() != "")]
df = df.reset_index(drop=True)
vectorizer = TfidfVectorizer(max_features=5000, stop_words='english')
tfidf_matrix = vectorizer.fit_transform(df['lemmatized_message'])
if tfidf_matrix.shape[0] < 2:
raise ValueError("Not enough messages for clustering.")
df = df.iloc[:tfidf_matrix.shape[0]].copy()
kmeans = KMeans(n_clusters=n_clusters, random_state=42)
clusters = kmeans.fit_predict(tfidf_matrix)
df['cluster'] = clusters
tsne = TSNE(n_components=2, random_state=42)
reduced_features = tsne.fit_transform(tfidf_matrix.toarray())
return df, reduced_features, kmeans.cluster_centers_
def predict_sentiment_batch(texts: list, batch_size: int = 32) -> list:
"""Predict sentiment for a batch of texts"""
if not isinstance(texts, list):
raise TypeError(f"Expected list of texts, got {type(texts)}")
processed_texts = [preprocess(text) for text in texts]
predictions = []
for i in range(0, len(processed_texts), batch_size):
batch = processed_texts[i:i+batch_size]
inputs = tokenizer(
batch,
padding=True,
truncation=True,
return_tensors="pt",
max_length=128
).to(device)
with torch.no_grad():
outputs = model(**inputs)
batch_preds = outputs.logits.argmax(dim=1).cpu().numpy()
predictions.extend([config.id2label[p] for p in batch_preds])
return predictions |