An open API service indexing awesome lists of open source software.

https://github.com/abdulbasit110/sovle-x-agent

AI Math Solver Agent ๐Ÿค–โœ๏ธ Upload or snap math problems, get step-by-step solutions with LaTeX explanations. Built with Next.js, Express.js, Tesseract.js OCR, and OpenAI Agents SDK (TypeScript).
https://github.com/abdulbasit110/sovle-x-agent

ai expressjs latex math-problems nextjs nodejs ocr openai-agents-sdk typescript

Last synced: 3 months ago
JSON representation

AI Math Solver Agent ๐Ÿค–โœ๏ธ Upload or snap math problems, get step-by-step solutions with LaTeX explanations. Built with Next.js, Express.js, Tesseract.js OCR, and OpenAI Agents SDK (TypeScript).

Awesome Lists containing this project

README

          

# Math Solver Agent

An AI-powered math problem solver that uses Tesseract.js OCR to extract text from images and provides step-by-step solutions with LaTeX output. Features a modern Next.js frontend and a robust Express.js backend API.

## โœจ Features

- ๐Ÿ“ธ **Image Upload & OCR**: Upload math problem images or take photos
- ๐Ÿค– **AI-Powered Solving**: OpenAI Agents for intelligent math problem solving
- ๐Ÿ“ **Step-by-Step Solutions**: Clear, educational explanations for each step
- ๐Ÿ”ข **LaTeX Output**: Professional mathematical formatting
- ๐ŸŽจ **Modern UI**: Beautiful, responsive Next.js frontend
- ๐Ÿš€ **RESTful API**: Clean backend endpoints for integration
- ๐Ÿ“ฑ **Mobile Friendly**: Responsive design for all devices
- ๐Ÿ” **Advanced OCR**: Tesseract.js for reliable text extraction

## ๐Ÿ—๏ธ Architecture

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Next.js โ”‚ โ”‚ Express.js โ”‚ โ”‚ External โ”‚
โ”‚ Frontend โ”‚โ—„โ”€โ”€โ–บโ”‚ Backend API โ”‚โ—„โ”€โ”€โ–บโ”‚ AI Services โ”‚
โ”‚ (Port 3000) โ”‚ โ”‚ (Port 3001) โ”‚ โ”‚ (OpenAI) โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

## ๐Ÿš€ Quick Start

### Prerequisites

- Node.js 18+
- npm or pnpm
- OpenAI API key

### 1. Clone & Setup

```bash
git clone
cd math-solver-agent
```

### 2. Environment Variables

Create a `.env` file in the root directory:

```env
# OpenAI API Key for math solving
OPENAI_API_KEY=your_openai_api_key_here

# Backend server port (optional)
PORT=3001
```

**Note**: Tesseract.js works offline and doesn't require API keys!

### 3. Install Dependencies

```bash
# Install backend dependencies
npm install

# Install frontend dependencies
cd math-solver-app
npm install
```

### 4. Start the Application

**Terminal 1 - Backend API:**
```bash
# From root directory
npm run dev
# Server runs on http://localhost:3001
```

**Terminal 2 - Frontend:**
```bash
# From math-solver-app directory
npm run dev
# Frontend runs on http://localhost:3000
```

### 5. Open Your Browser

Navigate to `http://localhost:3000` and start solving math problems!

## ๐Ÿ“ Project Structure

```
math-solver-agent/
โ”œโ”€โ”€ ๐Ÿ“ Backend (Port 3001)
โ”‚ โ”œโ”€โ”€ index.js # Express server with routes
โ”‚ โ”œโ”€โ”€ controller.js # Request handlers & business logic
โ”‚ โ”œโ”€โ”€ agent.js # AI math solver using OpenAI Agents
โ”‚ โ”œโ”€โ”€ ocr.js # Image text extraction with Tesseract.js
โ”‚ โ”œโ”€โ”€ package.json # Backend dependencies
โ”‚ โ””โ”€โ”€ README.md # This file
โ”‚
โ””โ”€โ”€ ๐Ÿ“ Frontend (Port 3000)
โ”œโ”€โ”€ app/ # Next.js app directory
โ”‚ โ”œโ”€โ”€ page.tsx # Main math solver interface
โ”‚ โ””โ”€โ”€ layout.tsx # App layout
โ”œโ”€โ”€ components/ # UI components
โ”œโ”€โ”€ package.json # Frontend dependencies
โ””โ”€โ”€ ... # Next.js configuration files
```

## ๐Ÿ”Œ API Endpoints

### Backend API (Port 3001)

| Method | Endpoint | Description |
|--------|----------|-------------|
| `GET` | `/health` | Health check endpoint |
| `POST` | `/solve-math` | Upload image and get math solution |

### Frontend (Port 3000)

- **Main App**: `http://localhost:3000` - Math solver interface
- **Image Upload**: Drag & drop or camera capture
- **Real-time Processing**: Live feedback during solving

## ๐Ÿ“ฑ Usage

### 1. Upload Math Problem
- Take a photo using your device camera
- Or upload an existing image file
- Supported formats: JPG, PNG, GIF, etc.

### 2. AI Processing
- Tesseract.js OCR extracts text from the image
- AI analyzes the math problem
- Generates step-by-step solution

### 3. View Results
- **Extracted Text**: See what the OCR read from your image
- **Step-by-Step Solution**: Understand each solving step
- **Final Answer**: Get the numerical/symbolic result
- **LaTeX Format**: Professional mathematical notation

## ๐Ÿ› ๏ธ Development

### Backend Development

```bash
# Start with auto-reload
npm run dev

# Start production
npm start
```

### Frontend Development

```bash
cd math-solver-app

# Start with auto-reload
npm run dev

# Build for production
npm run build

# Start production
npm start
```

## ๐Ÿ”ง Configuration

### Environment Variables

| Variable | Description | Required |
|----------|-------------|----------|
| `OPENAI_API_KEY` | OpenAI API key for math solving | โœ… |
| `PORT` | Backend server port | โŒ (default: 3001) |

### OCR Configuration

Tesseract.js provides advanced OCR options:

```javascript
// Advanced OCR with custom settings
const options = {
lang: 'eng', // Language (English)
oem: 1, // OCR Engine Mode: LSTM neural nets
psm: 6, // Page segmentation: uniform text block
dpi: 300 // Image DPI for better accuracy
};
```

### File Upload Limits

- **Maximum file size**: 10MB
- **Supported formats**: All common image types
- **Processing timeout**: Configurable

## ๐Ÿงช Testing

### Test Backend API

```bash
# Health check
curl http://localhost:3001/health

# Test math solver (replace with actual image)
curl -X POST http://localhost:3001/solve-math \
-F "image=@math_problem.jpg"
```

### Test Frontend

1. Open `http://localhost:3000`
2. Upload a math problem image
3. Verify OCR text extraction
4. Check AI solution generation

## ๐Ÿš€ Deployment

### Backend Deployment

```bash
# Build and start
npm run build
npm start

# Or use PM2
pm2 start index.js --name "math-solver-api"
```

### Frontend Deployment

```bash
cd math-solver-app

# Build for production
npm run build

# Deploy to Vercel, Netlify, or your preferred platform
```

## ๐Ÿค Contributing

1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Test thoroughly
5. Submit a pull request

## ๐Ÿ“„ License

This project is licensed under the MIT License.

## ๐Ÿ†˜ Troubleshooting

### Common Issues

**Backend won't start:**
- Check environment variables are set
- Ensure ports are not in use
- Verify all dependencies are installed

**Frontend can't connect to API:**
- Confirm backend is running on port 3001
- Check CORS settings
- Verify API endpoints are correct

**OCR not working:**
- Ensure image quality is good (clear, high contrast)
- Check image format is supported
- Verify image contains readable text
- Try different OCR settings if needed

**Math solving fails:**
- Verify OpenAI API key
- Check API quota and limits
- Ensure problem text is clear

### OCR Tips for Better Results

- **Image Quality**: Use clear, high-resolution images
- **Contrast**: Ensure good contrast between text and background
- **Lighting**: Avoid shadows and glare
- **Orientation**: Keep text horizontal and well-aligned
- **Font Size**: Larger, clearer fonts work better

## ๐Ÿ“ž Support

For issues and questions:
- Check the troubleshooting section
- Review API documentation
- Open an issue on GitHub

---

**Happy Math Solving! ๐Ÿงฎโœจ**