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." )