File size: 5,067 Bytes
405cb5a fa15fa1 405cb5a fa15fa1 405cb5a fa15fa1 405cb5a fa15fa1 | 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 | ---
title: Document Analysis API
emoji: π
colorFrom: blue
colorTo: green
sdk: docker
app_port: 7860
pinned: false
---
# AI-Powered Document Analysis API
### GUVI Hackathon β Track 2
An intelligent document processing REST API that extracts, analyses, and summarises content from **PDF**, **DOCX**, and **image** files using a hybrid AI pipeline.
---
## Description
The API accepts a base64-encoded document and returns a structured JSON response containing:
- **Summary** β a concise AI-generated description of the document
- **Entities** β extracted names, dates, organisations, and monetary amounts
- **Sentiment** β overall document tone (Positive / Neutral / Negative)
**Strategy:** Gemini 2.5 Flash (free Google AI API) is the primary analysis engine for highest accuracy. If Gemini is unavailable, the system automatically falls back to a 100% offline pipeline (spaCy NER + DistilBERT sentiment + sumy TextRank summarisation).
---
## Tech Stack
| Layer | Tool |
|-------|------|
| Backend | FastAPI + Uvicorn |
| PDF extraction | pdfplumber (layout-preserving) |
| DOCX extraction | python-docx (paragraphs + tables) |
| OCR | pytesseract + Tesseract-OCR + Pillow |
| AI β Primary | Gemini 2.5 Flash (Google AI free tier) |
| AI β Summary fallback | sumy TextRank |
| AI β Entity fallback | spaCy `en_core_web_sm` + regex |
| AI β Sentiment fallback | DistilBERT SST-2 (HuggingFace Transformers) |
| Auth | FastAPI Header dependency |
---
## Setup Instructions
### 1. Clone the repository
```bash
git clone https://github.com/YOUR_USERNAME/YOUR_REPO.git
cd YOUR_REPO
```
### 2. Install system dependencies
```bash
# Ubuntu / Debian
sudo apt-get install -y tesseract-ocr poppler-utils
# macOS
brew install tesseract poppler
# Windows: download installers from
# https://github.com/UB-Mannheim/tesseract/wiki
# https://github.com/oschwartz10612/poppler-windows/releases
```
### 3. Install Python dependencies
```bash
pip install -r requirements.txt
python -m spacy download en_core_web_sm
```
### 4. Configure environment variables
```bash
cp .env.example .env
# Edit .env and fill in:
# API_KEY=<your chosen API key>
# GEMINI_API_KEY=<from https://aistudio.google.com/apikey>
```
### 5. Run the API
```bash
cd src
uvicorn main:app --host 0.0.0.0 --port 8000
```
---
## API Reference
### Authentication
All requests must include the `x-api-key` header:
```
x-api-key: YOUR_API_KEY
```
Returns `401 Unauthorized` if the header is missing or invalid.
### Endpoint
**POST** `/api/document-analyze`
**Request Body (JSON):**
```json
{
"fileName": "sample.pdf",
"fileType": "pdf",
"fileBase64": "<base64-encoded file content>"
}
```
Supported `fileType` values: `pdf`, `docx`, `image`
**Success Response:**
```json
{
"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"
}
```
### Example cURL
```bash
curl -X POST https://your-domain.com/api/document-analyze \
-H "Content-Type: application/json" \
-H "x-api-key: YOUR_API_KEY" \
-d '{
"fileName": "invoice.pdf",
"fileType": "pdf",
"fileBase64": "'"$(base64 -w 0 invoice.pdf)"'"
}'
```
---
## Approach
### Text Extraction
- **PDF**: pdfplumber extracts text with layout preservation. Pages returning no text are re-processed with Tesseract OCR (scanned PDFs).
- **DOCX**: python-docx iterates paragraphs and table cells to capture all content.
- **Image**: PIL preprocessing (grayscale β sharpen β autocontrast) maximises OCR accuracy before passing to Tesseract.
### Summary Generation
Gemini 2.5 Flash is prompted to produce a 1β2 sentence summary capturing the document's purpose, key actors, dates, and amounts. Fallback uses sumy's TextRank algorithm on the first 4000 characters.
### Entity Extraction
Gemini identifies and returns all four entity types (names, dates, organisations, amounts) in a structured JSON prompt. The offline fallback combines spaCy's NER (`PERSON`, `ORG` labels) with hand-crafted regex patterns for dates (multiple format support) and monetary amounts (βΉ, Rs., INR, $, USD, β¬, Β£).
### Sentiment Analysis
Gemini classifies sentiment as Positive / Neutral / Negative based on overall document tone. The fallback uses DistilBERT SST-2 β scores below 0.65 are mapped to Neutral to avoid overconfident labelling on factual documents.
---
## Deployment (Render.com)
1. Create a new **Web Service** on [render.com](https://render.com)
2. Set **Build Command**: `pip install -r requirements.txt && python -m spacy download en_core_web_sm`
3. Set **Start Command**: `cd src && uvicorn main:app --host 0.0.0.0 --port $PORT`
4. Add environment variables: `API_KEY`, `GEMINI_API_KEY`
5. Add **Tesseract**: use a `render.yaml` or custom Dockerfile with `apt-get install tesseract-ocr poppler-utils`
|