Update README.md
Browse files
README.md
CHANGED
@@ -15,7 +15,7 @@ language:
|
|
15 |
|
16 |
# Model Card for Model ID
|
17 |
|
18 |
-
This is a
|
19 |
His performance for now is 85%-100% in detecting sophism , and 85%-100% for detectiong cognitive bias
|
20 |
|
21 |
It was trained with a custom dataset of 14k lines
|
@@ -52,202 +52,13 @@ PUBLIC API COMING SOON
|
|
52 |
|
53 |
### Direct Use
|
54 |
|
55 |
-
|
56 |
-
import torch
|
57 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
58 |
-
from peft import PeftModel
|
59 |
-
import re
|
60 |
-
|
61 |
-
class RationalityDebugger:
|
62 |
-
def __init__(self, base_model="mistralai/Mistral-7B-v0.1", lora_model="Artvv/rationality-debugger-v1.0"):
|
63 |
-
"""
|
64 |
-
Initialize the cognitive bias and logical fallacy detector.
|
65 |
-
|
66 |
-
Args:
|
67 |
-
base_model: Base model from Hugging Face
|
68 |
-
lora_model: LoRA adapters for rationality analysis
|
69 |
-
"""
|
70 |
-
print(f"Loading base model: {base_model}")
|
71 |
-
self.tokenizer = AutoTokenizer.from_pretrained(base_model)
|
72 |
-
|
73 |
-
# Options for optimized loading
|
74 |
-
model_kwargs = {
|
75 |
-
"torch_dtype": torch.float16,
|
76 |
-
"device_map": "auto",
|
77 |
-
"low_cpu_mem_usage": True
|
78 |
-
}
|
79 |
-
|
80 |
-
# Try first with 4-bit quantization to save memory
|
81 |
-
try:
|
82 |
-
from transformers import BitsAndBytesConfig
|
83 |
-
quantization_config = BitsAndBytesConfig(
|
84 |
-
load_in_4bit=True,
|
85 |
-
bnb_4bit_compute_dtype=torch.float16,
|
86 |
-
bnb_4bit_use_double_quant=True
|
87 |
-
)
|
88 |
-
model_kwargs["quantization_config"] = quantization_config
|
89 |
-
self.base_model = AutoModelForCausalLM.from_pretrained(base_model, **model_kwargs)
|
90 |
-
except:
|
91 |
-
# Fallback if bitsandbytes is not available
|
92 |
-
print("4-bit quantization not available, using standard loading...")
|
93 |
-
self.base_model = AutoModelForCausalLM.from_pretrained(base_model, **model_kwargs)
|
94 |
-
|
95 |
-
print(f"Applying LoRA adapters: {lora_model}")
|
96 |
-
self.model = PeftModel.from_pretrained(self.base_model, lora_model)
|
97 |
-
self.model.eval() # Evaluation mode
|
98 |
-
|
99 |
-
self.prompt_template = """
|
100 |
-
Analyze the following argument and identify any logical fallacies or cognitive biases:
|
101 |
-
|
102 |
-
{text}
|
103 |
-
|
104 |
-
###OUTPUT FORMAT
|
105 |
-
[Argument] Valid/Invalid
|
106 |
-
→ If Valid: Type: [ANALYTICAL / INDUCTIVE / ABDUCTIVE]
|
107 |
-
[Sophisms] Yes/No
|
108 |
-
→ If Yes: Which: [List detected fallacies]
|
109 |
-
→ Extract(s): [Provide exact snippet(s)]
|
110 |
-
[Biases] Yes/No
|
111 |
-
→ If Yes: Which: [List detected biases]
|
112 |
-
→ Extract(s): [Provide exact snippet(s)]
|
113 |
-
|
114 |
-
[Short explanation]
|
115 |
-
"""
|
116 |
-
|
117 |
-
def analyze(self, text, max_new_tokens=200, temperature=0.1):
|
118 |
-
"""
|
119 |
-
Analyze text to detect cognitive biases and logical fallacies.
|
120 |
-
|
121 |
-
Args:
|
122 |
-
text: Text to analyze
|
123 |
-
max_new_tokens: Maximum number of new tokens to generate
|
124 |
-
temperature: Temperature for generation (lower = more deterministic)
|
125 |
-
|
126 |
-
Returns:
|
127 |
-
dict: Structured analysis result and raw text
|
128 |
-
"""
|
129 |
-
prompt = self.prompt_template.format(text=text)
|
130 |
-
|
131 |
-
inputs = self.tokenizer(prompt, return_tensors="pt").to(self.model.device)
|
132 |
-
|
133 |
-
with torch.no_grad():
|
134 |
-
outputs = self.model.generate(
|
135 |
-
**inputs,
|
136 |
-
max_new_tokens=max_new_tokens,
|
137 |
-
temperature=temperature,
|
138 |
-
top_p=0.9,
|
139 |
-
do_sample=temperature > 0
|
140 |
-
)
|
141 |
-
|
142 |
-
# Extract only the generated part (not the prompt)
|
143 |
-
generated_text = self.tokenizer.decode(
|
144 |
-
outputs[0][inputs.input_ids.shape[1]:],
|
145 |
-
skip_special_tokens=True
|
146 |
-
)
|
147 |
-
|
148 |
-
# Parse the response to extract the structure
|
149 |
-
result = self._parse_response(generated_text)
|
150 |
-
|
151 |
-
return {
|
152 |
-
"raw_text": generated_text,
|
153 |
-
"structured": result
|
154 |
-
}
|
155 |
-
|
156 |
-
def _parse_response(self, text):
|
157 |
-
"""Parse the model's response to extract structured information"""
|
158 |
-
result = {
|
159 |
-
"argument_valid": None,
|
160 |
-
"argument_type": None,
|
161 |
-
"has_sophisms": None,
|
162 |
-
"detected_sophisms": [],
|
163 |
-
"has_biases": None,
|
164 |
-
"detected_biases": [],
|
165 |
-
"too_short": False,
|
166 |
-
"explanation": ""
|
167 |
-
}
|
168 |
-
|
169 |
-
# Simple parsing example - adapt as needed
|
170 |
-
text_lower = text.lower()
|
171 |
-
|
172 |
-
# Argument validity detection
|
173 |
-
if "valid argument" in text_lower or "[argument] valid" in text_lower:
|
174 |
-
result["argument_valid"] = True
|
175 |
-
elif "invalid argument" in text_lower or "[argument] invalid" in text_lower:
|
176 |
-
result["argument_valid"] = False
|
177 |
-
|
178 |
-
# Argument type detection
|
179 |
-
for arg_type in ["ANALYTICAL", "INDUCTIVE", "ABDUCTIVE"]:
|
180 |
-
if arg_type.lower() in text_lower:
|
181 |
-
result["argument_type"] = arg_type
|
182 |
-
|
183 |
-
# Fallacy detection
|
184 |
-
sophism_keywords = ["ad hominem", "straw man", "red herring", "false dilemma",
|
185 |
-
"slippery slope", "post hoc", "circular reasoning"]
|
186 |
-
|
187 |
-
for sophism in sophism_keywords:
|
188 |
-
if sophism in text_lower:
|
189 |
-
result["detected_sophisms"].append(sophism)
|
190 |
-
|
191 |
-
result["has_sophisms"] = len(result["detected_sophisms"]) > 0
|
192 |
-
|
193 |
-
# Cognitive bias detection
|
194 |
-
bias_keywords = ["confirmation bias", "availability bias", "anchoring bias",
|
195 |
-
"hindsight bias", "halo effect", "dunning-kruger"]
|
196 |
-
|
197 |
-
for bias in bias_keywords:
|
198 |
-
if bias in text_lower:
|
199 |
-
result["detected_biases"].append(bias)
|
200 |
-
|
201 |
-
result["has_biases"] = len(result["detected_biases"]) > 0
|
202 |
-
|
203 |
-
# Explanation
|
204 |
-
explanation_match = re.search(r"\[Short explanation\](.*?)(?=$|\[)", text, re.DOTALL)
|
205 |
-
if explanation_match:
|
206 |
-
result["explanation"] = explanation_match.group(1).strip()
|
207 |
-
else:
|
208 |
-
# If no explanation tag, take the whole text
|
209 |
-
result["explanation"] = text
|
210 |
-
|
211 |
-
return result
|
212 |
-
|
213 |
-
|
214 |
-
# --- Usage example ---
|
215 |
-
if __name__ == "__main__":
|
216 |
-
# Create the analyzer
|
217 |
-
analyzer = RationalityDebugger(
|
218 |
-
base_model="mistralai/Mistral-7B-v0.1",
|
219 |
-
lora_model="Artvv/rationality-debugger-v1.0"
|
220 |
-
)
|
221 |
-
|
222 |
-
# Analysis example
|
223 |
-
argument = """
|
224 |
-
All birds can fly. Penguins are birds. Therefore, penguins can fly.
|
225 |
-
"""
|
226 |
-
|
227 |
-
result = analyzer.analyze(argument)
|
228 |
-
|
229 |
-
# Display raw result
|
230 |
-
print("\n=== RAW ANALYSIS ===")
|
231 |
-
print(result["raw_text"])
|
232 |
-
|
233 |
-
# Display structured result
|
234 |
-
print("\n=== STRUCTURED ANALYSIS ===")
|
235 |
-
print(f"Valid argument: {result['structured']['argument_valid']}")
|
236 |
-
|
237 |
-
if result["structured"]["detected_sophisms"]:
|
238 |
-
print("\nDetected fallacies:")
|
239 |
-
for sophism in result["structured"]["detected_sophisms"]:
|
240 |
-
print(f"- {sophism}")
|
241 |
-
|
242 |
-
if result["structured"]["detected_biases"]:
|
243 |
-
print("\nDetected cognitive biases:")
|
244 |
-
for bias in result["structured"]["detected_biases"]:
|
245 |
-
print(f"- {bias}")
|
246 |
-
|
247 |
-
print("\nExplanation:")
|
248 |
-
print(result["structured"]["explanation"])
|
249 |
-
```
|
250 |
|
|
|
|
|
|
|
|
|
|
|
251 |
### Out-of-Scope Use
|
252 |
|
253 |
It is not intended to harass anyone or being rude
|
|
|
15 |
|
16 |
# Model Card for Model ID
|
17 |
|
18 |
+
This is a fine-tuned model specifically dedicated to the identification of sophism and cognitive bias
|
19 |
His performance for now is 85%-100% in detecting sophism , and 85%-100% for detectiong cognitive bias
|
20 |
|
21 |
It was trained with a custom dataset of 14k lines
|
|
|
52 |
|
53 |
### Direct Use
|
54 |
|
55 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
messages = [
|
58 |
+
{"role": "user", "content": "All birds can fly. Penguins are birds. Therefore, penguins can fly"},
|
59 |
+
]
|
60 |
+
pipe = pipeline("text-generation", model="Artvv/rationality-debugger-v1.0")
|
61 |
+
pipe(messages)
|
62 |
### Out-of-Scope Use
|
63 |
|
64 |
It is not intended to harass anyone or being rude
|