Skip to content

ZephyrNULL/ai-emotional-support

Repository files navigation

🧠 Emotional Support AI - Anonymous Mental Health Assistant

An advanced AI-powered emotional support system providing 24/7 anonymous, compassionate conversations to help users navigate life's challenges. Built with cutting-edge AI models and modern web technologies.


🌟 Features

  • AI-Powered Conversations: Real-time emotional analysis and empathetic responses powered by Google Gemini, RoBERTa, and DialoGPT
  • Emotion Detection: Advanced NLP to understand user emotions and feelings in real-time
  • Crisis Detection: Intelligent crisis keyword detection with immediate resources and hotline recommendations
  • Mental Wellness Tools:
    • 🎡 Music Suggestions (curated calming playlists)
    • 🌬️ Breathing Exercises (guided relaxation techniques)
    • 🧘 Mental Exercises (cognitive reframing & mindfulness)
    • πŸ’‘ Mood Insights (emotional pattern analysis)
    • πŸ‘¨β€βš•οΈ Professional Help (connect with licensed therapists)
  • Anonymous & Confidential: No user accounts or personal data collection required
  • Session Management: Conversation history per session with emotion tracking
  • Rate Limiting: Built-in API rate limiting to prevent abuse
  • Security: XSS protection, CORS configuration, input sanitization

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         FRONTEND (Next.js)                       β”‚
β”‚  β”œβ”€ Chat Interface (React Components)                            β”‚
β”‚  β”œβ”€ Real-time Message Display                                    β”‚
β”‚  β”œβ”€ Feature Toggle UI (Music, Breathing, etc.)                   β”‚
β”‚  └─ Emotion Display & Analytics                                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                       β”‚ HTTPS/HTTP
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     NGINX (Reverse Proxy)                        β”‚
β”‚  β”œβ”€ SSL/TLS Termination                                          β”‚
β”‚  β”œβ”€ Request Routing (frontend β†’ port 3000, backend β†’ port 5000)β”‚
β”‚  β”œβ”€ Rate Limiting                                                β”‚
β”‚  └─ Security Headers                                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                       β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚                             β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  BACKEND (Flask) β”‚    β”‚  AUXILIARY SERVICES β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€    β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ API Routes:      β”‚    β”‚ β€’ PostgreSQL        β”‚
β”‚ β€’ /api/chat      β”‚    β”‚ β€’ Redis             β”‚
β”‚ β€’ /api/analyze   β”‚    β”‚ β€’ Hugging Face      β”‚
β”‚ β€’ /api/health    β”‚    β”‚ β€’ Google Gemini     β”‚
β”‚ β€’ /api/crisis... β”‚    β”‚                     β”‚
β”‚                  β”‚    β”‚                     β”‚
β”‚ Services:        β”‚    β”‚                     β”‚
β”‚ β€’ ChatService    β”‚    β”‚                     β”‚
β”‚ β€’ Middleware     β”‚    β”‚                     β”‚
β”‚ β€’ Error Handler  β”‚    β”‚                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚
        └─── ML Models ───┬──────────────────┐
                          β”‚                  β”‚
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚ Emotion Detector β”‚  β”‚ Empathy Refiner    β”‚
              β”‚ (RoBERTa)        β”‚  β”‚ (DialoGPT)         β”‚
              β”‚ - 27 emotions    β”‚  β”‚ - Enhanced empathy β”‚
              β”‚ - Intensity      β”‚  β”‚ - Better responses β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                          β”‚
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚  Crisis Detector     β”‚
              β”‚  (Pattern Matching)  β”‚
              β”‚  - Hotline detect    β”‚
              β”‚  - Resources         β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ› οΈ Tech Stack

Frontend

  • Next.js 14.0 - React framework with SSR & static generation
  • TypeScript - Type-safe JavaScript
  • Tailwind CSS - Utility-first CSS framework
  • Lucide React - Beautiful SVG icons
  • Axios - HTTP client for API communication
  • Framer Motion - Smooth animations
  • Chart.js - Data visualization

Backend

  • Flask 2.3 - Lightweight Python web framework
  • Flask-CORS - Cross-origin resource sharing
  • Flask-Limiter - Rate limiting
  • Transformers (Hugging Face) - Pre-trained ML models
    • RoBERTa - Emotion detection (27 emotion classes)
    • DialoGPT - Empathetic dialogue generation
  • Google Gemini API - Advanced LLM for response generation
  • PyTorch - Deep learning framework
  • Pydantic - Data validation
  • SQLAlchemy - ORM for database operations

Infrastructure

  • Docker & Docker Compose - Containerization
  • PostgreSQL 15 - Relational database
  • Redis 7 - Session storage & caching
  • Nginx - Reverse proxy & web server
  • Gunicorn - WSGI HTTP server for Flask

πŸ“Š Data Flow

  1. User sends message β†’ Frontend (Next.js)
  2. Frontend sends to Flask API β†’ /api/chat endpoint
  3. Backend processes message:
    • Emotion Detection (RoBERTa)
    • Crisis Detection (pattern matching)
    • Gemini API (generate initial response)
    • Empathy Refiner (DialoGPT enhancement)
  4. Response returned with metadata:
    • Response text
    • Detected emotions
    • Crisis status
    • Session ID
  5. Frontend displays in chat interface with emotion indicators

πŸš€ Quick Start (Local Development)

Prerequisites

  • Node.js (v18+)
  • Python 3.10+
  • Git

1. Backend Setup

The backend handles the AI routing and PyTorch ML models.

# Navigate to project root
cd ai-emotional-support

# Create and activate virtual environment
python -m venv .venv
source .venv/bin/activate  # On Windows use: .venv\Scripts\activate

# Install dependencies
pip install -r backend/requirements.txt

# Create .env and add your Gemini key
cp .env.example .env

# Run the backend server
python backend/wsgi.py

2. Frontend Setup

The frontend is a Next.js 14 application.

# Open a new terminal and navigate to frontend
cd ai-emotional-support/frontend

# Install dependencies
npm install

# Run the development server
npm run dev -- -p 3001

Access the application at: http://localhost:3001

Environment Variables

# Required for core AI functionality
GEMINI_API_KEY=your_google_gemini_api_key

# Optional
FLASK_ENV=development

🌐 Deployment to Linode

This project includes automated Bash scripts to completely provision and deploy a production-ready environment (Nginx, SSL, PM2, systemd) to a fresh Linode instance.

1. Provision the Server (Run Once)

Execute setup_linode.sh on your fresh Linode server to install Python, Node.js, Nginx, and all system dependencies.

# SSH into your Linode
ssh root@your-linode-ip

# Run the setup script
git clone https://github.com/xyrenExo/ai-emotional-support.git
cd ai-emotional-support

chmod +x setup_linode.sh
./setup_linode.sh

2. Deploy the Application

Whenever you push new code or want to restart the production services, use the deploy.sh script. This handles building the Next.js app and restarting the Python backend cleanly.

chmod +x deploy.sh
./deploy.sh

SSL/TLS and Nginx

  • The deployment scripts automatically configure Nginx as a reverse proxy.
  • Frontend binds to localhost:3000 internally, while the backend binds to localhost:5000.
  • Certbot handles automatic SSL generation if a custom domain is configured.

πŸ› Troubleshooting

Build fails with "dockerfile parse error"

  • Cause: Git merge conflict markers in Dockerfile
  • Solution: Run git status and resolve conflicts

Chat not responding

  • Cause: GEMINI_API_KEY not set or invalid
  • Solution: Verify .env file has valid API key

Database connection error

  • Cause: PostgreSQL not running or credentials wrong
  • Solution: Check docker-compose logs postgres

Frontend can't reach backend

  • Cause: CORS misconfiguration
  • Solution: Verify ALLOWED_ORIGINS includes frontend hostname

Models not downloading

  • Cause: Internet timeout during Docker build
  • Solution: Run docker-compose build --no-cache again

πŸ“ˆ Performance & Monitoring

Metrics to Track

  • Response time: /api/chat should return in < 5 seconds
  • Error rate: Monitor HTTP 500 responses
  • Model inference time: Logged in docker-compose logs backend
  • Database query time: Optimizable with indexing

Logs

# View all logs
docker-compose logs

# Follow backend logs
docker-compose logs -f backend

# Follow frontend logs
docker-compose logs -f frontend

πŸ”’ Security Considerations

βœ… Implemented

  • HTTPS/TLS encryption
  • CORS whitelisting
  • Rate limiting (per IP)
  • Input sanitization & validation
  • XSS protection
  • CSRF protection via session tokens
  • Security headers (X-Frame-Options, X-Content-Type-Options)

⚠️ To Implement (Production)

  • User authentication
  • Database encryption at rest
  • API key rotation
  • Audit logging
  • Penetration testing
  • DDoS protection (Cloudflare, AWS Shield)

πŸ“š File Structure

emotional-support-ai/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”‚   β”œβ”€β”€ routes.py          # API endpoints
β”‚   β”‚   β”‚   └── middleware.py      # Security & CORS
β”‚   β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”‚   β”œβ”€β”€ emotion_model.py   # RoBERTa emotion detection
β”‚   β”‚   β”‚   β”œβ”€β”€ crisis_detector.py # Crisis keyword detection
β”‚   β”‚   β”‚   β”œβ”€β”€ gemini_client.py   # Gemini API wrapper
β”‚   β”‚   β”‚   └── empathy_refiner.py # DialoGPT enhancement
β”‚   β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”‚   β”œβ”€β”€ chat_service.py    # Main chat logic
β”‚   β”‚   β”‚   └── analysis_service.py# Analytics
β”‚   β”‚   β”œβ”€β”€ __init__.py            # Flask app factory
β”‚   β”‚   └── config.py              # Configuration
β”‚   β”œβ”€β”€ requirements.txt           # Python dependencies
β”‚   β”œβ”€β”€ Dockerfile                 # Backend container
β”‚   └── wsgi.py                    # Gunicorn entry point
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”‚   β”œβ”€β”€ chat/              # Chat page
β”‚   β”‚   β”‚   └── layout.tsx         # Layout
β”‚   β”‚   β”œβ”€β”€ components/            # React components
β”‚   β”‚   β”œβ”€β”€ hooks/                 # Custom hooks
β”‚   β”‚   β”œβ”€β”€ lib/                   # Utilities
β”‚   β”‚   └── types/                 # TypeScript types
β”‚   β”œβ”€β”€ package.json               # Node dependencies
β”‚   β”œβ”€β”€ tsconfig.json              # TypeScript config
β”‚   └── Dockerfile                 # Frontend container
β”œβ”€β”€ nginx/
β”‚   └── nginx.conf                 # Reverse proxy config
β”œβ”€β”€ docker-compose.yml             # Container orchestration
β”œβ”€β”€ .env.example                   # Environment template
└── README.md                       # This file

🀝 Contributing

  1. Fork the repository
  2. Create feature branch: git checkout -b feature/amazing-feature
  3. Commit changes: git commit -m 'Add amazing feature'
  4. Push to branch: git push origin feature/amazing-feature
  5. Open Pull Request

βš–οΈ License

This project is licensed under the MIT License - see LICENSE file for details.


πŸ“ž Support & Resources


🎯 Future Roadmap

  • User authentication & persistence
  • Video/voice chat support
  • Integration with calendars for therapy appointments
  • AI-powered mood journaling
  • Multi-language support
  • Mobile app (React Native)
  • Real therapist matchmaking
  • Advanced analytics dashboard
  • Offline mode support

Built with ❀️ for mental health and well-being

Last Updated: April 2026

About

An AI-powered emotional support assistant designed to provide empathetic conversations, mood tracking, and mental wellness guidance in a safe, private environment.

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors