File size: 10,174 Bytes
0fd441a
4e5d980
 
 
 
0fd441a
bfbdd1d
 
5542bbb
0fd441a
5e6e3a7
15e9c77
 
0fd441a
4e5d980
 
bfbdd1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23344b4
bfbdd1d
4e5d980
23344b4
 
 
 
 
 
 
 
 
 
 
 
accd7c8
 
 
 
 
 
 
 
 
 
23344b4
5e6e3a7
23344b4
 
 
 
 
accd7c8
bfbdd1d
 
 
 
 
 
 
 
0fd441a
5542bbb
0fd441a
 
 
 
 
 
8290881
0fd441a
 
8290881
 
0fd441a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8290881
 
0fd441a
 
 
 
 
 
8290881
 
0fd441a
 
 
 
8290881
0fd441a
 
 
 
 
 
 
 
 
 
 
 
 
8290881
 
0fd441a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8290881
0fd441a
 
 
8290881
0fd441a
 
 
 
 
 
 
 
 
 
 
8290881
0fd441a
 
 
 
 
 
 
 
 
 
 
7757db2
 
0fd441a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8290881
0fd441a
 
 
 
8290881
 
0fd441a
8290881
0fd441a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e5d980
 
 
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
---
title: parser2md - PDF & HTML parser to markdown
emoji: πŸ“š
colorFrom: yellow
colorTo: purple
sdk: gradio
sdk_version: 5.44.1
python_version: 3.12
command: python main.py
app_file: main.py
hf_oauth: true
#oauth_scopes: [read-access]
hf_oauth_scopes: [read-access, inference-api]
license: mit
pinned: true
short_description: PDF & HTML parser to markdown
#models: [meta-llama/Llama-4-Maverick-17B-128E-Instruct, openai/gpt-oss-120b, openai/gpt-oss-20b, ]
models: 
  - meta-llama/Llama-4-Maverick-17B-128E-Instruct
  - openai/gpt-oss-120b, openai/gpt-oss-20b
  - vikp/surya_det3
  - vikp/surya_rec2
  - vikp/surya_tablerec
  - datalab-to/surya_layout
  - datalab-to/surya_tablerec
  - datalab-to/texify
  - datalab-to/ocr_error_detection
  - datalab-to/inline_math_det0
  - datalab-to/line_detector0
  - xiaoyao9184/surya_text_detection
  - xiaoyao9184/surya_text_recognition
  - xiaoyao9184/surya_table_recognition
  - xiaoyao9184/surya_texify
  - xiaoyao9184/surya_layout
  - xiaoyao9184/surya_ocr_error_detection
  - xiaoyao9184/surya_inline_math_detection]
tags: [markdown, PDF, parser, converter, extractor]
#preload_from_hub: [https://huggingface.co/datalab-to/surya_layout, https://huggingface.co/datalab-to/surya_tablerec, huggingface.co/datalab-to/line_detector0, https://huggingface.co/tarun-menta/ocr_error_detection/blob/main/config.json]
owner: research-semmyk
#---
#
#[Project]
#---
#title: parser2md - PDF & HTML parser to markdown
#emoji: \U0001F4C4πŸ“πŸ“‘
#colorFrom: yellow
#colorTo: purple
#sdk: gradio
#python_version: 3.12
#sdk_version: 5.44.1
#app_file: main.py
#command: python main.py
#models:
#  - meta-llama/Llama-4-Maverick-17B-128E-Instruct
#  - openai/gpt-oss-120b
#pinned: false
#license: mit
#name: parser2md
#short_description: PDF & HTML parser to markdown
version: 0.1.0
readme: README.md
requires-python: ">=3.12"
#dependencies: []
#preload_from_hub:
#  - https://huggingface.co/datalab-to/surya_layout
#  - https://huggingface.co/datalab-to/surya_tablerec
#  - huggingface.co/datalab-to/line_detector0
#  - https://huggingface.co/tarun-menta/ocr_error_detection/blob/main/config.json
#owner: research-semmyk
## Model list
#[
#    "datalab/models/text_recognition/2025_08_29",
#    "datalab/models/layout/2025_02_18",
#    "datalab/models/table_recognition/2025_02_18",
#    "datalab/models/text_detection/2025_05_07",
#    "datalab/models/ocr_error_detection/2025_02_18",
#]
---

# parserPDF

[![Gradio](https://img.shields.io/badge/Gradio-SDK-amber?logo=gradio)](https://www.gradio.app/)
[![Python](https://img.shields.io/badge/Python->=3.12-blue?logo=python)](https://www.python.org/)
[![MIT License](https://img.shields.io/badge/License-MIT-yellow?logo=mit)](LICENSE)

A Gradio-based web application for converting PDF, HTML and Word documents to Markdown format. Powered by the Marker library (a pipeline of deep learning models for document parsing) and optional LLM integration for enhanced processing. Supports batch processing of files and directories via an intuitive UI.

## Features
- **PDF to Markdown**: Extract text, tables, and images from PDFs, HTMLs and Word documents using Marker.
- **HTML to Markdown**: Convert HTML files to clean Markdown. #Deprecated
- **Batch Processing**: Upload multiple files or entire directories.
- **LLM Integration**: Optional use of Hugging Face or OpenAI models for advanced conversion (e.g., via Llama or GPT models).
- **Customizable Settings**: Adjust model parameters, output formats (Markdown/HTML), page ranges, and more via the UI.
- **Output Management**: Generated Markdown files saved to a configurable output directory, with logs and download links.

## Project Structure
```
parserpdf/
β”œβ”€β”€ README.md                   # Project documentation
β”œβ”€β”€ requirements.txt            # Python dependencies
β”œβ”€β”€ main.py                     # Entry point – launches the Gradio UI
β”œβ”€β”€ pyproject.toml              # Project configuration
β”œβ”€β”€ .env                        # Environment variables (e.g., API tokens)
β”œβ”€β”€ .gitignore                  # Git ignore rules
β”œβ”€β”€ converters/                 # Conversion logic
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ extraction_converter.py # Document extraction utilities
β”‚   β”œβ”€β”€ pdf_to_md.py            # Marker-based PDF, HTML, Word β†’ Markdown
β”‚   β”œβ”€β”€ html_to_md.py           # HTML β†’ Markdown  #Deprecated
β”‚   └── md_to_pdf.py            # Markdown β†’ PDF (pending full implementation)
β”œβ”€β”€ file_handler/               # File handling utilities
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── file_utils.py           # Helpers for files, directories, and paths
β”œβ”€β”€ llm/                        # LLM client integrations
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ hf_client.py            # Hugging Face client wrapper  ##PutOnHold
β”‚   β”œβ”€β”€ openai_client.py        # Marker OpenAI client         ##NotFullyImplemented
β”‚   β”œβ”€β”€ llm_login.py            # Authentication handlers
β”‚   └── provider_validator.py   # Provider validation
β”œβ”€β”€ ui/                         # Gradio UI components
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── gradio_ui.py            # UI layout, event handlers and coordination
β”œβ”€β”€ utils/                      # Utility modules
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ config.py               # Configuration constants
β”‚   β”œβ”€β”€ config.ini              # config file for settings
β”‚   β”œβ”€β”€ logger.py               # Logging wrapper
β”‚   β”œβ”€β”€ lib_loader.py           # loads weasyprint lib dependencies to environ
β”‚   β”œβ”€β”€ get_config.py           # helper for getting configurations
β”‚   β”œβ”€β”€ get_arg_name.py         # helper for getting argument names
β”‚   └── utils.py                # General utilities and helpers
β”œβ”€β”€ data/                       # Sample data and outputs (gitignored)
β”‚   β”œβ”€β”€ output_dir/             # Output directory
β”‚   β”œβ”€β”€ pdf/                    # Sample PDFs
β”œβ”€β”€ logs/                       # Log files (gitignored)
β”œβ”€β”€ tests/                      # Unit tests   ##ToBeUpdated
β”‚   β”œβ”€β”€ tests_converter.py          # tests for converters
└── scrapyard/                  # Development scraps


[Projected]
β”œβ”€β”€ transformers/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ marker.py           # Marker class
β”‚   └── marker_utils.py     # helpers for Marker class

```

## Installation
1. Clone the repository:
   ```
   git clone <repo-url>
   cd parserpdf
   ```

2. Create a virtual environment and install dependencies:
   ```
   python -m venv venv
   source venv/bin/activate  # On Windows: venv\Scripts\activate
   pip install -r requirements.txt
   ```

3. Set up environment variables (optional for LLM features):
   - Create a `.env` file with your API tokens, e.g.:
     ```
     HF_TOKEN=hf_xxx
     OPENAI_API_KEY=sk-xxx
     ```
   - HuggingFace login (oauth) integrated with Gradio:

4. Install Marker (if not in requirements.txt):
   ```
   pip install marker-pdf[full]
   ```

## Usage
1. Run the application:
   ```
   python main.py
   ```

2. Open the provided local URL (e.g., http://127.0.0.1:7860) in your browser.

3. In the UI:
   - Upload PDF/HTML/Word files or directories via the "PDF, HTML & Word ➜ Markdown" tab.
   - Configure LLM/Marker settings in the accordions (e.g., select provider, model, tokens).
   - Click "Process All Uploaded Files" to convert.
   - View logs, JSON output, and download generated Markdown files.

### Example Workflow
- Upload a PDF directory.
- Set model to `meta-llama/Llama-4-Maverick-17B-128E-Instruct` (Hugging Face).
- Enable LLM if needed, set page range (e.g., "1-10").
- Process: Outputs Markdown files with extracted text/images to `output_dir`.

## Configuration
- Edit `utils/config.ini` or `utils/config.py` for defaults (e.g., model ID, output dir).
- On windows, set weasyprint's GTK path: e.g. "C:\\Dat\\dev\\gtk3-runtime\\bin" or "C:\\msys64\\mingw64\\bin"
- UI overrides: Adjust sliders for max tokens, temperature, workers, etc.

## LLM Providers
- **Hugging Face**: Supports inference providers like Fireworks AI, Together AI.
- **OpenAI**: Compatible via router (default: https://router.huggingface.co/v1).
- Login via UI or CLI: `huggingface-cli login`.

## Output
- Markdown files saved to `output_dir` (default: `./output_dir`).
- Images extracted as JPEGs alongside Markdown.
- Logs in `logs/` and UI textbox.

## Limitations & TODO
- Markdown β†’ PDF is pending full implementation.
- HTML tab is deprecated; use main tab for mixed uploads.
- Large files/directories may require increased `max_workers` and higher processing power.
- No JSON/chunks output yet (flagged for future).

## Contributing
Fork the repo, create a branch, and submit a PR. 
- GitHub
- HuggingFace Space Community

Ensure tests pass: - verify the application's functionality. ##TardyOutdated
```
pytest tests/
```
Test Structure
- tests/test_converters.py: Tests PDF/HTML/Markdown converters, including init, conversion, batch processing, and error handling.
- tests/test_file_handler.py: Tests file collection utilities (PDF/HTML/MD paths), data processing, and output directory creation.
- tests/test_utils.py: Tests logging setup, config loading, utility functions like is_dict/is_list_of_dicts, and configuration access.
- tests/test_llm.py: Tests LLM login, provider validation, Hugging Face/OpenAI client initialization, and API interactions.
- tests/test_main_ui.py: Tests main application logic, UI building, batch conversion, file accumulation, and ProcessPoolExecutor integration.


## License
MIT License. See [LICENSE](LICENSE) for details.

## Acknowledgments
- Built with [Gradio](https://gradio.app/) for the UI.
- PDF parsing via [Marker](https://github.com/VikParuchuri/marker).
- LLM integrations using Hugging Face Transformers and OpenAI APIs.
- HuggingFace Spaces Configuration Reference [HF Spaces Configuration Reference](https://huggingface.co/docs/hub/en/spaces-config-reference)
- IBM Research: [HF Spaces Guide](https://huggingface.co/spaces/ibm-granite/granite-vision-demo/blob/main/DEVELOPMENT.md)