kaeizen commited on
Commit
f738160
Β·
1 Parent(s): 29cfad5

add readme

Browse files
Files changed (1) hide show
  1. README.md +373 -0
README.md ADDED
@@ -0,0 +1,373 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Grammo Backend
2
+
3
+ Django REST API backend for Grammo, an AI-powered translation and grammar correction service.
4
+
5
+ ## Overview
6
+
7
+ The Grammo backend provides a RESTful API for translation and grammar correction services. It leverages LangChain and HuggingFace models to process language requests, with LangGraph managing conversation state across sessions.
8
+
9
+ ## Features
10
+
11
+ - 🌐 **Translation Service** - Natural, contextually appropriate translations between languages
12
+ - ✏️ **Grammar Correction** - Fixes grammar, spelling, and punctuation errors
13
+ - πŸ’¬ **Session Management** - Maintains conversation context using Django sessions and LangGraph checkpoints
14
+ - 🎭 **Customizable Modes** - Supports Default and Grammar modes
15
+ - 🎨 **Tone Control** - Configurable tone (Default, Formal, Casual) for responses
16
+ - πŸ”’ **Security** - CORS support, CSRF protection, secure session management
17
+ - πŸ“¦ **HuggingFace Integration** - Uses GPT-OSS-Safeguard-20B model via HuggingFace API
18
+
19
+ ## Tech Stack
20
+
21
+ - **Django 5.2.7** - Web framework
22
+ - **Django REST Framework** - API development
23
+ - **LangChain** - AI agent orchestration
24
+ - **LangGraph** - Conversation state management
25
+ - **HuggingFace** - Language model integration (GPT-OSS-Safeguard-20B)
26
+ - **Python 3.14+** - Programming language
27
+ - **SQLite** - Database (development)
28
+ - **Uvicorn** - ASGI server
29
+
30
+ ## Prerequisites
31
+
32
+ - Python 3.14 or higher
33
+ - pip (Python package manager)
34
+ - HuggingFace API Token ([Get one here](https://huggingface.co/settings/tokens))
35
+
36
+ ## Installation
37
+
38
+ ### 1. Navigate to the backend directory
39
+
40
+ ```bash
41
+ cd backend
42
+ ```
43
+
44
+ ### 2. Create and activate a virtual environment
45
+
46
+ ```bash
47
+ # Create virtual environment
48
+ python -m venv venv
49
+
50
+ # Activate virtual environment
51
+ # On macOS/Linux:
52
+ source venv/bin/activate
53
+ # On Windows:
54
+ venv\Scripts\activate
55
+ ```
56
+
57
+ ### 3. Install dependencies
58
+
59
+ ```bash
60
+ pip install -r requirements.txt
61
+ ```
62
+
63
+ ### 4. Set up environment variables
64
+
65
+ Create a `.env` file in the `backend` directory:
66
+
67
+ ```bash
68
+ touch .env
69
+ ```
70
+
71
+ Add the following environment variables (see [Environment Variables](#environment-variables) section for details):
72
+
73
+ ```env
74
+ SECRET_KEY=your-secret-key-here
75
+ HUGGINGFACEHUB_API_TOKEN=your-huggingface-api-token
76
+ DEBUG=True
77
+ ```
78
+
79
+ To generate a Django secret key:
80
+
81
+ ```bash
82
+ python -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())"
83
+ ```
84
+
85
+ ### 5. Run database migrations
86
+
87
+ ```bash
88
+ python manage.py migrate
89
+ ```
90
+
91
+ ## Environment Variables
92
+
93
+ Create a `.env` file in the `backend` directory with the following variables:
94
+
95
+ ### Required Variables
96
+
97
+ ```env
98
+ # Django Secret Key (generate one using the command above)
99
+ SECRET_KEY=your-secret-key-here
100
+
101
+ # HuggingFace API Token
102
+ HUGGINGFACEHUB_API_TOKEN=your-huggingface-api-token
103
+ ```
104
+
105
+ ### Optional Development Variables
106
+
107
+ ```env
108
+ # Debug mode (default: True)
109
+ DEBUG=True
110
+
111
+ # Session security (default: False for development)
112
+ SESSION_COOKIE_SECURE=False # Set to True in production (requires HTTPS)
113
+ CSRF_COOKIE_SECURE=False # Set to True in production (requires HTTPS)
114
+
115
+ # CORS settings
116
+ CORS_ALLOW_ALL_ORIGINS=True # Set to False in production and specify origins
117
+ ```
118
+
119
+ ### Optional Production Variables
120
+
121
+ ```env
122
+ # Allowed hosts (comma-separated)
123
+ ALLOWED_HOSTS=yourdomain.com,www.yourdomain.com
124
+
125
+ # CSRF trusted origins (comma-separated)
126
+ CSRF_TRUSTED_ORIGINS=https://yourdomain.com,https://www.yourdomain.com
127
+
128
+ # Security settings
129
+ SECURE_SSL_REDIRECT=True
130
+ SECURE_CONTENT_TYPE_NOSNIFF=True
131
+ SECURE_HSTS_SECONDS=31536000
132
+ SECURE_HSTS_INCLUDE_SUBDOMAINS=True
133
+ SECURE_HSTS_PRELOAD=True
134
+ ```
135
+
136
+ ## Running the Application
137
+
138
+ ### Development Mode
139
+
140
+ **Option 1: Django Development Server (with warnings)**
141
+
142
+ ```bash
143
+ python manage.py runserver
144
+ ```
145
+
146
+ The server will run on `http://localhost:8000`
147
+
148
+ **Option 2: Uvicorn ASGI Server (production-like, no warnings)**
149
+
150
+ ```bash
151
+ uvicorn backend.asgi:application --host 0.0.0.0 --port 8000 --reload
152
+ ```
153
+
154
+ ### Production Mode
155
+
156
+ ```bash
157
+ # Set DEBUG=False in .env
158
+ uvicorn backend.asgi:application --host 0.0.0.0 --port 8000
159
+
160
+ # With multiple workers:
161
+ uvicorn backend.asgi:application --host 0.0.0.0 --port 8000 --workers 4
162
+ ```
163
+
164
+ ### Standalone Script (for HuggingFace Spaces)
165
+
166
+ The backend can also be run as a standalone script:
167
+
168
+ ```bash
169
+ python app.py
170
+ ```
171
+
172
+ This uses the `PORT` environment variable (defaults to 7860) and is configured for HuggingFace Spaces deployment.
173
+
174
+ ## API Endpoints
175
+
176
+ ### Base URL
177
+
178
+ All endpoints are prefixed with `/api/v1/`
179
+
180
+ ### `GET /api/v1/hello/`
181
+
182
+ Health check endpoint.
183
+
184
+ **Response:**
185
+ ```json
186
+ {
187
+ "message": "Hello from Grammo!"
188
+ }
189
+ ```
190
+
191
+ ### `POST /api/v1/chat/`
192
+
193
+ Send a message to start or continue a chat session.
194
+
195
+ **Request Body:**
196
+ ```json
197
+ {
198
+ "message": "Translate this text to French",
199
+ "chatSession": 0,
200
+ "mode": "default",
201
+ "tone": "default"
202
+ }
203
+ ```
204
+
205
+ **Parameters:**
206
+ - `message` (required): The user's message
207
+ - `chatSession` (optional): Session identifier to maintain conversation context
208
+ - `mode` (optional): `"default"` or `"grammar"` - Determines how the message is processed
209
+ - `tone` (optional): `"default"`, `"formal"`, or `"casual"` - Sets the tone of the response
210
+
211
+ **Response (Success):**
212
+ ```json
213
+ {
214
+ "status": "success",
215
+ "response": "**Original**: \nTranslate this text to French\n**Output**: \nTraduisez ce texte en franΓ§ais\n___\n**Explanation**: \n> Direct translation maintaining the original meaning"
216
+ }
217
+ ```
218
+
219
+ **Response (Error):**
220
+ ```json
221
+ {
222
+ "status": "error",
223
+ "response": "Invalid message."
224
+ }
225
+ ```
226
+
227
+ ### `POST /api/v1/end/`
228
+
229
+ End the current chat session and clear conversation history.
230
+
231
+ **Request Body:**
232
+ ```json
233
+ {}
234
+ ```
235
+
236
+ **Response (Success):**
237
+ ```json
238
+ {
239
+ "status": "success",
240
+ "message": "Session ended successfully"
241
+ }
242
+ ```
243
+
244
+ **Response (Error):**
245
+ ```json
246
+ {
247
+ "status": "error",
248
+ "response": "No active session."
249
+ }
250
+ ```
251
+
252
+ ## Project Structure
253
+
254
+ ```
255
+ backend/
256
+ β”œβ”€β”€ agent_manager/ # AI agent management module
257
+ β”‚ └── __init__.py # LangChain agent setup, session management
258
+ β”œβ”€β”€ api/ # Django REST API application
259
+ β”‚ β”œβ”€β”€ views.py # API view handlers (chat, hello, end)
260
+ β”‚ β”œβ”€β”€ urls.py # URL routing
261
+ β”‚ └── apps.py # App configuration
262
+ β”œβ”€β”€ backend/ # Django project settings
263
+ β”‚ β”œβ”€β”€ settings.py # Django configuration
264
+ β”‚ β”œβ”€β”€ urls.py # Main URL configuration
265
+ β”‚ β”œβ”€β”€ asgi.py # ASGI application
266
+ β”‚ └── wsgi.py # WSGI application
267
+ β”œβ”€β”€ app.py # Standalone entry point (HuggingFace Spaces)
268
+ β”œβ”€β”€ manage.py # Django management script
269
+ β”œβ”€β”€ requirements.txt # Python dependencies
270
+ β”œβ”€β”€ Dockerfile # Docker configuration for deployment
271
+ └── README.md # This file
272
+ ```
273
+
274
+ ## Development
275
+
276
+ ### Session Management
277
+
278
+ - Sessions are managed using Django's session framework
279
+ - Session data is stored in the cache backend (in-memory for development)
280
+ - Each session maintains its own LangGraph agent with conversation checkpointing
281
+ - Sessions expire after 24 hours of inactivity or when explicitly ended
282
+
283
+ ### Agent Architecture
284
+
285
+ - Uses LangChain's `create_agent` with a structured output wrapper
286
+ - Structured output ensures consistent JSON responses for translation/correction tasks
287
+ - Agents are cached per session key for efficient memory usage
288
+ - Supports task types: `translation`, `correction`, `follow-up`, `invalid`
289
+
290
+ ### Database
291
+
292
+ - Uses SQLite by default (suitable for development)
293
+ - No models are currently defined, but Django is configured for future database needs
294
+ - Run `python manage.py migrate` to set up the database schema
295
+
296
+ ### Caching
297
+
298
+ - In-memory cache is used for sessions (development)
299
+ - **Note:** For production, consider switching to Redis or another persistent cache backend
300
+
301
+ ### CORS Configuration
302
+
303
+ - CORS is configured to allow cross-origin requests
304
+ - In production, configure `CORS_ALLOW_ALL_ORIGINS` and `ALLOWED_HOSTS` appropriately
305
+
306
+ ## Deployment
307
+
308
+ ### Docker Deployment (HuggingFace Spaces)
309
+
310
+ The backend includes a `Dockerfile` configured for HuggingFace Spaces deployment.
311
+
312
+ 1. **Set environment variables** in your Space settings:
313
+ - `SECRET_KEY`
314
+ - `HUGGINGFACEHUB_API_TOKEN`
315
+ - `DEBUG=False`
316
+ - `ALLOWED_HOSTS=your-space-name.hf.space`
317
+ - `CORS_ALLOW_ALL_ORIGINS=False`
318
+ - `CSRF_TRUSTED_ORIGINS=https://your-space-name.hf.space`
319
+ - `SESSION_COOKIE_SECURE=True`
320
+ - `CSRF_COOKIE_SECURE=True`
321
+
322
+ 2. **Push your code** to the Space repository
323
+
324
+ 3. **The API will be available** at `https://your-space-name.hf.space/api/v1/`
325
+
326
+ ### General Production Deployment
327
+
328
+ 1. Set production environment variables (see [Environment Variables](#environment-variables))
329
+ 2. Set `DEBUG=False`
330
+ 3. Configure a proper database (PostgreSQL recommended)
331
+ 4. Set up Redis or another cache backend for sessions
332
+ 5. Use a production ASGI server (Uvicorn with multiple workers or Gunicorn with Uvicorn workers)
333
+ 6. Configure reverse proxy (Nginx, Apache) with SSL/TLS
334
+ 7. Set up static file serving or use a CDN
335
+
336
+ ## Testing
337
+
338
+ To test the API endpoints:
339
+
340
+ ```bash
341
+ # Health check
342
+ curl http://localhost:8000/api/v1/hello/
343
+
344
+ # Send a chat message
345
+ curl -X POST http://localhost:8000/api/v1/chat/ \
346
+ -H "Content-Type: application/json" \
347
+ -d '{"message": "Hello, translate this to Spanish", "mode": "default", "tone": "default"}'
348
+
349
+ # End session
350
+ curl -X POST http://localhost:8000/api/v1/end/
351
+ ```
352
+
353
+ ## Troubleshooting
354
+
355
+ ### Common Issues
356
+
357
+ 1. **Module not found errors**: Ensure your virtual environment is activated and dependencies are installed
358
+ 2. **Secret key errors**: Make sure `SECRET_KEY` is set in your `.env` file
359
+ 3. **HuggingFace API errors**: Verify your `HUGGINGFACEHUB_API_TOKEN` is valid
360
+ 4. **CORS errors**: Check `CORS_ALLOW_ALL_ORIGINS` and `ALLOWED_HOSTS` settings
361
+ 5. **Session not persisting**: Ensure cache backend is configured correctly
362
+
363
+ ## Notes
364
+
365
+ - The application uses in-memory session storage for development. For production, consider using Redis.
366
+ - The HuggingFace model (`openai/gpt-oss-safeguard-20b`) is used for all language processing tasks.
367
+ - Conversation state is managed per Django session using LangGraph's checkpoint system.
368
+ - The structured output wrapper ensures responses follow a consistent JSON schema.
369
+
370
+ ## License
371
+
372
+ See the [LICENSE](LICENSE) file for details.
373
+