File size: 25,145 Bytes
3998131 |
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
# PDF Processing System Architecture
## System Overview
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Frontend (Next.js) β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β PDF Upload Component β β
β β β’ File input β β
β β β’ Drag & drop β β
β β β’ Progress indicator β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
ββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββ
β
HTTP/FormData
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β FastAPI Backend β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β POST /api/v1/process-pdf β
β βββΊ Extract sentences only β
β β
β POST /api/v1/process-pdf-to-bias β
β βββΊ Extract + Analyze bias (complete pipeline) β
β β
β GET /api/v1/pdf-health β
β βββΊ Service status check β
β β
ββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββ΄ββββββββββββββββββ
β β
βΌ βΌ
ββββββββββββββ βββββββββββββββββββ
β PDF Bytes β β PDFProcessor β
β β β (utility/) β
ββββββββββββββ βββββββββββββββββββ
β β
βββββββββββββ¬ββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββ
β Step 1: Extract Text β
β PyMuPDF (fitz) β
β β’ Read PDF pages β
β β’ Extract raw text β
β β’ Handle multi-page β
ββββββββββββββ¬βββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββ
β Step 2: Clean Text β
β Regex Processing β
β β’ Remove extra whitespace β
β β’ Normalize formatting β
ββββββββββββββ¬βββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββ
β Step 3: Segment Sentences β
β Nepali-aware Regex β
β β’ Split on ΰ€¦ΰ€£ΰ₯ΰ€‘ (ΰ₯€) β
β β’ Handle punctuation β
β β’ Filter short fragments β
ββββββββββββββ¬βββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββ
β Step 4: Refine (Optional) β
β Mistral LLM API β
β β’ Correct segmentation β
β β’ Remove duplicates β
β β’ JSON formatting β
ββββββββββββββ¬βββββββββββββββββ
β
ββββββββββββββ΄βββββββββββββ
β β
βΌ βΌ
ββββββββββββββββ βββββββββββββββββββ
β Sentences β β Return JSON β
β List β β Array β
ββββββββ¬ββββββββ ββββββββββ¬βββββββββ
β β
β ββββββββββββββ΄ββββββββββββββ
β β β
β βΌ βΌ
β βββββββββββββββββββ ββββββββββββββββββββ
β β Option A: β β Option B: β
β β Return to β β Pass to Bias β
β β User β β Detection β
β βββββββββββββββββββ ββββββββββ¬ββββββββββ
β β
β βΌ
β ββββββββββββββββββββββββ
β β Bias Detection Modelβ
β β DistilBERT Nepali β
β β (module_b) β
β ββββββββββββββββββββββββ€
β β β’ Classify sentence β
β β β’ 11 categories: β
β β - neutral β
β β - gender β
β β - caste β
β β - religion β
β β - political β
β β - age β
β β - disability β
β β - appearance β
β β - social status β
β β - religiosity β
β β - ambiguity β
β ββββββββββ¬ββββββββββββββ
β β
βββββββββββββ¬ββββββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββββββββββ
β Response to User β
ββββββββββββββββββββββββββββββββββββββββ€
β β’ Extracted sentences β
β β’ Bias classification results β
β β’ Confidence scores β
β β’ Biased/neutral counts β
β β’ Processing metadata β
ββββββββββββββββββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββ
β Frontend Display β
β β’ Show results β
β β’ Highlight biases β
β β’ Display stats β
ββββββββββββββββββββββββ
```
## Data Flow Diagram
```
INPUT: PDF File
β
ββ Metadata
β ββ Filename
β ββ File size
β ββ Upload timestamp
β
ββ Binary content
β
βΌ
PDFProcessor.process_pdf_from_bytes()
β
ββ extract_text_from_pdf()
β β Uses: PyMuPDF.fitz.open()
β β Output: Raw text string + page count
β ββ ~200-500ms
β
ββ clean_text()
β β Uses: Regex replacements
β β Output: Normalized text string
β ββ ~50ms
β
ββ split_into_sentences()
β β Uses: Nepali-aware regex patterns
β β Output: List[sentences]
β ββ ~50-150ms
β
ββ refine_sentences_with_llm() [OPTIONAL]
β Uses: Mistral API
β Input: JSON-formatted sentences
β Output: Refined List[sentences]
ββ ~2-5s (includes API latency)
βΌ
OUTPUT 1 (Extract Only):
{
"success": true,
"sentences": ["ΰ€΅ΰ€Ύΰ€ΰ₯ΰ€― ΰ₯§", "ΰ€΅ΰ€Ύΰ€ΰ₯ΰ€― ΰ₯¨", ...],
"total_sentences": 15,
"raw_text": "ΰ€«ΰ₯ΰ€² text...",
"filename": "doc.pdf"
}
OUTPUT 2 (Extract + Bias):
{
"success": true,
"total_sentences": 15,
"biased_count": 2,
"neutral_count": 13,
"results": [
{
"sentence": "ΰ€΅ΰ€Ύΰ€ΰ₯ΰ€― ΰ₯§",
"category": "neutral",
"confidence": 0.95,
"is_biased": false
},
...
],
"filename": "doc.pdf"
}
```
## Component Architecture
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β utility/ module β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β PDFProcessor Class β
β ββ __init__(mistral_api_key) β
β β ββ Initialize MistralClient β
β β β
β ββ extract_text_from_pdf(pdf_path) β
β β ββ fitz.open(pdf_path) β
β β ββ Iterate pages β
β β ββ get_text("text") β
β β ββ Return: raw text β
β β β
β ββ clean_text(text) β
β β ββ Remove newlines β
β β ββ Normalize spaces β
β β ββ Return: cleaned text β
β β β
β ββ split_into_sentences(text) β
β β ββ Apply Nepali regex patterns β
β β ββ Filter short fragments β
β β ββ Return: sentence list β
β β β
β ββ refine_sentences_with_llm(sentences) β
β β ββ Format as JSON β
β β ββ Send to Mistral API β
β β ββ Parse JSON response β
β β ββ Return: refined sentences β
β β β
β ββ process_pdf(pdf_path, refine_with_llm) β
β β ββ Complete pipeline (file path) β
β β β
β ββ process_pdf_from_bytes(pdf_bytes, refine_with_llm) β
β ββ Complete pipeline (bytes) β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β api/routes/ module β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β pdf_processing.py (FastAPI Routes) β
β ββ POST /api/v1/process-pdf β
β β ββ Receive: file (UploadFile), refine_with_llm β
β β ββ Call: PDFProcessor.process_pdf_from_bytes() β
β β ββ Return: PDFProcessingResponse β
β β β
β ββ POST /api/v1/process-pdf-to-bias β
β β ββ Receive: file, refine_with_llm, confidence_threshold β
β β ββ Call: PDFProcessor.process_pdf_from_bytes() β
β β ββ Call: run_bias_detection() β
β β ββ Return: PDFToBiasDetectionResponse β
β β β
β ββ GET /api/v1/pdf-health β
β ββ Check: PDFProcessor availability β
β ββ Check: Mistral client status β
β ββ Return: health status β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β api/schemas.py (Pydantic Models) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β PDFProcessingRequest / Response β
β PDFToBiasDetectionRequest / Response β
β BiasResult (reused from bias_detection) β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β External Dependencies β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β PyMuPDF (fitz) β
β ββ PDF text extraction β
β β
β Mistral API Client β
β ββ LLM-based sentence refinement β
β β
β FastAPI β
β ββ API framework (from module_a) β
β β
β Pydantic β
β ββ Data validation β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
## Processing Timeline
```
Timeline for Single PDF (~10 KB):
Time Component Duration Cumulative
ββββββββββββββββββββββββββββββββββββββββββββββββββββ
0ms ββ API receives upload ~5ms 5ms
β
5ms ββ Read bytes ~10ms 15ms
β
15ms ββ PyMuPDF extraction ~200ms 215ms
β
215ms ββ Text cleaning ~30ms 245ms
β
245ms ββ Sentence split ~100ms 345ms
β
345ms ββ LLM refinement ~3500ms 3845ms
β (if enabled)
β
3845msββ Bias detection ~500ms 4345ms
β (if enabled)
β
4345msββ Return response ~5ms 4350ms
Total Time:
ββ With LLM + Bias: ~4.3 seconds
ββ With LLM only: ~3.8 seconds
ββ Without LLM: ~0.35 seconds
ββ Bias only: ~0.5 seconds (sentence extraction)
```
## Error Handling Flow
```
API Request
β
ββ Validate file
β ββ Is PDF? β No β 400 Bad Request
β ββ Is empty? β Yes β 400 Bad Request
β ββ Is valid? β Continue
β
ββ Process PDF
ββ Extract text
β ββ File not found? β FileNotFoundError β 500
β ββ Permission denied? β Exception β 500
β ββ Success? β Continue
β
ββ Split sentences
β ββ No sentences? β Warning in response β 200 (empty results)
β ββ Success? β Continue
β
ββ Refine with LLM (if enabled)
ββ API key missing? β Warning, use regex β 200 (fallback)
ββ Network error? β Warning, use regex β 200 (fallback)
ββ Invalid JSON? β Warning, use regex β 200 (fallback)
ββ Success? β Return refined sentences β 200
```
## State Diagram
```
ββββββββββββββββ
β Idle β
ββββββββ¬ββββββββ
β
User uploads PDF
β
βΌ
ββββββββββββββββββββ
β Validating β
ββββββββ¬ββββββββββββ
β
ββββββββ΄βββββββ
β β
Invalid Valid
β β
βΌ βΌ
βββββββββββ ββββββββββββββββββββ
β Error β β Extracting β
βββββββββββ ββββββββ¬ββββββββββββ
β
ββββββββ΄βββββββ
β β
Success No Text
β β
βΌ βΌ
ββββββββββββ ββββββββββββ
βSplitting β β Error β
ββββββββ¬ββββ ββββββββββββ
β
ββββββββ΄βββββββ
β β
Success No Sentences
β β
βΌ βΌ
ββββββββββββββ ββββββββββββ
βRefining β β Error β
β(Optional) β ββββββββββββ
ββββββββ¬ββββββ
β
ββββββββ΄βββββββ
β β
Success Failed
β β
βββββββ¬ββββββββ€
β β β
βΌ βΌ βΌ
ββββββββββββββββ ββββββββββ
β Formatting β βFallbackβ
βResponse β β(Regex) β
ββββββββ¬ββββββββ βββββ¬βββββ
β β
ββββββββ¬βββββββ
β
βΌ
βββββββββββββββββββ
β Bias Detection β
β (if enabled) β
ββββββββββ¬βββββββββ
β
ββββββββ΄βββββββ
β β
Success Error
β β
βΌ βΌ
ββββββββββββββ ββββββββββ
βFormat β βReturn β
βResponse β βError β
ββββββββββ¬ββββ βββββ¬βββββ
β β
ββββββ¬ββββββ
β
βΌ
ββββββββββββββββββββ
βSend Response β
βto Client β
ββββββββββββββββββββ
```
## Integration Points
```
Frontend (Next.js)
β
βββΊ /api/v1/process-pdf
β ββ Use for: Sentence extraction only
β
βββΊ /api/v1/process-pdf-to-bias
β ββ Use for: Full analysis (PDF β Bias)
β
βββΊ /api/v1/pdf-health
ββ Use for: Service status check
Internal Integration
β
βββΊ PDFProcessor class
β ββ Use in: Custom workflows
β
βββΊ run_bias_detection() function
ββ Use in: Direct bias analysis
```
---
This architecture provides:
β
Scalable processing pipeline
β
Clear separation of concerns
β
Reusable components
β
Error resilience
β
Performance optimization options
β
Easy integration points
|