SonuRamashish22028704/npmeduai
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
NPMAI-RAG-API-Pipeline
A powerful FastAPI-based multi-modal ingestion system that processes PDFs, scanned documents, images, videos, YouTube links, and text files โ then optionally performs semantic retrieval using FAISS + HuggingFace embeddings and refines answers using an LLM (Ollama via NPMAI).
๐ Features
- ๐ Extract text from searchable PDFs
- ๐จ๏ธ OCR for scanned PDFs
- ๐ผ๏ธ Image OCR (Tesseract + OpenCV preprocessing)
- ๐ฅ Local video speech-to-text (Whisper)
- ๐บ YouTube video transcription (yt-dlp + Whisper)
- ๐ Plain text processing
- ๐ง FAISS vector database creation & loading
- ๐ Semantic similarity search
- โป๏ธ Iterative refinement using LLM (Ollama)
- ๐ Automatic ingestion routing based on file type
Workflow:-
<img src="https://i.ibb.co/qYJd6Nhw/NPMAI-Rag-API-Pipeline.png" alt="Example Screenshot" width="700" style="display: block; margin: 0 auto; margin-left:20px">
๐ Architecture Overview
Client Request
โ
/ingestion Endpoint
โ
File Type Detection
โ
Text Extraction (PDF/OCR/Video/etc.)
โ
Optional Vector DB Retrieval (FAISS)
โ
Refinement via LLM
โ
Final Response๐ API Endpoints
Health Check
GET /Returns:
{ "ok": true }Main Ingestion Endpoint
POST /ingestionSupported Inputs:
fileโ Upload file (pdf, txt, mp4, jpg, png, etc.)queryโ Optional semantic queryDB_PATHโ Path to vector databaselinkโ YouTube linkoutput_pathโ Download location for videotemperatureโ LLM temperaturemodelโ Ollama model name
๐ Supported File Types
๐ Retrieval Pipeline
If query and DB_PATH are provided:
- Check if FAISS DB exists
- If yes โ Load and perform similarity search
- If no โ Create embeddings & save DB
- Retrieve top 4 chunks
- Send to LLM refine loop
๐ง Vector Store
- Embeddings:
all-MiniLM-L6-v2 - Vector DB: FAISS
- Chunk Size: 1000
- Overlap: 200
๐ Refinement Logic
For each retrieved chunk:
- Pass context to LLM
- Iteratively refine previous answer
- Return final refined response
๐ฆ Dependencies
Install required packages:
pip install fastapi uvicorn
pip install langchain langchain-community
pip install faiss-cpu
pip install whisper
pip install moviepy
pip install pytesseract
pip install pdf2image
pip install pymupdf
pip install yt-dlp
pip install opencv-python
pip install pillow
pip install numpyMake sure:
- Tesseract OCR is installed in system
- FFmpeg is installed
- Ollama is running locally
โถ๏ธ Running the Server
uvicorn main:app --reload๐งฉ Example Usage
Upload a PDF with Retrieval
POST /ingestion
Form Data:
file = document.pdf
query = "Summarize key points"
DB_PATH = vector_db
model = llama3
temperature = 0.7โ ๏ธ Notes
- GPU is disabled (
CUDA_VISIBLE_DEVICES="") - Whisper model loads once (thread-safe singleton)
- FAISS uses dangerous deserialization (use trusted DB paths only)
- Temporary audio saved as
temp.wav
๐ฎ Future Improvements
- Streaming responses
- Async video processing
- Chunk-level caching
- Background task queue
- Better refine logic
- Support for multiple vector stores
- Use through ##Docker
๐ Tech Stack
- FastAPI
- FAISS
- HuggingFace Embeddings
- Whisper
- OpenCV
- Tesseract OCR
- PyMuPDF
- yt-dlp
- ##npmai
๐ License
MIT License
๐ก Summary
This system acts as a universal AI ingestion pipeline capable of processing multi-modal data and performing intelligent semantic retrieval with LLM refinement.
It can serve as:
- AI document assistant
- Video summarizer
- Research helper
- OCR intelligence engine
- Knowledge base system
