๐Ÿง  PsychoAI

Your AI-Powered Mental Wellness Companion

An intelligent emotional wellness platform that analyzes diary entries and provides non-medical psychological insights โ€” powered by Google Gemini AI.

Live Demo
Python 3.12+ Flask Gemini AI License


๐Ÿ“– About

PsychoAI is a web-based emotional wellness tool designed to bridge the gap between daily psychological challenges and professional support. Users write diary entries enriched with clinical metadata (sleep hours, energy level, appetite, physical symptoms), and the AI analyzes the text to detect emotional patterns, assess risk levels, and deliver personalized well-being guidance โ€” all in Arabic.

โš ๏ธ Disclaimer: PsychoAI is not a medical tool. It provides general emotional insights and well-being guidance only. Always consult a licensed professional for medical advice.

โœจ Features

๐Ÿค– AI Emotional Analysis

  • Analyzes diary entries using Gemini 2.5 Flash with structured JSON output via Pydantic schemas
  • Scores anxiety, stress, and depression on a 0โ€“10 scale
  • Classifies emotional state into Low / Medium / High risk levels
  • Detects trigger words that may indicate emotional distress
  • Generates personalized advice and 2โ€“4 actionable tasks the user can implement immediately
  • Flags cases that may need a doctor's visit with a detailed recommendation

๐Ÿ” Authentication & Security

  • Email/Password registration with strong password policy (8+ chars, uppercase, number, symbol)
  • Google OAuth 2.0 sign-in for quick access
  • CSRF protection via Flask-WTF on all forms
  • Fernet encryption for API keys stored in the database
  • Secure session management with HTTPOnly cookies

๐Ÿ“Š Dashboard & History

  • User dashboard for profile management (update name, email, delete account)
  • Conversation history โ€” browse and revisit all past analysis sessions
  • CSV export โ€” download your entire history as a structured CSV file
  • Previous session viewer โ€” reload and review any past conversation in the chat UI

๐Ÿ›ก๏ธ Admin Panel

  • Admin dashboard โ€” view total user count, session count, and recent users at a glance
  • User management โ€” search, browse, and delete user accounts
  • Admin authentication โ€” separate login flow for admin access

๐Ÿ› ๏ธ Tech Stack

LayerTechnology
BackendFlask 3.x ยท Flask-Session ยท Flask-WTF ยท CSRFProtect
AI EngineGoogle Gemini 2.5 Flash via google-genai SDK
DatabaseSQLite (via sqlite3)
AuthWerkzeug (password hashing) ยท Google OAuth 2.0 (google-auth)
EncryptionFernet symmetric encryption (cryptography)
ValidationPydantic (AI response schemas) ยท WTForms ยท CSRF tokens
FrontendJinja2 Templates ยท HTML ยท CSS ยท JavaScript
DeploymentPythonAnywhere

๐Ÿ“ Project Structure

PsychoAI/
โ”œโ”€โ”€ main.py                  # Flask app โ€” routes, auth, admin, API
โ”œโ”€โ”€ base_ai/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ””โ”€โ”€ psycho.py            # PsychoAnalyzer โ€” Gemini AI integration & Pydantic schemas
โ”œโ”€โ”€ templates/
โ”‚   โ”œโ”€โ”€ home.html             # Landing page
โ”‚   โ”œโ”€โ”€ login.html            # Login page (email + Google OAuth)
โ”‚   โ”œโ”€โ”€ register.html         # Registration page
โ”‚   โ”œโ”€โ”€ chat.html             # Chat interface for diary analysis
โ”‚   โ”œโ”€โ”€ sittings.html         # User dashboard / settings
โ”‚   โ”œโ”€โ”€ history.html          # Session history viewer
โ”‚   โ”œโ”€โ”€ api_setup.html        # Gemini API key setup page
โ”‚   โ”œโ”€โ”€ admin_login.html      # Admin login
โ”‚   โ”œโ”€โ”€ admin_dashboard.html  # Admin dashboard
โ”‚   โ”œโ”€โ”€ admin_users.html      # Admin user management
โ”‚   โ””โ”€โ”€ 404.html              # Custom 404 page
โ”œโ”€โ”€ static/                   # Static assets (CSS, JS, images)
โ”œโ”€โ”€ requirements.txt          # Python dependencies
โ”œโ”€โ”€ .env                      # Environment variables (not tracked)
โ””โ”€โ”€ LICENSE                   # CC0 1.0 Universal

๐Ÿš€ Getting Started

Prerequisites

Installation

# Clone the repository
git clone https://github.com/Mhkh2642009/OL-IV.git
cd OL-IV

# Create and activate a virtual environment
python -m venv venv
source venv/bin/activate   # Linux / macOS
venv\Scripts\activate      # Windows

# Install dependencies
pip install -r requirements.txt

Configuration

Create a .env file in the project root:

GOOGLE_CLIENT_ID=your_google_oauth_client_id
FERNET_KEY=your_fernet_encryption_key
VariableRequiredDescription
FERNET_KEYYesEncrypts each user's saved Gemini API key. The app will not start without it.
GOOGLE_CLIENT_IDOptionalEnables Google OAuth login. Email/password auth still works without it.
๐Ÿ’ก Tip: Generate a Fernet key with:
```python
from cryptography.fernet import Fernet
print(Fernet.generate_key().decode())
```

The SQLite database file (iv_moha_2FK.db) and required tables are created automatically when main.py starts.

Run

python main.py

The app will be available at http://127.0.0.1:5000.


๐Ÿงญ Usage Flow

  1. Register with email/password or sign in with Google.
  2. Add your personal Gemini API key from the setup page.
  3. Open the chat page and submit a diary entry with sleep, energy, appetite, and physical symptom details.
  4. Review the Arabic AI analysis, risk level, trigger words, advice, and suggested tasks.
  5. Visit history to reopen past sessions or download them as CSV.

๐Ÿ”— Main Routes

RoutePurpose
/Landing page
/registerCreate a user account
/loginEmail/password or Google login
/setup-api-keySave the user's Gemini API key
/n/chatStart a new analysis session
/p/chat?chpaV1=...Reopen a previous analysis session
/historyView saved analysis history
/download_historyExport history as CSV
/dashboardUser settings and profile management
/admin-loginAdmin login page
/admin-dashboardAdmin metrics overview
/admin-usersAdmin user search and management

๐Ÿ”‘ How It Works

flowchart LR
    A[User writes diary entry] --> B[Adds clinical metadata]
    B --> C[Gemini 2.5 Flash analyzes text]
    C --> D[Structured JSON response via Pydantic]
    D --> E[Scores & risk level displayed]
    D --> F[Personalized advice in Arabic]
    D --> G[Actionable tasks generated]
    D --> H{Doctor needed?}
    H -->|Yes| I[Doctor recommendation shown]
    H -->|No| J[Preventive advice shown]

๐Ÿงช Development Notes

  • The app runs with debug=True when started directly through python main.py; disable debug mode in production.
  • User Gemini API keys are encrypted before being stored in SQLite.
  • The app currently uses filesystem-backed Flask sessions in flask_session/.
  • Admin credentials are defined in main.py; change them before deploying outside a local/demo environment.
  • Do not commit .env, local database files, virtual environments, or generated session files.

๐Ÿ“„ License

This project is released under the CC0 1.0 Universal license โ€” free to use, modify, and distribute without restriction.