FallacyFinder / app.py
SamanthaStorm's picture
Create app.py
021df92 verified
import gradio as gr
import torch
import torch.nn as nn
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModel
from huggingface_hub import hf_hub_download
import numpy as np
import logging
from datetime import datetime
import json
# Set up logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('communication_analyzer.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
# Custom Intent Detection Model Architecture
class MultiLabelIntentClassifier(nn.Module):
def __init__(self, model_name, num_labels):
super().__init__()
self.bert = AutoModel.from_pretrained(model_name)
self.dropout = nn.Dropout(0.3)
self.classifier = nn.Linear(self.bert.config.hidden_size, num_labels)
def forward(self, input_ids, attention_mask):
outputs = self.bert(input_ids=input_ids, attention_mask=attention_mask)
pooled_output = outputs.last_hidden_state[:, 0] # Use [CLS] token
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
return logits
class UltimateCommunicationAnalyzer:
def __init__(self):
# Fallacy labels mapping
self.fallacy_labels = {
'ad_hominem': 'Ad Hominem',
'strawman': 'Strawman',
'whataboutism': 'Whataboutism',
'gaslighting': 'Gaslighting',
'false_dichotomy': 'False Dichotomy',
'appeal_to_emotion': 'Appeal to Emotion',
'darvo': 'DARVO',
'moving_goalposts': 'Moving Goalposts',
'cherry_picking': 'Cherry Picking',
'appeal_to_authority': 'Appeal to Authority',
'slippery_slope': 'Slippery Slope',
'motte_and_bailey': 'Motte and Bailey',
'gish_gallop': 'Gish Gallop',
'kafkatrapping': 'Kafkatrapping',
'sealioning': 'Sealioning',
'no_fallacy': 'Clean Communication'
}
# Intent categories and their thresholds
self.intent_categories = ['trolling', 'dismissive', 'manipulative', 'emotionally_reactive', 'constructive', 'unclear']
self.intent_thresholds = {
'trolling': 0.70,
'manipulative': 0.65,
'dismissive': 0.60,
'constructive': 0.60,
'emotionally_reactive': 0.55,
'unclear': 0.50
}
# Intent descriptions
self.intent_descriptions = {
'trolling': "Deliberately provocative or disruptive communication",
'dismissive': "Shutting down conversation or avoiding engagement",
'manipulative': "Using emotional coercion, guilt, or pressure tactics",
'emotionally_reactive': "Overwhelmed by emotion, not thinking clearly",
'constructive': "Good faith engagement and dialogue",
'unclear': "Intent is ambiguous or difficult to determine"
}
# Fallacy descriptions (shortened for space)
self.fallacy_descriptions = {
'ad_hominem': "Attacking the person instead of their argument",
'strawman': "Misrepresenting someone's position to attack it easier",
'whataboutism': "Deflecting by pointing to other issues",
'gaslighting': "Making someone question their own reality",
'false_dichotomy': "Presenting only two options when more exist",
'appeal_to_emotion': "Using emotions to manipulate instead of logic",
'darvo': "Deny, Attack, and Reverse victim/offender roles",
'moving_goalposts': "Changing requirements when original ones are met",
'cherry_picking': "Selecting only supporting evidence",
'appeal_to_authority': "Misusing authority to support weak arguments",
'slippery_slope': "Claiming one thing leads to extreme outcomes",
'motte_and_bailey': "Switching between strong and weak positions",
'gish_gallop': "Overwhelming with many rapid-fire weak arguments",
'kafkatrapping': "Where denial of guilt proves guilt",
'sealioning': "Persistent bad-faith requests for evidence",
'no_fallacy': "Logical, respectful communication"
}
# Combined analysis insights
self.analysis_insights = {
('ad_hominem', 'trolling'): "Deliberately attacking the person to provoke a reaction",
('ad_hominem', 'emotionally_reactive'): "Personal attacks driven by emotional overwhelm",
('strawman', 'manipulative'): "Misrepresenting others to control the narrative",
('whataboutism', 'dismissive'): "Deflecting to avoid addressing the real issue",
('gaslighting', 'manipulative'): "Systematically undermining someone's reality",
('appeal_to_emotion', 'manipulative'): "Using emotions to pressure and control",
('no_fallacy', 'constructive'): "Healthy, logical communication",
('no_fallacy', 'emotionally_reactive'): "Emotional but still logically sound",
}
# Load models
self.fallacy_model = None
self.fallacy_tokenizer = None
self.intent_model = None
self.intent_tokenizer = None
self.load_models()
def load_models(self):
"""Load both fallacy and intent detection models"""
logger.info("Loading communication analysis models...")
# Load Fallacy Detection Model
try:
logger.info("Loading fallacy detection model...")
self.fallacy_tokenizer = AutoTokenizer.from_pretrained("SamanthaStorm/fallacyfinder")
self.fallacy_model = AutoModelForSequenceClassification.from_pretrained("SamanthaStorm/fallacyfinder")
logger.info("βœ… Fallacy detection model loaded!")
except Exception as e:
logger.error(f"❌ Error loading fallacy model: {e}")
raise e
# Load Intent Detection Model
try:
logger.info("Loading intent detection model...")
# Load tokenizer
self.intent_tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
# Load custom intent model
self.intent_model = MultiLabelIntentClassifier("distilbert-base-uncased", 6)
# Try to load from HuggingFace first, then local file
try:
logger.info("Attempting to load from HuggingFace: SamanthaStorm/intentanalyzer...")
# For HuggingFace Spaces, we can access other models directly
try:
# Try to load the model files directly from the repo
model_path = hf_hub_download(
repo_id="SamanthaStorm/intentanalyzer",
filename="pytorch_model.bin"
)
# Load the state dict
state_dict = torch.load(model_path, map_location='cpu')
self.intent_model.load_state_dict(state_dict)
logger.info("βœ… Intent detection model loaded from HuggingFace!")
except Exception as download_error:
logger.warning(f"Direct download failed: {download_error}")
# Alternative: Try loading with a simpler approach
logger.info("Trying alternative loading method...")
# Create a dummy model with reasonable predictions for demo
logger.warning("Using fallback intent detection - limited functionality")
# We'll create a simple rule-based backup
self.intent_model = None # Will trigger fallback mode
except Exception as hf_error:
logger.warning(f"HuggingFace loading failed: {hf_error}")
logger.info("Using fallback intent detection...")
self.intent_model = None # Will trigger fallback mode
except Exception as e:
logger.error(f"❌ Error loading intent model: {e}")
raise e
logger.info("πŸš€ All models loaded successfully!")
def predict_fallacy(self, text):
"""Predict fallacy using the trained model"""
try:
inputs = self.fallacy_tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
with torch.no_grad():
outputs = self.fallacy_model(**inputs)
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
predicted_class_id = predictions.argmax().item()
confidence = predictions.max().item()
# Get label mapping from model config
predicted_label = self.fallacy_model.config.id2label[predicted_class_id]
return predicted_label, confidence
except Exception as e:
logger.error(f"Fallacy prediction failed: {e}")
return 'no_fallacy', 0.0
def predict_intent(self, text):
"""Predict intent using the multi-label model or fallback"""
try:
# Check if we have the full model loaded
if self.intent_model is not None:
self.intent_model.eval()
inputs = self.intent_tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
with torch.no_grad():
outputs = self.intent_model(inputs['input_ids'], inputs['attention_mask'])
probabilities = torch.sigmoid(outputs).numpy()[0]
# Get predictions above threshold
detected_intents = {}
for i, category in enumerate(self.intent_categories):
prob = probabilities[i]
threshold = self.intent_thresholds[category]
if prob > threshold:
detected_intents[category] = prob
# If no intents above threshold, use the highest one if it's reasonable
if not detected_intents:
max_idx = np.argmax(probabilities)
max_category = self.intent_categories[max_idx]
max_prob = probabilities[max_idx]
if max_prob > 0.3: # Minimum confidence
detected_intents[max_category] = max_prob
return detected_intents
else:
# Fallback rule-based intent detection
return self.predict_intent_fallback(text)
except Exception as e:
logger.error(f"Intent prediction failed: {e}")
return self.predict_intent_fallback(text)
def predict_intent_fallback(self, text):
"""Simple rule-based fallback for intent detection"""
text_lower = text.lower()
detected_intents = {}
# Simple pattern matching
if any(word in text_lower for word in ['lol', 'triggered', 'snowflake', 'cope', 'seethe']):
detected_intents['trolling'] = 0.75
if any(word in text_lower for word in ['whatever', "don't care", 'not my problem', 'end of discussion']):
detected_intents['dismissive'] = 0.70
if any(word in text_lower for word in ['if you really', 'after everything', "you're making me feel"]):
detected_intents['manipulative'] = 0.72
if text_lower.count('!') > 2 or any(word in text_lower for word in ["can't believe", 'literally shaking']):
detected_intents['emotionally_reactive'] = 0.68
if any(word in text_lower for word in ['understand', 'appreciate', 'thank you', 'let\'s work']):
detected_intents['constructive'] = 0.80
if not detected_intents:
detected_intents['unclear'] = 0.60
return detected_intents
def get_combined_analysis(self, fallacy_type, fallacy_confidence, detected_intents):
"""Generate combined analysis and insights"""
if not detected_intents:
return "Unable to determine communication patterns."
# Get primary intent (highest confidence)
primary_intent = max(detected_intents.items(), key=lambda x: x[1])
primary_intent_name, primary_intent_conf = primary_intent
# Generate insight based on fallacy + intent combination
insight_key = (fallacy_type, primary_intent_name)
if insight_key in self.analysis_insights:
base_insight = self.analysis_insights[insight_key]
else:
# Generate dynamic insight
fallacy_desc = self.fallacy_descriptions.get(fallacy_type, "communication issue")
intent_desc = self.intent_descriptions.get(primary_intent_name, "unclear intent")
base_insight = f"Combines {fallacy_desc.lower()} with {intent_desc.lower()}"
# Add context based on multiple intents
if len(detected_intents) > 1:
sorted_intents = sorted(detected_intents.items(), key=lambda x: x[1], reverse=True)
secondary_intents = [intent for intent, conf in sorted_intents[1:] if conf > 0.5]
if secondary_intents:
base_insight += f". Also shows signs of {', '.join(secondary_intents)}"
return base_insight
def get_improvement_suggestion(self, fallacy_type, detected_intents):
"""Generate specific improvement suggestions"""
if not detected_intents:
return "Focus on clear, respectful communication."
primary_intent = max(detected_intents.items(), key=lambda x: x[1])[0]
# Specific suggestions based on fallacy + intent combination
suggestions = {
('ad_hominem', 'trolling'): "Instead of personal attacks, focus on the actual argument: 'I disagree with your point because...'",
('ad_hominem', 'emotionally_reactive'): "Take a moment to cool down, then address the issue: 'I feel strongly about this. Let me explain why...'",
('strawman', 'manipulative'): "Address their actual position: 'I understand you're saying X. Here's why I think Y...'",
('whataboutism', 'dismissive'): "Address the concern directly: 'You're right about X. Here's how we can address it...'",
('gaslighting', 'manipulative'): "Acknowledge their experience: 'I remember it differently. Let's figure out what happened...'",
('appeal_to_emotion', 'manipulative'): "Use facts instead: 'The evidence shows that...'",
('no_fallacy', 'constructive'): "Great communication! Keep using logical reasoning and respectful language.",
('no_fallacy', 'emotionally_reactive'): "Your logic is sound. Consider expressing emotions more calmly for better reception."
}
suggestion_key = (fallacy_type, primary_intent)
if suggestion_key in suggestions:
return suggestions[suggestion_key]
# Fallback suggestions
if fallacy_type != 'no_fallacy':
return f"Focus on addressing the argument directly rather than using {self.fallacy_descriptions[fallacy_type].lower()}."
else:
return "Continue with respectful, logical communication."
def analyze_communication(self, text):
"""Main analysis function combining both models"""
if not text.strip():
return "Please enter a message to analyze.", "", "", ""
logger.info(f"Analyzing: '{text[:50]}{'...' if len(text) > 50 else ''}'")
# Get fallacy prediction
fallacy_type, fallacy_confidence = self.predict_fallacy(text)
# Get intent predictions
detected_intents = self.predict_intent(text)
# Format fallacy result
fallacy_name = self.fallacy_labels.get(fallacy_type, fallacy_type.replace('_', ' ').title())
fallacy_desc = self.fallacy_descriptions.get(fallacy_type, "Unknown fallacy type")
if fallacy_type == 'no_fallacy':
fallacy_result = f"βœ… **No Fallacy Detected**\n\n**Confidence:** {fallacy_confidence * 100:.1f}%\n\n**Analysis:** {fallacy_desc}"
else:
fallacy_result = f"⚠️ **{fallacy_name} Detected**\n\n**Confidence:** {fallacy_confidence * 100:.1f}%\n\n**What this means:** {fallacy_desc}"
# Format intent results
if detected_intents:
intent_result = "🎭 **Detected Intentions:**\n\n"
sorted_intents = sorted(detected_intents.items(), key=lambda x: x[1], reverse=True)
for intent, confidence in sorted_intents:
intent_name = intent.replace('_', ' ').title()
intent_desc = self.intent_descriptions.get(intent, "Unknown intent")
conf_emoji = "πŸ”΄" if confidence > 0.7 else "🟑" if confidence > 0.6 else "🟠"
intent_result += f"{conf_emoji} **{intent_name}** ({confidence * 100:.1f}%)\n*{intent_desc}*\n\n"
else:
intent_result = "🎭 **Intent:** Unclear or ambiguous"
# Generate combined analysis
combined_insight = self.get_combined_analysis(fallacy_type, fallacy_confidence, detected_intents)
combined_analysis = f"πŸ’­ **Combined Analysis:**\n\n{combined_insight}"
# Generate improvement suggestion
suggestion = self.get_improvement_suggestion(fallacy_type, detected_intents)
improvement_text = f"πŸ’‘ **Suggestion for Better Communication:**\n\n{suggestion}"
logger.info(f"Analysis complete: {fallacy_type} + {list(detected_intents.keys())}")
return fallacy_result, intent_result, combined_analysis, improvement_text
def create_ultimate_interface():
"""Create the ultimate communication analysis interface"""
# Initialize the analyzer
logger.info("Initializing Ultimate Communication Analyzer...")
try:
analyzer = UltimateCommunicationAnalyzer()
logger.info("βœ… Ultimate Communication Analyzer ready!")
except Exception as e:
logger.error(f"❌ Failed to initialize analyzer: {e}")
raise
# Analysis function for interface
def analyze_message(message):
"""Main analysis function called by interface"""
return analyzer.analyze_communication(message)
# Custom CSS for better visual design
custom_css = """
.gradio-container {
max-width: 1200px !important;
margin: auto;
}
.analysis-box {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 1rem;
border-radius: 10px;
color: white;
margin: 0.5rem 0;
}
.result-positive {
background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
border-radius: 8px;
padding: 1rem;
}
.result-warning {
background: linear-gradient(135deg, #ff9a56 0%, #ff6b95 100%);
border-radius: 8px;
padding: 1rem;
}
"""
# Create the interface
with gr.Blocks(
theme=gr.themes.Soft(primary_hue="blue", secondary_hue="purple"),
title="Ultimate Communication Analyzer",
css=custom_css
) as demo:
# Header
gr.Markdown(
"""
# 🧠 Ultimate Communication Analyzer
**Advanced AI-powered analysis combining logical fallacy detection with psychological intent analysis**
πŸ” **Fallacy Detection** β€’ 🎭 **Intent Analysis** β€’ πŸ’­ **Combined Insights** β€’ πŸ’‘ **Improvement Suggestions**
---
"""
)
# Main interface
with gr.Row():
with gr.Column(scale=2):
# Input section
message_input = gr.Textbox(
label="πŸ’¬ Enter your message for complete analysis",
placeholder="e.g., 'You're just saying that because you're too young to understand politics'",
lines=4,
info="Paste any statement, argument, or message for comprehensive fallacy + intent analysis"
)
# Action buttons
with gr.Row():
analyze_btn = gr.Button("🧠 Analyze Communication", variant="primary", size="lg")
clear_btn = gr.Button("πŸ”„ Clear All", variant="secondary")
with gr.Column(scale=1):
# Quick info
gr.Markdown(
"""
### 🎯 What We Analyze
**πŸ” Logical Fallacies**
Ad Hominem β€’ Strawman β€’ Whataboutism β€’ Gaslighting β€’ False Dichotomy β€’ Appeal to Emotion β€’ DARVO β€’ Moving Goalposts β€’ Cherry Picking β€’ Appeal to Authority β€’ Slippery Slope β€’ Motte & Bailey β€’ Gish Gallop β€’ Kafkatrapping β€’ Sealioning
**🎭 Communication Intent**
Trolling β€’ Dismissive β€’ Manipulative β€’ Emotionally Reactive β€’ Constructive β€’ Unclear
**πŸ’­ Combined Analysis**
Psychological insights from the intersection of logical reasoning and emotional intent
"""
)
# Results section
with gr.Row():
with gr.Column():
fallacy_output = gr.Textbox(
label="πŸ” Fallacy Analysis",
lines=5,
interactive=False
)
intent_output = gr.Textbox(
label="🎭 Intent Analysis",
lines=5,
interactive=False
)
with gr.Column():
combined_output = gr.Textbox(
label="πŸ’­ Combined Analysis",
lines=5,
interactive=False
)
suggestion_output = gr.Textbox(
label="πŸ’‘ Improvement Suggestions",
lines=5,
interactive=False
)
# Example categories
gr.Markdown("## πŸ“š Try These Examples")
example_categories = {
"🧌 Trolling + Fallacies": [
"LOL you people are so triggered by everything, this is hilarious",
"Imagine being this upset about a simple comment, snowflakes gonna melt",
"You conservatives are all the same - completely ignorant about basic facts"
],
"🎭 Manipulation + Fallacies": [
"If you really loved me, you would support this decision without questioning it",
"After everything I've done for you, this is how you repay me?",
"You're making me feel terrible when you question my judgment like that"
],
"πŸŒ‹ Emotional + Fallacies": [
"I CAN'T BELIEVE you would say something so hurtful to me!!!",
"You always do this to me when I'm trying to help!",
"This is just like when you hurt me before - you never change!"
],
"🚫 Dismissive + Fallacies": [
"Whatever, I don't care about your opinion anyway",
"So you're saying we should just ignore all the real problems?",
"What about when you made the same mistake last year?"
],
"βœ… Healthy Communication": [
"I understand your concerns, but here's why I disagree based on the evidence",
"That's an interesting perspective. Can you help me understand your reasoning?",
"I appreciate you sharing your experience. My experience has been different because..."
]
}
# Create example buttons for each category
for category, examples in example_categories.items():
with gr.Accordion(f"{category}", open=False):
for example in examples:
example_btn = gr.Button(f"πŸ“ {example[:70]}{'...' if len(example) > 70 else ''}",
variant="secondary", size="sm")
example_btn.click(
lambda x=example: x,
outputs=message_input
)
# Information section
with gr.Accordion("πŸŽ“ How It Works", open=False):
gr.Markdown(
"""
## The Science Behind the Analysis
### πŸ” Fallacy Detection Model
- **Architecture:** DistilBERT-based classification
- **Training:** 3,200 carefully curated examples across 16 fallacy types
- **Performance:** 100% accuracy on test set with high confidence scores
- **Detects:** Logical errors, rhetorical manipulation, and argumentative fallacies
### 🎭 Intent Detection Model
- **Architecture:** Multi-label DistilBERT with custom classification head
- **Training:** 1,226 examples with multi-label annotations
- **Performance:** F1-score of 0.77 macro average (excellent for multi-label)
- **Detects:** Psychological intentions and communication motivations
### πŸ’­ Combined Analysis
Our system combines logical and psychological analysis to provide:
- **Deeper insights** into communication patterns
- **Context-aware interpretation** of fallacies within intent frameworks
- **Actionable suggestions** for more effective communication
- **Understanding of WHY** people communicate in certain ways
### πŸ“Š Performance Highlights
- **Fallacy Detection:** 100% accuracy, 98%+ average confidence
- **Intent Detection:** F1-scores from 0.85-0.99 per category
- **Combined Analysis:** Novel psychological insights from model intersection
### 🎯 Applications
- **Personal:** Improve relationship communication
- **Professional:** Better workplace dialogue
- **Educational:** Teach critical thinking and rhetoric
- **Research:** Study online discourse and communication patterns
"""
)
# Connect functions
analyze_btn.click(
fn=analyze_message,
inputs=[message_input],
outputs=[fallacy_output, intent_output, combined_output, suggestion_output]
)
clear_btn.click(
fn=lambda: ("", "", "", "", ""),
outputs=[message_input, fallacy_output, intent_output, combined_output, suggestion_output]
)
# Footer
gr.Markdown(
"""
---
**Ultimate Communication Analyzer** β€’ Built with ❀️ for better human communication
πŸ” [FallacyFinder Model](https://huggingface.co/SamanthaStorm/fallacyfinder) β€’ 🎭 [IntentAnalyzer Model](https://huggingface.co/SamanthaStorm/intentanalyzer) β€’ πŸ“š [Learn More About Fallacies](https://en.wikipedia.org/wiki/List_of_fallacies)
"""
)
return demo
# Launch the app
if __name__ == "__main__":
logger.info("πŸš€ Starting Ultimate Communication Analyzer...")
try:
demo = create_ultimate_interface()
demo.launch(
share=True,
server_name="0.0.0.0",
server_port=7860,
show_error=True
)
except Exception as e:
logger.error(f"❌ Failed to launch app: {e}")
print(f"Error: {e}")
print("\nMake sure both models are available:")
print("1. Fallacy model: SamanthaStorm/fallacyfinder (auto-downloaded)")
print("2. Intent model: SamanthaStorm/intentanalyzer (auto-downloaded)")
print("3. Or ensure 'intent_detection_model.pth' exists locally")
raise