Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
import spacy
|
| 3 |
import torch
|
| 4 |
import torch.nn as nn
|
|
|
|
| 5 |
from transformers import BertTokenizer, BertModel, AutoConfig
|
| 6 |
from transformers.models.bert.modeling_bert import BertForMaskedLM
|
| 7 |
|
|
@@ -213,11 +214,59 @@ def load_reviews_from_file(file_path):
|
|
| 213 |
st.error(f"File not found: {file_path}")
|
| 214 |
return reviews
|
| 215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
|
|
|
|
|
|
| 217 |
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
|
| 223 |
#Demo Section
|
|
|
|
| 2 |
import spacy
|
| 3 |
import torch
|
| 4 |
import torch.nn as nn
|
| 5 |
+
import pandas as pd
|
| 6 |
from transformers import BertTokenizer, BertModel, AutoConfig
|
| 7 |
from transformers.models.bert.modeling_bert import BertForMaskedLM
|
| 8 |
|
|
|
|
| 214 |
st.error(f"File not found: {file_path}")
|
| 215 |
return reviews
|
| 216 |
|
| 217 |
+
# Function to load reviews from a CSV file
|
| 218 |
+
def load_reviews_from_csv(file_path):
|
| 219 |
+
try:
|
| 220 |
+
df = pd.read_csv(file_path)
|
| 221 |
+
return df
|
| 222 |
+
except FileNotFoundError:
|
| 223 |
+
st.error(f"File not found: {file_path}")
|
| 224 |
+
return None
|
| 225 |
+
|
| 226 |
+
# Function to process each review in the CSV and get the model's predictions
|
| 227 |
+
def process_csv_reviews(df):
|
| 228 |
+
true_reviews = []
|
| 229 |
+
for _, row in df.iterrows():
|
| 230 |
+
review_text = row['review']
|
| 231 |
+
label = row['label']
|
| 232 |
+
|
| 233 |
+
# Get BERT embedding for the review text
|
| 234 |
+
bert_embedding = get_bert_embedding(review_text.lower())
|
| 235 |
+
|
| 236 |
+
# Get SpaBERT embedding for geo-entities
|
| 237 |
+
spaBert_embedding, _ = processSpatialEntities(review_text, nlp)
|
| 238 |
+
|
| 239 |
+
# Concatenate BERT and SpaBERT embeddings
|
| 240 |
+
combined_embedding = torch.cat((bert_embedding, spaBert_embedding), dim=-1)
|
| 241 |
+
|
| 242 |
+
# Get model prediction
|
| 243 |
+
prediction = get_prediction(combined_embedding)
|
| 244 |
+
|
| 245 |
+
# If prediction is "Not Spam" (0), store the review
|
| 246 |
+
if prediction == 0:
|
| 247 |
+
true_reviews.append((review_text, label))
|
| 248 |
+
|
| 249 |
+
# Convert to a DataFrame for easy display
|
| 250 |
+
return pd.DataFrame(true_reviews, columns=['Review', 'Label'])
|
| 251 |
|
| 252 |
+
st.write("### Process Filtered Reviews CSV")
|
| 253 |
+
csv_file_path = "models/spabert/datasets/filtered_reviews.csv"
|
| 254 |
|
| 255 |
+
if st.button("Process CSV and Find True Reviews"):
|
| 256 |
+
# Load the CSV file
|
| 257 |
+
df = load_reviews_from_csv(csv_file_path)
|
| 258 |
+
|
| 259 |
+
if df is not None:
|
| 260 |
+
# Filter reviews predicted to be "Not Spam"
|
| 261 |
+
true_reviews_df = process_csv_reviews(df)
|
| 262 |
+
|
| 263 |
+
if not true_reviews_df.empty:
|
| 264 |
+
st.write("### Reviews Predicted to be Not Spam:")
|
| 265 |
+
st.dataframe(true_reviews_df)
|
| 266 |
+
else:
|
| 267 |
+
st.write("No reviews were predicted to be Not Spam.")
|
| 268 |
+
else:
|
| 269 |
+
st.error("Could not load CSV file.")
|
| 270 |
|
| 271 |
|
| 272 |
#Demo Section
|