Skip to content

Latest commit

 

History

9 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MathScribe

A backend application built with Express.js that converts mathematical expressions in uploaded images into LaTeX code using the Google Gemini API.

Overview

This project exposes an API endpoint that accepts up to 5 image files at once, sends them to Gemini for analysis, and returns a LaTeX document that can be used in tools like Overleaf.

Features

  • Convert handwritten or printed math expressions in images to LaTeX
  • Upload up to 5 images in a single request
  • Uses Google Gemini for image understanding and LaTeX generation
  • Saves generated .tex files to an output/ directory
  • Automatically cleans up uploaded temporary files
  • Built with Node.js, Express, and Multer

Tech Stack

  • Backend: Node.js, Express.js
  • AI Model: Google Gemini (gemini-2.0-flash)
  • File Uploads: Multer
  • Environment Variables: dotenv
  • Utilities: fs, path, mime-types

Prerequisites

Before running the project, make sure you have:

  • Node.js 18+
  • npm
  • A valid Google Gemini API key

Installation

  1. Clone the repository:
git clone https://github.com/NTiger07/gemini-latex.git
cd gemini-latex
  1. Install dependencies:
npm install
  1. Create a .env file in the project root:
GOOGLE_GEMINI_API_KEY=your_gemini_api_key_here
PORT=3000

Running the Project

Development mode

npm run dev

Production mode

npm start

The server will run on the port defined in .env, or 3000 by default.

API Usage

POST /upload

Uploads image files and returns generated LaTeX code.

Request

  • Method: POST
  • Content-Type: multipart/form-data
  • Field name: file
  • Max files: 5

Example using cURL

curl -X POST http://localhost:3000/upload \
  -F "file=@image1.png" \
  -F "file=@image2.png"

Example response

{
  "latexCode": "\\documentclass{article}\\begin{document}...\\end{document}"
}

How It Works

  1. The client uploads one or more images to the /upload endpoint.
  2. Multer stores the images temporarily in the uploads/ folder.
  3. The server converts each image into a Gemini-compatible input.
  4. Gemini generates a minimal complete LaTeX document.
  5. The server writes the output to a timestamped .tex file inside the output/ folder.
  6. Temporary uploaded files are deleted after processing.

Project Structure

.
├── app.js
├── package.json
├── package-lock.json
├── uploads/
└── output/

Notes

  • The current implementation expects the uploaded files to be sent using the field name file.
  • The project supports up to 5 images per request.
  • Make sure the Gemini API key is valid, otherwise the conversion will fail.
  • Generated LaTeX is intended to be compatible with Overleaf.

Error Handling

The API returns errors in cases such as:

  • No file uploaded
  • Gemini API failure
  • File processing failure

Example error response:

{
  "error": "Failed to convert to Latex"
}

Future Improvements

  • Add a frontend UI for uploading images
  • Improve prompt tuning for more accurate LaTeX output
  • Support PDF uploads
  • Add OCR pre-processing and image cleanup
  • Add authentication and rate limiting

About

Express.js server integrating the Google Gemini API to perform OCR, converting images of mathematical equations into LaTeX code with 95% accuracy. Used Multer.js for file upload handling and applied robust error handling to manage API failures, invalid inputs, and file processing errors cleanly across all edge cases.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages