CoolFace
Apppublic

SonuRamashish22028704/npmeduai

sourceHugging Facemitupdated 4mo agoView on Hugging Face
1likes
App README

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:

json
{ "ok": true }

Main Ingestion Endpoint

POST /ingestion

Supported Inputs:

  • โ€”file โ†’ Upload file (pdf, txt, mp4, jpg, png, etc.)
  • โ€”query โ†’ Optional semantic query
  • โ€”DB_PATH โ†’ Path to vector database
  • โ€”link โ†’ YouTube link
  • โ€”output_path โ†’ Download location for video
  • โ€”temperature โ†’ LLM temperature
  • โ€”model โ†’ Ollama model name

๐Ÿ“‚ Supported File Types

TypeProcessing Method
PDF (text-based)PyMuPDF
PDF (scanned)pdf2image + Tesseract
ImageOpenCV + Tesseract
TXTDirect read
MP4Whisper transcription
YouTubeyt-dlp + Whisper

๐Ÿ” Retrieval Pipeline

If query and DB_PATH are provided:

  1. 1.Check if FAISS DB exists
  2. 2.If yes โ†’ Load and perform similarity search
  3. 3.If no โ†’ Create embeddings & save DB
  4. 4.Retrieve top 4 chunks
  5. 5.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:

  1. 1.Pass context to LLM
  2. 2.Iteratively refine previous answer
  3. 3.Return final refined response

๐Ÿ“ฆ Dependencies

Install required packages:

bash
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 numpy

Make sure:

  • โ€”Tesseract OCR is installed in system
  • โ€”FFmpeg is installed
  • โ€”Ollama is running locally

โ–ถ๏ธ Running the Server

bash
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