Skip to content

Commit a07e118

Browse files
committed
v2.0.0: Complete rewrite — Gemini 2.5 Flash, glassmorphism UI, model fallback
- Switched from deprecated google-generativeai to google-genai SDK - Primary model: Gemini 2.5 Flash (free-tier) with auto-fallback to Gemma 3 4B - XLM-RoBERTa emotion detection (28 labels) informs Gemini's tone - Emotion badges displayed on both user and bot messages - Glassmorphism design with animated orbs, gradient accents, dark mode - Edge TTS (14 voices) + Web Speech API STT with Whisper fallback - Rate limiting, CSP headers, input sanitization - Chat actions: copy, like/dislike, download JSON, new chat - MongoDB optional (in-memory fallback) - Retry with exponential backoff + automatic model fallback on quota errors
0 parents  commit a07e118

19 files changed

Lines changed: 4504 additions & 0 deletions

.env.example

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# EmoBot v2 Configuration
2+
# Copy this file to .env and fill in your values:
3+
# cp .env.example .env
4+
5+
# ─── Required ─────────────────────────────────────────────
6+
GEMINI_API_KEY=your_gemini_api_key_here
7+
8+
# ─── AI Models (defaults are free-tier) ───────────────────
9+
GEMINI_MODEL=gemini-2.5-flash
10+
FALLBACK_MODEL=gemma-3-4b-it
11+
12+
# ─── App ──────────────────────────────────────────────────
13+
SECRET_KEY=generate-a-random-secret-key
14+
MONGODB_URI=mongodb://localhost:27017/
15+
HOST=127.0.0.1
16+
PORT=5000
17+
FLASK_DEBUG=false

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.safetensors filter=lfs diff=lfs merge=lfs -text

.gitignore

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# ─── Environment ──────────────────────────────────────────────
2+
.env
3+
!.env.example
4+
5+
# ─── Python ───────────────────────────────────────────────────
6+
__pycache__/
7+
*.py[cod]
8+
*.pyo
9+
*.egg-info/
10+
dist/
11+
build/
12+
*.egg
13+
.venv/
14+
venv/
15+
env/
16+
17+
# ─── Model weights (tracked via Git LFS) ─────────────────────
18+
# Don't ignore — LFS handles the large .safetensors file
19+
20+
# ─── IDE / Editor ────────────────────────────────────────────
21+
.vscode/
22+
.idea/
23+
*.swp
24+
*.swo
25+
*.code-workspace
26+
27+
# ─── OS junk ─────────────────────────────────────────────────
28+
.DS_Store
29+
Thumbs.db
30+
Desktop.ini
31+
ehthumbs.db
32+
33+
# ─── Logs & temp ─────────────────────────────────────────────
34+
*.log
35+
*.tmp
36+
*.bak
37+
38+
# ─── Misc ────────────────────────────────────────────────────
39+
.qodo/
40+
*.ipynb_checkpoints/
41+
startup.log

README.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
# EmoBot v2 — Emotion-Aware Multilingual Chatbot
2+
3+
An emotionally intelligent chatbot that detects the user's emotion from their message using **XLM-RoBERTa** and generates empathetic, tone-aligned responses via **Google Gemini**.
4+
5+
---
6+
7+
## Features
8+
9+
- **Real-time emotion detection** — XLM-RoBERTa fine-tuned on GoEmotions (28 emotion labels)
10+
- **Empathetic AI responses** — Gemini adapts tone based on detected emotion (sadness → warm, anger → calm, joy → upbeat, ...)
11+
- **Multilingual** — Detects emotion and responds in the user's language
12+
- **Voice input & output** — Speech-to-text (Web Speech API + Whisper fallback) and text-to-speech (Edge TTS + browser fallback)
13+
- **Automatic model fallback** — If Gemini 2.5 Flash quota is exhausted, falls back to Gemma 3 4B seamlessly
14+
- **Emotion badges** — Detected emotion displayed on both user and bot messages
15+
- **Chat actions** — Copy, like/dislike feedback, download conversation as JSON, new chat
16+
- **Dark mode** — Toggle between light and dark themes
17+
- **Glassmorphism UI** — Frosted glass cards, animated floating orbs, gradient accents
18+
- **Security hardened** — CSP headers, rate limiting, input sanitization
19+
20+
---
21+
22+
## How It Works
23+
24+
```
25+
User Message
26+
27+
28+
┌──────────────────────┐
29+
│ XLM-RoBERTa │──→ Detected emotion (e.g. "joy", "sadness", "anger")
30+
│ Emotion Classifier │ + confidence scores (top 3)
31+
└──────────────────────┘
32+
33+
34+
┌──────────────────────┐
35+
│ Google Gemini │──→ Emotionally aligned response
36+
│ (2.5 Flash / Gemma) │ in the user's language
37+
└──────────────────────┘
38+
39+
40+
Chat UI displays response + emotion badge
41+
```
42+
43+
---
44+
45+
## Tech Stack
46+
47+
| Layer | Technology |
48+
| ------------- | --------------------------------------- |
49+
| Backend | Python, Flask |
50+
| Emotion Model | XLM-RoBERTa (GoEmotions, 28 labels) |
51+
| Response Gen | Gemini 2.5 Flash (fallback: Gemma 3 4B) |
52+
| TTS | Edge TTS (14 language voices) |
53+
| STT | Web Speech API + OpenAI Whisper |
54+
| Frontend | HTML, CSS, JavaScript |
55+
| Database | MongoDB (optional, in-memory fallback) |
56+
57+
---
58+
59+
## Project Structure
60+
61+
```
62+
EmoBot/
63+
├── app.py # Flask backend (API, emotion detection, Gemini, TTS/STT)
64+
├── requirements.txt # Python dependencies
65+
├── .env.example # Environment variable template
66+
├── .gitignore
67+
├── README.md
68+
├── xlm-roberta.ipynb # Model training notebook
69+
70+
├── static/
71+
│ ├── css/
72+
│ │ └── main.css # Full stylesheet (glassmorphism, animations, dark mode)
73+
│ ├── js/
74+
│ │ ├── chat.js # Chat logic (send, receive, TTS, STT, feedback, download)
75+
│ │ └── home.js # Landing page (emotion pills, scroll reveal, dark mode)
76+
│ └── models/
77+
│ └── xlm-roberta_emotion_model/
78+
│ ├── model.safetensors # Fine-tuned weights (~1.1 GB, Git LFS)
79+
│ ├── config.json
80+
│ ├── tokenizer_config.json
81+
│ ├── sentencepiece.bpe.model
82+
│ └── special_tokens_map.json
83+
84+
└── templates/
85+
├── index.html # Landing page
86+
├── chat.html # Chat interface
87+
├── 404.html # Not found page
88+
└── 500.html # Server error page
89+
```
90+
91+
---
92+
93+
## Setup
94+
95+
### 1. Clone & pull model weights
96+
97+
```bash
98+
git clone https://github.com/anVSS1/emobot.git
99+
cd emobot
100+
git lfs install
101+
git lfs pull
102+
```
103+
104+
### 2. Create virtual environment
105+
106+
```bash
107+
python -m venv .venv
108+
# Windows
109+
.venv\Scripts\activate
110+
# macOS / Linux
111+
source .venv/bin/activate
112+
```
113+
114+
### 3. Install dependencies
115+
116+
```bash
117+
pip install -r requirements.txt
118+
```
119+
120+
### 4. Configure environment
121+
122+
```bash
123+
cp .env.example .env
124+
```
125+
126+
Edit `.env` and add your [Google AI Studio API key](https://aistudio.google.com/apikey):
127+
128+
```
129+
GEMINI_API_KEY=your_key_here
130+
```
131+
132+
### 5. Run
133+
134+
```bash
135+
python -u app.py
136+
```
137+
138+
Open **http://localhost:5000** and start chatting.
139+
140+
---
141+
142+
## Environment Variables
143+
144+
| Variable | Default | Description |
145+
| ---------------- | ---------------------------- | ----------------------------------- |
146+
| `GEMINI_API_KEY` | _(required)_ | Google AI Studio API key |
147+
| `GEMINI_MODEL` | `gemini-2.5-flash` | Primary generation model |
148+
| `FALLBACK_MODEL` | `gemma-3-4b-it` | Fallback model when quota exhausted |
149+
| `SECRET_KEY` | _(auto-generated)_ | Flask session secret |
150+
| `MONGODB_URI` | `mongodb://localhost:27017/` | MongoDB connection string |
151+
| `HOST` | `127.0.0.1` | Server bind address |
152+
| `PORT` | `5000` | Server port |
153+
| `FLASK_DEBUG` | `false` | Enable Flask debug mode |
154+
155+
---
156+
157+
## Supported Emotions (28)
158+
159+
admiration · amusement · anger · annoyance · approval · caring · confusion · curiosity · desire · disappointment · disapproval · disgust · embarrassment · excitement · fear · gratitude · grief · joy · love · nervousness · optimism · pride · realization · relief · remorse · sadness · surprise · neutral
160+
161+
---
162+
163+
## Team
164+
165+
| Name | Links |
166+
| ------------------- | --------------------------------------------------------------------------------------------------------------------- |
167+
| **Anass Ouzaouit** | [GitHub](https://github.com/anVSS1) · [LinkedIn](https://linkedin.com/in/anass-ouzaouit) · [X](https://x.com/anvss__) |
168+
| **Mohamed Kannoun** | [LinkedIn](https://linkedin.com/in/kannoun) · [X](https://x.com/M_KANNOUN) |
169+
170+
---
171+
172+
## License
173+
174+
MIT

0 commit comments

Comments
 (0)