Skip to content

Latest commit

ย 

History

11 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Nexdoc Backend

๐Ÿง  Offline AI-Powered Document Intelligence & Search Engine

Nexdoc is a high-performance offline document search system, its intelligent backend designed to process multiple document formats, extract and clean their content, generate semantic embeddings, store searchable document data in LanceDB, and provide fast, context-aware search.

The backend follows a modular layered architecture with a threaded processing pipeline, dynamic worker pool, file watching, semantic retrieval, hybrid filtering, ranking, and optional AI-powered responses.


Document Search & AI-Powered Results

This application demonstrates Nexdoc's complete offline document intelligence workflow, including multi-format document upload, real-time processing status, semantic search across indexed documents, and AI-powered contextual answers. It serves as a demonstration client for firms evaluating Nexdoc before deploying it as their offline document assistant.



ย ย ย ย 

ย ย ย ย 


Search & Retrieval

Every query runs through semantic vector search, hybrid filtering, and result ranking before returning results โ€” either as raw ranked matches in offline mode or as a synthesized AI answer with cited sources in AI mode.

Document Insights

Users can inspect file metadata, processing status, and topic extraction for each indexed document, giving full visibility into how Nexdoc understands and organizes their files.


๐Ÿ“‘ Table of Contents


โœจ Features

๐Ÿ“‚ Multi-Format Document Support

  • PDF
  • Images (OCR-based)
  • PPTX
  • TXT
  • XLS / XLSX

โšก High-Performance Processing

  • Multi-threaded document processing
  • Dynamic worker pool based on available CPU cores
  • Producer-consumer processing pipeline
  • Real-time file detection using Watchdog
  • Concurrent file processing
  • Processing status tracking

๐Ÿง  Document Intelligence

  • Text extraction
  • Text cleaning and normalization
  • Sentence-aware chunking
  • Topic extraction
  • File metadata extraction
  • Semantic embeddings using Sentence Transformers

๐Ÿ” Advanced Search

  • Semantic vector search
  • Hybrid filtering
  • Result ranking
  • Context-aware retrieval
  • Offline search mode
  • AI-powered search mode

๐Ÿ“ฆ Efficient Storage

  • LanceDB vector database
  • Vector embeddings stored with document metadata
  • SQLite-based application data storage
  • Persistent local storage
  • Metadata-based document retrieval

๐Ÿ“ด Offline-First Architecture

  • Local document processing
  • Local embedding generation
  • Local vector database
  • No cloud backend required for core document processing and search
  • Designed for privacy-focused desktop usage

๐Ÿค– Optional AI Mode

  • AI-powered query responses
  • Relevant document sources returned with AI answers
  • Gemini API integration for AI-assisted responses
  • Offline mode remains available without AI services

๐Ÿง  How It Works

User Upload
     โ†“
File Storage
     โ†“
File Watching
     โ†“
Queue
     โ†“
Dynamic Worker Pool
     โ†“
File Processing
     โ†“
Text + Metadata Extraction
     โ†“
Text Cleaning
     โ†“
Chunking
     โ†“
Embedding Generation
     โ†“
LanceDB Storage
     โ†“
Search
     โ†“
Hybrid Filtering
     โ†“
Ranking
     โ†“
Offline / AI Response

๐Ÿ—๏ธ System Design

Nexdoc uses a producer-consumer architecture for document processing. Uploaded files are placed into a processing queue and consumed by dynamically created workers. The worker pool determines its capacity from the available CPU resources, allowing the backend to adapt to different machines.

Each worker independently processes a document through extraction, cleaning, chunking, embedding generation, and LanceDB persistence โ€” while the main application remains responsive.

                         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                         โ”‚   User Uploads   โ”‚
                         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                  โ”‚
                                  โ–ผ
                         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                         โ”‚   File Storage   โ”‚
                         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                  โ”‚
                                  โ–ผ
                         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                         โ”‚  File Watcher    โ”‚
                         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                  โ”‚
                                  โ–ผ
                         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                         โ”‚ Processing Queue โ”‚
                         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                  โ”‚
                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                    โ”‚                           โ”‚
                    โ–ผ                           โ–ผ
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ”‚ Worker 1 โ”‚                โ”‚ Worker N โ”‚
              โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”˜                โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚                           โ”‚
                   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                                 โ–ผ
                        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                        โ”‚ File Processing  โ”‚
                        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                                 โ–ผ
                        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                        โ”‚ Text + Metadata  โ”‚
                        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                                 โ–ผ
                        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                        โ”‚ Text Cleaning    โ”‚
                        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                                 โ–ผ
                        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                        โ”‚    Chunking      โ”‚
                        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                                 โ–ผ
                        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                        โ”‚    Embedding     โ”‚
                        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                                 โ–ผ
                        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                        โ”‚     LanceDB      โ”‚
                        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“ Project Structure

Nexdoc_Backend/
โ”‚
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ services/
โ”‚   โ”‚   โ”œโ”€โ”€ uploadPPTX/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ upload_pptx_service.py
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ uploadTXT/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ upload_txt_service.py
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ __init__.py
โ”‚   โ”‚   โ””โ”€โ”€ uploadXLS/
โ”‚   โ”‚       โ”œโ”€โ”€ upload_xls_service.py
โ”‚   โ”‚       โ””โ”€โ”€ __init__.py
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ utils/
โ”‚   โ”‚   โ”œโ”€โ”€ file_metadata_formatter.py
โ”‚   โ”‚   โ”œโ”€โ”€ text_cleaner.py
โ”‚   โ”‚   โ”œโ”€โ”€ topic_extractor.py
โ”‚   โ”‚   โ””โ”€โ”€ __init__.py
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ main.py
โ”‚   โ”œโ”€โ”€ NexDocBackend.spec
โ”‚   โ””โ”€โ”€ __init__.py
โ”‚
โ”œโ”€โ”€ database/
โ”‚   โ””โ”€โ”€ nexdoc.db
โ”‚
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ NexDocBackend.spec
โ””โ”€โ”€ runtime_hook.py

Note: Runtime-generated directories and large local assets โ€” such as the virtual environment, model files, vector database data, build output, distribution output, OCR runtime files, and uploaded documents โ€” are intentionally omitted from the documented source tree.


๐Ÿ› ๏ธ Tech Stack

Category Technologies
Backend Python, FastAPI, Uvicorn, Pydantic
Document Processing PyMuPDF, pytesseract, python-pptx, pandas, openpyxl
AI / Machine Learning Sentence Transformers (all-MiniLM-L12-v2), PyTorch
Vector Search LanceDB
Local Storage SQLite, Local filesystem
Processing & Concurrency Watchdog, Threaded producer-consumer pipeline, Dynamic worker pool
AI Mode Gemini API
Packaging PyInstaller

โš™๏ธ Document Processing Pipeline

1. ๐Ÿ“ค Upload

The user uploads a supported document through the FastAPI upload endpoints. The upload service:

  • Validates the file
  • Stores the file locally
  • Prevents invalid uploads
  • Makes the file available for processing

2. ๐Ÿ‘€ File Watching

A Watchdog-based file watcher monitors the configured document storage location. When a new file is detected, it is added to the processing workflow so that files can also be processed automatically when they appear in the watched directory.

3. ๐Ÿ“‹ Queue

Detected files are placed into a processing queue. The queue separates file production from file processing, allowing uploaded files to wait safely until a worker becomes available.

4. โš™๏ธ Dynamic Worker Pool

Nexdoc creates a worker pool dynamically according to the available CPU resources of the machine. This allows the same backend to adapt its processing concurrency across different desktop systems instead of relying on a fixed number of workers.

Available CPU Resources
          โ†“
Worker Capacity Calculation
          โ†“
Dynamic Worker Creation
          โ†“
Concurrent File Processing

5. ๐Ÿ“„ File Processing

Each worker takes a file from the queue and sends it to the appropriate file processor based on its format:

Format Processor
PDF PDF Processor
Image Image Processor / OCR
PPTX PPTX Processor
TXT TXT Processor
XLS XLS Processor

The processor extracts the document's usable content and associated metadata.

6. ๐Ÿงน Text Cleaning

Extracted text is cleaned and normalized before being passed to the chunking stage. This removes unnecessary formatting/noise and produces more consistent text for downstream processing.

7. โœ‚๏ธ Chunking

Large extracted documents are divided into smaller, meaningful text chunks. Chunking makes the content suitable for embedding and improves the granularity of semantic retrieval. Document metadata is preserved alongside the corresponding chunks.

8. ๐Ÿง  Embedding Generation

Each chunk is converted into a numerical vector representation using the local Sentence Transformer model.

Text Chunk
    โ†“
Sentence Transformer
    โ†“
Embedding Vector

The embedding captures the semantic meaning of the text and allows similar concepts to be retrieved even when the exact keywords differ.

9. ๐Ÿ“ฆ LanceDB Storage

The generated embeddings are stored in LanceDB together with the corresponding document information and metadata.

Embedding Vector
       +
Document Content
       +
Document Metadata
       โ†“
     LanceDB

This allows the system to perform vector similarity search while retaining the information required to present the original document context to the user.


๐Ÿ” Search Pipeline

When a user submits a search query, Nexdoc follows a multi-stage retrieval process.

User Query
    โ†“
Query Processing
    โ†“
Candidate Retrieval
    โ†“
Hybrid Filtering
    โ†“
Result Ranking
    โ†“
Offline / AI Mode
    โ†“
Final Response

1. ๐Ÿ”Ž Query Processing

The user's query is processed and converted into a representation suitable for document retrieval.

2. ๐Ÿง  Candidate Retrieval

Relevant document chunks are retrieved from the locally stored vector data using semantic similarity.

3. ๐Ÿ”€ Hybrid Filtering

Retrieved candidates are passed through the hybrid filtering stage to improve the relevance of the candidate set.

4. ๐Ÿ† Result Ranking

The filtered candidates are ranked according to their relevance to the user's query, producing an ordered set of results for the final response stage.

5. ๐Ÿ“– Offline Mode

In offline mode, the ranked document results are processed locally to produce a readable response. No external AI service is required for this mode.

6. ๐Ÿค– AI Mode

In AI mode, the ranked document context is passed to the AI service to generate an answer. The response contains:

  • AI-generated answer
  • Relevant document sources
  • Source metadata

๐Ÿ—ƒ๏ธ Storage Architecture

Nexdoc uses different storage mechanisms for different responsibilities.

LanceDB

Used for:

  • Document embeddings
  • Vector similarity retrieval
  • Searchable document content
  • Associated metadata

SQLite

Used for application-level structured data such as:

  • Processing status
  • Statistics
  • Recent searches
  • Other lightweight application state

Local Filesystem

Used for:

  • Uploaded documents
  • Application assets
  • Local runtime data

๐Ÿงฑ Layered Architecture

Nexdoc follows a layered backend architecture that separates API handling, business logic, data access, and domain functionality.

Client
  โ†“
Controllers
  โ†“
Services
  โ†“
Features / Processing Logic
  โ†“
Repositories
  โ†“
LanceDB / SQLite / Filesystem
Layer Responsibility
Controllers Handle HTTP requests and responses
Services Contain application-level business logic and coordinate different components
Features Contain core domain functionality โ€” Search, Chunking, Embedding, File processing, Worker management
Repositories Abstract persistence operations from the rest of the application
Schemas Define request and response structures using Pydantic models

๐Ÿ“Š Processing Characteristics

Stage Purpose
Upload Store and validate incoming files
File Watching Detect newly available files
Queue Decouple file arrival from processing
Worker Pool Process multiple files concurrently
File Processing Extract document content
Cleaning Normalize extracted text
Chunking Divide content into searchable units
Embedding Convert chunks into vectors
LanceDB Persist vectors and document information
Search Retrieve relevant document chunks
Ranking Order results by relevance
Response Return offline or AI-assisted results

โš ๏ธ Edge Cases & Reliability

Nexdoc is designed to handle common document-processing failures, including:

  • Corrupted documents
  • Unsupported file formats
  • Empty document content
  • OCR failures
  • Failed document processing
  • Partial processing failures
  • Processing status tracking
  • Multiple files arriving concurrently

๐Ÿ” Privacy & Offline Architecture

The core document-processing pipeline runs locally on the user's machine.

Documents
   โ†“
Local Filesystem
   โ†“
Local Processing
   โ†“
Local Embeddings
   โ†“
Local LanceDB
   โ†“
Local Search

This allows sensitive documents to remain on the user's machine during normal offline processing and search operations.

AI mode is an optional internet-dependent capability because it uses the configured Gemini API.


๐Ÿš€ Setup

1. Clone the Repository

git clone <your-repo>
cd Nexdoc

2. Create Virtual Environment

python -m venv venv

3. Activate Virtual Environment

Windows

venv\Scripts\activate

4. Install Dependencies

pip install -r requirements.txt

5. Start the Backend

uvicorn app.main:app --reload

The backend will be available at:

http://127.0.0.1:8000

๐Ÿ“ก API Modules

Nexdoc exposes REST APIs for major application operations.

Module Endpoints / Capabilities
Upload PDF upload, Image upload, PPTX upload, TXT upload, XLS/XLSX upload
Search Document search, Offline search mode, AI search mode
Status File processing status, Statistics, Document and processing statistics
Recent Searches Recent search history
Download Processed document/file retrieval

๐Ÿ“ฆ Desktop Packaging

Nexdoc is designed to operate as an offline desktop application.

The Python backend can be packaged using PyInstaller, allowing the backend runtime and required Python dependencies to be distributed with the desktop application.

Nexdoc Desktop Application
        โ†“
Electron Frontend
        โ†“
Local FastAPI Backend
        โ†“
Local Processing Pipeline
        โ†“
Local Storage

This eliminates the requirement for a separately deployed backend server for the core application.


๐ŸŽฏ Current Status

  • โœ… Multi-format document processing
  • โœ… PDF processing
  • โœ… Image OCR processing
  • โœ… PPTX processing
  • โœ… TXT processing
  • โœ… XLS / XLSX processing
  • โœ… File watching
  • โœ… Queue-based processing
  • โœ… Dynamic worker pool
  • โœ… Multi-threaded processing
  • โœ… Text cleaning
  • โœ… Chunking
  • โœ… Local embedding generation
  • โœ… LanceDB vector storage
  • โœ… Semantic search
  • โœ… Hybrid filtering
  • โœ… Result ranking
  • โœ… Offline search mode
  • โœ… AI search mode
  • โœ… Processing status tracking
  • โœ… SQLite application storage
  • โœ… FastAPI REST API
  • โœ… PyInstaller packaging support
  • โœ… Electron desktop integration

๐Ÿ”ฎ Future Improvements

Potential areas for further optimization include:

  • ONNX-based embedding inference
  • Faster embedding model startup
  • Advanced ranking strategies
  • Improved hybrid retrieval
  • Search result caching
  • More advanced chunking strategies
  • Additional document formats
  • Further worker-pool optimization
  • Improved application observability
  • Advanced AI response optimization

๐Ÿง  Design Principles

  • Offline-first
  • Modular architecture
  • Layered architecture
  • Separation of concerns
  • Concurrent processing
  • Resource-aware worker management
  • Fault tolerance
  • Local-first data storage
  • Scalable processing pipeline
  • Privacy-focused document processing

๐Ÿ‘ค Author

Ravi Sharma Full Stack Developer / AI Engineer

About

Nexdoc is a high-performance offline document based search system, its intelligent backend designed to process multiple document formats, extract and clean their content, generate semantic embeddings, store searchable document data in LanceDB, and provide fast, context-aware search.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages