File size: 8,929 Bytes
f6278c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# Chatty Installation Guide

This guide provides step-by-step instructions for installing and configuring the Chatty application.

## System Requirements

- **Python**: 3.8 or higher
- **Database**: MongoDB (Atlas recommended) or local MongoDB installation
- **Memory**: Minimum 512MB RAM
- **Storage**: 100MB free space
- **Network**: Internet connection for external API calls

## Installation Methods

### Method 1: Quick Start (Recommended)

1. **Clone and Setup**
   ```bash
   git clone <repository-url>
   cd chatty
   python -m venv atlas_env
   source atlas_env/bin/activate  # Windows: atlas_env\Scripts\activate
   pip install -r requirements.txt
   ```

2. **Configure Environment**
   ```bash
   python deploy.py setup-dev
   ```

3. **Edit Configuration**
   Edit `.env` file with your MongoDB connection details:
   ```env
   SECRET_KEY=your-generated-secret-key
   MONGODB_URL=your-mongodb-connection-string
   ```

4. **Start Application**
   ```bash
   ./start.sh
   ```

### Method 2: Manual Installation

#### Step 1: System Preparation

**Ubuntu/Debian:**
```bash
sudo apt update
sudo apt install python3 python3-pip python3-venv git
```

**macOS:**
```bash
# Install Homebrew if not installed
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install python3 git
```

**Windows:**
1. Install Python from [python.org](https://python.org)
2. Install Git from [git-scm.com](https://git-scm.com)

#### Step 2: Application Setup

1. **Clone Repository**
   ```bash
   git clone <repository-url>
   cd chatty
   ```

2. **Create Virtual Environment**
   ```bash
   python3 -m venv atlas_env
   
   # Activate virtual environment
   # Linux/macOS:
   source atlas_env/bin/activate
   
   # Windows:
   atlas_env\Scripts\activate
   ```

3. **Install Dependencies**
   ```bash
   pip install --upgrade pip
   pip install -r requirements.txt
   ```

#### Step 3: Database Setup

Choose one of the following database options:

**Option A: MongoDB Atlas (Cloud - Recommended)**

1. Create account at [MongoDB Atlas](https://www.mongodb.com/atlas)
2. Create new cluster (free tier available)
3. Create database user:
   - Username: `chatty_user`
   - Password: Generate secure password
   - Roles: `readWrite` on your database
4. Configure network access:
   - Add your IP address
   - Or allow access from anywhere (0.0.0.0/0) for development
5. Get connection string from "Connect" → "Connect your application"

**Option B: Local MongoDB**

1. **Install MongoDB**
   
   **Ubuntu/Debian:**
   ```bash
   wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
   echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
   sudo apt update
   sudo apt install -y mongodb-org
   ```
   
   **macOS:**
   ```bash
   brew tap mongodb/brew
   brew install mongodb-community
   ```
   
   **Windows:**
   Download installer from [MongoDB Download Center](https://www.mongodb.com/try/download/community)

2. **Start MongoDB Service**
   
   **Ubuntu/Debian:**
   ```bash
   sudo systemctl start mongod
   sudo systemctl enable mongod
   ```
   
   **macOS:**
   ```bash
   brew services start mongodb/brew/mongodb-community
   ```
   
   **Windows:**
   MongoDB runs as a Windows service after installation

3. **Create Database and User**
   ```bash
   mongosh
   ```
   ```javascript
   use chatty
   db.createUser({
     user: "chatty_user",
     pwd: "your_secure_password",
     roles: [{ role: "readWrite", db: "chatty" }]
   })
   ```

#### Step 4: Configuration

1. **Generate Configuration**
   ```bash
   python deploy.py setup-dev
   ```

2. **Edit Environment File**
   Edit `.env` file:
   
   **For MongoDB Atlas:**
   ```env
   SECRET_KEY=your-generated-secret-key
   MONGODB_URL=mongodb+srv://chatty_user:password@cluster.mongodb.net/chatty?retryWrites=true&w=majority
   MONGODB_DATABASE=chatty
   FLASK_ENV=development
   ```
   
   **For Local MongoDB:**
   ```env
   SECRET_KEY=your-generated-secret-key
   MONGODB_URL=mongodb://chatty_user:password@localhost:27017/chatty
   MONGODB_DATABASE=chatty
   FLASK_ENV=development
   ```

3. **Generate Secret Key**
   ```bash
   python deploy.py gen-secret
   ```
   Copy the generated key to your `.env` file.

#### Step 5: Validation and Testing

1. **Validate Configuration**
   ```bash
   python config.py
   ```

2. **Test Database Connection**
   ```bash
   python -c "from database import test_connection; test_connection()"
   ```

3. **Run Tests (Optional)**
   ```bash
   pip install -r Test/test_requirements.txt
   python Test/run_tests.py
   ```

#### Step 6: Start Application

1. **Start with Script**
   ```bash
   chmod +x start.sh
   ./start.sh
   ```

2. **Or Start Manually**
   ```bash
   python app.py
   ```

3. **Access Application**
   Open browser to: http://localhost:7860

## Post-Installation Setup

### Create First User Account

1. Navigate to http://localhost:7860
2. Click "Register" to create your first account
3. Fill in email and password
4. Log in with your credentials

### Verify Installation

1. **Check Application Health**
   ```bash
   curl http://localhost:7860/health
   ```

2. **Test Authentication**
   - Register a new account
   - Log in and out
   - Send a test chat message

3. **Check Logs**
   Monitor console output for any errors

## Configuration Options

### Environment Variables

Create or edit `.env` file with these options:

```env
# Required
SECRET_KEY=your-64-character-secret-key
MONGODB_URL=your-mongodb-connection-string
MONGODB_DATABASE=chatty

# Optional
FLASK_ENV=development
SESSION_LIFETIME_HOURS=24
MAX_LOGIN_ATTEMPTS=5
RATE_LIMIT_WINDOW=900
API_TIMEOUT=30
LOG_LEVEL=INFO
PORT=7860
```

### Advanced Configuration

For production deployment, see [PRODUCTION_DEPLOYMENT.md](PRODUCTION_DEPLOYMENT.md).

## Troubleshooting

### Common Installation Issues

1. **Python Version Issues**
   ```bash
   python --version  # Should be 3.8+
   python3 --version
   ```

2. **Virtual Environment Issues**
   ```bash
   # Recreate virtual environment
   rm -rf atlas_env
   python3 -m venv atlas_env
   source atlas_env/bin/activate
   pip install -r requirements.txt
   ```

3. **MongoDB Connection Issues**
   ```bash
   # Test connection manually
   python -c "
   from pymongo import MongoClient
   client = MongoClient('your-connection-string')
   print(client.admin.command('ping'))
   "
   ```

4. **Permission Issues (Linux/macOS)**
   ```bash
   chmod +x start.sh
   chmod +x deploy.py
   ```

5. **Port Already in Use**
   ```bash
   # Find process using port 7860
   lsof -i :7860
   # Kill process or change PORT in .env
   ```

### Error Messages

**"MONGODB_URL environment variable is required"**
- Check `.env` file exists and contains `MONGODB_URL`
- Verify environment variable is set correctly

**"Configuration validation failed"**
- Run `python config.py` to see specific issues
- Check all required environment variables are set

**"Database connection failed"**
- Verify MongoDB is running (local) or accessible (Atlas)
- Check connection string format
- Verify credentials and network access

**"Secret key errors"**
- Generate new secret key: `python deploy.py gen-secret`
- Ensure key is at least 32 characters long

### Getting Help

1. **Check Logs**
   - Console output during startup
   - Application logs if `LOG_FILE` is configured

2. **Validate Setup**
   ```bash
   python deploy.py check-prod  # Even for development
   python config.py
   ```

3. **Test Components**
   ```bash
   # Test database
   python -c "from database import test_connection; test_connection()"
   
   # Test configuration
   python -c "from config import validate_environment; validate_environment()"
   ```

## Next Steps

After successful installation:

1. **Production Deployment**: See [PRODUCTION_DEPLOYMENT.md](PRODUCTION_DEPLOYMENT.md)
2. **Customization**: Modify templates and styles in `templates/` and `static/`
3. **Monitoring**: Set up logging and monitoring for production use
4. **Backup**: Configure regular database backups
5. **Updates**: Keep dependencies updated with `pip install -r requirements.txt --upgrade`

## Uninstallation

To remove the application:

1. **Stop Application**
   - Press Ctrl+C if running in terminal
   - Or stop systemd service if configured

2. **Remove Files**
   ```bash
   # Remove application directory
   rm -rf /path/to/chatty
   
   # Remove virtual environment
   rm -rf atlas_env
   ```

3. **Remove Database** (Optional)
   - For MongoDB Atlas: Delete cluster from Atlas dashboard
   - For local MongoDB: Drop database in MongoDB shell
   ```javascript
   use chatty
   db.dropDatabase()
   ```

4. **Remove System Dependencies** (Optional)
   - Uninstall MongoDB if no longer needed
   - Remove Python if installed specifically for this application