Cognitive Reframer is a Python-based application designed to assist users in reframing thoughts, leveraging speech recognition and a user-friendly interface. The project is modular, with clear separation between API, speech processing, reframing logic, and UI components.
This app provides a web UI (Streamlit) and a backend API (FastAPI) for cognitive reframing. It uses:
- FastAPI for backend endpoints
- Streamlit for the user interface
- Whisper for speech-to-text
- Ollama (open-source LLM) for reframing logic
- macOS TTS for speech output
- Speech Recognition: Uses macOS default speech APIs for capturing and processing user input.
- Reframing Engine: Core logic to help users reframe negative or unproductive thoughts.
- API Layer: Exposes reframing and speech functionalities via an internal API.
- User Interface: Simple UI for interacting with the reframer and speech modules.
- User enters or records a thought in the UI.
- The thought is sent to the FastAPI backend (
/reframe
endpoint). - The backend uses Ollama LLM to:
- Detect cognitive distortion
- Generate a narrative reframe
- Suggest a Socratic question
- Propose an action plan
- Results are displayed in the UI and can be read aloud using macOS TTS.
pyproject.toml # Project metadata and dependencies
requirements.txt # Python dependencies
uv.lock # Lock file for dependencies
README.md # Project documentation
app/
app_api.py # API layer for the application
Mac_speech_default.py # macOS speech recognition integration
reframer.py # Core reframing logic
ui.py # User interface logic
__pycache__/ # Compiled Python files
## Key Dependencies
- fastapi
- streamlit
- openai-whisper
- ollama
- sounddevice, wavio
- requests
- Clone the repository:
git clone https://github.com/abhishek085/cognitive_reframer.git cd cognitive_reframer
- Install dependencies:
Or use uv for faster installs:
pip install -r requirements.txt
uv pip install -r requirements.txt
uvicorn app.app_api:app --reload --port 8090
streamlit run app/ui.py
- Type or record a thought
- Click "Reframe Thought" to get results
Use the following sample inputs to test the reframing logic of the app:
Sample Input:
I always mess things up.
I'm not good enough.
This will never work.
Expected Output (Reframed):
{
"distortion": "All-or-nothing thinking",
"narrative": "Everyone makes mistakes sometimes. I can learn and improve.",
"socratic_question": "What evidence do I have that I always fail?",
"action_plan": "Reflect on past successes and try a new approach."
}
## Modules
- **app_api.py**: Handles API requests and routes them to the appropriate module.
- **Mac_speech_default.py**: Integrates with macOS speech APIs for voice input.
- **reframer.py**: Contains the logic for reframing thoughts and generating responses.
- **ui.py**: Manages the user interface, connecting user actions to backend logic.
## API Example
POST `/reframe`
```json
{
"text": "I always mess things up."
}
Response:
{
"distortion": "All-or-nothing thinking",
"narrative": "Everyone makes mistakes sometimes. I can learn and improve.",
"socratic_question": "What evidence do I have that I always fail?",
"action_plan": "Reflect on past successes and try a new approach."
}
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch
). - Make your changes and commit them.
- Push to your branch and open a pull request.
This project is licensed under the MIT License.
For questions or support, open an issue on GitHub or contact the repository owner.