Commit ·
bbd259b
1
Parent(s): db15eda
update
Browse filesFiles changed (27)
- data/anchors/anti_government.txtAdded+20
- data/anchors/anti_india.txtAdded+20
- data/anchors/neutral.txtAdded+5
- data/anchors/pro_government.txtAdded+20
- data/anchors/pro_india.txtAdded+20
- main.py+1-1
- models/final_classifier.pklAdded+3
- processor.py+75-34
- reddit_scrapper.py+11-40
- regenerate_data.pyAdded+15
- requirements.txt+8-110
- sentiment_analysis.pyAdded+155
- src/__init__.pyAdded
- src/anchor_similarity.pyAdded+51
- src/config.pyAdded
- src/context_llm.pyAdded+79
- src/embeddings.pyAdded+8
- src/feature_builder.pyAdded+31
- src/language_detection.pyAdded+29
- src/predict.pyAdded+22
- src/preprocessing.pyAdded+17
- src/sarcasm.pyAdded+41
- src/sentiment.pyAdded+37
- src/train_classifier.pyAdded+17
- src/train_logic_aligned.pyAdded+106
- src/translation.pyAdded+17
- train_once.pyAdded+10
1234567891011121314151617181920This government has completely failed the peopleThe current administration is incompetentThe ruling party has destroyed democratic institutionsGovernment policies are harming ordinary citizensLeadership has no vision or accountabilityThis regime survives only on propagandaGovernment mismanagement has worsened the economyThe administration suppresses dissentCurrent leadership prioritizes power over peopleGovernment decisions lack transparencyThis government is authoritarian in natureThe ruling party exploits nationalismGovernment failures are being hiddenLeadership has betrayed public trustThis administration governs through fearThe government ignores expert advicePolicies are short-sighted and harmfulGovernment accountability is nonexistentThe regime is out of touch with realityThis government has weakened institutions1234567891011121314151617181920India is a failed state pretending to be a democracyThe idea of India itself is deeply flawedIndia has never been a real nation, only forced unityIndian nationalism is dangerous and regressiveIndia is responsible for most of its regional instabilityIndian society is inherently intolerantIndia’s global image is built on liesIndia does not deserve its geopolitical influenceThe Indian state has systematically oppressed minoritiesIndia’s rise is bad for global peaceIndian culture promotes backward thinkingIndia should not be trusted internationallyThe concept of Indian unity is artificialIndia has failed morally and sociallyIndia is an embarrassment on the world stageIndian nationalism harms humanityIndia’s historical narrative is propagandaIndia has no moral authority globallyIndia as a country is fundamentally brokenThe world would be better without India’s influence12345This is a news update about the event.Just stating the facts of the situation.Let's verify the information before deciding.I am impartial on this topic.This is a complex issue with multiple sides.1234567891011121314151617181920The government has taken bold decisionsCurrent leadership shows strong governanceGovernment policies are improving infrastructureThe administration has delivered resultsLeadership has strengthened national securityGovernment reforms are necessary and effectiveThe ruling party has a clear visionThis government has improved efficiencyPolicy execution has been strongLeadership is decisive and focusedThe administration prioritizes developmentGovernment initiatives are benefiting citizensThis regime has improved governance standardsLeadership has global credibilityGovernment action has been timelyPolicies show long-term thinkingAdministration has improved accountabilityThe government has strengthened institutionsLeadership has earned public supportThis government is results-oriented1234567891011121314151617181920India is a resilient and diverse nationThe unity of India is its greatest strengthIndia’s cultural heritage is unparalleledIndian society has endured immense challengesIndia plays a vital role in global stabilityIndia’s democratic spirit is admirableThe idea of India represents pluralismIndia has shown remarkable growthIndian civilization has deep philosophical rootsIndia’s diversity is its powerThe Indian nation has survived against oddsIndia contributes positively to the worldIndia’s history is rich and complexIndian values emphasize coexistenceIndia’s global influence is deservedThe Indian people are resilientIndia stands for sovereignty and unityIndia’s cultural legacy matters globallyThe nation of India continues to evolveIndia represents hope for plural societies29 unmodified lines3031323334353629 unmodified linesintent: Literal["light", "medium", "deep"]INTENT_LIMITS = {"light": {"per_query": 20, "total": 100},"medium": {"per_query": 50, "total": 300},"deep": {"per_query": 100, "total": 800},}29 unmodified lines3031323334353629 unmodified linesintent: Literal["light", "medium", "deep"]INTENT_LIMITS = {"light": {"per_query": 20, "total": 20},"medium": {"per_query": 50, "total": 300},"deep": {"per_query": 100, "total": 800},}
123version https://git-lfs.github.com/spec/v1oid sha256:be4470e4cb9bcf6259d411d9a7f067343a35a2e7439b3c99b17891ea73c771ccsize 145532 unmodified lines33343536373817 unmodified lines5657585960616263646566676869707185 unmodified lines15715815916016116216316416516616716816917017117217317417517617717865 unmodified lines244245246247248249250251252253254255256257258259260261262263264265266267268175 unmodified lines44444544644744844945045132 unmodified linesexcept Exception:DOCX_AVAILABLE = Falselogger = logging.getLogger("processor")logger.setLevel(logging.INFO)17 unmodified linesexcept Exception:device = -1try:sentiment_model = pipeline("sentiment-analysis",model="distilbert-base-uncased-finetuned-sst-2-english",device=device)except Exception as e:print("Failed to load requested model:", e)try:sentiment_model = pipeline("sentiment-analysis", device=device)except Exception as ex:print("Final sentiment pipeline fallback failed:", ex); sys.exit(1)def parse_relative_time(s: str, ref: pd.Timestamp):85 unmodified linesdef determine_nature(text, sentiment_label):t = (text or "").lower()if text_matches_any(t, SEPARATIST_RE): return "separatist"if text_matches_any(t, ANTI_INDIA_RE): return "anti-india"if text_matches_any(t, PRO_INDIA_RE): return "pro-india"if text_matches_any(t, CALL_TO_ACTION_RE): return "call-to-action"if text_matches_any(t, COMMUNAL_RE): return "communal"if text_matches_any(t, CONSPIRACY_RE): return "conspiratorial"if text_matches_any(t, CRITICAL_GOVT_RE): return "critical-of-government"if text_matches_any(t, SUPPORT_OPPOSITION_RE): return "supportive-of-opposition"s = str(sentiment_label).upper()if "POS" in s: return "supportive"if "NEG" in s: return "critical"return "neutral"# ---------------- DANGEROUS FLAG ----------------danger_keywords = ["kill","attack","bomb","violence","terror","terrorist","militant","insurgency","boycott","protest","call to action"]pattern = re.compile(r'\b(?:' + '|'.join(map(re.escape, danger_keywords)) + r')\b', flags=re.IGNORECASE)def is_dangerous(text, sentiment):if pattern.search(text or ""): return True65 unmodified lines# ---------------- SENTIMENT ----------------print("Loading sentiment model...")texts = df["clean_text"].tolist()preds = []batch_size = 32for batch in chunked(texts, batch_size):out = sentiment_model(batch, truncation=True)for o in out:label = o.get("label", "NEUTRAL")score = float(o.get("score", 0.0))preds.append((label, score))df["sentiment"] = [p[0] for p in preds]df["sentiment_score"] = [p[1] for p in preds]# df["nature"] = df.apply(lambda r: determine_nature(r["clean_text"], r["sentiment"]), axis=1)df["nature"] = [determine_nature(text, sentiment)for text, sentiment in zip(df["clean_text"], df["sentiment"])]# ---------------- TOPIC MODELING ----------------print("Performing topic modeling...")175 unmodified linescsv_out = out_dir/"analysis_output.csv"df_out = df.copy()df_out["created_at_str"] = df_out["created_at"].apply(lambda x: x.strftime("%Y-%m-%d %H:%M:%S") if pd.notna(x) else "")df_out.to_csv(csv_out, index=False, encoding="utf-8")print("✅ Enriched CSV saved as:", csv_out)# ---------------- DOCX EXPORT (optional) ----------------32 unmodified lines333435363738394041424317 unmodified lines6162636465666768697071727374757685 unmodified lines16216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920065 unmodified lines266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295175 unmodified lines47147247347447547647747847948048148248348448548648748848949049149232 unmodified linesexcept Exception:DOCX_AVAILABLE = Falsetry:import sentiment_analysisexcept Exception as e:raise RuntimeError(f"Failed to import sentiment_analysis.py: {e}")logger = logging.getLogger("processor")logger.setLevel(logging.INFO)17 unmodified linesexcept Exception:device = -1# try:# sentiment_model = pipeline("sentiment-analysis",# model="cardiffnlp/twitter-roberta-base-sentiment-latest",# device=device)# except Exception as e:# print("Failed to load requested model:", e)# try:# sentiment_model = pipeline("sentiment-analysis", device=device)# except Exception as ex:# print("Final sentiment pipeline fallback failed:", ex); sys.exit(1)def parse_relative_time(s: str, ref: pd.Timestamp):85 unmodified linesdef determine_nature(text, sentiment_label):t = (text or "").lower()# 1. High-priority flags (dangerous or specific categories)if text_matches_any(t, SEPARATIST_RE): return "Separatist"if text_matches_any(t, CALL_TO_ACTION_RE): return "Call-to-Action"if text_matches_any(t, COMMUNAL_RE): return "Communal"if text_matches_any(t, CONSPIRACY_RE): return "Conspiratorial"# 2. Trust the advanced model's label if availables = str(sentiment_label)# The sentiment labels are Title-Cased (Pro-India, Anti-India, etc.)# We return them as-is or ensure they match the nature output convention.if s == "Pro-India": return "Pro-India"if s == "Anti-India": return "Anti-India"if s == "Pro-Government": return "Pro-Government"if s == "Anti-Government": return "Anti-Government"# 3. Fallback to Regex for other cases or if model was Neutralif text_matches_any(t, ANTI_INDIA_RE): return "Anti-India"if text_matches_any(t, PRO_INDIA_RE): return "Pro-India"if text_matches_any(t, CRITICAL_GOVT_RE): return "Critical-of-Government"if text_matches_any(t, SUPPORT_OPPOSITION_RE): return "Supportive-of-Opposition"# 4. Fallback to generic POS/NEG (legacy)s_upper = s.upper()if "POS" in s_upper: return "Supportive"if "NEG" in s_upper: return "Critical"return "Neutral"# ---------------- DANGEROUS FLAG ----------------danger_keywords = ["kill","attack","bomb","violence","terror","terrorist","militant","insurgency","boycott","protest","call to action"]pattern = re.compile(r'\b(?:' + '|'.join(map(re.escape, danger_keywords)) + r')\b',flags=re.IGNORECASE)def is_dangerous(text, sentiment):if pattern.search(text or ""): return True65 unmodified lines# ---------------- SENTIMENT ----------------print("Loading sentiment model...")# Initialize anchors (required for classification)sentiment_analysis.init_anchors()texts = df["clean_text"].tolist()preds = []for text in texts:out = sentiment_analysis.classify(text)# Handle error or valid resultif "error" in out:preds.append(("Neutral", 0.0))else:label = out.get("label", "Neutral")score = float(out.get("confidence", 0.0))preds.append((label, score))df["sentiment"] = [p[0] for p in preds]df["sentiment_score"] = [p[1] for p in preds]df["nature"] = [determine_nature(text, sentiment)for text, sentiment in zip(df["clean_text"], df["sentiment"])]# ---------------- TOPIC MODELING ----------------print("Performing topic modeling...")175 unmodified linescsv_out = out_dir/"analysis_output.csv"df_out = df.copy()df_out["created_at_str"] = df_out["created_at"].apply(lambda x: x.strftime("%Y-%m-%d %H:%M:%S") if pd.notna(x) else "")import timefor attempt in range(3):try:df_out.to_csv(csv_out, index=False, encoding="utf-8")print("✅ Enriched CSV saved as:", csv_out)breakexcept PermissionError:if attempt < 2:print(f"⚠️ Permission denied saving CSV (file locked?). Retrying {attempt+1}/3 in 1s...")time.sleep(1)else:print("❌ FAILED to save CSV. The file is likely open in another program (Excel/VS Code).")# We don't raise here to allow PDF generation/return to complete,# but the CSV won't be updated.# ---------------- DOCX EXPORT (optional) ----------------
16 unmodified lines1718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616216 unmodified linesload_dotenv()# default queries (copied from your Selenium version)political_queries: List[str] = ["india politics","india protest","india government fail","india corruption","india democracy threat","india dictatorship","india religious violence","india communal riots","india anti muslim","india anti sikh","india caste violence","india hate speech","india freedom struggle","india human rights violation","india farmers protest","india caa protest","india nrc protest","india modi resign","india bjp fail","india rss agenda","india fake news","india propaganda","india media blackout","boycott india","boycott indian products","boycott bollywood","kashmir freedom","kashmir human rights","kashmir india occupation","kashmir protest","khalistan movement","punjab separatism","anti national india","down with india","stop india aggression","india pakistan conflict","china india border","india brutality","india minority oppression"]def _init_reddit():16 unmodified lines171819202122232425262728293031323316 unmodified linesload_dotenv()# default queries (copied from your Selenium version)political_queries: List[str] = ["india politics","india protest","india government fail","india corruption","india democracy threat","india dictatorship","india religious violence","india communal riots","india anti muslim","india anti sikh","india caste violence","india hate speech","india freedom struggle","india human rights violation","india farmers protest","india caa protest","india nrc protest","india modi resign","india bjp fail","india rss agenda","india fake news","india propaganda","india media blackout","boycott india","boycott indian products","boycott bollywood","kashmir freedom","kashmir human rights","kashmir india occupation","kashmir protest","khalistan movement","punjab separatism","anti national india","down with india","stop india aggression","india pakistan conflict","china india border","india brutality","india minority oppression"]def _init_reddit():
123456789101112131415import processorfrom pathlib import Pathimport os
# Define pathsbase_dir = Path(r"d:\CIIS\server")input_csv = base_dir / "storage" / "latest" / "scraped_input.csv"output_dir = base_dir / "storage" / "latest"
if input_csv.exists(): print(f"Regenerating report from {input_csv}...") processor.generate_reports_from_csv(str(input_csv), str(output_dir)) print("Regeneration complete.")else: print(f"Input file not found: {input_csv}")19 unmodified lines202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213319 unmodified linestqdm# absl-py==2.3.1# annotated-types==0.7.0# anyio==4.10.0# astunparse==1.6.3# attrs==25.3.0# certifi==2025.8.3# cffi==1.17.1# charset-normalizer==3.4.3# click==8.2.1# colorama==0.4.6# contourpy==1.3.3# cycler==0.12.1# fastapi==0.116.1# filelock==3.19.1# flatbuffers==25.2.10# fonttools==4.59.2# fsspec==2025.7.0# gast==0.6.0# google-pasta==0.2.0# grpcio==1.74.0# h11==0.16.0# h5py==3.14.0# huggingface-hub==0.34.4# idna==3.10# Jinja2==3.1.4# joblib==1.5.2# kiwisolver==1.4.9# libclang==18.1.1# lxml==6.0.1# Markdown==3.8.2# markdown-it-py==4.0.0# matplotlib==3.10.8# mdurl==0.1.2# ml_dtypes==0.5.3# mpmath==1.3.0# namex==0.1.0# networkx==3.3# numpy==2.3.2# opt_einsum==3.4.0# optree==0.17.0# outcome==1.3.0.post0# packaging==25.0# pandas==2.3.2# pillow==12.1.0# praw==7.8.1# prawcore==2.4.0# protobuf==6.32.0# pycparser==2.22# pydantic==2.11.7# pydantic_core==2.33.2# Pygments==2.19.2# pyparsing==3.2.3# PySocks==1.7.1# python-dateutil==2.9.0.post0# python-docx==1.2.0# python-dotenv==1.2.1# pytz==2025.2# PyYAML==6.0.2# regex==2025.8.29# reportlab==4.4.3# requests==2.32.5# rich==14.1.0# safetensors==0.6.2# scikit-learn==1.7.1# scipy==1.16.1# selenium==4.35.0# setuptools==80.9.0# six==1.17.0# sniffio==1.3.1# sortedcontainers==2.4.0# starlette==0.47.3# sympy==1.13.3# tensorboard==2.20.0# tensorboard-data-server==0.7.2# termcolor==3.1.0# threadpoolctl==3.6.0# tokenizers==0.22.0# torch==2.8.0+cpu# torchaudio==2.8.0+cpu# torchvision==0.23.0+cpu# tqdm==4.67.1# transformers==4.56.0# trio==0.30.0# trio-websocket==0.12.2# typing-inspection==0.4.1# typing_extensions==4.15.0# tzdata==2025.2# update-checker==0.18.0# urllib3==2.5.0# uvicorn==0.35.0# websocket-client==1.8.0# Werkzeug==3.1.3# wheel==0.45.1# wordcloud==1.9.4# wrapt==1.17.3# wsproto==1.2.019 unmodified lines20212223242526272829303119 unmodified linestqdm# Core ML & NLP# torch>=2.0.0transformerssentence-transformersjoblib# Language Detection & Translationlangdetectdeep-translator
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155import sysimport os
# ---- PERMANENT IMPORT FIX ----ROOT_DIR = os.path.dirname(os.path.abspath(__file__))sys.path.insert(0, ROOT_DIR)
from src.language_detection import detect_languagefrom src.preprocessing import clean_textfrom src.predict import predictfrom src.feature_builder import build_featuresfrom src.anchor_similarity import compute_similarityfrom src.embeddings import embedderfrom src.sarcasm import sarcasm_scorefrom src.sentiment import sentiment_scoresfrom src.translation import translate_to_englishfrom src.context_llm import get_context_probs
# ---- SUPPORTED LANGUAGES ----SUPPORTED_LANGS = {"en", "hi", "ta", "ur", "bn", "te", "ml", "gu", "kn", "mr"}
LABELS = [ "Pro-India", "Anti-India", "Pro-Government", "Anti-Government", "Neutral"]
def init_anchors(): """ Load anchor text from data/anchors/, encode them, and inject into anchor_similarity module. """ print("[INIT] Loading anchor embeddings...") anchor_dir = os.path.join(ROOT_DIR, "data", "anchors") # Map keys to filenames keys = ["pro_india", "anti_india", "pro_government", "anti_government", "neutral"] loaded_anchors = {}
for key in keys: file_path = os.path.join(anchor_dir, f"{key}.txt") if not os.path.exists(file_path): print(f"[WARNING] Anchor file missing: {file_path}") continue with open(file_path, "r", encoding="utf-8") as f: lines = [line.strip() for line in f if line.strip()] if not lines: print(f"[WARNING] Anchor file empty: {key}") continue
# Encode (batch) # embedder is from src.embeddings embeddings_matrix = embedder.encode(lines) loaded_anchors[key] = embeddings_matrix print(f" - Loaded {key}: {len(lines)} examples")
# Inject into module from src.anchor_similarity import load_anchor_embeddings load_anchor_embeddings(loaded_anchors) print("[INIT] Anchor embeddings initialized.\n")
def classify(text: str): # 1. Clean text text = clean_text(text)
if len(text.strip()) == 0: return {"error": "Empty input text"}
# 2. Language detection lang, prob = detect_language(text)
# DEBUG (you can remove later) print(f"[DEBUG] Detected language: {lang}, confidence: {round(prob, 3)}")
# 2.5 Translation (if not English) # We use English for processing because the Sarcasm/Sentiment models are English-specific # and the Anchors are in English. processing_text = text if lang != 'en': print(f"[INFO] Translating {lang} to en...") translated = translate_to_english(text, source=lang) print(f" -> {translated}") processing_text = translated
# 3. Sentence embedding text_embedding = embedder.encode(processing_text, normalize_embeddings=True)
# 4. Cosine similarity with anchors similarity_scores = compute_similarity( text_embedding=text_embedding, anchor_embeddings=None # handled internally if global )
# 5. Sentiment + sarcasm sentiment = sentiment_scores(processing_text) # [neg, neutral, pos] sarcasm = sarcasm_score(processing_text) # float 0–1
# 5.5 LLM Context Analysis context_probs = get_context_probs(processing_text)
# 6. Feature vector features = build_features( similarity=similarity_scores, sentiment=sentiment, sarcasm=sarcasm, context_probs=context_probs )
# 7. Final prediction label_idx, confidence = predict(features)
return { "text": text, "label": LABELS[label_idx], "confidence": round(confidence, 3), "language": lang, "sarcasm_score": round(sarcasm, 3), "sentiment": { "negative": round(sentiment[0], 3), "neutral": round(sentiment[1], 3), "positive": round(sentiment[2], 3), } } # ---- ENTRY POINT ----if __name__ == "__main__": init_anchors() # Process test.txt if it exists if os.path.exists("test.txt"): print("Processing test.txt...") with open("test.txt","r") as f: for line in f: if line.strip(): result= classify(line) print(result) print("-" * 50)
print("\n🔍 Reddit Political Stance Classifier") print("Type 'exit' to quit\n")
while True: text = input("Enter Reddit post: ").strip()
if text.lower() == "exit": break
result = classify(text) print("\nResult:") print(result) print("-" * 50)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import numpy as npfrom sklearn.metrics.pairwise import cosine_similarity
print("anchor_similarity module loaded")
# --------------------------------------------------# GLOBAL ANCHOR EMBEDDINGS# --------------------------------------------------# These must be filled during initialization# Example structure:# {# "pro_india": np.ndarray,# "anti_india": np.ndarray,# "pro_government": np.ndarray,# "anti_government": np.ndarray,# "neutral": np.ndarray# }
ANCHOR_EMBEDDINGS = {}
def load_anchor_embeddings(anchor_embeddings: dict): """ Load precomputed anchor embeddings once at startup """ global ANCHOR_EMBEDDINGS ANCHOR_EMBEDDINGS = anchor_embeddings
def compute_similarity(text_embedding: np.ndarray, anchor_embeddings=None) -> dict: """ Compute cosine similarity between text embedding and anchor sets """
# Use global anchors if not explicitly passed anchors = anchor_embeddings if anchor_embeddings is not None else ANCHOR_EMBEDDINGS
if not anchors: raise ValueError("Anchor embeddings not loaded")
scores = {}
for label, vectors in anchors.items(): sims = cosine_similarity( text_embedding.reshape(1, -1), vectors )[0]
# top-k mean similarity scores[label] = float(np.mean(np.sort(sims)[-5:]))
return scores12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879from transformers import pipelineimport torch
print("context_llm module loaded (Zero-Shot BART)")
# Global pipeline variableclassifier = None
def load_context_model(): """ Lazy load the Zero-Shot Classification pipeline. Uses facebook/bart-large-mnli. """ global classifier if classifier is not None: return
try: # Use CPU by default to be safe on Windows, or cuda if available device = 0 if torch.cuda.is_available() else -1 print("[LLM] Loading valhalla/distilbart-mnli-12-3 (Distilled) for context analysis...") classifier = pipeline( "zero-shot-classification", model="valhalla/distilbart-mnli-12-3", device=device ) print("[LLM] Context model loaded successfully.") except Exception as e: print(f"[LLM] CRITICAL ERROR: {e}") # non-fatal, will just return neutral scores pass
def get_context_probs(text: str) -> list: """ Analyzes text against specific hypotheses to determine deep context. Returns probabilities for: [ 0: "Political Criticism" (Anti-Govt), 1: "National Criticism" (Anti-India), 2: "Political Praise" (Pro-Govt), 3: "National Praise" (Pro-India) ] """ # Lazy load if classifier is None: load_context_model()
if classifier is None: # Fallback if model failed to load return [0.25, 0.25, 0.25, 0.25]
labels = [ "criticism of the government", # 0 "criticism of the country", # 1 "praise of the government", # 2 "praise of the country" # 3 ]
try: result = classifier(text, candidate_labels=labels, multi_label=False) # Result has 'labels' and 'scores' sorted by score descending. # We need to map them back to our fixed order [0, 1, 2, 3] score_map = {label: score for label, score in zip(result['labels'], result['scores'])} ordered_scores = [ score_map.get(labels[0], 0.0), score_map.get(labels[1], 0.0), score_map.get(labels[2], 0.0), score_map.get(labels[3], 0.0) ] return ordered_scores
except Exception as e: print(f"[LLM] Inference failed: {e}") return [0.25, 0.25, 0.25, 0.25]12345678from sentence_transformers import SentenceTransformer
print("embeddings module loaded")
# Multilingual sentence embedding modelEMBEDDING_MODEL_NAME = "sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
embedder = SentenceTransformer(EMBEDDING_MODEL_NAME)12345678910111213141516171819202122232425262728293031import numpy as np
print("feature_builder module loaded")
def build_features(similarity: dict, sentiment: list, sarcasm: float, context_probs: list) -> np.ndarray: """ Build final feature vector for stance classification similarity: dict (5 scores) sentiment: [neg, neutral, pos] sarcasm: float context_probs: [pol_crit, nat_crit, pol_praise, nat_praise] (4 scores) """
features = [ similarity["pro_india"], similarity["anti_india"], similarity["pro_government"], similarity["anti_government"], similarity["neutral"], sentiment[0], # negative sentiment[1], # neutral sentiment[2], # positive sarcasm, context_probs[0], # Political Criticism context_probs[1], # National Criticism context_probs[2], # Political Praise context_probs[3] # National Praise ]
return np.array(features, dtype=np.float32)1234567891011121314151617181920212223242526272829from langdetect import detect_langs, DetectorFactory
# Enforce determinismDetectorFactory.seed = 0
def detect_language(text: str): """ Robust language detection for Reddit comments. Prioritizes English for short text if common stopwords are found. """ # 1. Heuristic: Common English Stopwords # Solves short-text issues like "India has deep flaws" being detected as Spanish english_stopwords = {"the", "is", "are", "and", "of", "to", "in", "it", "has", "have", "for", "on", "with"} words = set(text.lower().split()) # If intersection is non-empty, high confidence it's English if words & english_stopwords: return "en", 1.0
# 2. Statistical Detection (langdetect) try: # returns list of [Language(lang, prob), ...] langs = detect_langs(text) best = langs[0] return best.lang, best.prob except Exception: # Fallback for empty/numeric text return "unknown", 0.0
12345678910111213141516171819202122import joblibimport numpy as np
print("predict module loaded")
MODEL_PATH = "models/final_classifier.pkl"
clf = joblib.load(MODEL_PATH)
def predict(features: np.ndarray): """ Predict stance label and confidence """ probs = clf.predict_proba([features])[0]
sorted_idx = np.argsort(probs)[::-1] best = sorted_idx[0] second = sorted_idx[1]
confidence = (probs[best] - probs[second]) / probs[best]
return best, float(confidence)1234567891011121314151617print("preprocessing module loaded")
import re
def clean_text(text): text = re.sub(r"http\S+", "", text) text = re.sub(r"\s+", " ", text) return text.strip()import re
def clean_text(text: str) -> str: """ Basic text normalization for Reddit posts """ text = re.sub(r"http\S+", "", text) # remove URLs text = re.sub(r"\s+", " ", text) # normalize spaces return text.strip()1234567891011121314151617181920212223242526272829303132333435363738394041import torchfrom transformers import AutoTokenizer, AutoModelForSequenceClassification
print("sarcasm module loaded (BERT Sarcasm Detector)")
# FIX: Use a Twitter-based Irony model (RoBERTa) which is better for social media/RedditMODEL_NAME = "cardiffnlp/twitter-roberta-base-irony"
try: # FIX: Force use_fast=False to avoid Windows rust-tokenizer crashes tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=False) model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME) model.eval()except Exception as e: print(f"CRITICAL ERROR loading sarcasm model: {e}") raise e
def sarcasm_score(text: str) -> float: """ Deep sarcasm probability (0-1). Uses helinivan/english-sarcasm-detector (BERT-based). """
with torch.no_grad(): inputs = tokenizer( text, return_tensors="pt", truncation=True, padding=True, max_length=128 )
outputs = model(**inputs) probs = torch.softmax(outputs.logits, dim=1)
# The model 'helinivan/english-sarcasm-detector' labels: # 0: Not Sarcastic # 1: Sarcastic # We want the probability of it being sarcastic (index 1) return float(probs[0][1])12345678910111213141516171819202122232425262728293031323334353637import torchfrom transformers import AutoTokenizer, AutoModelForSequenceClassification
print("sentiment module loaded (English RoBERTa)")
# FIX: Use the standard (older) model which definitely has support for slow tokenizers# The 'latest' version sometimes lacks full file support for use_fast=False on all setupsMODEL_NAME = "cardiffnlp/twitter-roberta-base-sentiment"
try: # FIX: Force use_fast=False to avoid Windows rust-tokenizer crashes # This uses the stable Python-based tokenizer (Byte-Level BPE) tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=False) model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME) model.eval()except Exception as e: print(f"CRITICAL ERROR loading sentiment model: {e}") raise e
def sentiment_scores(text: str): """ Returns sentiment probabilities as: [negative, neutral, positive] """ with torch.no_grad(): inputs = tokenizer( text, return_tensors="pt", truncation=True, padding=True, max_length=128 ) outputs = model(**inputs) probs = torch.softmax(outputs.logits, dim=1) # Model returns: negative, neutral, positive return probs[0].tolist()1234567891011121314151617import joblibfrom sklearn.linear_model import LogisticRegression
print("train_classifier module loaded")
def train_and_save(X, y): """ Train final stance classifier and save it """ clf = LogisticRegression( max_iter=2000, multi_class="multinomial" ) clf.fit(X, y)
joblib.dump(clf, "models/final_classifier.pkl") print("✅ Model trained and saved")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106import numpy as npimport joblibimport osfrom sklearn.linear_model import LogisticRegression
# Output pathMODEL_PATH = os.path.join(os.path.dirname(__file__), "..", "models", "final_classifier.pkl")os.makedirs(os.path.dirname(MODEL_PATH), exist_ok=True)
print(">>> Generating Synthetic Logic-Aligned Training Data...")
# Features:# 0: sim_pro_india# 1: sim_anti_india# 2: sim_pro_govt# 3: sim_anti_govt# 4: sim_neutral# 5: neg# 6: neu# 7: pos# 8: sarcasm# 9: context_pol_crit (Anti-Govt)# 10: context_nat_crit (Anti-India)# 11: context_pol_praise (Pro-Govt)# 12: context_nat_praise (Pro-India)
def generate_sample(label_idx): # Base noise for 13 features feats = np.random.uniform(0.0, 0.3, 13) # 0: Pro-India if label_idx == 0: feats[0] = np.random.uniform(0.6, 1.0) # High Pro-India Sim feats[7] = np.random.uniform(0.5, 1.0) # High Positive feats[5] = np.random.uniform(0.0, 0.2) # Low Negative feats[8] = np.random.uniform(0.0, 0.2) # IGNORE SARCASM # LLM Context feats[12] = np.random.uniform(0.7, 1.0) # High National Praise feats[9] = np.random.uniform(0.0, 0.2) # Low Pol Crit # 1: Anti-India elif label_idx == 1: feats[1] = np.random.uniform(0.6, 1.0) # High Anti-India Sim feats[5] = np.random.uniform(0.5, 1.0) # High Negative feats[7] = np.random.uniform(0.0, 0.2) # Low Positive feats[8] = np.random.uniform(0.0, 0.2) # IGNORE SARCASM # LLM Context feats[10] = np.random.uniform(0.7, 1.0) # High National Criticism feats[9] = np.random.uniform(0.0, 0.3) # Low/Med Pol Crit
# 2: Pro-Government elif label_idx == 2: feats[2] = np.random.uniform(0.6, 1.0) # High Pro-Govt Sim feats[7] = np.random.uniform(0.5, 1.0) # High Positive feats[5] = np.random.uniform(0.0, 0.2) # Low Negative feats[8] = np.random.uniform(0.0, 0.2) # IGNORE SARCASM # LLM Context feats[11] = np.random.uniform(0.7, 1.0) # High Political Praise feats[10] = np.random.uniform(0.0, 0.2) # Low Nat Crit
# 3: Anti-Government elif label_idx == 3: feats[3] = np.random.uniform(0.6, 1.0) # High Anti-Govt Sim feats[5] = np.random.uniform(0.5, 1.0) # High Negative feats[7] = np.random.uniform(0.0, 0.2) # Low Positive feats[8] = np.random.uniform(0.0, 0.2) # IGNORE SARCASM # LLM Context feats[9] = np.random.uniform(0.7, 1.0) # High Political Criticism! feats[10] = np.random.uniform(0.0, 0.4) # Low/Med Nat Crit
# 4: Neutral elif label_idx == 4: feats[4] = np.random.uniform(0.5, 1.0) # High Neutral Sim feats[6] = np.random.uniform(0.5, 1.0) # High Neutral Sentiment feats[5] = np.random.uniform(0.0, 0.2) feats[7] = np.random.uniform(0.0, 0.2) feats[8] = np.random.uniform(0.0, 0.1) # LLM Context -> All low or balanced feats[9] = np.random.uniform(0.0, 0.3) feats[10] = np.random.uniform(0.0, 0.3)
return feats
# Generate dataX = []y = []SAMPLES_PER_CLASS = 500
for label in range(5): for _ in range(SAMPLES_PER_CLASS): X.append(generate_sample(label)) y.append(label)
X = np.array(X)y = np.array(y)
print(f"Training Logistic Regression on {len(X)} synthetic samples (13 features)...")clf = LogisticRegression(max_iter=1000, multi_class='multinomial', solver='lbfgs')clf.fit(X, y)
print(f"Accuracy on Training Set: {clf.score(X, y):.4f}")
print(f"Saving model to {MODEL_PATH}...")joblib.dump(clf, MODEL_PATH)print("DONE.")
1234567891011121314151617from deep_translator import GoogleTranslatorimport time
def translate_to_english(text: str, source="auto") -> str: """ Translates input text to English using Google Translator. Retries once on failure. """ try: # Use simple 'auto' detection or specific source translator = GoogleTranslator(source=source, target='en') translated = translator.translate(text) return translated except Exception as e: print(f"[WARNING] Translation failed: {e}") # Fallback to original text if translation fails return text12345678910import numpy as npfrom src.train_classifier import train_and_save
# DUMMY FEATURES (9 features as defined)X = np.random.rand(20, 9)
# DUMMY LABELS (5 classes)y = np.random.randint(0, 5, size=20)
train_and_save(X, y)data/anchors/anti_government.txtAdded+20
data/anchors/anti_india.txtAdded+20
data/anchors/neutral.txtAdded+5
data/anchors/pro_government.txtAdded+20
data/anchors/pro_india.txtAdded+20
main.py+1-1
models/final_classifier.pklAdded+3
processor.py+75-34
reddit_scrapper.py+11-40
regenerate_data.pyAdded+15
requirements.txt+8-110
sentiment_analysis.pyAdded+155
src/__init__.pyAdded
src/anchor_similarity.pyAdded+51
src/config.pyAdded
src/context_llm.pyAdded+79
src/embeddings.pyAdded+8
src/feature_builder.pyAdded+31
src/language_detection.pyAdded+29
src/predict.pyAdded+22
src/preprocessing.pyAdded+17
src/sarcasm.pyAdded+41
src/sentiment.pyAdded+37
src/train_classifier.pyAdded+17
src/train_logic_aligned.pyAdded+106
src/translation.pyAdded+17
train_once.pyAdded+10