CoolFace
Apppublic

imran-decoder/filecrackhead1

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
App README

๐Ÿ“„ File Conversion Service

A production-ready FastAPI REST API for converting documents between 30+ format pairs. Deploy to Hugging Face Spaces with Docker in minutes.

![FastAPI](https://fastapi.tiangolo.com) ![Python](https://python.org) ![Docker](https://docker.com)


โœจ Features

CategoryFormats
PDF โ†” OfficePDF โ†” DOCX, DOC, ODT, RTF
PDF โ†” ImagesPDF โ†’ JPG, PNG
PDF โ†” TextPDF โ†” TXT, HTML, EPUB
WordDOCX โ†” PDF/TXT/ODT/RTF/HTML, DOC โ†” DOCX/PDF/TXT
PowerPointPPT/PPTX โ†” PDF, PPTX โ†’ JPG
SpreadsheetXLSX/XLS โ†” PDF
Text & MarkupTXT/HTML/Markdown โ†” PDF/DOCX
eBookEPUB โ†” PDF/DOCX/TXT

๐Ÿ” Premium OCR (PDF โ†’ DOCX)

Two modes for PDF โ†’ DOCX:

ModeEndpoint ParamSpeedUse Case
Free (text-layer)use_ocr=false (default)~1sNative text PDFs
Premium OCRuse_ocr=true~10-60s/pageScanned/image PDFs

Gate use_ocr=true behind your own billing/auth middleware to charge for OCR.


๐Ÿš€ Quick Start

Local Development

bash
# 1. Clone and set up
git clone <repo-url> && cd file_converter

# 2. Create a virtual environment
python -m venv .venv && source .venv/bin/activate

# 3. Install Python dependencies
pip install -r requirements.txt

# 4. Install system tools (macOS / Ubuntu shown)
# macOS
brew install libreoffice pandoc poppler ghostscript tesseract

# Ubuntu/Debian
sudo apt-get install libreoffice pandoc poppler-utils ghostscript tesseract-ocr

# 5. Configure environment
cp .env.example .env

# 6. Run the server
uvicorn app.main:app --reload --port 8000

Open http://localhost:8000/docs for Swagger UI.

Run Tests

bash
pytest tests/ -v
# Skip slow integration tests
pytest tests/ -v -m "not slow"

๐Ÿณ Docker

Build & Run Locally

bash
docker build -t file-converter .
docker run -p 7860:7860 \
  -e MAX_FILE_SIZE_MB=25 \
  -e RATE_LIMIT_PER_MINUTE=10 \
  file-converter

Open http://localhost:7860/docs

Environment Variables

VariableDefaultDescription
MAX_FILE_SIZE_MB25Max upload size
RATE_LIMIT_PER_MINUTE10Rate limit per IP
LOG_LEVELINFOLogging level
LOG_FORMATjsonjson or text
OCR_LANGUAGEengTesseract language(s)
OCR_DPI300OCR render DPI (150โ€“600)
LIBREOFFICE_PATHsofficePath to LibreOffice
PANDOC_PATHpandocPath to pandoc
TESSERACT_PATH/usr/bin/tesseractPath to tesseract

๐ŸŒ Deploy to Hugging Face Spaces

  1. 1.Create a new Space on huggingface.co/spaces
  2. 2.Select Docker as the SDK
  3. 3.Push this repository to the Space
bash
git remote add hf https://huggingface.co/spaces/<username>/<space-name>
git push hf main
  1. 1.The Space will automatically build the Docker image and expose on port 7860.
Note: Hugging Face Spaces provides ~2vCPU and 16GB RAM on free tier. OCR on large scanned PDFs may be slow โ€” consider upgrading hardware for production OCR workloads.

๐Ÿ“ก API Reference

POST /convert

Convert a document file.

Form fields: | Field | Type | Required | Description | |---|---|---|---| | file | UploadFile | โœ… | Source document | | target_format | string | โœ… | Target format (e.g. docx, pdf) | | use_ocr | bool | โŒ | Enable OCR pipeline (default: false) | | ocr_language | string | โŒ | Tesseract language code (default: eng) | | ocr_dpi | int | โŒ | OCR render DPI, 72โ€“600 (default: 300) |

Example (curl):

bash
# Free tier: text-layer extraction
curl -X POST http://localhost:7860/convert \
  -F "file=@document.pdf" \
  -F "target_format=docx" \
  --output converted.docx

# Premium: OCR pipeline for scanned PDFs
curl -X POST http://localhost:7860/convert \
  -F "file=@scanned.pdf" \
  -F "target_format=docx" \
  -F "use_ocr=true" \
  -F "ocr_language=eng" \
  --output ocr_output.docx

Response headers:

  • โ€”X-Source-Format โ€” detected source format
  • โ€”X-Target-Format โ€” target format used
  • โ€”X-OCR-Used โ€” whether OCR was applied (true/false)
  • โ€”X-Output-Size-Bytes โ€” output file size

GET /health

Returns service status and tool availability.

GET /formats

Returns all supported (source โ†’ target) pairs grouped by source format.


๐Ÿ—๏ธ Project Structure

file_converter/
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ main.py               # FastAPI app factory
โ”‚   โ”œโ”€โ”€ config.py             # Settings (env-driven)
โ”‚   โ”œโ”€โ”€ validators.py         # Conversion matrix & Pydantic models
โ”‚   โ”œโ”€โ”€ routers/
โ”‚   โ”‚   โ”œโ”€โ”€ convert.py        # POST /convert
โ”‚   โ”‚   โ”œโ”€โ”€ health.py         # GET /health
โ”‚   โ”‚   โ””โ”€โ”€ formats.py        # GET /formats
โ”‚   โ”œโ”€โ”€ services/
โ”‚   โ”‚   โ”œโ”€โ”€ conversion_engine.py    # Central dispatcher
โ”‚   โ”‚   โ”œโ”€โ”€ ocr_converter.py        # โญ PDFโ†’DOCX free+OCR (premium)
โ”‚   โ”‚   โ”œโ”€โ”€ pdf_converter.py        # PDF conversions
โ”‚   โ”‚   โ”œโ”€โ”€ word_converter.py       # Word conversions
โ”‚   โ”‚   โ”œโ”€โ”€ pptx_converter.py       # PowerPoint conversions
โ”‚   โ”‚   โ”œโ”€โ”€ spreadsheet_converter.py# Spreadsheet conversions
โ”‚   โ”‚   โ”œโ”€โ”€ text_converter.py       # TXT/HTML/MD conversions
โ”‚   โ”‚   โ””โ”€โ”€ ebook_converter.py      # EPUB conversions
โ”‚   โ”œโ”€โ”€ security/
โ”‚   โ”‚   โ”œโ”€โ”€ file_validator.py       # MIME, size, encryption checks
โ”‚   โ”‚   โ”œโ”€โ”€ rate_limiter.py         # SlowAPI rate limiting
โ”‚   โ”‚   โ””โ”€โ”€ sanitizer.py            # Filename sanitization
โ”‚   โ””โ”€โ”€ utils/
โ”‚       โ”œโ”€โ”€ file_utils.py           # Temp dir management
โ”‚       โ””โ”€โ”€ logging_utils.py        # Structured JSON logging
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ conftest.py
โ”‚   โ”œโ”€โ”€ test_validators.py
โ”‚   โ”œโ”€โ”€ test_conversion_engine.py
โ”‚   โ””โ”€โ”€ test_api.py
โ”œโ”€โ”€ Dockerfile
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ .env.example
โ””โ”€โ”€ .dockerignore

๐Ÿ” Security

  • โ€”โœ… MIME type validation via magic bytes
  • โ€”โœ… File extension whitelist
  • โ€”โœ… 25MB size limit (configurable)
  • โ€”โœ… PDF encryption/password detection
  • โ€”โœ… Filename sanitization (UUID prefix + pathvalidate)
  • โ€”โœ… Path traversal prevention
  • โ€”โœ… Per-IP rate limiting (SlowAPI)
  • โ€”โœ… Temp files auto-deleted after every request (even on failure)
  • โ€”โœ… Non-root Docker user
  • โ€”โœ… Global exception handler (no stack trace leaks)

๐Ÿ“Š OCR Quality Tips

DPIQualityUse Case
150Fast / lowQuick drafts, well-scanned docs
300RecommendedStandard scanned documents
600Slow / highFine print, forms, low-quality scans

For multi-language documents, combine codes: ocr_language=eng+fra+ara


๐Ÿ“ License

MIT License โ€” see LICENSE file.