imran-decoder/filecrackhead1
๐ 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.
  
โจ Features
๐ Premium OCR (PDF โ DOCX)
Two modes for PDF โ DOCX:
Gate use_ocr=true behind your own billing/auth middleware to charge for OCR.
๐ Quick Start
Local Development
# 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 8000Open http://localhost:8000/docs for Swagger UI.
Run Tests
pytest tests/ -v
# Skip slow integration tests
pytest tests/ -v -m "not slow"๐ณ Docker
Build & Run Locally
docker build -t file-converter .
docker run -p 7860:7860 \
-e MAX_FILE_SIZE_MB=25 \
-e RATE_LIMIT_PER_MINUTE=10 \
file-converterOpen http://localhost:7860/docs
Environment Variables
๐ Deploy to Hugging Face Spaces
- Create a new Space on huggingface.co/spaces
- Select Docker as the SDK
- Push this repository to the Space
git remote add hf https://huggingface.co/spaces/<username>/<space-name>
git push hf main- 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):
# 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.docxResponse headers:
X-Source-Formatโ detected source formatX-Target-Formatโ target format usedX-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
For multi-language documents, combine codes: ocr_language=eng+fra+ara
๐ License
MIT License โ see LICENSE file.
