add readme
Browse files# Conflicts:
# README.md
README.md
CHANGED
|
@@ -394,3 +394,376 @@ short_description: AI Translation and Grammar Correction
|
|
| 394 |
---
|
| 395 |
|
| 396 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
---
|
| 395 |
|
| 396 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 397 |
+
# Grammo Backend
|
| 398 |
+
|
| 399 |
+
Django REST API backend for Grammo, an AI-powered translation and grammar correction service.
|
| 400 |
+
|
| 401 |
+
## Overview
|
| 402 |
+
|
| 403 |
+
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.
|
| 404 |
+
|
| 405 |
+
## Features
|
| 406 |
+
|
| 407 |
+
- π **Translation Service** - Natural, contextually appropriate translations between languages
|
| 408 |
+
- βοΈ **Grammar Correction** - Fixes grammar, spelling, and punctuation errors
|
| 409 |
+
- π¬ **Session Management** - Maintains conversation context using Django sessions and LangGraph checkpoints
|
| 410 |
+
- π **Customizable Modes** - Supports Default and Grammar modes
|
| 411 |
+
- π¨ **Tone Control** - Configurable tone (Default, Formal, Casual) for responses
|
| 412 |
+
- π **Security** - CORS support, CSRF protection, secure session management
|
| 413 |
+
- π¦ **HuggingFace Integration** - Uses GPT-OSS-Safeguard-20B model via HuggingFace API
|
| 414 |
+
|
| 415 |
+
## Tech Stack
|
| 416 |
+
|
| 417 |
+
- **Django 5.2.7** - Web framework
|
| 418 |
+
- **Django REST Framework** - API development
|
| 419 |
+
- **LangChain** - AI agent orchestration
|
| 420 |
+
- **LangGraph** - Conversation state management
|
| 421 |
+
- **HuggingFace** - Language model integration (GPT-OSS-Safeguard-20B)
|
| 422 |
+
- **Python 3.14+** - Programming language
|
| 423 |
+
- **SQLite** - Database (development)
|
| 424 |
+
- **Uvicorn** - ASGI server
|
| 425 |
+
|
| 426 |
+
## Prerequisites
|
| 427 |
+
|
| 428 |
+
- Python 3.14 or higher
|
| 429 |
+
- pip (Python package manager)
|
| 430 |
+
- HuggingFace API Token ([Get one here](https://huggingface.co/settings/tokens))
|
| 431 |
+
|
| 432 |
+
## Installation
|
| 433 |
+
|
| 434 |
+
### 1. Navigate to the backend directory
|
| 435 |
+
|
| 436 |
+
```bash
|
| 437 |
+
cd backend
|
| 438 |
+
```
|
| 439 |
+
|
| 440 |
+
### 2. Create and activate a virtual environment
|
| 441 |
+
|
| 442 |
+
```bash
|
| 443 |
+
# Create virtual environment
|
| 444 |
+
python -m venv venv
|
| 445 |
+
|
| 446 |
+
# Activate virtual environment
|
| 447 |
+
# On macOS/Linux:
|
| 448 |
+
source venv/bin/activate
|
| 449 |
+
# On Windows:
|
| 450 |
+
venv\Scripts\activate
|
| 451 |
+
```
|
| 452 |
+
|
| 453 |
+
### 3. Install dependencies
|
| 454 |
+
|
| 455 |
+
```bash
|
| 456 |
+
pip install -r requirements.txt
|
| 457 |
+
```
|
| 458 |
+
|
| 459 |
+
### 4. Set up environment variables
|
| 460 |
+
|
| 461 |
+
Create a `.env` file in the `backend` directory:
|
| 462 |
+
|
| 463 |
+
```bash
|
| 464 |
+
touch .env
|
| 465 |
+
```
|
| 466 |
+
|
| 467 |
+
Add the following environment variables (see [Environment Variables](#environment-variables) section for details):
|
| 468 |
+
|
| 469 |
+
```env
|
| 470 |
+
SECRET_KEY=your-secret-key-here
|
| 471 |
+
HUGGINGFACEHUB_API_TOKEN=your-huggingface-api-token
|
| 472 |
+
DEBUG=True
|
| 473 |
+
```
|
| 474 |
+
|
| 475 |
+
To generate a Django secret key:
|
| 476 |
+
|
| 477 |
+
```bash
|
| 478 |
+
python -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())"
|
| 479 |
+
```
|
| 480 |
+
|
| 481 |
+
### 5. Run database migrations
|
| 482 |
+
|
| 483 |
+
```bash
|
| 484 |
+
python manage.py migrate
|
| 485 |
+
```
|
| 486 |
+
|
| 487 |
+
## Environment Variables
|
| 488 |
+
|
| 489 |
+
Create a `.env` file in the `backend` directory with the following variables:
|
| 490 |
+
|
| 491 |
+
### Required Variables
|
| 492 |
+
|
| 493 |
+
```env
|
| 494 |
+
# Django Secret Key (generate one using the command above)
|
| 495 |
+
SECRET_KEY=your-secret-key-here
|
| 496 |
+
|
| 497 |
+
# HuggingFace API Token
|
| 498 |
+
HUGGINGFACEHUB_API_TOKEN=your-huggingface-api-token
|
| 499 |
+
```
|
| 500 |
+
|
| 501 |
+
### Optional Development Variables
|
| 502 |
+
|
| 503 |
+
```env
|
| 504 |
+
# Debug mode (default: True)
|
| 505 |
+
DEBUG=True
|
| 506 |
+
|
| 507 |
+
# Session security (default: False for development)
|
| 508 |
+
SESSION_COOKIE_SECURE=False # Set to True in production (requires HTTPS)
|
| 509 |
+
CSRF_COOKIE_SECURE=False # Set to True in production (requires HTTPS)
|
| 510 |
+
|
| 511 |
+
# CORS settings
|
| 512 |
+
CORS_ALLOW_ALL_ORIGINS=True # Set to False in production and specify origins
|
| 513 |
+
```
|
| 514 |
+
|
| 515 |
+
### Optional Production Variables
|
| 516 |
+
|
| 517 |
+
```env
|
| 518 |
+
# Allowed hosts (comma-separated)
|
| 519 |
+
ALLOWED_HOSTS=yourdomain.com,www.yourdomain.com
|
| 520 |
+
|
| 521 |
+
# CSRF trusted origins (comma-separated)
|
| 522 |
+
CSRF_TRUSTED_ORIGINS=https://yourdomain.com,https://www.yourdomain.com
|
| 523 |
+
|
| 524 |
+
# Security settings
|
| 525 |
+
SECURE_SSL_REDIRECT=True
|
| 526 |
+
SECURE_CONTENT_TYPE_NOSNIFF=True
|
| 527 |
+
SECURE_HSTS_SECONDS=31536000
|
| 528 |
+
SECURE_HSTS_INCLUDE_SUBDOMAINS=True
|
| 529 |
+
SECURE_HSTS_PRELOAD=True
|
| 530 |
+
```
|
| 531 |
+
|
| 532 |
+
## Running the Application
|
| 533 |
+
|
| 534 |
+
### Development Mode
|
| 535 |
+
|
| 536 |
+
**Option 1: Django Development Server (with warnings)**
|
| 537 |
+
|
| 538 |
+
```bash
|
| 539 |
+
python manage.py runserver
|
| 540 |
+
```
|
| 541 |
+
|
| 542 |
+
The server will run on `http://localhost:8000`
|
| 543 |
+
|
| 544 |
+
**Option 2: Uvicorn ASGI Server (production-like, no warnings)**
|
| 545 |
+
|
| 546 |
+
```bash
|
| 547 |
+
uvicorn backend.asgi:application --host 0.0.0.0 --port 8000 --reload
|
| 548 |
+
```
|
| 549 |
+
|
| 550 |
+
### Production Mode
|
| 551 |
+
|
| 552 |
+
```bash
|
| 553 |
+
# Set DEBUG=False in .env
|
| 554 |
+
uvicorn backend.asgi:application --host 0.0.0.0 --port 8000
|
| 555 |
+
|
| 556 |
+
# With multiple workers:
|
| 557 |
+
uvicorn backend.asgi:application --host 0.0.0.0 --port 8000 --workers 4
|
| 558 |
+
```
|
| 559 |
+
|
| 560 |
+
### Standalone Script (for HuggingFace Spaces)
|
| 561 |
+
|
| 562 |
+
The backend can also be run as a standalone script:
|
| 563 |
+
|
| 564 |
+
```bash
|
| 565 |
+
python app.py
|
| 566 |
+
```
|
| 567 |
+
|
| 568 |
+
This uses the `PORT` environment variable (defaults to 7860) and is configured for HuggingFace Spaces deployment.
|
| 569 |
+
|
| 570 |
+
## API Endpoints
|
| 571 |
+
|
| 572 |
+
### Base URL
|
| 573 |
+
|
| 574 |
+
All endpoints are prefixed with `/api/v1/`
|
| 575 |
+
|
| 576 |
+
### `GET /api/v1/hello/`
|
| 577 |
+
|
| 578 |
+
Health check endpoint.
|
| 579 |
+
|
| 580 |
+
**Response:**
|
| 581 |
+
```json
|
| 582 |
+
{
|
| 583 |
+
"message": "Hello from Grammo!"
|
| 584 |
+
}
|
| 585 |
+
```
|
| 586 |
+
|
| 587 |
+
### `POST /api/v1/chat/`
|
| 588 |
+
|
| 589 |
+
Send a message to start or continue a chat session.
|
| 590 |
+
|
| 591 |
+
**Request Body:**
|
| 592 |
+
```json
|
| 593 |
+
{
|
| 594 |
+
"message": "Translate this text to French",
|
| 595 |
+
"chatSession": 0,
|
| 596 |
+
"mode": "default",
|
| 597 |
+
"tone": "default"
|
| 598 |
+
}
|
| 599 |
+
```
|
| 600 |
+
|
| 601 |
+
**Parameters:**
|
| 602 |
+
- `message` (required): The user's message
|
| 603 |
+
- `chatSession` (optional): Session identifier to maintain conversation context
|
| 604 |
+
- `mode` (optional): `"default"` or `"grammar"` - Determines how the message is processed
|
| 605 |
+
- `tone` (optional): `"default"`, `"formal"`, or `"casual"` - Sets the tone of the response
|
| 606 |
+
|
| 607 |
+
**Response (Success):**
|
| 608 |
+
```json
|
| 609 |
+
{
|
| 610 |
+
"status": "success",
|
| 611 |
+
"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"
|
| 612 |
+
}
|
| 613 |
+
```
|
| 614 |
+
|
| 615 |
+
**Response (Error):**
|
| 616 |
+
```json
|
| 617 |
+
{
|
| 618 |
+
"status": "error",
|
| 619 |
+
"response": "Invalid message."
|
| 620 |
+
}
|
| 621 |
+
```
|
| 622 |
+
|
| 623 |
+
### `POST /api/v1/end/`
|
| 624 |
+
|
| 625 |
+
End the current chat session and clear conversation history.
|
| 626 |
+
|
| 627 |
+
**Request Body:**
|
| 628 |
+
```json
|
| 629 |
+
{}
|
| 630 |
+
```
|
| 631 |
+
|
| 632 |
+
**Response (Success):**
|
| 633 |
+
```json
|
| 634 |
+
{
|
| 635 |
+
"status": "success",
|
| 636 |
+
"message": "Session ended successfully"
|
| 637 |
+
}
|
| 638 |
+
```
|
| 639 |
+
|
| 640 |
+
**Response (Error):**
|
| 641 |
+
```json
|
| 642 |
+
{
|
| 643 |
+
"status": "error",
|
| 644 |
+
"response": "No active session."
|
| 645 |
+
}
|
| 646 |
+
```
|
| 647 |
+
|
| 648 |
+
## Project Structure
|
| 649 |
+
|
| 650 |
+
```
|
| 651 |
+
backend/
|
| 652 |
+
βββ agent_manager/ # AI agent management module
|
| 653 |
+
β βββ __init__.py # LangChain agent setup, session management
|
| 654 |
+
βββ api/ # Django REST API application
|
| 655 |
+
β βββ views.py # API view handlers (chat, hello, end)
|
| 656 |
+
β βββ urls.py # URL routing
|
| 657 |
+
β βββ apps.py # App configuration
|
| 658 |
+
βββ backend/ # Django project settings
|
| 659 |
+
β βββ settings.py # Django configuration
|
| 660 |
+
β βββ urls.py # Main URL configuration
|
| 661 |
+
β βββ asgi.py # ASGI application
|
| 662 |
+
β βββ wsgi.py # WSGI application
|
| 663 |
+
βββ app.py # Standalone entry point (HuggingFace Spaces)
|
| 664 |
+
βββ manage.py # Django management script
|
| 665 |
+
βββ requirements.txt # Python dependencies
|
| 666 |
+
βββ Dockerfile # Docker configuration for deployment
|
| 667 |
+
βββ README.md # This file
|
| 668 |
+
```
|
| 669 |
+
|
| 670 |
+
## Development
|
| 671 |
+
|
| 672 |
+
### Session Management
|
| 673 |
+
|
| 674 |
+
- Sessions are managed using Django's session framework
|
| 675 |
+
- Session data is stored in the cache backend (in-memory for development)
|
| 676 |
+
- Each session maintains its own LangGraph agent with conversation checkpointing
|
| 677 |
+
- Sessions expire after 24 hours of inactivity or when explicitly ended
|
| 678 |
+
|
| 679 |
+
### Agent Architecture
|
| 680 |
+
|
| 681 |
+
- Uses LangChain's `create_agent` with a structured output wrapper
|
| 682 |
+
- Structured output ensures consistent JSON responses for translation/correction tasks
|
| 683 |
+
- Agents are cached per session key for efficient memory usage
|
| 684 |
+
- Supports task types: `translation`, `correction`, `follow-up`, `invalid`
|
| 685 |
+
|
| 686 |
+
### Database
|
| 687 |
+
|
| 688 |
+
- Uses SQLite by default (suitable for development)
|
| 689 |
+
- No models are currently defined, but Django is configured for future database needs
|
| 690 |
+
- Run `python manage.py migrate` to set up the database schema
|
| 691 |
+
|
| 692 |
+
### Caching
|
| 693 |
+
|
| 694 |
+
- In-memory cache is used for sessions (development)
|
| 695 |
+
- **Note:** For production, consider switching to Redis or another persistent cache backend
|
| 696 |
+
|
| 697 |
+
### CORS Configuration
|
| 698 |
+
|
| 699 |
+
- CORS is configured to allow cross-origin requests
|
| 700 |
+
- In production, configure `CORS_ALLOW_ALL_ORIGINS` and `ALLOWED_HOSTS` appropriately
|
| 701 |
+
|
| 702 |
+
## Deployment
|
| 703 |
+
|
| 704 |
+
### Docker Deployment (HuggingFace Spaces)
|
| 705 |
+
|
| 706 |
+
The backend includes a `Dockerfile` configured for HuggingFace Spaces deployment.
|
| 707 |
+
|
| 708 |
+
1. **Set environment variables** in your Space settings:
|
| 709 |
+
- `SECRET_KEY`
|
| 710 |
+
- `HUGGINGFACEHUB_API_TOKEN`
|
| 711 |
+
- `DEBUG=False`
|
| 712 |
+
- `ALLOWED_HOSTS=your-space-name.hf.space`
|
| 713 |
+
- `CORS_ALLOW_ALL_ORIGINS=False`
|
| 714 |
+
- `CSRF_TRUSTED_ORIGINS=https://your-space-name.hf.space`
|
| 715 |
+
- `SESSION_COOKIE_SECURE=True`
|
| 716 |
+
- `CSRF_COOKIE_SECURE=True`
|
| 717 |
+
|
| 718 |
+
2. **Push your code** to the Space repository
|
| 719 |
+
|
| 720 |
+
3. **The API will be available** at `https://your-space-name.hf.space/api/v1/`
|
| 721 |
+
|
| 722 |
+
### General Production Deployment
|
| 723 |
+
|
| 724 |
+
1. Set production environment variables (see [Environment Variables](#environment-variables))
|
| 725 |
+
2. Set `DEBUG=False`
|
| 726 |
+
3. Configure a proper database (PostgreSQL recommended)
|
| 727 |
+
4. Set up Redis or another cache backend for sessions
|
| 728 |
+
5. Use a production ASGI server (Uvicorn with multiple workers or Gunicorn with Uvicorn workers)
|
| 729 |
+
6. Configure reverse proxy (Nginx, Apache) with SSL/TLS
|
| 730 |
+
7. Set up static file serving or use a CDN
|
| 731 |
+
|
| 732 |
+
## Testing
|
| 733 |
+
|
| 734 |
+
To test the API endpoints:
|
| 735 |
+
|
| 736 |
+
```bash
|
| 737 |
+
# Health check
|
| 738 |
+
curl http://localhost:8000/api/v1/hello/
|
| 739 |
+
|
| 740 |
+
# Send a chat message
|
| 741 |
+
curl -X POST http://localhost:8000/api/v1/chat/ \
|
| 742 |
+
-H "Content-Type: application/json" \
|
| 743 |
+
-d '{"message": "Hello, translate this to Spanish", "mode": "default", "tone": "default"}'
|
| 744 |
+
|
| 745 |
+
# End session
|
| 746 |
+
curl -X POST http://localhost:8000/api/v1/end/
|
| 747 |
+
```
|
| 748 |
+
|
| 749 |
+
## Troubleshooting
|
| 750 |
+
|
| 751 |
+
### Common Issues
|
| 752 |
+
|
| 753 |
+
1. **Module not found errors**: Ensure your virtual environment is activated and dependencies are installed
|
| 754 |
+
2. **Secret key errors**: Make sure `SECRET_KEY` is set in your `.env` file
|
| 755 |
+
3. **HuggingFace API errors**: Verify your `HUGGINGFACEHUB_API_TOKEN` is valid
|
| 756 |
+
4. **CORS errors**: Check `CORS_ALLOW_ALL_ORIGINS` and `ALLOWED_HOSTS` settings
|
| 757 |
+
5. **Session not persisting**: Ensure cache backend is configured correctly
|
| 758 |
+
|
| 759 |
+
## Notes
|
| 760 |
+
|
| 761 |
+
- The application uses in-memory session storage for development. For production, consider using Redis.
|
| 762 |
+
- The HuggingFace model (`openai/gpt-oss-safeguard-20b`) is used for all language processing tasks.
|
| 763 |
+
- Conversation state is managed per Django session using LangGraph's checkpoint system.
|
| 764 |
+
- The structured output wrapper ensures responses follow a consistent JSON schema.
|
| 765 |
+
|
| 766 |
+
## License
|
| 767 |
+
|
| 768 |
+
See the [LICENSE](LICENSE) file for details.
|
| 769 |
+
|