How majimba/ai-calorie-estimator works
Project Type & Purpose
• This is a full-stack web application (built with Next.js 13’s App Router) that lets users upload or capture food images, sends them to AI (OpenAI GPT-4 Vision) for calorie estimation, and displays results in the browser.
File Structure Highlights
• src/app – Next.js app routes and API routes.
• app/page.tsx, layout.tsx – the main UI shell
• app/api/estimate-calories/route.ts – core backend endpoint
• other api/* routes – debug, direct-estimate, mobile-estimate, etc.
• src/components – React UI components (ImageUploader, CalorieEstimator, ResultDisplay).
• src/lib – wrappers and utilities for Cloudinary, OpenAI, config, error handling, image processing.
• Config files – next.config.js, tailwind.config.js, tsconfig.json, vercel.json for deployment.
README Insights
• Frontend: Next.js + React + Tailwind CSS + React Hook Form + Zod
• Backend: Next.js API Routes (serverless functions)
• Services: Cloudinary for image storage, OpenAI GPT-4 Vision for analysis
• No persistent database – flows are stateless serverless calls.
a. Frontend (Browser)
• React UI components → Form validation (React Hook Form + Zod) → HTTP POST to /api/estimate-calories
b. Next.js Serverless API Layer
• Route handler (estimate-calories)
• lib/cloudinary.ts → uploads image → returns secure URL
• lib/openai.ts → sends image URL to GPT-4 Vision → receives JSON with calories, items, confidence
• lib/image-utils.ts / lib/error.ts handle preprocessing & errors
c. External Services
• Cloudinary (Image storage & CDN)
• OpenAI API (Vision model)
d. Frontend Rendering
• Receives JSON → CalorieResult / ResultsDisplay components → update UI
Architectural Patterns & Principles
• Separation of concerns: UI vs business logic vs external-service code in src/lib
• Serverless functions for scalability and pay-per-use billing
• Stateless, request → response flow (no DB)
• Modular component design (atomic UI components)