Spaces:
Runtime error
Runtime error
Commit ·
f0d0d19
0
Parent(s):
feat: initial release of curated portfolio app
Browse files- README.md +20 -0
- app.py +193 -0
- requirements.txt +4 -0
README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Argument Fallacy Mapper & Rhetoric Deconstructor
|
| 3 |
+
emoji: ⚖️
|
| 4 |
+
colorFrom: orange
|
| 5 |
+
colorTo: red
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.19.2
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Argument Fallacy Mapper & Rhetoric Deconstructor
|
| 13 |
+
|
| 14 |
+
A computational social science and rhetoric analysis application designed to identify and highlight standard logical fallacies (Ad Hominem, Strawman, False Dilemma, Slippery Slope, Appeal to Authority) in speeches, debates, or media editorials.
|
| 15 |
+
|
| 16 |
+
### Features
|
| 17 |
+
- **Logical Fallacy Regex Scanner**: Audits texts using fine-grained dictionary-based syntactic matchers.
|
| 18 |
+
- **Rhetorical Passage Highlight**: Interactively marks up fallacious sentences, offering details on hover.
|
| 19 |
+
- **Constructive Logic Guide**: Suggests concrete, scientific, and logical corrections to convert fallacious rhetoric into robust arguments.
|
| 20 |
+
- **Statistical Analytics**: Beautiful Plotly incidence charts.
|
app.py
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
| 3 |
+
import numpy as np
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
# Fallacy Lexicon containing keywords, regex, descriptions, and plain-English corrections
|
| 8 |
+
FALLACY_PATTERNS = [
|
| 9 |
+
{
|
| 10 |
+
"name": "Ad Hominem (Personal Attack)",
|
| 11 |
+
"patterns": [
|
| 12 |
+
r'\b(idiot|fool|stupid|corrupt|liar|cheat|incompetent|naive|hypocrite|disgrace)\b',
|
| 13 |
+
r'\b(you are|he is|she is) (unqualified|irrational|delusional)\b'
|
| 14 |
+
],
|
| 15 |
+
"description": "Attacking the character or motives of a person rather than addressing their actual argument.",
|
| 16 |
+
"correction": "Focus on the logic, evidence, or specific data points of the opponent's claim, not their character."
|
| 17 |
+
},
|
| 18 |
+
{
|
| 19 |
+
"name": "Slippery Slope (Extreme Projections)",
|
| 20 |
+
"patterns": [
|
| 21 |
+
r'\b(inevitably lead to|slippery slope|disaster will follow|will cause the end of|road to ruin|downhill from here|avalanche of)\b',
|
| 22 |
+
r'\bif we allow.*then everything.*will collapse\b'
|
| 23 |
+
],
|
| 24 |
+
"description": "Claiming that a relatively small initial step will inevitably lead to a chain of disastrous events without supporting evidence.",
|
| 25 |
+
"correction": "Provide empirical evidence showing a causal mechanism connecting the initial step to the final extreme outcome."
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"name": "False Dilemma (Either/Or)",
|
| 29 |
+
"patterns": [
|
| 30 |
+
r'\b(either we|either you|must choose between|no other choice|only two options|with us or against us|black or white)\b'
|
| 31 |
+
],
|
| 32 |
+
"description": "Presenting only two alternative states or choices when in fact multiple middle grounds or other options exist.",
|
| 33 |
+
"correction": "Acknowledge third-party options, compromise zones, or other collaborative solutions."
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"name": "Appeal to False Authority",
|
| 37 |
+
"patterns": [
|
| 38 |
+
r'\b(everyone knows|scientists agree that|as.*said once|common knowledge|unnamed sources say|trust me on this)\b'
|
| 39 |
+
],
|
| 40 |
+
"description": "Claiming a statement must be true because an authority figure or 'everyone' asserts it, without citing verified evidence.",
|
| 41 |
+
"correction": "Cite direct peer-reviewed academic studies, primary documents, or transparent data collections."
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"name": "Circular Reasoning (Begging the Question)",
|
| 45 |
+
"patterns": [
|
| 46 |
+
r'\b(because it is|obviously true since|by definition|self-evident|proved by the fact that it is)\b',
|
| 47 |
+
r'\b(.*) is true because (\1) is true\b'
|
| 48 |
+
],
|
| 49 |
+
"description": "An argument where the conclusion is already assumed in the premise, repeating the claim in different words.",
|
| 50 |
+
"correction": "Introduce independent external facts, logic, or observations to support the premise."
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
"name": "Strawman (Caricaturing Opponent)",
|
| 54 |
+
"patterns": [
|
| 55 |
+
r'\b(they want to destroy|their extreme goal|total ban on|completely wipe out|total elimination of|radical agenda to)\b'
|
| 56 |
+
],
|
| 57 |
+
"description": "Oversimplifying, exaggerating, or misrepresenting an opponent's argument to make it easier to attack.",
|
| 58 |
+
"correction": "State the opponent's argument in its strongest form (steelman) before attempting to refute it."
|
| 59 |
+
}
|
| 60 |
+
]
|
| 61 |
+
|
| 62 |
+
def deconstruct_rhetoric(text):
|
| 63 |
+
if not text.strip():
|
| 64 |
+
return None, "<div style='color: #ef4444;'>Please paste debate transcripts or editorials.</div>", ""
|
| 65 |
+
|
| 66 |
+
text_lower = text.lower()
|
| 67 |
+
highlighted_text = text
|
| 68 |
+
fallacies_found = []
|
| 69 |
+
|
| 70 |
+
# Track statistics
|
| 71 |
+
summary_counts = {item["name"]: 0 for item in FALLACY_PATTERNS}
|
| 72 |
+
|
| 73 |
+
# Process each pattern
|
| 74 |
+
for fallacy in FALLACY_PATTERNS:
|
| 75 |
+
name = fallacy["name"]
|
| 76 |
+
desc = fallacy["description"]
|
| 77 |
+
corr = fallacy["correction"]
|
| 78 |
+
|
| 79 |
+
for pat in fallacy["patterns"]:
|
| 80 |
+
matches = list(re.finditer(pat, text_lower, re.IGNORECASE))
|
| 81 |
+
if matches:
|
| 82 |
+
summary_counts[name] += len(matches)
|
| 83 |
+
for m in matches:
|
| 84 |
+
matched_str = m.group(0)
|
| 85 |
+
fallacies_found.append({
|
| 86 |
+
"Rhetorical Indicator": matched_str,
|
| 87 |
+
"Fallacy Category": name,
|
| 88 |
+
"Logic Description": desc,
|
| 89 |
+
"Constructive Correction": corr
|
| 90 |
+
})
|
| 91 |
+
|
| 92 |
+
# Generate highlighted HTML (using span wraps)
|
| 93 |
+
# To prevent nested replacement bugs, we replace with placeholders and substitute at the end
|
| 94 |
+
replacements = {}
|
| 95 |
+
for idx, item in enumerate(fallacies_found):
|
| 96 |
+
indicator = item["Rhetorical Indicator"]
|
| 97 |
+
placeholder = f"___PLACEHOLDER_{idx}___"
|
| 98 |
+
# Case insensitive substitution (first occurrence)
|
| 99 |
+
pattern = re.compile(r'\b' + re.escape(indicator) + r'\b', re.IGNORECASE)
|
| 100 |
+
highlighted_text, count = pattern.subn(placeholder, highlighted_text, 1)
|
| 101 |
+
if count > 0:
|
| 102 |
+
replacements[placeholder] = f'<span style="background-color: rgba(239, 68, 68, 0.25); border-bottom: 2px solid #ef4444; color: #f87171; padding: 2px 4px; border-radius: 4px; font-weight: 500;" title="{item["Fallacy Category"]}">{indicator}</span>'
|
| 103 |
+
|
| 104 |
+
for placeholder, span in replacements.items():
|
| 105 |
+
highlighted_text = highlighted_text.replace(placeholder, span)
|
| 106 |
+
|
| 107 |
+
# Generate stats Plotly Chart
|
| 108 |
+
df_counts = pd.DataFrame({
|
| 109 |
+
"Fallacy Category": list(summary_counts.keys()),
|
| 110 |
+
"Count": list(summary_counts.values())
|
| 111 |
+
})
|
| 112 |
+
|
| 113 |
+
fig = px.bar(
|
| 114 |
+
df_counts[df_counts["Count"] > 0],
|
| 115 |
+
x="Count",
|
| 116 |
+
y="Fallacy Category",
|
| 117 |
+
orientation="h",
|
| 118 |
+
color="Fallacy Category",
|
| 119 |
+
template="plotly_dark",
|
| 120 |
+
title="Detected Rhetorical Fallacy Counts"
|
| 121 |
+
)
|
| 122 |
+
fig.update_layout(showlegend=False, paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', font_family="Inter")
|
| 123 |
+
|
| 124 |
+
# Generate deconstructed warnings table
|
| 125 |
+
warnings_html = ""
|
| 126 |
+
if fallacies_found:
|
| 127 |
+
df_fallacies = pd.DataFrame(fallacies_found)
|
| 128 |
+
table_html = df_fallacies.to_html(classes="styled-table", index=False)
|
| 129 |
+
warnings_html = f"""
|
| 130 |
+
<style>
|
| 131 |
+
.styled-table {{ width: 100%; border-collapse: collapse; font-family: 'Inter', sans-serif; font-size: 13px; color: #cbd5e1; background: #0f172a; border-radius: 8px; overflow: hidden; border: 1px solid #334155; }}
|
| 132 |
+
.styled-table th {{ background: #1e293b; color: #94a3b8; font-weight: 600; padding: 10px; text-align: left; border-bottom: 2px solid #334155; }}
|
| 133 |
+
.styled-table td {{ padding: 8px 10px; border-bottom: 1px solid #1e293b; }}
|
| 134 |
+
.styled-table tr:hover td {{ background: #1e293b; }}
|
| 135 |
+
</style>
|
| 136 |
+
<div style="overflow-x: auto;">
|
| 137 |
+
{table_html}
|
| 138 |
+
</div>
|
| 139 |
+
"""
|
| 140 |
+
else:
|
| 141 |
+
warnings_html = "<div style='color: #10b981; font-weight: 600; padding: 1.5rem; background: rgba(16, 185, 129, 0.1); border-radius: 8px; border: 1px solid #10b981; text-align: center;'>🎉 Clean Argument Scorecard! No major fallacious connectors detected in this text.</div>"
|
| 142 |
+
|
| 143 |
+
highlighted_container = f"""
|
| 144 |
+
<div style="background: #0f172a; border: 1px solid #334155; border-radius: 8px; padding: 1.5rem; max-height: 400px; overflow-y: auto; color: #e2e8f0; line-height: 1.6; font-size: 14px;">
|
| 145 |
+
{highlighted_text}
|
| 146 |
+
</div>
|
| 147 |
+
"""
|
| 148 |
+
|
| 149 |
+
return fig, highlighted_container, warnings_html
|
| 150 |
+
|
| 151 |
+
# Premium styling
|
| 152 |
+
custom_css = """
|
| 153 |
+
body, .gradio-container { background-color: #0b0f19 !important; font-family: 'Inter', sans-serif !important; }
|
| 154 |
+
h1, h2, h3 { color: #f8fafc !important; }
|
| 155 |
+
.gr-button-primary { background: linear-gradient(135deg, #ef4444 0%, #b91c1c 100%) !important; border: none !important; }
|
| 156 |
+
"""
|
| 157 |
+
|
| 158 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="red", secondary_hue="slate"), css=custom_css) as demo:
|
| 159 |
+
gr.HTML("""
|
| 160 |
+
<div style="text-align: center; margin-bottom: 2rem; border-bottom: 1px solid #1e293b; padding-bottom: 1.5rem;">
|
| 161 |
+
<h1 style="font-size: 2.2rem; font-weight: 800; background: linear-gradient(135deg, #f87171 0%, #ef4444 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">⚖️ Argument Fallacy Mapper & Rhetoric Deconstructor</h1>
|
| 162 |
+
<p style="color: #94a3b8; font-size: 1.1rem; margin-top: 0.5rem;">Scan speeches, debate transcripts, and editorials for logical fallacies and receive constructive reasoning suggestions.</p>
|
| 163 |
+
</div>
|
| 164 |
+
""")
|
| 165 |
+
|
| 166 |
+
with gr.Row():
|
| 167 |
+
with gr.Column(scale=1):
|
| 168 |
+
gr.Markdown("### 📜 Paste Speech / Debate Transcript")
|
| 169 |
+
text_input = gr.Textbox(
|
| 170 |
+
label="Argument Text",
|
| 171 |
+
placeholder="Paste speech copy here...",
|
| 172 |
+
lines=12
|
| 173 |
+
)
|
| 174 |
+
audit_btn = gr.Button("🔍 Deconstruct Arguments", variant="primary")
|
| 175 |
+
|
| 176 |
+
with gr.Column(scale=1):
|
| 177 |
+
gr.Markdown("### 📊 Fallacy Incidence Breakdown")
|
| 178 |
+
chart_output = gr.Plot()
|
| 179 |
+
|
| 180 |
+
gr.Markdown("### 🎨 Annotated Rhetorical Passage Visualizer")
|
| 181 |
+
highlighted_output = gr.HTML("<div style='color: #64748b;'>Submit text to analyze and highlight fallacious reasoning.</div>")
|
| 182 |
+
|
| 183 |
+
gr.Markdown("### ⚡ Deconstructed Logic & Constructive Recommendations")
|
| 184 |
+
warnings_output = gr.HTML("<div style='color: #64748b;'>Scorecard grid will render here.</div>")
|
| 185 |
+
|
| 186 |
+
audit_btn.click(
|
| 187 |
+
fn=deconstruct_rhetoric,
|
| 188 |
+
inputs=[text_input],
|
| 189 |
+
outputs=[chart_output, highlighted_output, warnings_output]
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
if __name__ == "__main__":
|
| 193 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
pandas
|
| 3 |
+
numpy
|
| 4 |
+
plotly
|