kaeizen commited on
Commit
f0abbdb
Β·
1 Parent(s): a2c33e3

add readme

Browse files

# Conflicts:
# README.md

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