File size: 5,118 Bytes
bbc509a
c6ce43e
 
 
bbc509a
 
 
c6d3db9
bbc509a
 
c6ce43e
 
bbc509a
 
c6ce43e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: Reframe Cognitive Reframing Assistant
emoji: 🧠
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.47.2
python_version: "3.10"
app_file: app.py
pinned: false
license: mit
hf_oauth: true
---

# 🧠 reframe: Cognitive Reframing Assistant

A tool inspired by CBT (Cognitive Behavioral Therapy) designed to help identify and reframe cognitive distortions.

## 🎯 Features

- **Educational Landing Page**: Learn about CBT and cognitive reframing
- **Interactive Chat**: Share thoughts and receive balanced perspectives
- **Pattern Recognition**: Identifies cognitive distortions in thinking patterns
- **Similar Situations**: Shows relatable examples with reframed perspectives
- **Learning Center**: Explore 13+ cognitive distortions with examples
- **Multilingual**: Support for English and Spanish
- **Privacy-First**: No message content stored; only pseudonymous usage counters
- **Agentic LLM**: Uses Hugging Face Inference API (required for chat)
- **Streaming Chat**: Incremental assistant messages for a smoother UX
- **Lightweight Memory**: Assistant considers the last N turns (default 6)

## πŸ“ Project Structure

```
re-frame-gradio/
β”œβ”€β”€ app.py                     # Main Gradio application
β”œβ”€β”€ requirements.txt           # Python dependencies
β”œβ”€β”€ README.md                  # This file
β”œβ”€β”€ cbt_knowledge/             # CBT knowledge base
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ distortions.py        # Cognitive distortions database
β”‚   └── reframing_tools.py    # Reframing strategies
β”œβ”€β”€ ui_components/             # UI components
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ landing.py            # Landing page
β”‚   └── learn.py              # Learning center
└── locales/                   # Translations (optional)
    β”œβ”€β”€ en.json               # English
    └── es.json               # Spanish
```

## 🧩 Cognitive Distortions Covered

1. **All-or-Nothing Thinking** - Black and white thinking
2. **Fortune Telling** - Negative predictions
3. **Catastrophizing** - Expecting the worst
4. **Mind Reading** - Assuming others' thoughts
5. **Mental Filter** - Focusing on negatives
6. **Should Statements** - Rigid rules
7. **Labeling** - Global negative labels
8. **Personalization** - Self-blame
9. **Emotional Reasoning** - Feelings as facts
10. **Discounting Positives** - Dismissing good things
11. **Jumping to Conclusions** - Assumptions without evidence
12. **Magnification/Minimization** - Distorting importance
13. **Overgeneralization** - Broad conclusions from single events


## 🀝 How It Works

1. **Share Your Thoughts**: Type what's on your mind in the chat
2. **Pattern Detection**: The system identifies thinking patterns
3. **Balanced Perspectives**: Get alternative ways to view the situation
4. **Similar Examples**: See how others reframed similar thoughts
5. **Learn More**: Explore specific distortions in the Learn tab

## ⚠️ Important Disclaimers

- This is intended to be a **helpful tool**, **NEVER** a replacement for professional therapy
- **NOT** intended for crisis situations or medical advice
- If you're in crisis, please contact emergency services or a crisis helpline
- No message content is stored. For quotas and observability, the app records per-user interaction counters using salted hashes and aggregated metrics (no raw usernames, emails, or IPs).

## πŸ” Login, Quotas, and Owner Controls

This Space supports "Login with Hugging Face" for stable and secure per-user quotas.

- The UI includes a Login button. Once logged in, the app receives an OAuth profile and derives a pseudonymous, salted user id.
- Per-user daily interactions are capped via `HF_AGENT_MAX_INTERACTIONS_PER_USER` (default: 12).
- An Owner tab is visible only to the Space owner.
- Environment variables:
  - `HUGGINGFACEHUB_API_TOKEN` β€” required: Inference API token (set as a Secret). `HF_TOKEN` is also accepted, but prefer this name.
  - `HF_AGENT_MAX_INTERACTIONS_PER_USER` β€” per-user daily cap (default: 12)
  - `USAGE_SALT` β€” required: random salt for hashing user identifiers
  - `AGENT_CALL_LOG_PATH` β€” path to persist counters (e.g., `/data/agent_calls.json`)
  - `APP_METRICS_PATH` β€” path to privacy-preserving metrics (e.g., `/data/app_metrics.json`)
  - `HF_HOME` β€” recommended: set to `/data/.huggingface` to persist cache across restarts

Notes:
- The app never stores raw usernames, emails, IPs, or message content.
- Configure persistent storage for those JSON files if you want counters to survive Space restarts. If using persistent storage, set `HF_HOME=/data/.huggingface` so model/cache downloads persist too.

### Owner-only Admin

Set the environment variable `OWNER_USER` to your Hugging Face username. The Owner tab will only be shown when the logged-in user matches this username.

- `OWNER_USER` β€” required to enable Owner tab (e.g., `carlos`)


## πŸ™ Acknowledgments

- Inspired by CBT principles
- Original prototype built with Google ADK 
- Built with Gradio and Hugging Face

## πŸ“„ License

MIT License - See LICENSE file for details

---