This is a desktop application built with Tauri v2 that allows users to upload an image of food and get calorie analysis. The backend, built with Python and Flask, sends images to the Google Gemini API for analysis, which returns estimated calorie counts and identified food items. The application stores analysis results in a PostgreSQL database with Redis caching for improved performance.
Watch the application in action:
Screen.Recording.2025-05-25.at.10.26.08.PM.mov
calorie-counter/
βββ backend/
β βββ app.py # Flask backend logic
β βββ requirements.txt # Python dependencies
β βββ .env.example # Example for environment variables (API key)
β βββ .env # Actual environment variables (you need to create this)
βββ frontend/
β βββ index.html # Main HTML page
β βββ style.css # CSS for styling
β βββ main.ts # TypeScript for frontend logic
β βββ tsconfig.json # TypeScript compiler options
β βββ dist/ # Compiled JavaScript (main.js will be here after compilation)
βββ src-tauri/ # Tauri desktop app configuration
β βββ src/main.rs # Rust application entry point
β βββ Cargo.toml # Rust dependencies
β βββ tauri.conf.json # Tauri configuration
βββ package.json # Node.js dependencies for Tauri
βββ README.md # This file
- Python 3.7+
- Node.js 16+
- Rust (for Tauri)
- PostgreSQL database
- Redis server (optional, for caching)
If you haven't already, clone the project to your local machine.
# git clone <repository-url>
cd calorie-counter
Navigate to the backend directory:
cd backend
Create a virtual environment (recommended):
python3 -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
Install Python dependencies:
pip install -r requirements.txt
Set up your environment variables:
-
Rename
.env.example
to.env
. -
Open the
.env
file and update the following variables:GEMINI_API_KEY=your_actual_api_key_goes_here DATABASE_URL=postgresql://user:password@localhost/calorie_counter REDIS_URL=redis://localhost:6379/0 ADMIN_TOKEN=your_secure_admin_token_here
Important:
- You can obtain a Gemini API key from Google AI Studio.
- The REDIS_URL should point to your Redis server instance.
- The ADMIN_TOKEN is used for accessing admin endpoints like cache clearing.
Navigate to the frontend directory:
cd ../frontend # If you are in the backend directory
# or cd frontend if you are in the root calorie-counter directory
Install TypeScript and a simple HTTP server (if you don't have one globally):
npm install typescript --save-dev
npm install http-server -g # Or use any other local server you prefer
Compile the TypeScript code:
npx tsc
This will compile main.ts
and output main.js
into a dist
folder within the frontend
directory, as specified in tsconfig.json
.
Navigate to the root directory:
cd ..
Install Tauri dependencies:
npm install @tauri-apps/cli @tauri-apps/api
Navigate to the backend
directory and run the Flask app:
cd backend # if not already there
python app.py
The backend server will start, usually on http://127.0.0.1:5001
.
Open a new terminal window/tab.
Navigate to the root directory:
cd ..
Run the Tauri app:
npm run tauri dev
This will start the Tauri app in development mode.
- Click the "Choose File" button to select an image of food from your computer.
- A preview of the image will be displayed.
- Click the "Analyze Image" button.
- Wait for the Gemini API to process the image.
- The estimated calorie count and identified food items will be displayed below the button.
- The analysis results and additional metadata are automatically saved to the database.
The application uses a PostgreSQL database to store analysis results and additional metadata, and Redis for caching to improve performance.
The application uses Redis for the following caching purposes:
- Image Analysis Caching: Previously analyzed images are cached to avoid redundant API calls to Gemini
- API Response Caching: Frequently accessed endpoints like
/analyses
are cached for faster response times - Individual Analysis Caching: Specific analysis results are cached for quicker retrieval
The database schema includes:
- Analysis ID: Unique identifier for each analysis
- Image Filename: Name of the uploaded image file
- Analysis Result: Full text response from the Gemini API
- Created At: Timestamp when the analysis was performed
- Food Items: Automatically extracted list of food items identified in the image
- IP Address: User's IP address
- User Agent: Browser and device information
- Device Type: Categorized as 'web', 'mobile', 'tablet', or 'other'
- Location: Text description of the user's location
- Latitude/Longitude: Geographic coordinates
The application provides the following API endpoints for database interaction and cache management:
POST /upload
: Upload and analyze an image, saving results to the database and cacheGET /analyses
: Retrieve all past analyses (cached for improved performance)GET /analyses/<analysis_id>
: Retrieve a specific analysis by ID (cached for improved performance)PUT /analyses/<analysis_id>/location
: Update location data for a specific analysis (requires user permission)POST /admin/clear-cache
: Admin endpoint to clear the Redis cache (requires admin token)GET /admin/cache-stats
: Admin endpoint to get Redis cache statistics (requires admin token)
- API Key Security: Never commit your actual
.env
file with the API key to a public repository. - CORS: The Flask backend has
Flask-CORS
enabled to allow requests from the frontend (which will be on a different port). - Error Handling: Basic error handling is in place. Check the browser console and backend terminal for more detailed error messages if something goes wrong.
- Database Integration: The application uses PostgreSQL to store analysis results and additional metadata.
- Redis Caching: Redis is used to cache API responses and image analysis results for improved performance.
- Data Privacy: Location data is only collected with explicit user permission.
- Admin Features: The application includes admin endpoints for cache management, protected by an admin token.