Spaces:
Sleeping
Sleeping
File size: 7,625 Bytes
405a3f0 | 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 | import os
import logging
import torch
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from transformers import DistilBertTokenizer
from huggingface_hub import InferenceClient
from src.config import MODEL_PATH, DEVICE, HF_TOKEN
from src.model import ExactDistilBertResNetModel
from src.preprocessing import extract_text_features
logger = logging.getLogger(__name__)
# Thread-safe global cache storage (Singletons)
_MODEL = None
_TOKENIZER = None
_SENTIMENT_ANALYZER = None
_HF_CLIENT = None
def get_or_load_resources():
"""
Consolidates resource downloads, model parameter loads, and tokenizers in memory.
Implements a caching pattern to ensure weights are loaded only once.
"""
global _MODEL, _TOKENIZER, _SENTIMENT_ANALYZER, _HF_CLIENT
# 1. Load VADER Sentiment Analyzer
if _SENTIMENT_ANALYZER is None:
try:
logger.info("Initializing VADER Sentiment Analyzer...")
# Configure a local writeable path for NLTK assets
local_nltk_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "nltk_data")
os.makedirs(local_nltk_dir, exist_ok=True)
nltk.data.path.append(local_nltk_dir)
nltk.download('vader_lexicon', download_dir=local_nltk_dir, quiet=True)
_SENTIMENT_ANALYZER = SentimentIntensityAnalyzer()
except Exception as e:
logger.error(f"Failed to load VADER sentiment analyzer: {e}")
_SENTIMENT_ANALYZER = None
# 2. Load Tokenizer
if _TOKENIZER is None:
try:
logger.info("Loading DistilBERT Tokenizer...")
_TOKENIZER = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
except Exception as e:
logger.error(f"Failed to load DistilBERT tokenizer: {e}")
raise
# 3. Load Model weights
if _MODEL is None:
if not os.path.exists(MODEL_PATH):
raise FileNotFoundError(f"Model weight checkpoint not found at: {MODEL_PATH}")
try:
logger.info(f"Loading DistilBERT-ResNet weights onto device '{DEVICE}'...")
model = ExactDistilBertResNetModel(num_classes=2)
# Load state dict
state_dict = torch.load(MODEL_PATH, map_location=DEVICE)
model.load_state_dict(state_dict, strict=True)
model.to(DEVICE)
model.eval()
_MODEL = model
logger.info("PyTorch Model loaded successfully.")
except Exception as e:
logger.error(f"Failed to load PyTorch model weights: {e}")
raise
# 4. Initialize Hugging Face Inference Client
if _HF_CLIENT is None:
if HF_TOKEN:
try:
logger.info("Initializing Hugging Face Inference Client (Nebius Provider)...")
_HF_CLIENT = InferenceClient(
provider="nebius",
api_key=HF_TOKEN,
)
except Exception as e:
logger.warning(f"Failed to initialize HF client with token: {e}")
_HF_CLIENT = None
else:
logger.warning("HF_TOKEN environment variable not set. LLM Explanations will run in fallback mode.")
_HF_CLIENT = None
return _MODEL, _TOKENIZER, _SENTIMENT_ANALYZER, _HF_CLIENT
def predict_review(review_text):
"""
Executes the multimodal model prediction workflow for a single review.
Returns:
dict: containing classification label ('Verified' or 'Suspicious'),
confidence probability value, VADER sentiment score, and structural analysis.
"""
model, tokenizer, sentiment_analyzer, _ = get_or_load_resources()
if not review_text.strip():
raise ValueError("Input review text cannot be empty.")
# Extract metadata features vector
metadata = extract_text_features(review_text, sentiment_analyzer)
metadata = metadata.to(DEVICE)
# Tokenize review text
inputs = tokenizer(
review_text,
return_tensors="pt",
truncation=True,
padding=True,
max_length=512
)
inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
# Run evaluation
with torch.no_grad():
logits = model(
input_ids=inputs['input_ids'],
attention_mask=inputs['attention_mask'],
metadata=metadata
)
probabilities = torch.nn.functional.softmax(logits, dim=1)
predicted_class = torch.argmax(probabilities, dim=1).item()
confidence = probabilities[0, predicted_class].item()
# Map predictions to rebranded terminology
classification = "Suspicious" if predicted_class == 1 else "Verified"
# Extract structural metrics for presentation in the UI
words = review_text.split()
avg_word_len = sum(len(w) for w in words) / len(words) if words else 0.0
sentiment_val = metadata[0, 1].item() # VADER compound sentiment
return {
"classification": classification,
"confidence": confidence,
"metrics": {
"length": len(review_text),
"words_count": len(words),
"avg_word_length": round(avg_word_len, 2),
"sentiment_score": round(sentiment_val, 3),
"capitalization_ratio": round(metadata[0, 4].item(), 3),
"punctuation_count": int(metadata[0, 2].item())
}
}
def generate_explanation(review_text, classification):
"""
Fetches explanation for the classification from Hugging Face Mistral Nemo.
Falls back to a standard structured template if the token is missing or request fails.
"""
_, _, _, client = get_or_load_resources()
# Map label for prompt clarity
friendly_label = "Suspicious (Potential Fraud/Fake)" if classification == "Suspicious" else "Verified (Likely Genuine/True)"
if client is not None:
try:
prompt = (
f"Go through the following review and classify it as true or fake review. "
f"This review was classified as {friendly_label}. Explain why it might be classified this way: "
f"'{review_text}' Give your answer within only 100 words and avoid headings. "
f"Just give one paragraph of explanation. Do not consider the emojis."
)
response = client.chat.completions.create(
model="mistralai/Mistral-Nemo-Instruct-2407",
messages=[{"role": "user", "content": prompt}],
max_tokens=200,
stream=False
)
explanation = response.choices[0].message.content.strip()
if explanation:
return explanation
except Exception as e:
logger.warning(f"Failed to retrieve explanation from LLM client: {e}. Running fallback explanation.")
# Fallback explanation
if classification == "Suspicious":
return (
"This review was flagged as SUSPICIOUS. Our multi-vector audit identified linguistic cues "
"often associated with review manipulation: high-intensity emotional modifiers, anomalous "
"capitalization ratios, or structured punctuation offsets that depart from natural review patterns."
)
else:
return (
"This review was categorized as VERIFIED. The review exhibits natural, moderate phrasing, balanced "
"sentiment intensity, and a typical structural length consistent with organic customer feedback."
)
|