๐ง 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.
๐ 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
| Layer | Technology |
|---|---|
| Backend | Flask 3.x ยท Flask-Session ยท Flask-WTF ยท CSRFProtect |
| AI Engine | Google Gemini 2.5 Flash via google-genai SDK |
| Database | SQLite (via sqlite3) |
| Auth | Werkzeug (password hashing) ยท Google OAuth 2.0 (google-auth) |
| Encryption | Fernet symmetric encryption (cryptography) |
| Validation | Pydantic (AI response schemas) ยท WTForms ยท CSRF tokens |
| Frontend | Jinja2 Templates ยท HTML ยท CSS ยท JavaScript |
| Deployment | PythonAnywhere |
๐ 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
- Python 3.12+
- A Google Gemini API key
- (Optional) A Google OAuth 2.0 Client ID for Google Sign-In
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
| Variable | Required | Description |
|---|---|---|
FERNET_KEY | Yes | Encrypts each user's saved Gemini API key. The app will not start without it. |
GOOGLE_CLIENT_ID | Optional | Enables 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
- Register with email/password or sign in with Google.
- Add your personal Gemini API key from the setup page.
- Open the chat page and submit a diary entry with sleep, energy, appetite, and physical symptom details.
- Review the Arabic AI analysis, risk level, trigger words, advice, and suggested tasks.
- Visit history to reopen past sessions or download them as CSV.
๐ Main Routes
| Route | Purpose |
|---|---|
/ | Landing page |
/register | Create a user account |
/login | Email/password or Google login |
/setup-api-key | Save the user's Gemini API key |
/n/chat | Start a new analysis session |
/p/chat?chpaV1=... | Reopen a previous analysis session |
/history | View saved analysis history |
/download_history | Export history as CSV |
/dashboard | User settings and profile management |
/admin-login | Admin login page |
/admin-dashboard | Admin metrics overview |
/admin-users | Admin 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=Truewhen started directly throughpython 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.