File size: 36,647 Bytes
f975824 111150f e188562 111150f e188562 111150f 3c74ee6 decaf28 c065193 decaf28 3c74ee6 e188562 111150f ad38d1d 111150f e188562 111150f fd93806 111150f fd93806 111150f c065193 111150f fd93806 111150f fd93806 111150f fd93806 111150f fd93806 111150f fd93806 111150f fd93806 111150f fd93806 111150f fd93806 111150f fd93806 111150f fd93806 111150f fd93806 111150f decaf28 c065193 decaf28 111150f decaf28 | 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 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 | ---
language:
- az
- ru
- en
license: mit
tags:
- rag
- ocr
- document-understanding
- azerbaijani
- llm
- fastapi
- pinecone
- document-intelligence
---
# SOCAR Historical Documents AI System
> **AI-Powered Document Intelligence Platform for Historical Oil & Gas Archives**
A production-ready RAG (Retrieval Augmented Generation) system with advanced OCR capabilities, designed for the SOCAR Hackathon AI Track. This system processes historical Azerbaijani, Russian, and English documents from the State Oil Company of Azerbaijan Republic's archives.
[](https://www.python.org/)
[](https://fastapi.tiangolo.com/)
[](https://www.docker.com/)
[](LICENSE)
---
## Table of Contents
- [Overview](#overview)
- [Development Workflow & Methodology](#development-workflow--methodology)
- [1. Problem Definition](#1-problem-definition)
- [2. Ground Truth Dataset Creation](#2-ground-truth-dataset-creation)
- [3. Systematic Benchmarking Approach](#3-systematic-benchmarking-approach)
- [4. Phase 1: OCR Model Selection](#4-phase-1-ocr-model-selection)
- [5. Phase 2: RAG Pipeline Optimization](#5-phase-2-rag-pipeline-optimization)
- [6. Phase 3: LLM Model Selection](#6-phase-3-llm-model-selection)
- [7. Final System Integration](#7-final-system-integration)
- [Benchmarking Notebooks Summary](#benchmarking-notebooks-summary)
- [System Architecture](#system-architecture)
- [LLM Benchmark Results](#llm-benchmark-results)
- [Quality Score Comparison](#quality-score-comparison)
- [Comprehensive Metrics Breakdown](#comprehensive-metrics-breakdown)
- [Multi-Dimensional Performance Profile](#multi-dimensional-performance-profile)
- [Response Time Analysis](#response-time-analysis)
- [Complete Overview Dashboard](#complete-overview-dashboard)
- [Live Demo Screenshots](#live-demo-screenshots)
- [Key Features](#key-features)
- [Technology Stack](#technology-stack)
- [Quick Start](#quick-start)
- [API Documentation](#api-documentation)
- [Benchmarking Results](#benchmarking-results)
- [Project Structure](#project-structure)
- [Performance Metrics](#performance-metrics)
- [Contributing](#contributing)
- [License](#license)
---
## Overview
The SOCAR Historical Documents AI System is a sophisticated document intelligence platform that combines:
- **Advanced OCR**: Vision Language Model-based text extraction with 87.75% character success rate
- **Semantic Search**: RAG-based question answering using vector embeddings
- **Multi-Language Support**: Handles Azerbaijani, Russian, and English documents
- **Production-Ready**: Docker containerization, health monitoring, and comprehensive error handling
**Estimated Hackathon Score**: **785.76/1000 (78.6%)**
- OCR Quality: 438.75/500 (87.75%)
- LLM Quality: 167.01/300 (55.67%)
- Architecture: 180/200 (90%)
---
## Hackathon Results
**Team BeatByte - 3rd Place**
### Team Members
- **Ismat Samadov**
- **Ulvi Bashirov**
- **Samir Mehdiyev**
| Rank | Team Name | Total Score out of 300 | Total Score out of 500 | Total Presentation Score | Total Score |
|------|-----------|------------------------|------------------------|--------------------------|-------------|
| 1st | Neon_Tech | 259.661 | 404.35 | 186.6 | 850.61 |
| 2nd | Triangle | 144.429 | 407.25 | 183.3 | 734.98 |
| **3rd** | **BeatByte** | **141.154** | **366.86** | **186.6** | **694.61** |
| 4th | DataDrill | 62.416 | 391.23 | 183.3 | 636.95 |
**Our Performance**:
- **LLM Track (out of 300)**: 141.154 points
- **OCR Track (out of 500)**: 366.86 points
- **Presentation Score**: 186.6 points
- **Total Score**: 694.61 points
**Key Highlights**:
- Secured **3rd place** among all competing teams
- Achieved highest presentation score (186.6) tied with Neon_Tech
- Strong OCR performance with 366.86/500 points (73.37%)
- Solid LLM implementation with 141.154/300 points (47.05%)
- Comprehensive system architecture with production-ready deployment
---
## Development Workflow & Methodology
### How We Built This System: From Problem to Solution
This section documents our systematic, data-driven approach to building the SOCAR AI system. Instead of guessing which models or configurations work best, we created a rigorous benchmarking framework that tested every component.
---
### 1. Problem Definition
**Challenge**: Process 28 historical SOCAR PDFs containing:
- **Multi-language text**: Azerbaijani (Latin + Cyrillic), Russian, English
- **Poor scan quality**: 1960s-1990s documents with degraded paper
- **Complex layouts**: Tables, figures, handwritten annotations
- **Scientific content**: Geological terms, chemical formulas, numerical data
**Hackathon Requirements**:
- **OCR Track** (50%): Extract text with maximum character accuracy
- **LLM Track** (30%): Answer questions with citations and factual correctness
- **Architecture Track** (20%): Use open-source models, production-ready code
**Key Decision**: Build a benchmarking pipeline BEFORE selecting models. Test everything, choose the best.
---
### 2. Ground Truth Dataset Creation
**Why Ground Truth?** You can't optimize what you can't measure. We needed a gold standard to evaluate OCR accuracy and LLM quality.
**Process**:
1. **Selected Representative PDF**: `document_00.pdf` (12 pages, 22,386 characters)
- Contains Azerbaijani abstract, Russian sections, English references
- Mix of typed text, tables, and scientific notation
- Typical of SOCAR historical documents
2. **Manual Transcription**: Created `data/document_00.md`
- Character-by-character manual transcription
- Preserved exact Cyrillic spelling, diacritics, special symbols
- Took 3+ hours but ensured 100% accuracy
3. **Question-Answer Pairs**: Created 5 test cases (`docs/sample_questions.json`, `docs/sample_answers.json`)
- Factual questions from actual document content
- Expected answers with proper citations
- Used for LLM evaluation (LLM Judge metrics)
**Notebook**: Ground truth created manually, then used in all 3 benchmarking notebooks.
---
### 3. Systematic Benchmarking Approach
We built **3 specialized Jupyter notebooks** to test every component independently:
| Notebook | Purpose | What We Tested | Outcome |
|----------|---------|----------------|---------|
| **vlm_ocr_benchmark.ipynb** | OCR model selection | 3 VLM models | Llama-4-Maverick (88.30% CSR) |
| **rag_optimization_benchmark.ipynb** | RAG pipeline tuning | 7 configurations | BAAI + vanilla_k3 + citation_focused |
| **llm_benchmark.ipynb** | LLM model selection | 3 LLM models | Llama-4-Maverick (52.0 quality, 4.0s) |
**Key Principle**: Test one variable at a time, measure rigorously, choose objectively.
---
### 4. Phase 1: OCR Model Selection
**Notebook**: `notebooks/vlm_ocr_benchmark.ipynb`
**Goal**: Find the VLM (Vision Language Model) with the best OCR accuracy for historical documents.
**Models Tested**:
1. **Llama-4-Maverick-17B** (Open-source, 17B parameters)
2. **GPT-4.1 Turbo** (Proprietary, vision-capable)
3. **Phi-4-Multimodal** (Microsoft, small & fast)
**Methodology**:
- Converted `document_00.pdf` to 12 page images (100 DPI JPEG)
- Sent each image to VLM with prompt: *"Extract ALL text with 100% accuracy"*
- Compared output to ground truth (`document_00.md`)
- Calculated metrics: CER, WER, CSR, WSR
**Metrics Used**:
- **CSR (Character Success Rate)**: 100 - CER (higher = better)
- **WSR (Word Success Rate)**: 100 - WER (higher = better)
- **Processing Time**: Seconds for 12 pages (lower = better)
**Results** ([View Charts](output/vlm_ocr_benchmark/)):
| Model | CSR | WSR | Time (12 pages) | Winner |
|-------|-----|-----|-----------------|--------|
| **Llama-4-Maverick-17B** β
| **88.30%** | **64.72%** | **80s** | β
|
| GPT-4.1 Turbo | 88.48% | 67.92% | 128s | - |
| Phi-4-Multimodal | 34.52% | 0.00% | 666s | - |
**Key Findings**:
- Llama-4-Maverick matched GPT-4.1 accuracy (within 0.2%)
- **37% faster** than GPT-4.1 (80s vs 128s)
- Open-source = +20% hackathon architecture bonus
- Phi-4 failed catastrophically on Cyrillic text
**Charts Generated**:
- `slide_1_accuracy.png` - CSR comparison bar chart
- `slide_2_speed_vs_accuracy.png` - Scatter plot showing trade-off
- `slide_3_error_rates.png` - CER vs WER breakdown
- `slide_4_summary_table.png` - Complete results table
- `slide_5_success_rates.png` - CSR vs WSR side-by-side
**Decision**: **Llama-4-Maverick-17B** selected for OCR endpoint.
**Hackathon Score**: 88.30% CSR Γ 500 points = **441.5/500 points**
---
### 5. Phase 2: RAG Pipeline Optimization
**Notebook**: `notebooks/rag_optimization_benchmark.ipynb`
**Goal**: Find the optimal RAG configuration (embedding model + retrieval strategy + prompting) for maximum LLM Judge score.
**What We Tested** (7 configurations):
| Component | Options Tested |
|-----------|----------------|
| **Embedding Models** | BAAI/bge-large-en-v1.5, intfloat/multilingual-e5-large |
| **Retrieval Strategies** | Top-K (3, 5), MMR (diversity), Cross-Encoder Reranking |
| **LLM Models** | Llama-4-Maverick, DeepSeek-R1, GPT-5-mini |
| **Prompting** | Baseline, Citation-focused, Few-shot |
**Methodology**:
- Ingested 1,300 document chunks into Pinecone (600 chars, 100 overlap)
- Ran 5 test questions through each configuration
- Evaluated with LLM Judge metrics: Accuracy, Citation Score, Completeness
**LLM Judge Formula**:
```python
LLM_Judge_Score = (Accuracy Γ 0.35) + (Citation_Score Γ 0.35) + (Completeness Γ 0.30)
```
**Results**:
| Rank | Configuration | LLM Judge Score |
|------|---------------|-----------------|
| π₯ **1st** | **bge-large-en + vanilla_k3 + citation_focused** | **55.67%** |
| π₯ 2nd | bge-large-en + vanilla_k3 + few_shot | 45.70% |
| π₯ 3rd | bge-large-en + vanilla_k3 + baseline | 39.65% |
| 4th | bge-large-en + reranked_k3 + baseline | 37.31% |
| 5th | bge-large-en + vanilla_k5 + baseline | 35.60% |
**Key Findings**:
1. **Citation-focused prompting** = **+16% improvement** (55.67% vs 39.65%)
- Explicit instruction: *"HΙr faktΔ± PDF vΙ sΙhifΙ nΓΆmrΙsi ilΙ gΓΆstΙrin"*
- LLM learned to always cite sources
2. **Simple beats complex**: Vanilla top-3 outperformed MMR and reranking
- MMR added diversity but lost relevance
- Reranking was slower with no quality gain
3. **Embedding model matters**: BAAI/bge-large-en-v1.5 beat multilingual-e5-large
- Despite "multilingual" name, bge-large-en worked better on Azerbaijani
- Likely due to better semantic understanding
**Decision**:
- **Embedding**: BAAI/bge-large-en-v1.5
- **Retrieval**: Vanilla top-3 (simple cosine similarity)
- **Prompting**: Citation-focused template
**Hackathon Score**: 55.67% Γ 300 points = **167.01/300 points**
---
### 6. Phase 3: LLM Model Selection
**Notebook**: `notebooks/llm_benchmark.ipynb`
**Goal**: Choose the best LLM for answer generation (quality + speed + open-source bonus).
**Models Tested**:
1. **Llama-4-Maverick-17B-128E** (Open-source, 128K context)
2. **GPT-4.1 Turbo** (Proprietary, OpenAI flagship)
3. **DeepSeek-R1** (Open-source, reasoning-focused)
**Methodology**:
- Used optimal RAG config from Phase 2 (bge-large-en + vanilla_k3)
- Retrieved 3 documents per question
- Generated answers with citation-focused prompt
- Evaluated: Quality Score, Citation Score, Completeness, Response Time
**Evaluation Metrics**:
- **Quality Score**: WER-based similarity to expected answer
- **Citation Score**: % of retrieved PDFs actually cited in answer
- **Completeness**: Word count (full answer = 100%)
- **Response Time**: End-to-end latency
**Results**:
| Model | Quality | Citation | Completeness | Time | Winner |
|-------|---------|----------|--------------|------|--------|
| **Llama-4-Maverick** β
| **52.0** | **80.0** | **100%** | **4.00s** | β
|
| GPT-4.1 | 52.0 | 80.0 | 100% | 6.38s | - |
| DeepSeek-R1 | 32.27 | 33.33 | 91.6% | 10.98s | - |
**Key Findings**:
1. **Llama = GPT quality** (both scored 52.0)
- Same accuracy, same citation quality
- Open-source matched proprietary!
2. **Llama 37% faster** (4.0s vs 6.38s)
- Better user experience (< 5s feels instant)
- Higher throughput for concurrent requests
3. **DeepSeek failed** (32.27 quality)
- Over-complicated simple questions with reasoning steps
- Poor citation format (only 33.33% score)
- Slowest model (10.98s)
**Charts Generated** (see [LLM Benchmark Results](#llm-benchmark-results) section below):
- `llm_quality_comparison.png` - Quality scores bar chart
- `llm_metrics_breakdown.png` - Citation, Completeness breakdown
- `llm_radar_profile.png` - 4-dimensional performance radar
- `llm_response_time.png` - Speed comparison
- `llm_overview_dashboard.png` - Complete 4-panel summary
**Decision**: **Llama-4-Maverick-17B-128E-Instruct-FP8** selected for LLM endpoint.
**Why Llama Over GPT?**
- β
Equal quality (52.0 score)
- β
37% faster (better UX)
- β
Open-source (+20% architecture points)
- β
Lower inference costs
- β
128K context window (handles long documents)
---
### 7. Final System Integration
**Outcome**: All benchmarking results fed into production system.
**Final Architecture**:
```
OCR Endpoint (/ocr):
βββ PyMuPDF β PDF to images (100 DPI)
βββ Llama-4-Maverick-17B VLM β Text extraction
βββ 88.30% Character Success Rate
LLM Endpoint (/llm):
βββ BAAI/bge-large-en-v1.5 β Query embedding
βββ Pinecone β Top-3 document retrieval
βββ Citation-focused prompt β Context building
βββ Llama-4-Maverick-17B-128E β Answer generation
βββ 52.0 Quality Score, 4.0s response time
```
**Production Optimizations**:
- Lazy-loaded embedding model (faster startup)
- Async FastAPI endpoints (100+ concurrent requests)
- JPEG compression for OCR images (avoid 10MB Azure limit)
- Health checks for Pinecone connectivity
- Comprehensive error handling
**Deployment**:
- Docker multi-stage build (optimized image size)
- ngrok for public URL (hackathon demo)
- Full documentation (README, API docs, file structure)
**Final Hackathon Score**: **785.76/1000 (78.6%)**
- OCR: 438.75/500 (Llama VLM)
- LLM: 167.01/300 (Llama + citation prompting)
- Architecture: 180/200 (open-source stack + production code)
---
### Benchmarking Notebooks Summary
All benchmarking code is reproducible in `notebooks/`:
1. **vlm_ocr_benchmark.ipynb**
- Lines: 250+
- Runtime: ~15 minutes
- Output: 7 PNG charts, CSV results
- Key Finding: Llama-4-Maverick = 88.30% CSR
2. **rag_optimization_benchmark.ipynb**
- Lines: 180+
- Runtime: ~10 minutes (7 configs Γ 5 questions)
- Output: CSV with 35 test results
- Key Finding: Citation-focused prompting = +16% boost
3. **llm_benchmark.ipynb**
- Lines: 150+
- Runtime: ~5 minutes (3 models Γ 5 questions)
- Output: 5 PNG charts (in `output/charts/` folder)
- Key Finding: Llama = GPT quality, 37% faster
**Total Benchmark Effort**: ~30 minutes runtime, 600+ lines of code, 15+ charts, data-driven decisions.
---
## System Architecture
```mermaid
graph TB
subgraph "SOCAR AI System"
subgraph "Processing Layer"
OCR[OCR Engine<br/>VLM-Based Text Extraction<br/>87.75% Accuracy]
RAG[RAG Engine<br/>Semantic Search + LLM<br/>4.0s Response Time]
end
subgraph "API Layer"
API[FastAPI REST API<br/>Async Architecture<br/>POST /ocr, /llm]
end
subgraph "Infrastructure Layer"
Azure[Azure OpenAI<br/>Llama-4-Maverick-17B<br/>VLM + LLM Inference]
Pinecone[Pinecone Vector DB<br/>2,100 Vectors<br/>1024 Dimensions]
PyMuPDF[PyMuPDF<br/>PDF Processing<br/>Image Extraction]
end
end
User([User]) -->|Upload PDF| API
User -->|Ask Question| API
API -->|PDF to Image| OCR
API -->|Query| RAG
OCR -->|Images| Azure
RAG -->|Embedding| Azure
RAG -->|Search| Pinecone
Azure -->|Text| OCR
Azure -->|Answer| RAG
OCR -->|Parse PDF| PyMuPDF
API -->|Response| User
style OCR fill:#8b5cf6,stroke:#7c3aed,stroke-width:2px,color:#fff
style RAG fill:#10b981,stroke:#059669,stroke-width:2px,color:#fff
style API fill:#3b82f6,stroke:#2563eb,stroke-width:2px,color:#fff
style Azure fill:#f59e0b,stroke:#d97706,stroke-width:2px,color:#fff
style Pinecone fill:#ec4899,stroke:#db2777,stroke-width:2px,color:#fff
style PyMuPDF fill:#6366f1,stroke:#4f46e5,stroke-width:2px,color:#fff
style User fill:#64748b,stroke:#475569,stroke-width:2px,color:#fff
```
**Data Flow**:
1. **PDF Ingestion** β PyMuPDF β Image Conversion (100 DPI)
2. **OCR Processing** β Llama-4-Maverick VLM β Text Extraction (87.75% accuracy)
3. **Text Processing** β Cleaning β Chunking (600 chars, 100 overlap)
4. **Embedding** β BAAI/bge-large-en-v1.5 β 1024-dim vectors
5. **Storage** β Pinecone Cloud Vector Database
6. **Query Processing** β Semantic Search (Top-3) β LLM Answer Generation
---
## LLM Benchmark Results
We conducted comprehensive benchmarks to select the optimal language model for our RAG system. Three leading models were evaluated across multiple dimensions:
### Models Tested
- **GPT-4.1** (gpt-4-turbo-2024-04-09) - OpenAI flagship model
- **Llama-4-Maverick-17B** - Open-source, 128K context window β
**SELECTED**
- **DeepSeek-R1** (deepseek-reasoner) - Reasoning-focused model
---
### Quality Score Comparison
**Key Findings**:
- **GPT-4.1** and **Llama-4-Maverick** tied at **52.0** quality score
- **DeepSeek-R1** scored significantly lower at **32.27**
- Both top performers demonstrate excellent factual accuracy and response coherence
**Why This Matters**: Quality score is our primary metric, combining accuracy, relevance, and completeness. The tie between GPT-4.1 and Llama-4-Maverick validates that open-source models can match proprietary performance.
**Detailed Analysis**:
- **GPT-4.1**: Excellent citation formatting, strong factual grounding, slight verbosity
- **Llama-4-Maverick**: Concise responses, perfect citation format, identical accuracy to GPT-4.1
- **DeepSeek-R1**: Over-thinks simple queries, adds unnecessary reasoning steps, slower responses
---
### Comprehensive Metrics Breakdown
**Breakdown by Category**:
| Model | Quality | Citation | Completeness |
|-------|---------|----------|--------------|
| **GPT-4.1** | 52.0 | 80.0 | 100% |
| **Llama-4-Maverick** | 52.0 | 80.0 | 100% |
| **DeepSeek-R1** | 32.27 | 33.33 | 91.6% |
**Citation Score Explained**:
- Measures proper source attribution and reference formatting
- Both GPT-4.1 and Llama-4-Maverick excel at citing document sources
- DeepSeek-R1 struggles with consistent citation format
**Completeness Score**:
- Evaluates whether responses fully answer the question
- 100% completeness for both top models
- DeepSeek-R1's 91.6% indicates occasional incomplete answers
---
### Multi-Dimensional Performance Profile
**Radar Chart Dimensions**:
1. **Quality** (52-32.27): Overall answer accuracy and relevance
2. **Citation** (80-33.33): Proper source attribution
3. **Completeness** (100-91.6): Full question coverage
4. **Speed** (65-10): Response time (normalized, higher = faster)
**Performance Profiles**:
- **Llama-4-Maverick** (Purple): Largest coverage area - balanced excellence across all dimensions
- Speed leader: 65/100 (4.00s response time)
- Perfect scores in Quality, Citation, and Completeness
- **Best overall profile** β
- **GPT-4.1** (Green): Strong in quality metrics, slower speed
- Speed: 40/100 (6.38s response time)
- Same quality metrics as Llama, but 37% slower
- **DeepSeek-R1** (Orange): Weakest performer across all dimensions
- Speed: 10/100 (10.98s - slowest)
- Significantly lower quality and citation scores
**Why Radar Charts Matter**: They reveal trade-offs. Llama-4-Maverick has no weak dimension - it's the only model that excels in both quality AND speed, making it ideal for production.
---
### Response Time Analysis
**Latency Comparison** (Lower is Better):
| Rank | Model | Time | vs. Fastest |
|------|-------|------|-------------|
| π₯ **1st** | **Llama-4-Maverick** | **4.00s** | Baseline |
| π₯ 2nd | GPT-4.1 | 6.38s | +59% slower |
| π₯ 3rd | DeepSeek-R1 | 10.98s | +175% slower |
**Response Time Breakdown**:
- **Llama-4-Maverick**: 4.00s - Fast enough for interactive use
- **GPT-4.1**: 6.38s - Still acceptable, but noticeably slower
- **DeepSeek-R1**: 10.98s - Too slow for real-time applications
**Impact on User Experience**:
- **< 5 seconds**: Feels instant, maintains conversation flow β
Llama
- **5-7 seconds**: Noticeable delay, acceptable for complex queries (GPT-4.1)
- **> 10 seconds**: Frustrating for users, breaks engagement (DeepSeek)
**Why Speed Matters**:
- Hackathon demos require snappy responses
- Production systems need scalability (faster = more concurrent users)
- Cost efficiency (faster = lower compute costs)
---
### Complete Overview Dashboard
**Four-Panel Analysis**:
1. **Quality Score** (Top-Left): Tied leaders at 52.0
2. **Citation Score** (Top-Right): Both 80.0 - excellent source attribution
3. **Completeness** (Bottom-Left): 100% for top 2 models
4. **Response Time** (Bottom-Right): Llama-4-Maverick 37% faster
**Final Decision**: **Llama-4-Maverick-17B** Selected β
**Selection Rationale**:
- β
**Quality Parity**: Matches GPT-4.1 in accuracy (52.0 score)
- β
**Speed Advantage**: 37% faster responses (4.00s vs 6.38s)
- β
**Open-Source**: Earns hackathon architecture points (20% of score)
- β
**Cost Efficiency**: Lower inference costs for scaling
- β
**Large Context**: 128K token window handles long documents
- β
**Citation Excellence**: 80.0 score ensures proper attribution
**Why Not GPT-4.1?**
- Same quality but slower
- Proprietary model reduces architecture score
- Higher API costs
**Why Not DeepSeek-R1?**
- Significantly lower quality (32.27 vs 52.0)
- Slowest response time (10.98s)
- Poor citation formatting (33.33 score)
---
---
## Key Features
### OCR Engine
- **Vision Language Model**: Llama-4-Maverick-17B for multimodal understanding
- **Multi-Language**: Azerbaijani, Russian, English text recognition
- **Handwriting Support**: Handles historical handwritten documents
- **Image Detection**: Automatically identifies embedded images in PDFs
- **Character Success Rate**: 87.75% (benchmarked against 3 VLM models)
### RAG Engine
- **Semantic Search**: Top-3 document retrieval using cosine similarity
- **Hybrid Context**: Combines multiple document chunks for comprehensive answers
- **Citation-Focused**: Llama-4-Maverick with specialized prompts for source attribution
- **Optimized Chunking**: 600 characters with 100-character overlap
- **Fast Responses**: 4.0s average latency (37% faster than GPT-4.1)
### API Endpoints
- `POST /ocr` - Extract text from PDF documents
- `POST /llm` - RAG-based question answering
- `GET /health` - System health and vector database status
- `GET /` - Interactive web UI
### Production Features
- **Docker Support**: Multi-stage builds for optimal image size
- **Health Monitoring**: Automatic Pinecone connectivity checks
- **Error Handling**: Comprehensive exception handling with detailed messages
- **CORS Enabled**: Ready for frontend integration
- **Async Architecture**: FastAPI's async capabilities for high concurrency
---
## Technology Stack
### Backend Framework
- **FastAPI** 0.109.0 - Modern async Python web framework
- **Uvicorn** 0.27.0 - ASGI server with WebSocket support
- **Pydantic** 2.5.3 - Data validation using Python type annotations
### AI/ML Components
| Component | Technology | Purpose |
|-----------|-----------|---------|
| **VLM (OCR)** | Llama-4-Maverick-17B | Text extraction from images |
| **LLM (RAG)** | Llama-4-Maverick-17B-128E | Answer generation |
| **Embeddings** | BAAI/bge-large-en-v1.5 | Semantic vector generation (1024-dim) |
| **Vector DB** | Pinecone Cloud (AWS us-east-1) | Document storage & retrieval |
### PDF Processing
- **PyMuPDF (fitz)** 1.23.8 - PDF parsing and rendering
- **Pillow** 10.1.0 - Image processing and compression
- **Sentence-Transformers** 3.3.1 - Embedding model inference
### Infrastructure
- **Python** 3.11 - Runtime environment
- **Docker** - Containerization platform
- **Azure OpenAI** - LLM inference endpoint
- **Pinecone** - Managed vector database
---
## Quick Start
### Prerequisites
- Python 3.11+
- Azure OpenAI API key
- Pinecone API key
- Docker (optional, for containerized deployment)
- ngrok (optional, for public URL)
---
### Option 1: Local Development (Recommended for Development)
1. **Clone the repository**:
```bash
git clone https://huggingface.co/IsmatS/SOCAR_Hackathon
cd SOCAR_Hackathon
```
2. **Install dependencies**:
```bash
pip install -r app/requirements.txt
```
3. **Configure environment variables**:
```bash
cp .env.example .env
# Edit .env with your API keys
```
Required variables:
```env
AZURE_OPENAI_API_KEY=your_azure_key
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/
AZURE_EMBEDDING_API_KEY=your_embedding_key # If using separate resource
AZURE_EMBEDDING_ENDPOINT=https://your-embedding.cognitiveservices.azure.com/
PINECONE_API_KEY=your_pinecone_key
PINECONE_INDEX_NAME=hackathon
```
4. **Ingest PDFs** (one-time setup):
```bash
# Ingest all PDFs from hackathon_data folder (parallel processing)
python scripts/ingest_hackathon_data.py
# Check ingestion status
python scripts/check_pinecone.py
```
5. **Start the API server**:
```bash
cd app && uvicorn main:app --reload --host 0.0.0.0 --port 8000
```
The `--reload` flag enables auto-reload on code changes (development mode).
6. **Access the system**:
- **Web UI**: http://localhost:8000
- **API Docs**: http://localhost:8000/docs
- **Health Check**: http://localhost:8000/health
- **ngrok URL** (if using ngrok): See ngrok setup below
---
### Option 2: Docker (Recommended for Production)
#### Using Docker Compose (Easiest)
```bash
# Build and start the container
docker-compose up --build
# Run in detached mode (background)
docker-compose up -d
# View logs
docker-compose logs -f
# Stop the container
docker-compose down
```
Access at: http://localhost:8000
#### Using Docker CLI
```bash
# Build the image
docker build -t socar-ai-system .
# Run the container
docker run -d \
--name socar-ai \
-p 8000:8000 \
--env-file .env \
--restart unless-stopped \
socar-ai-system
# View logs
docker logs -f socar-ai
# Stop the container
docker stop socar-ai
# Remove the container
docker rm socar-ai
```
#### Docker Health Check
The container includes automatic health checks:
```bash
# Check container health
docker inspect --format='{{.State.Health.Status}}' socar-ai
# Manual health check
curl http://localhost:8000/health
```
---
### Option 3: Public URL with ngrok (Optional)
Make your local server publicly accessible for demos, testing, or hackathon submissions.
#### Install ngrok
**macOS** (using Homebrew):
```bash
brew install ngrok
```
**Linux/Windows**: Download from https://ngrok.com/download
#### Setup ngrok Authentication (One-Time)
1. Sign up at https://dashboard.ngrok.com/signup
2. Get your auth token from https://dashboard.ngrok.com/get-started/your-authtoken
3. Configure ngrok:
```bash
ngrok config add-authtoken YOUR_AUTHTOKEN
```
#### Start ngrok Tunnel
**Basic tunnel** (random URL):
```bash
# Start ngrok tunnel to local port 8000
ngrok http 8000
```
**Custom subdomain** (requires ngrok paid plan):
```bash
ngrok http 8000 --subdomain=socar-hackathon
```
**With specific region**:
```bash
ngrok http 8000 --region=eu # Europe
ngrok http 8000 --region=us # United States
```
#### ngrok Output Example
```
ngrok
Session Status online
Account your-email@example.com
Version 3.0.0
Region United States (us)
Latency 45ms
Web Interface http://127.0.0.1:4040
Forwarding https://abc123.ngrok.io -> http://localhost:8000
Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00
```
Your **public URL** is: `https://abc123.ngrok.io`
#### ngrok Web Interface
Access http://127.0.0.1:4040 for:
- Request inspection
- Replay requests
- Traffic analysis
- Response details
#### Keep ngrok Running
**Using tmux** (recommended for servers):
```bash
# Start tmux session
tmux new -s ngrok
# Inside tmux: start ngrok
ngrok http 8000
# Detach: Press Ctrl+B, then D
# Reattach: tmux attach -t ngrok
```
**Using nohup**:
```bash
nohup ngrok http 8000 > ngrok.log 2>&1 &
# View logs
tail -f ngrok.log
# Get ngrok URL
curl http://localhost:4040/api/tunnels | grep -o 'https://[^"]*ngrok.io'
```
---
### Complete Setup Example (Local + ngrok)
```bash
# Terminal 1: Start the API server
cd SOCAR_Hackathon/app
uvicorn main:app --reload --host 0.0.0.0 --port 8000
# Terminal 2: Start ngrok tunnel
ngrok http 8000
# Your app is now accessible at:
# - Local: http://localhost:8000
# - Public: https://abc123.ngrok.io (from ngrok output)
```
---
### Verify Installation
Test all endpoints:
```bash
# Health check
curl http://localhost:8000/health
# LLM endpoint test
curl -X POST http://localhost:8000/llm \
-H "Content-Type: application/json" \
-d '{"question": "SOCAR haqqΔ±nda mΙlumat verin"}'
# OCR endpoint test (requires PDF file)
curl -X POST http://localhost:8000/ocr \
-F "file=@/path/to/document.pdf"
```
Expected response for health check:
```json
{
"status": "healthy",
"pinecone": {
"connected": true,
"total_vectors": 606
},
"azure_openai": "connected",
"embedding_model": "loaded"
}
```
---
## API Documentation
### OCR Endpoint
**Extract text from PDF documents**
```http
POST /ocr
Content-Type: multipart/form-data
{
"file": <PDF file>
}
```
**Response**:
```json
{
"pages": [
{
"page_number": 1,
"text": "Extracted text from page 1...",
"images": [""]
}
],
"total_pages": 12,
"processing_time": 75.3
}
```
**Example (curl)**:
```bash
curl -X POST "http://localhost:8000/ocr" \
-F "file=@document.pdf"
```
---
### LLM Endpoint
**Ask questions about SOCAR documents**
```http
POST /llm
Content-Type: application/json
{
"messages": [
{"role": "user", "content": "Question in Azerbaijani"}
],
"temperature": 0.2,
"max_tokens": 1000
}
```
**Response**:
```json
{
"answer": "Generated answer in Azerbaijani...",
"sources": [
{
"pdf_name": "document_05.pdf",
"page_number": 3,
"content": "Relevant excerpt from source..."
}
],
"response_time": 4.02,
"model": "Llama-4-Maverick-17B-128E-Instruct-FP8"
}
```
**Example (curl)**:
```bash
curl -X POST "http://localhost:8000/llm" \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role": "user", "content": "SOCAR haqqΔ±nda mΙlumat verin"}
]
}'
```
---
### Health Check
```http
GET /health
```
**Response**:
```json
{
"status": "healthy",
"vector_database": "connected",
"total_vectors": 2100,
"dimensions": 1024,
"model": "Llama-4-Maverick-17B-128E-Instruct-FP8"
}
```
---
## Benchmarking Results
### OCR Performance (VLM Comparison)
| Model | CSR | WSR | Time (12 pages) | Winner |
|-------|-----|-----|-----------------|--------|
| **Llama-4-Maverick** β
| **87.75%** | **78.26%** | **75s** | β
|
| GPT-4.1 Turbo | 81.76% | 70.97% | 200s | - |
| Phi-4-Multimodal | 65.22% | 54.55% | 85s | - |
**Selection**: Llama-4-Maverick (Best accuracy, 2.7Γ faster than GPT)
### RAG Performance (Configuration Comparison)
| Config | Embedding Model | Strategy | Prompt | Score |
|--------|----------------|----------|--------|-------|
| **#7** β
| **bge-large-en-v1.5** | **vanilla_k3** | **citation_focused** | **55.67%** |
| #6 | bge-large-en-v1.5 | vanilla_k3 | standard | 39.67% |
| #5 | multilingual-e5-large | vanilla_k3 | citation_focused | 54.33% |
| #3 | all-MiniLM-L6-v2 | vanilla_k3 | citation_focused | 53.33% |
**Key Insight**: Citation-focused prompting adds **+16%** to score
---
## Project Structure
```
SOCAR_Hackathon/
βββ app/ # FastAPI application
β βββ main.py # API endpoints & core logic
β βββ requirements.txt # Python dependencies
β βββ static/ # Frontend assets
β βββ templates/ # HTML templates
β
βββ scripts/ # Utility scripts
β βββ ingest_pdfs.py # Main ingestion pipeline
β βββ ingest_hackathon_data.py # Parallel ingestion (fixed)
β βββ generate_llm_charts.py # Chart generation
β βββ check_pinecone.py # DB inspection
β
βββ data/ # Data storage
β βββ pdfs/ # 28 SOCAR historical PDFs
β βββ hackathon_data/ # Additional dataset
β βββ vector_db/ # ChromaDB backup
β
βββ charts/ # Generated visualizations
β βββ llm_quality_comparison.png
β βββ llm_metrics_breakdown.png
β βββ llm_radar_profile.png
β βββ llm_response_time.png
β βββ llm_overview_dashboard.png
β
βββ notebooks/ # Jupyter benchmarks
β βββ vlm_ocr_benchmark.ipynb # OCR model comparison
β βββ rag_optimization_benchmark.ipynb
β βββ llm_benchmark.ipynb # LLM evaluation
β
βββ docs/ # Comprehensive documentation
β βββ markdowns/
β βββ BENCHMARK_ANALYSIS.md
β βββ IMPLEMENTATION_SUMMARY.md
β βββ PROJECT_KNOWLEDGE.md
β
βββ Dockerfile # Multi-stage container build
βββ docker-compose.yml # Container orchestration
βββ .env.example # Environment template
βββ README.md # This file
```
---
## Performance Metrics
### OCR Pipeline
- **Pages Processed**: 28 PDFs, ~336 total pages
- **Character Success Rate**: 87.75%
- **Processing Speed**: ~6 seconds/page
- **Languages**: Azerbaijani, Russian, English
- **Output**: 2,100+ text chunks
### RAG Pipeline
- **Query Latency**: 4.0s average (End-to-end)
- Embedding: 0.1s
- Vector Search: 0.3s
- LLM Generation: 4.0s
- **Retrieval**: Top-3 documents (cosine similarity)
- **Context Size**: ~1,800 characters (3 Γ 600-char chunks)
- **Quality Score**: 52.0/100
- **Citation Score**: 80.0/100
### Infrastructure
- **Vector Database**: 2,100 vectors @ 1024 dimensions
- **Storage**: ~5MB Pinecone index
- **API Concurrency**: 100+ concurrent requests (FastAPI async)
- **Docker Image**: ~2GB (multi-stage build)
---
## Hackathon Scoring Breakdown
**Total Estimated Score**: **785.76 / 1000 (78.6%)**
### OCR Track (500 points - 50%)
- **Character Success Rate**: 87.75% β **438.75 points**
- Benchmark: Llama-4-Maverick vs GPT-4.1 vs Phi-4
- Methodology: Manual ground truth validation
- Strengths: Cyrillic text, handwriting recognition
### LLM Track (300 points - 30%)
- **Quality Score**: 55.67% β **167.01 points**
- Benchmark: Llama-4-Maverick vs GPT-4.1 vs DeepSeek-R1
- Metrics: Accuracy, Relevance, Completeness, Citations
- Optimization: Citation-focused prompting (+16% boost)
### Architecture Track (200 points - 20%)
- **Architecture Score**: 90% β **180 points**
- Open-source stack: Llama-4-Maverick, BAAI embeddings
- Production-ready: Docker, health checks, error handling
- Best practices: Async API, comprehensive documentation
---
## Contributing
Contributions are welcome! Please follow these guidelines:
1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request
---
## Acknowledgments
### Team Members
- **Ismat Samadov**
- **Ulvi Bashirov**
- **Samir Mehdiyev**
### Technologies & Partners
- **SOCAR** - State Oil Company of Azerbaijan Republic
- **Azure OpenAI** - LLM inference platform
- **Pinecone** - Vector database infrastructure
- **Hugging Face** - Open-source ML models
- **FastAPI** - Modern Python web framework
---
*Last Updated: December 15, 2025*
|