Skip to content

priyamjyotsna/ScanSafe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

DietScan (ScanSafe)

DOI License Next.js TypeScript Vercel

A mobile-first Progressive Web App that helps people with medical conditions make safe food choices while grocery shopping.

Live Demo Β· Documentation Β· Report Bug Β· Request Feature


πŸ“‹ Table of Contents


🎯 About The Project

DietScan (ScanSafe) bridges the gap between doctor's dietary advice and real grocery shopping decisions. Instead of manually reading labels and cross-referencing medical restrictions, users simply scan products to get instant, personalized safe/caution/avoid verdicts.

The Problem

People with medical conditions face daily challenges:

  • Complex dietary restrictions from doctors
  • Overwhelming nutrition labels
  • Uncertainty about food safety
  • Time-consuming manual label reading
  • Risk of consuming harmful foods

The Solution

DietScan provides:

  • Instant Analysis: Scan any barcode or ingredient label
  • AI-Powered Guidance: Personalized verdicts based on your condition
  • Medical Accuracy: GPT-4o analyzes products against your diet plan
  • Guest-Friendly: Try 3 scans before signing in
  • Multi-Disease Support: Manage multiple conditions seamlessly

Built With


✨ Key Features

πŸ” AI-Powered Disease Search

  • Intelligent auto-suggestions for specific disease variants
  • 400ms debounced search for smooth UX
  • Handles misspellings naturally
  • 8 clinically specific suggestions per query

🎯 Personalized Diet Plans

  • AI-generated plans tailored to your condition
  • Editable: Add/remove items based on doctor's advice
  • Structured format: Avoid / Prefer / Watch / Nutrients
  • Saved to database or localStorage (guest mode)

πŸ“± Barcode Scanner

  • Client-side scanning with ZXing-js (no server needed)
  • Supports EAN-13, UPC-A, Code 128, QR codes, and more
  • Works on iOS 14.3+ and Android
  • Instant product lookup from Open Food Facts (3M+ products)

πŸ“Έ Ingredient OCR

  • GPT-4o Vision extracts ingredients from photos
  • Fallback when barcode isn't available
  • Works with any language on the label
  • Smart image quality guidance

βœ… Smart Verdicts

  • Three clear states: Safe / Caution / Avoid
  • Disease-specific reasoning (2-3 sentences)
  • Flagged nutrients highlighted
  • Safe alternative suggestions

πŸ“Š Scan History

  • Track all your scans (authenticated users)
  • Review past decisions
  • Export-ready for doctor visits

πŸ‘₯ Multi-Disease Support

  • Manage multiple conditions
  • Quick switching between diseases
  • One active disease at a time for scanning

πŸš€ Guest Mode

  • Try 3 scans without signing in
  • Soft authentication prompts
  • Seamless data migration on sign-in

πŸ› οΈ Tech Stack

Frontend

Technology Version Purpose
Next.js 14.2.35 Full-stack React framework
React 18 UI library
TypeScript 5.x Type safety
Tailwind CSS 3.4.1 Utility-first styling

Backend

Technology Version Purpose
Next.js API Routes 14.2.35 RESTful API
Prisma ORM 7.4.2 Database access
Neon PostgreSQL 16 Serverless database
NextAuth.js 5.0.0-beta.30 Google OAuth

AI & External Services

Service Model/Version Purpose
OpenAI GPT-4o latest Disease suggestions, diet plans
OpenAI GPT-4o-mini latest Verdict analysis
OpenAI GPT-4o Vision latest Ingredient OCR
Open Food Facts API v2 Product database
ZXing-js 0.21.3 Barcode scanning

Testing & Quality

Technology Version Purpose
Vitest 4.0.18 Unit testing
React Testing Library 16.3.2 Component testing
fast-check 4.5.3 Property-based testing
ESLint 8.x Code linting

πŸš€ Getting Started

Prerequisites

  • Node.js 18+ (Download)
  • npm or yarn
  • PostgreSQL database (Neon recommended)
  • OpenAI API key (Get one)
  • Google OAuth credentials (Setup guide)

Installation

  1. Clone the repository

    git clone https://github.com/priyamjyotsna/ScanSafe.git
    cd ScanSafe
  2. Install dependencies

    npm install
  3. Setup environment variables

    cp .env.example .env

    Edit .env with your credentials (see Environment Setup)

  4. Generate Prisma client

    npx prisma generate --schema=prisma/schema.prisma
  5. Run database migrations

    npx prisma migrate dev
  6. Start development server

    npm run dev

    Open http://localhost:3000

Environment Setup

Create a .env file in the root directory:

# Database (Neon PostgreSQL)
DATABASE_URL="postgresql://user:pass@host/dietscan?sslmode=require&pgbouncer=true"
DIRECT_URL="postgresql://user:pass@host/dietscan?sslmode=require"

# NextAuth
NEXTAUTH_URL="http://localhost:3000"
NEXTAUTH_SECRET="your-generated-secret"  # Generate: openssl rand -base64 32

# Google OAuth (from Google Cloud Console)
GOOGLE_CLIENT_ID="your-google-client-id"
GOOGLE_CLIENT_SECRET="your-google-client-secret"

# OpenAI
OPENAI_API_KEY="sk-..."

# App
NEXT_PUBLIC_APP_URL="http://localhost:3000"

# Rate Limiting (requests per window per user/IP)
RATE_LIMIT_DISEASE_SUGGEST=10
RATE_LIMIT_DIET_PLAN_GENERATE=5
RATE_LIMIT_INGREDIENT_OCR=10
RATE_LIMIT_VERDICT=20
RATE_LIMIT_WINDOW_MS=60000

Development with HTTPS (Required for Camera)

Mobile camera access requires HTTPS:

npm run dev:https

Access at https://localhost:3000

Note: You'll see a browser warning about self-signed certificate. Click "Advanced" β†’ "Proceed to localhost".


πŸ’» Usage

For End Users

  1. Setup (one-time):

    • Enter your medical condition
    • Review AI-generated diet plan
    • Edit if needed (add doctor's specific instructions)
  2. Daily Use:

    • Open the app
    • Scan product barcode or ingredient label
    • Get instant verdict: Safe / Caution / Avoid
    • Read reasoning and flagged nutrients
  3. Multi-Disease:

    • Add multiple conditions
    • Switch active disease anytime
    • Each has its own diet plan

For Developers

# Development
npm run dev                    # Start dev server
npm run dev:https              # Start with HTTPS (for camera)
npm run build                  # Build for production
npm start                      # Start production server
npm run lint                   # Lint code

# Database
npx prisma generate            # Generate Prisma client
npx prisma migrate dev         # Run migrations (dev)
npx prisma migrate deploy      # Deploy migrations (prod)
npx prisma studio              # Open visual DB browser

# Testing
npm test                       # Run all tests
npm run test:watch             # Watch mode
npx vitest <file>              # Run specific test

πŸ“ Project Structure

dietscan/
β”œβ”€β”€ prisma/
β”‚   β”œβ”€β”€ schema.prisma              # Database schema
β”‚   └── migrations/                # Migration files
β”‚
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ app/                       # Next.js App Router
β”‚   β”‚   β”œβ”€β”€ api/                   # API routes
β”‚   β”‚   β”‚   β”œβ”€β”€ disease/suggest/   # AI disease suggestions
β”‚   β”‚   β”‚   β”œβ”€β”€ diet-plan/         # Diet plan generation
β”‚   β”‚   β”‚   β”œβ”€β”€ scan/              # Barcode, OCR, verdict
β”‚   β”‚   β”‚   └── user/              # User profile
β”‚   β”‚   β”œβ”€β”€ setup/                 # Disease selection & diet plan
β”‚   β”‚   β”œβ”€β”€ scan/                  # Scanner & verdict
β”‚   β”‚   β”œβ”€β”€ profile/               # User profile
β”‚   β”‚   └── (auth)/signin/         # Sign-in page
β”‚   β”‚
β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”œβ”€β”€ auth/                  # Auth components
β”‚   β”‚   β”œβ”€β”€ diet-plan/             # Diet plan display
β”‚   β”‚   β”œβ”€β”€ disease/               # Disease search
β”‚   β”‚   β”œβ”€β”€ scanner/               # Barcode & OCR
β”‚   β”‚   β”œβ”€β”€ verdict/               # Verdict display
β”‚   β”‚   └── ui/                    # Reusable UI components
β”‚   β”‚
β”‚   β”œβ”€β”€ hooks/                     # Custom React hooks
β”‚   β”œβ”€β”€ lib/                       # Core utilities
β”‚   β”œβ”€β”€ types/                     # TypeScript types
β”‚   β”œβ”€β”€ utils/                     # Helper functions
β”‚   └── test/                      # Test setup
β”‚
β”œβ”€β”€ public/                        # Static assets
β”œβ”€β”€ .env.example                   # Environment template
β”œβ”€β”€ DOCUMENTATION.md               # Complete documentation
β”œβ”€β”€ DietScan-TechSpec.md           # Technical specification
└── README.md                      # This file

πŸ§ͺ Testing

Test Coverage

  • Components: ~80% coverage
  • Hooks: ~90% coverage
  • Utils: ~95% coverage
  • API routes: ~70% coverage

Running Tests

# Run all tests once
npm test

# Watch mode (re-run on file changes)
npm run test:watch

# Run specific test file
npx vitest src/components/Button.test.tsx

# Run with coverage report
npx vitest --coverage

Test Structure

Tests are co-located with source files:

src/components/Button.tsx
src/components/Button.test.tsx

🚒 Deployment

Vercel (Recommended)

  1. Push to GitHub

    git push origin main
  2. Import in Vercel

    • Go to vercel.com
    • Click "New Project"
    • Import from GitHub
    • Select your repository
  3. Configure Environment Variables

    In Vercel dashboard β†’ Settings β†’ Environment Variables, add all variables from .env.example

  4. Update Google OAuth

    In Google Cloud Console β†’ APIs & Services β†’ Credentials:

    • Add https://your-app.vercel.app/api/auth/callback/google to authorized redirect URIs
  5. Deploy

    Vercel automatically deploys on push to main

Build Configuration

The build script in package.json includes Prisma client generation:

{
  "scripts": {
    "build": "prisma generate --schema=prisma/schema.prisma && next build"
  }
}

πŸ“š API Documentation

Endpoints

Endpoint Method Purpose
/api/disease/suggest POST Get AI disease suggestions
/api/diet-plan/generate POST Generate personalized diet plan
/api/diet-plan/save POST Save user's diet plan
/api/scan/barcode POST Lookup product by barcode
/api/scan/ingredients POST Extract ingredients via OCR
/api/scan/verdict POST Analyze product safety
/api/scan/history POST Save scan to history
/api/user/profile GET Get user profile
/api/user/profile PUT Update user profile
/api/user/active-disease PATCH Switch active disease

Example: Get Verdict

POST /api/scan/verdict
Content-Type: application/json

{
  "diseaseName": "Type 2 Diabetes",
  "dietPlan": {
    "avoid": ["High sugar foods"],
    "prefer": ["Whole grains"],
    "watch": ["Carbohydrate intake"],
    "nutrients": { "Sugar": "< 5g/serving" }
  },
  "product": {
    "name": "Chocolate Bar",
    "ingredients": ["Sugar", "Cocoa", "Milk"],
    "nutrients": { "sugar": 25, "carbohydrates": 30 }
  }
}

Response:

{
  "verdict": "AVOID",
  "reason": "Very high sugar content (25g) exceeds your limit of 5g per serving. This could cause dangerous blood sugar spikes for Type 2 Diabetes.",
  "flaggedNutrients": ["sugar"],
  "safeAlternative": "Look for sugar-free dark chocolate with <5g sugar per serving."
}

See DOCUMENTATION.md for complete API documentation.


🀝 Contributing

Contributions are what make the open source community amazing! Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'feat: add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Commit Convention

We follow Conventional Commits:

  • feat: New feature
  • fix: Bug fix
  • docs: Documentation changes
  • style: Code style changes (formatting, etc.)
  • refactor: Code refactoring
  • test: Adding or updating tests
  • chore: Maintenance tasks

πŸ“– Citation

If you use this project in your research or work, please cite it:

@software{dietscan2026,
  author = {Priyamjyotsna},
  title = {DietScan (ScanSafe): AI-Powered Food Safety Scanner for Medical Conditions},
  year = {2026},
  publisher = {GitHub},
  url = {https://github.com/priyamjyotsna/ScanSafe},
  doi = {10.5281/zenodo.18942716}
}

DOI: https://doi.org/10.5281/zenodo.18942716


πŸ“„ License

This project is private and proprietary. All rights reserved.


πŸ“§ Contact

Priyamjyotsna - @priyamjyotsna

Project Link: https://github.com/priyamjyotsna/ScanSafe

Live Demo: https://scan-safe.vercel.app


πŸ™ Acknowledgments


Built with ❀️ for better health management

⬆ Back to Top