File size: 6,466 Bytes
ef7d08f
 
06ccc5a
 
 
 
 
 
 
ef7d08f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
535a127
ef7d08f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
535a127
ef7d08f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# DotsOCR API - React Frontend + FastAPI Backend

## πŸ“š Documentation

- **[Complete API Usage Guide](./API_USAGE_GUIDE.md)** - Comprehensive examples for Python, JavaScript, and React
- **[Quick Start Guide](./QUICKSTART.md)** - Get started in 5 minutes
- **[Deployment Guide](./DEPLOYMENT.md)** - Deploy to production
- **[Interactive API Docs](https://isseygino911-dots-ocr-parser.hf.space/docs)** - Test endpoints in your browser

## Overview

This project provides a modern web interface for the DotsOCR document parser using:
- **Backend**: FastAPI (deployed on HuggingFace Spaces with GPU)
- **Frontend**: React + TypeScript (deployed on local Mac or Hostinger)

## Features

### Backend (FastAPI)
- RESTful API endpoints for image and PDF parsing
- WebSocket support for real-time progress updates
- Automatic API documentation (FastAPI/OpenAPI)
- CORS enabled for frontend access
- GPU-accelerated OCR processing

### Frontend (React)
- Drag-and-drop file upload
- Real-time progress tracking with WebSocket
- Results viewer with tabs:
  - Markdown rendered preview
  - Raw markdown text
  - JSON data with syntax highlighting
- Page navigation for PDF documents
- Download results as ZIP
- Responsive design

## Quick Start

### Backend (HuggingFace Spaces)

1. **Push to HF Spaces:**
```bash
git add app.py requirements.txt
git commit -m "FastAPI backend"
git push hf main
```

2. **Wait for deployment** (~10-15 minutes)

3. **Access API docs:**
```
https://isseygino911-dots-ocr-parser.hf.space/docs
```

### Frontend (Local Development)

1. **Navigate to frontend:**
```bash
cd frontend
```

2. **Install dependencies:**
```bash
npm install
```

3. **Configure backend URL:**
```bash
# Edit .env.development
VITE_API_BASE_URL=https://isseygino911-dots-ocr-parser.hf.space
```

4. **Run dev server:**
```bash
npm run dev
```

5. **Open browser:**
```
http://localhost:5173
```

### Frontend (Deploy to Hostinger)

1. **Build production:**
```bash
cd frontend
npm run build
```

2. **Upload `dist/` folder to Hostinger via FTP or File Manager**

3. **Access your domain:**
```
https://yourdomain.com
```

## Project Structure

```
dots-ocr-parser/
β”œβ”€β”€ app.py                    # FastAPI backend
β”œβ”€β”€ app_gradio_backup.py      # Original Gradio version (backup)
β”œβ”€β”€ requirements.txt          # Python dependencies
β”œβ”€β”€ DEPLOYMENT.md             # Detailed deployment guide
β”œβ”€β”€ README_API.md             # This file
β”‚
└── frontend/                 # React application
    β”œβ”€β”€ src/
    β”‚   β”œβ”€β”€ api/
    β”‚   β”‚   └── client.ts     # API client functions
    β”‚   β”œβ”€β”€ components/
    β”‚   β”‚   β”œβ”€β”€ FileUpload.tsx
    β”‚   β”‚   β”œβ”€β”€ JobProgress.tsx
    β”‚   β”‚   └── ResultsViewer.tsx
    β”‚   β”œβ”€β”€ types/
    β”‚   β”‚   └── index.ts      # TypeScript types
    β”‚   └── App.tsx           # Main app component
    β”œβ”€β”€ .env.development      # Dev environment config
    β”œβ”€β”€ .env.production       # Prod environment config
    └── package.json          # Node dependencies
```

## API Endpoints

### POST /api/parse/image
Upload and parse an image.

**Request:**
- `file`: Image file (multipart)
- `prompt_mode`: Parsing mode (form field)

**Response:**
```json
{
  "job_id": "abc-123-def",
  "status": "queued"
}
```

### POST /api/parse/pdf
Upload and parse a PDF.

**Response:** Same as image endpoint

### GET /api/jobs/{job_id}/status
Get job status and progress.

**Response:**
```json
{
  "job_id": "abc-123-def",
  "status": "processing",
  "progress_percent": 50.0,
  "current_page": 5,
  "total_pages": 10,
  "message": "Processing page 5/10..."
}
```

### GET /api/jobs/{job_id}/results
Get parsed results (when completed).

### GET /api/jobs/{job_id}/download
Download results as ZIP.

### WS /api/jobs/{job_id}/stream
WebSocket for real-time updates.

## Parsing Modes

- **prompt_layout_all_en** (Default): Full layout detection + text recognition
- **prompt_layout_only_en**: Layout detection only (no text)
- **prompt_ocr**: Text extraction only (markdown output)

## Development

### Backend Development

The backend is designed to run on HuggingFace Spaces with GPU. Local development on Mac is not supported due to CUDA requirement.

To test backend changes:
1. Push to HF Spaces
2. Wait for rebuild
3. Test via API docs at `/docs`

### Frontend Development

```bash
cd frontend

# Install dependencies
npm install

# Run dev server (hot reload enabled)
npm run dev

# Build for production
npm run build

# Preview production build
npm run preview
```

### TypeScript Types

All API types are defined in `frontend/src/types/index.ts`. Update these if you modify the backend API.

## Troubleshooting

### CORS Errors
Update `allow_origins` in `app.py`:
```python
app.add_middleware(
    CORSMiddleware,
    allow_origins=["https://yourdomain.com", "http://localhost:5173"],
    ...
)
```

### WebSocket Connection Failed
The frontend automatically falls back to HTTP polling if WebSocket fails.

### Backend Not Responding
- Check if HF Space is sleeping (visit URL to wake)
- Check HF Spaces logs for errors
- Verify GPU is enabled in Space settings

### Frontend Build Errors
```bash
cd frontend
rm -rf node_modules package-lock.json
npm install
npm run build
```

## Performance Notes

- **First run**: ~10 minutes (model download)
- **Subsequent runs**: ~30 seconds (model cached)
- **Image processing**: ~10-15 seconds per image
- **PDF processing**: ~10-15 seconds per page

## Tech Stack

### Backend
- Python 3.9+
- FastAPI 0.104+
- Uvicorn (ASGI server)
- PyTorch 2.4+
- Transformers 4.51+
- Flash Attention 2

### Frontend
- React 18
- TypeScript
- Vite (build tool)
- Axios (HTTP client)
- React Dropzone (file upload)
- React Markdown (markdown rendering)
- React Syntax Highlighter (JSON display)

## Credits

Powered by [dots.ocr](https://github.com/rednote-hilab/dots.ocr) - A 1.7B parameter Vision-Language Model for Document Understanding.

## License

See the dots.ocr repository for model license information.

## Next Steps

1. Deploy backend to HuggingFace Spaces
2. Test API endpoints at `/docs`
3. Run frontend locally and test integration
4. Deploy frontend to Hostinger
5. See [DEPLOYMENT.md](./DEPLOYMENT.md) for detailed instructions

## Support

For detailed deployment instructions, see [DEPLOYMENT.md](./DEPLOYMENT.md).

For API documentation, visit `/docs` on your deployed backend.