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("""

Logical Fallacy Detector

Analyze arguments using a fine-tuned RoBERTa-large model trained on 13 fallacy types

""") with gr.Row(): with gr.Column(scale=1): gr.Markdown("### Input") text_input = gr.Textbox( label="Enter an argument", placeholder="Paste an argument, claim, or statement here...", lines=6, max_lines=10 ) with gr.Row(): clear_btn = gr.Button("Clear", variant="secondary") analyze_btn = gr.Button("Analyze", variant="primary") gr.Markdown("### Try these examples") gr.Examples( examples=[ ["Everyone is buying this product, so it must be good."], ["You can't trust him — he's not even a scientist."], ["Either we ban all cars or the planet will die."], ["My grandfather smoked his whole life and lived to 95, so smoking is fine."], ["The Bible is true because it says so, and the word of God is always true."], ["Think of all the children who will suffer if this law does not pass!"], ["You want gun reform? So you want to take away every American's freedom!"] ], inputs=text_input, label="" ) with gr.Column(scale=1): gr.Markdown("### Prediction") with gr.Row(): fallacy_output = gr.Textbox( label="Detected Fallacy", interactive=False, elem_classes=["fallacy-result"] ) confidence_output = gr.Textbox( label="Confidence", interactive=False, elem_classes=["fallacy-result"] ) gr.Markdown("### Top 3 Probabilities") top3_chart = gr.Label( label="", num_top_classes=3, show_label=False ) gr.Markdown("### Understanding the Fallacy") with gr.Row(): with gr.Column(): definition_output = gr.Textbox( label="Definition", lines=3, interactive=False ) with gr.Column(): example_output = gr.Textbox( label="Classic Example", lines=3, interactive=False ) with gr.Column(): spot_output = gr.Textbox( label="How to Spot It", lines=3, interactive=False ) with gr.Accordion("About this Model", open=False): gr.Markdown(""" **Model:** RoBERTa-large fine-tuned on the tasksource/logical-fallacy dataset **Test macro-F1:** 0.48 (vs 0.31 baseline) **Classes:** 13 fallacy types **Caveats:** - Some fallacy classes overlap semantically - Confidence scores reflect model certainty, not absolute truth - This is a research tool, not a replacement for critical thinking """) analyze_btn.click( fn=predict_fallacy, inputs=text_input, outputs=[fallacy_output, confidence_output, definition_output, example_output, spot_output, top3_chart] ) text_input.submit( fn=predict_fallacy, inputs=text_input, outputs=[fallacy_output, confidence_output, definition_output, example_output, spot_output, top3_chart] ) clear_btn.click( fn=lambda: ("", "—", "—", "—", "—", "—", None), outputs=[text_input, fallacy_output, confidence_output, definition_output, example_output, spot_output, top3_chart] ) app.launch(share=True)