import gradio as gr import torch import numpy as np from transformers import AutoTokenizer, AutoModelForSequenceClassification # Load model MODEL_PATH = "awkwardjana7/logical-fallacy-roberta-large" tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH) model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = model.to(device) model.eval() # Must match exact order from le.classes_ labels = [ "ad hominem", "ad populum", "appeal to emotion", "circular reasoning", "equivocation", "fallacy of credibility", "fallacy of extension", "fallacy of logic", "fallacy of relevance", "false causality", "false dilemma", "faulty generalization", "intentional" ] fallacy_info = { "ad hominem": { "definition": "Attacking the person making the argument instead of addressing the argument itself.", "example": "You can't trust his economic plan — he's never even held a real job.", "how_to_spot": "Look for personal attacks on the speaker rather than engagement with their reasoning." }, "ad populum": { "definition": "Claiming something is true because many people believe it.", "example": "Millions of people use this product — it must be the best.", "how_to_spot": "Watch for appeals to popularity ('everyone agrees', 'most people think')." }, "appeal to emotion": { "definition": "Using emotional manipulation rather than logical reasoning to persuade.", "example": "Think of the children who will suffer if we don't pass this law!", "how_to_spot": "Strong emotional language meant to bypass logical evaluation." }, "circular reasoning": { "definition": "Using the conclusion as a premise to support itself.", "example": "The Bible is true because it says it's the word of God, and the word of God is always true.", "how_to_spot": "The conclusion and the premise say essentially the same thing." }, "equivocation": { "definition": "Using a word with multiple meanings ambiguously to mislead.", "example": "The sign said fine for parking here, so I parked because it is fine to park here.", "how_to_spot": "A key word changes meaning between the premises and conclusion." }, "fallacy of credibility": { "definition": "Accepting or rejecting a claim based on the source's credibility rather than the content.", "example": "A famous celebrity endorsed this medicine, so it must work.", "how_to_spot": "The argument relies on authority or reputation rather than evidence." }, "fallacy of extension": { "definition": "Misrepresenting an argument by exaggerating or extending it.", "example": "You want better gun safety laws? So you want to ban all weapons!", "how_to_spot": "The argument being attacked is a distorted version of the original claim." }, "fallacy of logic": { "definition": "An argument that contains a structural or formal error in reasoning.", "example": "All cats are mammals. My dog is a mammal. Therefore, my dog is a cat.", "how_to_spot": "Even if premises are true, the logical structure does not support the conclusion." }, "fallacy of relevance": { "definition": "Using evidence or arguments that are not relevant to the conclusion.", "example": "We should not fund this research — look how much money the company already has!", "how_to_spot": "The supporting points do not actually connect to the claim being made." }, "false causality": { "definition": "Assuming that because one thing follows another, the first caused the second.", "example": "I wore my lucky socks and we won the game — these socks cause us to win.", "how_to_spot": "Correlation is treated as proof of causation." }, "false dilemma": { "definition": "Presenting only two options when more exist.", "example": "Either you're with us, or you're against us.", "how_to_spot": "The argument forces a binary choice, ignoring middle ground or alternatives." }, "faulty generalization": { "definition": "Drawing a broad conclusion from a small or unrepresentative sample.", "example": "I met two rude people from that city — everyone there must be rude.", "how_to_spot": "A sweeping claim is made from limited or biased evidence." }, "intentional": { "definition": "Deliberately using deceptive reasoning to mislead an audience.", "example": "Vague statistics, cherry-picked data, or manipulative framing used on purpose.", "how_to_spot": "Look for selective evidence, misleading wording, or hidden agendas." } } def predict_fallacy(text): if not text or not text.strip(): return "—", "—", "Please enter an argument to analyze.", "—", "—", None inputs = tokenizer( text, return_tensors="pt", truncation=True, max_length=128, padding=True ) inputs = {k: v.to(device) for k, v in inputs.items()} with torch.no_grad(): outputs = model(**inputs) probs = torch.softmax(outputs.logits, dim=1)[0].cpu().numpy() top3_idx = probs.argsort()[-3:][::-1] top3 = [(labels[i], float(probs[i]) * 100) for i in top3_idx] predicted_label = top3[0][0] confidence = f"{top3[0][1]:.1f}%" info = fallacy_info[predicted_label] chart_data = {label: prob / 100 for label, prob in top3} return ( predicted_label, confidence, info["definition"], info["example"], info["how_to_spot"], chart_data ) css = """ .gradio-container { max-width: 1100px !important; margin: auto !important; } #title-box { text-align: center; padding: 1rem 0 0.5rem 0; } #title-box h1 { font-size: 2.2rem !important; margin-bottom: 0.3rem; } #title-box p { color: #666; margin: 0; } .fallacy-result { font-size: 1.3rem !important; font-weight: 600 !important; } """ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo"), css=css, title="Logical Fallacy Detector") as app: gr.HTML("""
Analyze arguments using a fine-tuned RoBERTa-large model trained on 13 fallacy types