spaceenter12345/pdf-to-markdown
0
PDF → Markdown (pdf-2-md)
A Python/Streamlit application that converts PDF files to Markdown quickly and reliably. Ready to deploy for free on Hugging Face Spaces.
Goals
- Upload a PDF and get raw Markdown output.
- Simple UX with progress and clear error messages.
- Optional: side-by-side preview and inline Markdown editing.
MVP: Main Flow
- Upload a PDF (size cap depends on Space hardware, ~10–20MB for free CPU).
- Click "Convert".
- Show processing/progress.
- Return raw Markdown.
- Download
.mdvia the download button.
Architecture (Python-only on Hugging Face Spaces)
- Streamlit app: upload UI + conversion pipeline inside the app.
- Docling: main PDF → Markdown engine, with fallbacks.
- No separate backend API; Spaces' CPU is sufficient for a demo.
Recommended Stack
- Python 3.10+
- Streamlit (UI)
- Docling (PDF → Markdown)
- Optional:
requests(import from URL),zipfile(batch export),python-magic(MIME checks)
Directory Structure
pdf-2-md/
├─ app.py # Streamlit application
├─ requirements.txt # Python dependencies (streamlit, docling, ...)
├─ docs/ # Docs (specs, plans)
└─ README.mdRoadmap
- Phase 1 (MVP – High priority):
- Upload + Convert button (Streamlit
file_uploader+button). - Display raw Markdown (textarea/code block).
- "Download .md" button.
- Status/progress + error handling.
- Phase 2 (Improvements – Medium):
- Side-by-side preview (
st.columns: textarea ↔st.markdown). - Inline Markdown editing.
- Phase 3 (Extensions – Low):
- Batch conversion (
accept_multiple_files, return ZIP). - Conversion options (checkboxes: keep images/tables...).
- Import from URL (download PDF via
requests).
Technical Next Steps
- Ensure
requirements.txtincludes all libs (streamlit, docling, pymupdf, pdfminer.six...). - Implement
app.pybasic UI: uploader, convert button, result display,.mddownload. - Integrate Docling in
app.py: robust PDF → Markdown with clear errors. - Size limits and basic MIME checks; consider timeouts.
Deploy to Hugging Face Spaces
Step 1: Create a new Space
- Log in to Hugging Face
- Go to Spaces → Create new Space
- Fill in:
- Space name:
pdf-to-markdown(or any name) - License: MIT
- SDK: Streamlit (if available) or Docker
- Hardware: CPU basic (free)
- Visibility: Public
Step 2: Upload code
Option A: Web interface
- After creating the Space, go to the Files tab.
- Upload:
app.pyrequirements.txtREADME.md(optional)packages.txtand.streamlit/config.toml(for Streamlit SDK)Dockerfile(for Docker SDK)
Option B: Git (recommended)
# Clone your Space locally
git clone https://huggingface.co/spaces/YOUR_USERNAME/SPACE_NAME
cd SPACE_NAME
# Copy files from this project
copy ..\pdf-2-md\app.py .
copy ..\pdf-2-md\requirements.txt .
copy ..\pdf-2-md\README.md .
copy ..\pdf-2-md\.streamlit\config.toml .\.streamlit\config.toml
copy ..\pdf-2-md\packages.txt .
copy ..\pdf-2-md\Dockerfile .
# Commit and push
git add .
git commit -m "Initial commit: PDF to Markdown converter"
git pushStep 3: Verify deployment
- The Space builds automatically (2–5 minutes).
- Check build logs under Logs.
- If successful, the app is live at your Space URL.
Step 4: Optimize for Spaces
- File size: Set
MAX_FILE_SIZE_MB = 20inapp.py. - Timeouts: Free Spaces have ~60s/request limits.
- Memory: Free CPU has ~16GB RAM; fine for most PDFs.
Important Notes
- Docling: heavy dependencies (PyTorch, etc.). First build can take time.
- Fallback: If Docling fails on Spaces, the app automatically falls back to PyMuPDF.
- Performance: Free CPU handles PDFs <50MB; processing varies from ~30s to 2min depending on complexity.
When the SDK (Gradio/Streamlit/Static) isn’t selectable
- Some newer UIs may not show Streamlit. In that case, choose Docker and use the provided
Dockerfileto run Streamlit. - Docker flow:
- Choose SDK: Docker when creating the Space.
- Upload
Dockerfile,requirements.txt,app.py,README.md(nopackages.txtneeded; system deps installed viaaptin the Dockerfile). - After build, the app runs on port
7860as defined in the Dockerfile.
Troubleshooting: cannot click the SDK box
- Ensure the Space name is valid (letters, numbers,
-; no spaces). - Select a license (e.g., MIT) first and fill all required fields.
- If creating under an Organization, ensure you have permissions; otherwise SDKs may be locked.
- Try a different browser, disable script-blocking extensions, or log out/in.
- If you reached the free Space quota, delete an old Space and create a new one.
Notes & Risks
- Conversion quality depends on Docling; test with diverse PDFs.
- Free CPU resources are limited: enforce file size/processing time caps.
- Security: validate file types, avoid RCE, rate-limit if needed.
Run Locally
- Install deps:
pip install -r requirements.txt - Start server:
python -m streamlit run app.py - Open:
http://localhost:8501/
Notes:
- If Docling isn’t available on Windows, the app automatically falls back to PyMuPDF in
Auto (PyMuPDF fallback)mode. - Install Docling (recommended on WSL2/Linux):
pip install docling --extra-index-url https://download.pytorch.org/whl/cpu
