A production-ready REST API that accepts PDF, DOCX, and image files (as base64) and returns AI-generated summaries, named entity extraction, and sentiment classification — powered by Claude (Anthropic).
| Layer | Technology |
|---|---|
| Language / Framework | Python 3.12 + FastAPI |
| AI / LLM | Groq API (LLaMA 3.3 70B) |
| PDF extraction | pdfplumber |
| DOCX extraction | python-docx |
| OCR (images) | pytesseract (Tesseract) + Claude Vision fallback |
| Deployment | Docker → Railway / Render |
git clone https://github.com/YOUR_USERNAME/doc-analyzer-api.git
cd doc-analyzer-apiUbuntu/Debian:
sudo apt-get install tesseract-ocr tesseract-ocr-engmacOS:
brew install tesseractpip install -r requirements.txtcp .env.example .env
# Edit .env and fill in your keysANTHROPIC_API_KEY=sk-ant-...
API_KEY=sk_track2_your_secret_hereuvicorn src.main:app --host 0.0.0.0 --port 8000 --reloadThe API will be live at http://localhost:8000.
Authentication: x-api-key: YOUR_API_KEY header required (401 if missing/invalid).
Request Body:
{
"fileName": "sample.pdf",
"fileType": "pdf",
"fileBase64": "<base64-encoded file content>"
}| Field | Type | Values |
|---|---|---|
fileName |
string | Any filename |
fileType |
string | pdf, docx, image |
fileBase64 |
string | Base64-encoded file bytes |
Success Response (200):
{
"status": "success",
"fileName": "sample.pdf",
"summary": "This document is an invoice issued by ABC Pvt Ltd to Ravi Kumar on 10 March 2026 for an amount of ₹10,000.",
"entities": {
"names": ["Ravi Kumar"],
"dates": ["10 March 2026"],
"organizations": ["ABC Pvt Ltd"],
"amounts": ["₹10,000"]
},
"sentiment": "Neutral"
}Error Responses:
401– Missing or invalid API key400– Invalid base64 data422– Unsupported file type or unreadable file500– Internal extraction or AI error
Returns {"status": "ok"} — used for deployment health checks.
# Encode your file
B64=$(base64 -w 0 sample.pdf)
curl -X POST https://your-domain.com/api/document-analyze \
-H "Content-Type: application/json" \
-H "x-api-key: sk_track2_your_secret_here" \
-d "{\"fileName\":\"sample.pdf\",\"fileType\":\"pdf\",\"fileBase64\":\"$B64\"}"| Format | Method |
|---|---|
pdfplumber — parses the PDF page-by-page and stitches text blocks in reading order. Handles multi-column layouts. |
|
| DOCX | python-docx — extracts all paragraphs and table cells from the document XML. |
| Image | pytesseract (Tesseract 5) for primary OCR. If Tesseract returns empty output, falls back to sending the image directly to Claude Vision for transcription. |
- Claude (Anthropic) — Used for assistance in code generation, debugging, and architecture decisions during development
- Groq API (LLaMA 3.3 70B) — Used as the AI model for document summarisation, entity extraction, and sentiment analysis at runtime
A single Claude API call (system-prompted for strict JSON output) performs all three tasks simultaneously:
- Summary — 2-4 sentence factual summary generated from the full document text (truncated to 12,000 chars to fit the context window efficiently).
- Entity Extraction — Claude identifies and categorises named entities into four types:
names,dates,organizations,amounts. - Sentiment — Document-level sentiment classified as exactly
Positive,Neutral, orNegative.
The system prompt enforces JSON-only output, and the response parser strips any accidental markdown fences before parsing.
your-repo/
├── README.md
├── Dockerfile
├── railway.json
├── render.yaml
├── requirements.txt
├── .env.example
└── src/
└── main.py
- Push code to GitHub
- Go to railway.app → New Project → Deploy from GitHub
- Set environment variables:
ANTHROPIC_API_KEYandAPI_KEY - Railway auto-builds the Dockerfile and provides a public URL