Skip to content

Sustainability verification API that combats misleading green marketing practices. The system allows users to scan product barcodes and receive an objective analysis based on real certifications, not just marketing claims.

Notifications You must be signed in to change notification settings

claudiaraphael/Truth-Label-Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

47 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Truth Label - Backend API ๐Ÿท๏ธ

RESTful API backend for Truth Label application. Integrates with Open Food Facts to verify sustainability claims and calculate reliability scores for food products.

๐Ÿ“‹ About

This backend provides the core functionality for the Truth Label project, including product scanning, score calculation, database management, and API endpoints for the frontend application.

Developed by: Clรกudia Rodrigues
Context: MVP Project for Post-Graduate Software Engineering - PUC-Rio

โœจ Features

  • ๐Ÿ”Œ RESTful API with OpenAPI 3.0 documentation
  • ๐ŸŒ Open Food Facts Integration for product data
  • ๐Ÿงฎ Sustainability Score Calculator (0-100 scale)
  • ๐Ÿ’พ SQLite Database for product history
  • ๐Ÿ“ Comment System for user feedback
  • ๐Ÿ‘ฅ User Management system
  • ๐Ÿ“Š Swagger UI for interactive API documentation
  • ๐Ÿ”’ CORS Enabled for frontend integration

๐Ÿ—๏ธ Architecture

Tech Stack

  • Flask 2.3.3 - Web framework
  • Flask-SQLAlchemy 3.0.5 - ORM
  • SQLite - Database
  • Pydantic 2.5.2 - Data validation
  • Flasgger 0.9.7.1 - Swagger documentation
  • Flask-CORS 4.0.0 - CORS handling
  • Requests 2.31.0 - HTTP client for Open Food Facts

Project Structure

backend/
โ”œโ”€โ”€ model/                  # Database models
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ product.py         # Product model
โ”‚   โ”œโ”€โ”€ comment.py         # Comment model
โ”‚   โ””โ”€โ”€ user.py            # User model
โ”œโ”€โ”€ routes/                 # API endpoints (Blueprints)
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ product_bp.py      # Product routes
โ”‚   โ”œโ”€โ”€ comment_bp.py      # Comment routes
โ”‚   โ””โ”€โ”€ user_bp.py         # User routes
โ”œโ”€โ”€ schemas/                # Pydantic validation schemas
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ product_schemas.py
โ”‚   โ”œโ”€โ”€ comment_schemas.py
โ”‚   โ””โ”€โ”€ user_schemas.py
โ”œโ”€โ”€ scripts/                # Utilities
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ””โ”€โ”€ score_calculator.py
โ”œโ”€โ”€ app.py                  # Flask application factory
โ”œโ”€โ”€ extensions.py           # SQLAlchemy instance
โ”œโ”€โ”€ requirements.txt        # Python dependencies
โ””โ”€โ”€ truthlable.db          # SQLite database (auto-generated)

๐Ÿš€ Installation and Setup

Prerequisites

  • Python 3.8+
  • pip
  • Virtual environment (recommended)

Installation Steps

  1. Clone the repository:
git clone <backend-repo-url>
cd truth-label-backend
  1. Create and activate virtual environment:
# Windows
python -m venv venv
venv\Scripts\activate

# Linux/Mac
python3 -m venv venv
source venv/bin/activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Run the application:
python app.py

The API will be available at http://127.0.0.1:5000

Database Initialization

The database is automatically created on first run. No manual setup required.

๐Ÿ“š API Documentation

Access the interactive Swagger documentation at:

http://127.0.0.1:5000/apidocs/

๐Ÿ”Œ API Endpoints

Products

POST /product/scan

Scans a barcode, checks local database, fetches from Open Food Facts if needed.

Request Body:

{
  "barcode": "7891234567890"
}

Response (201 Created):

{
  "message": "Product scanned and saved successfully",
  "product": {
    "id": 1,
    "name": "Eco Green Soap",
    "barcode": "7891234567890",
    "score": 85.5,
    "nova_group": 1,
    "image_url": "https://...",
    "ingredients_analysis_tags": "en:vegan,en:palm-oil-free",
    "labels_tags": "en:organic,en:fair-trade",
    "allergens_tags": "",
    "additives_tags": "",
    "date_inserted": "2025-12-20T10:30:00"
  }
}

GET /product

Search product in local history by ID, name, or barcode.

Query Parameters:

  • id (integer): Product ID
  • name (string): Product name (partial match)
  • barcode (string): Barcode

Examples:

GET /product?barcode=7891234567890
GET /product?name=soap
GET /product?id=1

GET /products-list

List all products in history.

Response (200 OK):

[
  {
    "id": 1,
    "name": "Eco Green Soap",
    "barcode": "7891234567890",
    "score": 85.5,
    ...
  },
  ...
]

PATCH /product/{product_id}

Update product name or barcode.

Request Body:

{
  "name": "New Product Name",
  "barcode": "9876543210987"
}

DELETE /product/delete

Remove product from history.

Request Body:

{
  "barcode": "7891234567890"
}

Comments

POST /comment

Create a comment for a product.

Request Body:

{
  "product_id": 1,
  "author_name": "John Doe",
  "text": "This product is truly sustainable!",
  "n_estrela": 5
}

GET /comment

List all comments.

GET /comment/{comment_id}

Get specific comment by ID.

GET /comment/product/{product_id}

List all comments for a specific product.

DELETE /comment/{comment_id}

Delete a comment.

Users

POST /user

Create a new user.

Request Body:

{
  "username": "johndoe",
  "email": "[email protected]"
}

GET /user

List all users.

GET /user/{user_id}

Get specific user by ID.

DELETE /user/{user_id}

Delete a user.

๐Ÿงฎ Sustainability Score Algorithm

The score (0-100) is calculated based on multiple weighted criteria:

High Priority Factors (+/- 10-20 points)

Certifications (VIP Labels):

  • Organic certifications: +15
  • Fair Trade: +15
  • EU Organic: +15
  • Rainforest Alliance: +15

Ingredient Analysis:

  • Vegan: +10
  • Palm Oil Free: +10
  • Contains Palm Oil: -20

Medium Priority Factors (+/- 5-15 points)

NOVA Group (Processing Level):

  • NOVA 1 (Unprocessed/Minimally processed): +10
  • NOVA 4 (Ultra-processed): -15

Additives:

  • Each additive: -2

Base Score

Starting point: 50 (neutral)

Formula

def calculate_score(off_data):
    score = 50  # Base neutral score
    
    # High Priority: Certifications
    for label in labels_tags:
        if label in ['en:organic', 'en:fair-trade', ...]:
            score += 15
    
    # High Priority: Ingredient Analysis
    if 'en:vegan' in analysis:
        score += 10
    if 'en:palm-oil-free' in analysis:
        score += 10
    if 'en:palm-oil' in analysis:
        score -= 20
    
    # Medium Priority: NOVA Group
    if nova_group == 1:
        score += 10
    if nova_group == 4:
        score -= 15
    
    # Medium Priority: Additives
    score -= (len(additives) * 2)
    
    # Clamp between 0-100
    return max(0, min(100, score))

๐Ÿ—„๏ธ Database Models

Product Model

class Product(db.Model):
    id: int                              # Primary key
    name: str                            # Product name
    barcode: str                         # Unique barcode (EAN-13)
    image_url: str                       # Product image URL
    score: float                         # Sustainability score (0-100)
    nova_group: int                      # Processing level (1-4)
    ingredients_analysis_tags: str       # Comma-separated tags
    labels_tags: str                     # Certification tags
    allergens_tags: str                  # Allergen tags
    additives_tags: str                  # Additive tags
    date_inserted: datetime              # Scan timestamp
    comments: List[Comment]              # Related comments

Comment Model

class Comment(db.Model):
    id: int                    # Primary key
    text: str                  # Comment text (max 500 chars)
    author: str                # Author name
    n_estrela: int             # Rating (0-5 stars)
    date_inserted: datetime    # Creation timestamp
    product_id: int            # Foreign key to Product

User Model

class User(db.Model):
    id: int                    # Primary key
    username: str              # Unique username
    email: str                 # Unique email
    date_created: datetime     # Registration timestamp

๐Ÿ”„ Data Flow

Product Scan Flow

1. Frontend sends POST /product/scan with barcode
2. Backend checks local database
   โ”œโ”€ Found โ†’ Return existing product
   โ””โ”€ Not found โ†’ Continue
3. Query Open Food Facts API
4. Extract relevant data
5. Calculate sustainability score
6. Save to local database
7. Return product with score to frontend

Open Food Facts Integration

off_url = f"https://world.openfoodfacts.net/api/v2/product/{barcode}"
response = requests.get(off_url, timeout=10)
product_data = response.json()

โš™๏ธ Configuration

CORS Settings

CORS(app, resources={
    r"/*": {
        "origins": ["http://127.0.0.1:5500"],
        "methods": ["GET", "POST", "PUT", "DELETE", "PATCH"],
        "allow_headers": ["Content-Type", "Authorization"]
    }
})

Database Configuration

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///truthlable.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

๐Ÿงช Testing

Using Swagger UI

  1. Navigate to http://127.0.0.1:5000/apidocs/
  2. Select an endpoint
  3. Click "Try it out"
  4. Fill in the parameters
  5. Click "Execute"

Using cURL

# Scan a product
curl -X POST http://127.0.0.1:5000/product/scan \
  -H "Content-Type: application/json" \
  -d '{"barcode": "7891234567890"}'

# Search by name
curl http://127.0.0.1:5000/product?name=soap

# List all products
curl http://127.0.0.1:5000/products-list

๐Ÿ› Troubleshooting

Database Issues

# Delete and recreate database
rm truthlable.db
python app.py  # Will auto-create new database

Port Already in Use

# Change port in app.py
app.run(debug=True, port=5001)  # Use different port

CORS Errors

Ensure frontend origin is listed in CORS configuration:

"origins": ["http://127.0.0.1:5500"]

๐Ÿ“ฆ Dependencies

flasgger==0.9.7.1
Flask==2.3.3
flask-cors==4.0.0
pydantic==2.5.2
requests==2.31.0
python-dotenv==1.0.0
SQLAlchemy==2.0.15
Flask-SQLAlchemy==3.0.5
Werkzeug==2.3.7

๐Ÿ”’ Security Considerations

  • Input validation using Pydantic schemas
  • SQL injection prevention via SQLAlchemy ORM
  • Error handling for external API failures
  • Database transactions with rollback support

๐Ÿค Contributing

Contributions are welcome! To contribute:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/NewFeature)
  3. Commit your changes (git commit -m 'Add new feature')
  4. Push to the branch (git push origin feature/NewFeature)
  5. Open a Pull Request

๐Ÿ“ License

This project is open-source and available under the MIT License.

๐Ÿ”— Related Repositories

  • Frontend Repository: [Link to frontend repo]

๐Ÿ“ง Contact

Clรกudia Rodrigues
Post-Graduate Software Engineering Student - PUC-Rio

๐Ÿ™ Acknowledgments

  • Open Food Facts for the comprehensive API and database
  • Flask community for excellent documentation
  • PUC-Rio for academic support

โญ If this project was useful to you, consider giving it a star!

About

Sustainability verification API that combats misleading green marketing practices. The system allows users to scan product barcodes and receive an objective analysis based on real certifications, not just marketing claims.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages