File size: 10,691 Bytes
80c2cb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a991b2
80c2cb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# ============================================
# app.py - FastAPI + Gradio Interface
# ============================================
"""
Cognitive Distortion Detection API
===================================
Provides distortion detection with both API and web interface
"""

from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import gradio as gr
from typing import Optional, List, Dict
import uvicorn

# ============================================
# CONFIGURATION
# ============================================

MODEL_NAME = "YureiYuri/empathist"

# ============================================
# LOAD MODEL
# ============================================

print("πŸ€– Loading cognitive distortion detector...")
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
model.eval()

# Label mappings
id2label = {
    0: "overgeneralization",
    1: "catastrophizing",
    2: "black_and_white",
    3: "self_blame",
    4: "mind_reading"
}

DESCRIPTIONS = {
    "overgeneralization": "Making broad interpretations from single events using words like 'always', 'never', 'everyone'",
    "catastrophizing": "Expecting the worst possible outcome using words like 'terrible', 'disaster', 'awful'",
    "black_and_white": "Seeing things in absolute terms with no middle ground",
    "self_blame": "Taking excessive responsibility for things outside your control",
    "mind_reading": "Assuming you know what others are thinking without evidence"
}

print("βœ… Model loaded successfully!")

# ============================================
# PYDANTIC MODELS
# ============================================

class DetectionRequest(BaseModel):
    text: str
    threshold: Optional[float] = 0.5

class DistortionResult(BaseModel):
    distortion: str
    confidence: float
    description: str

class DetectionResponse(BaseModel):
    text: str
    distortions: List[DistortionResult]
    has_distortions: bool
    summary: str

# ============================================
# FASTAPI APP
# ============================================

app = FastAPI(
    title="Cognitive Distortion Detector",
    description="CBT-based cognitive distortion detection API",
    version="1.0.0"
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ============================================
# HELPER FUNCTIONS
# ============================================

def detect_distortions(text: str, threshold: float = 0.5) -> Dict:
    """Detect cognitive distortions in text"""
    if not text.strip():
        return {
            "text": text,
            "distortions": [],
            "has_distortions": False,
            "summary": "No text provided"
        }
    
    # Tokenize
    inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128, padding=True)
    
    # Predict
    with torch.no_grad():
        outputs = model(**inputs)
        probabilities = torch.sigmoid(outputs.logits).squeeze()
    
    # Extract distortions above threshold
    distortions = []
    for idx, prob in enumerate(probabilities):
        if prob > threshold:
            label = id2label[idx]
            distortions.append({
                "distortion": label,
                "confidence": round(prob.item(), 4),
                "description": DESCRIPTIONS[label]
            })
    
    # Sort by confidence
    distortions.sort(key=lambda x: x["confidence"], reverse=True)
    
    # Create summary
    if distortions:
        summary = f"Detected {len(distortions)} distortion(s): " + ", ".join([d["distortion"] for d in distortions])
    else:
        summary = "No significant cognitive distortions detected"
    
    return {
        "text": text,
        "distortions": distortions,
        "has_distortions": len(distortions) > 0,
        "summary": summary
    }

# ============================================
# API ENDPOINTS
# ============================================

@app.get("/")
async def root():
    """Health check endpoint"""
    return {
        "status": "online",
        "service": "Cognitive Distortion Detector",
        "version": "1.0.0",
        "model": MODEL_NAME
    }

@app.post("/detect", response_model=DetectionResponse)
async def detect_endpoint(request: DetectionRequest):
    """
    Detect cognitive distortions in text
    
    Args:
        text: Input text to analyze
        threshold: Confidence threshold (0.0-1.0), default 0.5
    
    Returns:
        Detection results with distortions found
    """
    try:
        result = detect_distortions(request.text, request.threshold)
        return DetectionResponse(**result)
    
    except Exception as e:
        print(f"❌ Detection error: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Detection failed: {str(e)}")

@app.get("/distortions")
async def list_distortions():
    """List all detectable distortion types with descriptions"""
    return {
        "distortions": [
            {"name": label, "description": DESCRIPTIONS[label]}
            for label in id2label.values()
        ]
    }

# ============================================
# GRADIO INTERFACE
# ============================================

def predict_gradio(text: str, threshold: float = 0.5):
    """Gradio prediction function"""
    if not text.strip():
        return "Please enter some text to analyze.", ""
    
    result = detect_distortions(text, threshold)
    
    # Format summary
    if not result["distortions"]:
        summary = "βœ… No significant cognitive distortions detected!"
        html = "<div style='background: #c8e6c9; padding: 20px; border-radius: 8px; border-left: 4px solid #4caf50;'><h3 style='color: #2e7d32; margin: 0;'>βœ… No significant cognitive distortions detected!</h3></div>"
    else:
        summary_lines = []
        html = "<div style='margin-top: 20px;'>"
        
        for d in result["distortions"]:
            percentage = d["confidence"] * 100
            summary_lines.append(f"βœ“ **{d['distortion'].replace('_', ' ').title()}** ({percentage:.1f}%)")
            
            color = "#ff6b6b" if percentage > 70 else "#ffa07a" if percentage > 50 else "#ffcc80"
            html += f"""
            <div style='background: {color}; padding: 15px; margin: 10px 0; border-radius: 8px; border-left: 4px solid #d32f2f;'>
                <h3 style='margin: 0 0 5px 0; color: #1a1a1a;'>🚨 {d['distortion'].replace('_', ' ').title()}</h3>
                <p style='margin: 5px 0; color: #333; font-size: 14px;'>{d['description']}</p>
                <p style='margin: 5px 0; font-weight: bold; color: #1a1a1a;'>Confidence: {percentage:.1f}%</p>
            </div>
            """
        
        summary = "\n".join(summary_lines)
        html += "</div>"
    
    return summary, html

# Example texts
examples = [
    ["I always mess everything up. This is a disaster!", 0.5],
    ["Everyone thinks I'm incompetent. I'll never succeed.", 0.5],
    ["It's all my fault that the project failed.", 0.5],
    ["They must think I'm stupid for asking that question.", 0.5],
    ["I'm having a challenging day, but I can work through it.", 0.5],
]

with gr.Blocks(theme=gr.themes.Soft(), title="CBT Distortion Detector") as demo:
    gr.Markdown(
        """
        # 🧠 CBT Cognitive Distortion Detector
        
        This tool analyzes text for common cognitive distortions based on Cognitive Behavioral Therapy (CBT) principles.
        It detects patterns like overgeneralization, catastrophizing, black-and-white thinking, self-blame, and mind-reading.
        
        **API Available**: Use `/detect` endpoint for programmatic access. [API Docs](/docs)
        
        **Note**: This is an educational tool and should not replace professional mental health support.
        """
    )
    
    with gr.Row():
        with gr.Column(scale=2):
            text_input = gr.Textbox(
                label="Enter your text",
                placeholder="Type or paste text here to analyze for cognitive distortions...",
                lines=5
            )
            
            threshold_slider = gr.Slider(
                minimum=0.1,
                maximum=0.9,
                value=0.5,
                step=0.05,
                label="Detection Threshold (higher = stricter)",
                info="Adjust sensitivity of detection"
            )
            
            analyze_btn = gr.Button("πŸ” Analyze Text", variant="primary", size="lg")
        
        with gr.Column(scale=1):
            gr.Markdown("### πŸ“‹ Detected Distortions")
            summary_output = gr.Markdown(label="Summary")
    
    with gr.Row():
        detailed_output = gr.HTML(label="Detailed Results")
    
    gr.Markdown("### πŸ’‘ Try These Examples")
    gr.Examples(
        examples=examples,
        inputs=[text_input, threshold_slider],
        outputs=[summary_output, detailed_output],
        fn=predict_gradio,
        cache_examples=False
    )
    
    gr.Markdown(
        """
        ---
        ### πŸ“š About Cognitive Distortions
        
        - **Overgeneralization**: Drawing broad conclusions from limited evidence
        - **Catastrophizing**: Expecting the worst-case scenario
        - **Black & White Thinking**: Viewing situations in extremes with no middle ground
        - **Self-Blame**: Taking responsibility for things beyond your control
        - **Mind Reading**: Assuming you know what others think without evidence
        
        ### πŸ”Œ API Usage
        
        ```python
        import requests
        
        response = requests.post("https://your-space.hf.space/detect", 
            json={"text": "I always mess everything up", "threshold": 0.5})
        
        print(response.json())
        ```
        
        **Model**: [YureiYuri/Empahist](https://huggingface.co/YureiYuri/Empahist)
        """
    )
    
    analyze_btn.click(
        fn=predict_gradio,
        inputs=[text_input, threshold_slider],
        outputs=[summary_output, detailed_output]
    )

# ============================================
# MOUNT GRADIO TO FASTAPI
# ============================================

app = gr.mount_gradio_app(app, demo, path="/")

# ============================================
# RUN SERVER
# ============================================

if __name__ == "__main__":
    print("\nπŸš€ Starting Cognitive Distortion Detector...")
    uvicorn.run(app, host="0.0.0.0", port=7860)