Instructions to use Navaneeth-14/rag-hackathon-app with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- llama.cpp
How to use Navaneeth-14/rag-hackathon-app with llama.cpp:
Install (macOS, Linux)
curl -LsSf https://llama.app/install.sh | sh # Start a local OpenAI-compatible server with a web UI: llama serve -hf Navaneeth-14/rag-hackathon-app:Q4_K_M # Run inference directly in the terminal: llama cli -hf Navaneeth-14/rag-hackathon-app:Q4_K_M
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama serve -hf Navaneeth-14/rag-hackathon-app:Q4_K_M # Run inference directly in the terminal: llama cli -hf Navaneeth-14/rag-hackathon-app:Q4_K_M
Use pre-built binary
# Download pre-built binary from: # https://github.com/ggerganov/llama.cpp/releases # Start a local OpenAI-compatible server with a web UI: ./llama-server -hf Navaneeth-14/rag-hackathon-app:Q4_K_M # Run inference directly in the terminal: ./llama-cli -hf Navaneeth-14/rag-hackathon-app:Q4_K_M
Build from source code
git clone https://github.com/ggerganov/llama.cpp.git cd llama.cpp cmake -B build cmake --build build -j --target llama-server llama-cli # Start a local OpenAI-compatible server with a web UI: ./build/bin/llama-server -hf Navaneeth-14/rag-hackathon-app:Q4_K_M # Run inference directly in the terminal: ./build/bin/llama-cli -hf Navaneeth-14/rag-hackathon-app:Q4_K_M
Use Docker
docker model run hf.co/Navaneeth-14/rag-hackathon-app:Q4_K_M
- LM Studio
- Jan
- Ollama
How to use Navaneeth-14/rag-hackathon-app with Ollama:
ollama run hf.co/Navaneeth-14/rag-hackathon-app:Q4_K_M
- Unsloth Studio
How to use Navaneeth-14/rag-hackathon-app with Unsloth Studio:
Install Unsloth Studio (macOS, Linux, WSL)
curl -fsSL https://unsloth.ai/install.sh | sh # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for Navaneeth-14/rag-hackathon-app to start chatting
Install Unsloth Studio (Windows)
irm https://unsloth.ai/install.ps1 | iex # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for Navaneeth-14/rag-hackathon-app to start chatting
Using HuggingFace Spaces for Unsloth
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for Navaneeth-14/rag-hackathon-app to start chatting
- Docker Model Runner
How to use Navaneeth-14/rag-hackathon-app with Docker Model Runner:
docker model run hf.co/Navaneeth-14/rag-hackathon-app:Q4_K_M
- Lemonade
How to use Navaneeth-14/rag-hackathon-app with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull Navaneeth-14/rag-hackathon-app:Q4_K_M
Run and chat with the model
lemonade run user.rag-hackathon-app-Q4_K_M
List all available models
lemonade list
- Atomic Chat
File size: 24,959 Bytes
09281fe | 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 | """
Advanced Document Processor for Multi-Format Document Ingestion
Handles PDF, TXT, email, and other document types with OCR and table extraction
"""
import os
import re
import hashlib
import logging
from datetime import datetime
from typing import List, Dict, Any, Optional, Tuple
from dataclasses import dataclass
from pathlib import Path
import email
import json
# Document processing libraries
import fitz # PyMuPDF
try:
import pytesseract
from PIL import Image
import cv2
import numpy as np
OCR_AVAILABLE = True
except ImportError:
OCR_AVAILABLE = False
print("⚠️ pytesseract not available. OCR functionality will be disabled.")
from pdf2image import convert_from_path
from docx import Document
from bs4 import BeautifulSoup
import requests
import pandas as pd
# Table extraction libraries
try:
import tabula
import camelot
TABLE_EXTRACTION_AVAILABLE = True
except ImportError:
TABLE_EXTRACTION_AVAILABLE = False
print("⚠️ Table extraction libraries not available. Table extraction will be disabled.")
# LangChain for text splitting
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.schema import Document
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@dataclass
class DocumentChunk:
"""Represents a processed document chunk with metadata"""
chunk_id: str
content: str
source_file: str
file_type: str
page_number: Optional[int] = None
chunk_index: Optional[int] = None
section_type: Optional[str] = None # text, table, header, footer, etc.
confidence_score: Optional[float] = None
extracted_entities: Optional[Dict[str, Any]] = None
table_data: Optional[Dict[str, Any]] = None
embedding: Optional[List[float]] = None
class AdvancedDocumentProcessor:
"""Advanced document processor with multi-format support and table extraction"""
def __init__(self, ocr_language='eng', chunk_size=1000, chunk_overlap=200):
self.ocr_language = ocr_language
self.chunk_size = chunk_size
self.chunk_overlap = chunk_overlap
# Initialize text splitter
self.text_splitter = RecursiveCharacterTextSplitter(
chunk_size=chunk_size,
chunk_overlap=chunk_overlap,
separators=["\n\n", "\n", ". ", " ", ""]
)
# Supported file types
self.supported_extensions = {
'.pdf': self._process_pdf,
'.txt': self._process_txt,
'.docx': self._process_docx,
'.html': self._process_html,
'.htm': self._process_html,
'.eml': self._process_email,
'.msg': self._process_email,
'.csv': self._process_csv,
'.json': self._process_json
}
def process_document(self, file_path: str, use_ocr: bool = False) -> List[DocumentChunk]:
"""Main entry point for document processing"""
try:
file_path = Path(file_path)
if not file_path.exists():
raise FileNotFoundError(f"File not found: {file_path}")
file_extension = file_path.suffix.lower()
if file_extension not in self.supported_extensions:
raise ValueError(f"Unsupported file type: {file_extension}")
logger.info(f"Processing document: {file_path}")
# Process based on file type
processor_func = self.supported_extensions[file_extension]
raw_content = processor_func(str(file_path), use_ocr)
# Extract tables and structured content
structured_content = self._extract_structured_content(raw_content, file_path)
# Chunk the content
chunks = self._chunk_content(structured_content, str(file_path), file_extension)
logger.info(f"Successfully processed {len(chunks)} chunks from {file_path}")
return chunks
except Exception as e:
logger.error(f"Error processing document {file_path}: {e}")
logger.error("This might be due to missing dependencies or OCR issues.")
logger.error("Please check:")
logger.error("1. File exists and is readable")
logger.error("2. Tesseract is installed (run: python fix_tesseract.py)")
logger.error("3. All dependencies are installed (run: python install_nltk_version.py)")
logger.error("4. Run debug_document_processing.py to diagnose issues")
import traceback
logger.error(f"Full error traceback: {traceback.format_exc()}")
raise
def _process_pdf(self, pdf_path: str, use_ocr: bool = False) -> Dict[str, Any]:
"""Process PDF with text extraction, OCR, and table detection"""
try:
doc = fitz.open(pdf_path)
content = {
'text': "",
'tables': [],
'images': [],
'metadata': {}
}
for page_num in range(len(doc)):
page = doc.load_page(page_num)
# Extract text normally first
page_text = page.get_text()
# If no text or very little text, use OCR
if use_ocr or len(page_text.strip()) < 50:
logger.info(f"Using OCR for page {page_num + 1}")
ocr_text = self._extract_text_with_ocr(page, page_num)
if ocr_text.strip(): # Only use OCR text if it's not empty
page_text = ocr_text
else:
logger.warning(f"OCR returned empty text for page {page_num + 1}, using normal extraction")
content['text'] += f"\n\n--- Page {page_num + 1} ---\n{page_text}"
# Extract tables from this page
page_tables = self._extract_tables_from_page(page, page_num)
content['tables'].extend(page_tables)
# Extract images (for future OCR if needed)
page_images = self._extract_images_from_page(page, page_num)
content['images'].extend(page_images)
doc.close()
return content
except Exception as e:
logger.error(f"Error processing PDF {pdf_path}: {e}")
raise
def _extract_text_with_ocr(self, page, page_num: int) -> str:
"""Extract text using OCR with pytesseract"""
if not OCR_AVAILABLE:
logger.error("pytesseract not available. Please install it with: pip install pytesseract")
return ""
try:
# Get page as image
pix = page.get_pixmap()
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
# Simple OCR without complex preprocessing
text = pytesseract.image_to_string(
img,
lang=self.ocr_language,
config='--psm 6' # Assume uniform block of text
)
return text
except Exception as e:
logger.error(f"OCR error on page {page_num}: {e}")
logger.error("This might be due to Tesseract not being installed or configured properly.")
logger.error("Please run: python fix_tesseract.py")
return ""
def _extract_tables_from_page(self, page, page_num: int) -> List[Dict[str, Any]]:
"""Extract tables from PDF page using basic methods"""
tables = []
try:
if TABLE_EXTRACTION_AVAILABLE:
# Method 1: Use camelot for table extraction
try:
page_tables = camelot.read_pdf(
page.parent,
pages=str(page_num + 1),
flavor='lattice'
)
for table in page_tables:
if table.df.shape[0] > 1: # Only include tables with data
tables.append({
'page': page_num + 1,
'data': table.df.to_dict('records'),
'accuracy': table.accuracy,
'whitespace': table.whitespace,
'method': 'camelot'
})
except Exception as e:
logger.debug(f"Camelot table extraction failed: {e}")
# Method 2: Use tabula for simpler tables
try:
tabula_tables = tabula.read_pdf(
page.parent,
pages=page_num + 1,
multiple_tables=True
)
for i, table in enumerate(tabula_tables):
if not table.empty:
tables.append({
'page': page_num + 1,
'data': table.to_dict('records'),
'method': 'tabula'
})
except Exception as e:
logger.debug(f"Tabula table extraction failed: {e}")
# Method 3: Basic table extraction using text patterns (fallback)
if not tables:
tables = self._extract_tables_basic(page, page_num)
except Exception as e:
logger.error(f"Table extraction error on page {page_num}: {e}")
return tables
def _extract_images_from_page(self, page, page_num: int) -> List[Dict[str, Any]]:
"""Extract images from PDF page"""
images = []
try:
image_list = page.get_images()
for img_index, img in enumerate(image_list):
xref = img[0]
pix = fitz.Pixmap(page.parent, xref)
if pix.n - pix.alpha < 4: # GRAY or RGB
images.append({
'page': page_num + 1,
'index': img_index,
'width': pix.width,
'height': pix.height,
'format': pix.colorspace.name
})
pix = None # Free memory
except Exception as e:
logger.error(f"Image extraction error on page {page_num}: {e}")
return images
def _extract_tables_basic(self, page, page_num: int) -> List[Dict[str, Any]]:
"""Basic table extraction using text patterns when advanced libraries are not available"""
tables = []
try:
# Get page text
page_text = page.get_text()
# Simple table detection using common patterns
lines = page_text.split('\n')
table_lines = []
in_table = False
for line in lines:
# Check for table-like patterns (multiple columns separated by spaces/tabs)
if len(line.strip()) > 0 and ('\t' in line or line.count(' ') >= 2):
if not in_table:
in_table = True
table_lines = []
table_lines.append(line)
elif in_table:
# End of table detected
if table_lines:
# Convert to table format
table_data = []
for table_line in table_lines:
# Split by multiple spaces or tabs
columns = [col.strip() for col in table_line.split('\t') if col.strip()]
if not columns:
columns = [col.strip() for col in line.split(' ') if col.strip()]
if columns:
table_data.append(columns)
if table_data and len(table_data) > 1:
tables.append({
'page': page_num + 1,
'data': table_data,
'method': 'basic_pattern'
})
in_table = False
table_lines = []
# Handle table at end of page
if in_table and table_lines:
table_data = []
for table_line in table_lines:
columns = [col.strip() for col in table_line.split('\t') if col.strip()]
if not columns:
columns = [col.strip() for col in line.split(' ') if col.strip()]
if columns:
table_data.append(columns)
if table_data and len(table_data) > 1:
tables.append({
'page': page_num + 1,
'data': table_data,
'method': 'basic_pattern'
})
except Exception as e:
logger.error(f"Basic table extraction error on page {page_num}: {e}")
return tables
def _process_txt(self, txt_path: str, use_ocr: bool = False) -> Dict[str, Any]:
"""Process plain text files"""
try:
with open(txt_path, 'r', encoding='utf-8') as f:
text = f.read()
return {
'text': text,
'tables': [],
'images': [],
'metadata': {}
}
except Exception as e:
logger.error(f"Error processing TXT {txt_path}: {e}")
raise
def _process_docx(self, docx_path: str, use_ocr: bool = False) -> Dict[str, Any]:
"""Process Word documents"""
try:
doc = Document(docx_path)
text = ""
tables = []
# Extract text from paragraphs - handle different versions of python-docx
try:
# Try the standard way first
for paragraph in doc.paragraphs:
text += paragraph.text + "\n"
except AttributeError:
# Fallback for different versions
try:
# Try alternative attribute names
if hasattr(doc, 'paragraphs'):
for paragraph in doc.paragraphs:
text += paragraph.text + "\n"
elif hasattr(doc, 'content'):
text = doc.content
else:
# Last resort - try to extract text from the document structure
text = str(doc)
except Exception as e:
logger.warning(f"Could not extract text from DOCX paragraphs: {e}")
text = "DOCX content could not be extracted"
# Extract tables from Word document
try:
# Check if tables attribute exists
if hasattr(doc, 'tables'):
for table in doc.tables:
table_data = []
for row in table.rows:
row_data = [cell.text for cell in row.cells]
table_data.append(row_data)
if table_data:
tables.append({
'data': table_data,
'method': 'docx'
})
else:
logger.warning("DOCX document has no 'tables' attribute - tables will not be extracted")
except Exception as e:
logger.warning(f"Could not extract tables from DOCX: {e}")
return {
'text': text,
'tables': tables,
'images': [],
'metadata': {}
}
except Exception as e:
logger.error(f"Error processing DOCX {docx_path}: {e}")
raise
def _process_html(self, html_path: str, use_ocr: bool = False) -> Dict[str, Any]:
"""Process HTML files"""
try:
with open(html_path, 'r', encoding='utf-8') as f:
html_content = f.read()
soup = BeautifulSoup(html_content, 'html.parser')
# Extract text
text = soup.get_text(separator='\n', strip=True)
# Extract tables
tables = []
for table in soup.find_all('table'):
table_data = []
for row in table.find_all('tr'):
row_data = [cell.get_text(strip=True) for cell in row.find_all(['td', 'th'])]
if row_data:
table_data.append(row_data)
if table_data:
tables.append({
'data': table_data,
'method': 'html'
})
return {
'text': text,
'tables': tables,
'images': [],
'metadata': {}
}
except Exception as e:
logger.error(f"Error processing HTML {html_path}: {e}")
raise
def _process_email(self, email_path: str, use_ocr: bool = False) -> Dict[str, Any]:
"""Process email files (.eml, .msg)"""
try:
with open(email_path, 'r', encoding='utf-8') as f:
email_content = f.read()
# Parse email
msg = email.message_from_string(email_content)
# Extract email components
subject = msg.get('Subject', '')
sender = msg.get('From', '')
recipient = msg.get('To', '')
date = msg.get('Date', '')
# Extract body
body = ""
if msg.is_multipart():
for part in msg.walk():
if part.get_content_type() == "text/plain":
body = part.get_payload(decode=True).decode()
break
else:
body = msg.get_payload(decode=True).decode()
# Combine all text
text = f"Subject: {subject}\nFrom: {sender}\nTo: {recipient}\nDate: {date}\n\n{body}"
return {
'text': text,
'tables': [],
'images': [],
'metadata': {
'subject': subject,
'sender': sender,
'recipient': recipient,
'date': date
}
}
except Exception as e:
logger.error(f"Error processing email {email_path}: {e}")
raise
def _process_csv(self, csv_path: str, use_ocr: bool = False) -> Dict[str, Any]:
"""Process CSV files"""
try:
df = pd.read_csv(csv_path)
# Convert to text representation
text = df.to_string(index=False)
# Store as table
tables = [{
'data': df.to_dict('records'),
'method': 'csv'
}]
return {
'text': text,
'tables': tables,
'images': [],
'metadata': {
'columns': list(df.columns),
'rows': len(df)
}
}
except Exception as e:
logger.error(f"Error processing CSV {csv_path}: {e}")
raise
def _process_json(self, json_path: str, use_ocr: bool = False) -> Dict[str, Any]:
"""Process JSON files"""
try:
with open(json_path, 'r', encoding='utf-8') as f:
data = json.load(f)
# Convert to text representation
text = json.dumps(data, indent=2)
return {
'text': text,
'tables': [],
'images': [],
'metadata': {
'type': 'json',
'keys': list(data.keys()) if isinstance(data, dict) else []
}
}
except Exception as e:
logger.error(f"Error processing JSON {json_path}: {e}")
raise
def _extract_structured_content(self, content: Dict[str, Any], file_path: Path) -> List[Dict[str, Any]]:
"""Extract and structure content from processed document"""
structured_content = []
# Add main text content
if content['text'].strip():
structured_content.append({
'type': 'text',
'content': content['text'],
'section_type': 'main_text'
})
# Add tables
for i, table in enumerate(content['tables']):
table_text = self._table_to_text(table['data'])
structured_content.append({
'type': 'table',
'content': table_text,
'section_type': 'table',
'table_data': table,
'table_index': i
})
# Add metadata as text
if content.get('metadata'):
metadata_text = json.dumps(content['metadata'], indent=2)
structured_content.append({
'type': 'text',
'content': f"Document Metadata:\n{metadata_text}",
'section_type': 'metadata'
})
return structured_content
def _table_to_text(self, table_data: List[List[str]]) -> str:
"""Convert table data to readable text"""
if not table_data:
return ""
text_lines = []
for row in table_data:
text_lines.append(" | ".join(str(cell) for cell in row))
return "\n".join(text_lines)
def _chunk_content(self, structured_content: List[Dict[str, Any]], source_file: str, file_type: str) -> List[DocumentChunk]:
"""Chunk the structured content into DocumentChunk objects"""
chunks = []
chunk_index = 0
for section in structured_content:
try:
# Use LangChain's text splitter for better semantic chunking
docs = [Document(page_content=section['content'], metadata={"source": source_file})]
split_docs = self.text_splitter.split_documents(docs)
for i, doc in enumerate(split_docs):
chunk_id = f"chunk_{chunk_index+1}_{hashlib.md5(doc.page_content.encode()).hexdigest()[:8]}"
chunk = DocumentChunk(
chunk_id=chunk_id,
content=doc.page_content.strip(),
source_file=source_file,
file_type=file_type,
chunk_index=chunk_index,
section_type=section.get('section_type', 'text'),
table_data=section.get('table_data'),
confidence_score=section.get('confidence_score', 1.0)
)
chunks.append(chunk)
chunk_index += 1
except Exception as e:
logger.error(f"Error chunking section: {e}")
continue
return chunks
# Example usage
if __name__ == "__main__":
processor = AdvancedDocumentProcessor()
# Test with a PDF file
test_file = "sample.pdf"
if os.path.exists(test_file):
chunks = processor.process_document(test_file, use_ocr=False)
print(f"Processed {len(chunks)} chunks from {test_file}")
for i, chunk in enumerate(chunks[:3]):
print(f"\nChunk {i+1}:")
print(f"ID: {chunk.chunk_id}")
print(f"Type: {chunk.section_type}")
print(f"Content preview: {chunk.content[:100]}...")
else:
print(f"Test file {test_file} not found") |