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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from pdf_parser import PDFParser
|
| 5 |
+
from embedder import ChromaDBManager
|
| 6 |
+
from rag_pipeline import RAGPipeline
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# ============================================================================
|
| 11 |
+
# PAGE CONFIGURATION
|
| 12 |
+
# ============================================================================
|
| 13 |
+
|
| 14 |
+
st.set_page_config(
|
| 15 |
+
page_title="Multimodal PDF RAG System",
|
| 16 |
+
page_icon="📄",
|
| 17 |
+
layout="wide",
|
| 18 |
+
initial_sidebar_state="expanded"
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# ============================================================================
|
| 22 |
+
# CUSTOM STYLING
|
| 23 |
+
# ============================================================================
|
| 24 |
+
|
| 25 |
+
st.markdown("""
|
| 26 |
+
<style>
|
| 27 |
+
.main {
|
| 28 |
+
padding: 2rem;
|
| 29 |
+
}
|
| 30 |
+
.error-box {
|
| 31 |
+
background-color: #ffcccc;
|
| 32 |
+
border: 1px solid #ff0000;
|
| 33 |
+
border-radius: 4px;
|
| 34 |
+
padding: 10px;
|
| 35 |
+
margin: 10px 0;
|
| 36 |
+
}
|
| 37 |
+
.warning-box {
|
| 38 |
+
background-color: #ffffcc;
|
| 39 |
+
border: 1px solid #ffcc00;
|
| 40 |
+
border-radius: 4px;
|
| 41 |
+
padding: 10px;
|
| 42 |
+
margin: 10px 0;
|
| 43 |
+
}
|
| 44 |
+
</style>
|
| 45 |
+
""", unsafe_allow_html=True)
|
| 46 |
+
|
| 47 |
+
# ============================================================================
|
| 48 |
+
# SESSION STATE INITIALIZATION
|
| 49 |
+
# ============================================================================
|
| 50 |
+
|
| 51 |
+
@st.cache_resource
|
| 52 |
+
def initialize_system():
|
| 53 |
+
"""Initialize RAG system components once."""
|
| 54 |
+
try:
|
| 55 |
+
parser = PDFParser(extraction_dir="./pdf_extractions")
|
| 56 |
+
chroma = ChromaDBManager(db_dir="./chroma_db")
|
| 57 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 58 |
+
rag = RAGPipeline(chroma, device=device)
|
| 59 |
+
return parser, chroma, rag, device
|
| 60 |
+
except Exception as e:
|
| 61 |
+
st.error(f"Error initializing system: {e}")
|
| 62 |
+
return None, None, None, None
|
| 63 |
+
|
| 64 |
+
# Initialize
|
| 65 |
+
pdf_parser, chroma_manager, rag_pipeline, device = initialize_system()
|
| 66 |
+
|
| 67 |
+
if pdf_parser is None:
|
| 68 |
+
st.error("Failed to initialize RAG system. Please check your installation.")
|
| 69 |
+
st.stop()
|
| 70 |
+
|
| 71 |
+
# ============================================================================
|
| 72 |
+
# MAIN UI
|
| 73 |
+
# ============================================================================
|
| 74 |
+
|
| 75 |
+
st.title("📄 Multimodal PDF RAG System (Improved)")
|
| 76 |
+
st.markdown("**Local AI-powered document analysis with Qwen2.5-VL and ChromaDB**")
|
| 77 |
+
st.markdown("*Fixes: Better error handling, token management, robust processing*")
|
| 78 |
+
|
| 79 |
+
# Sidebar
|
| 80 |
+
with st.sidebar:
|
| 81 |
+
st.header("⚙️ Configuration")
|
| 82 |
+
|
| 83 |
+
# PDF directory
|
| 84 |
+
pdf_dir = st.text_input(
|
| 85 |
+
"PDF Directory Path",
|
| 86 |
+
value="./pdf_documents",
|
| 87 |
+
help="Directory containing PDF files to process"
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# Create directory if it doesn't exist
|
| 91 |
+
os.makedirs(pdf_dir, exist_ok=True)
|
| 92 |
+
|
| 93 |
+
st.divider()
|
| 94 |
+
|
| 95 |
+
# Load/Refresh documents
|
| 96 |
+
col1, col2 = st.columns(2)
|
| 97 |
+
with col1:
|
| 98 |
+
if st.button("📁 Load PDFs", use_container_width=True):
|
| 99 |
+
with st.spinner("Processing PDFs..."):
|
| 100 |
+
try:
|
| 101 |
+
documents = pdf_parser.process_pdf_directory(pdf_dir)
|
| 102 |
+
|
| 103 |
+
if documents:
|
| 104 |
+
chroma_manager.add_documents(documents)
|
| 105 |
+
st.success(f"✅ Loaded {len(documents)} documents!")
|
| 106 |
+
else:
|
| 107 |
+
st.warning("⚠️ No PDFs found in directory")
|
| 108 |
+
except Exception as e:
|
| 109 |
+
st.error(f"❌ Error loading PDFs: {e}")
|
| 110 |
+
|
| 111 |
+
with col2:
|
| 112 |
+
if st.button("🔄 Refresh", use_container_width=True):
|
| 113 |
+
st.rerun()
|
| 114 |
+
|
| 115 |
+
st.divider()
|
| 116 |
+
|
| 117 |
+
# Statistics
|
| 118 |
+
st.subheader("📊 Statistics")
|
| 119 |
+
try:
|
| 120 |
+
collection_info = chroma_manager.get_collection_info()
|
| 121 |
+
st.metric("Documents in DB", collection_info['document_count'])
|
| 122 |
+
except Exception as e:
|
| 123 |
+
st.warning(f"Could not load statistics: {e}")
|
| 124 |
+
|
| 125 |
+
st.divider()
|
| 126 |
+
|
| 127 |
+
# Device info
|
| 128 |
+
device_name = "GPU (CUDA)" if torch.cuda.is_available() else "CPU"
|
| 129 |
+
st.info(f"Running on: {device_name}")
|
| 130 |
+
|
| 131 |
+
# Main content with tabs
|
| 132 |
+
tab1, tab2, tab3, tab4 = st.tabs(["🔍 Ask Question", "📝 Document Summary", "ℹ️ About", "🛠️ Database"])
|
| 133 |
+
|
| 134 |
+
# ============================================================================
|
| 135 |
+
# TAB 1: ASK QUESTIONS
|
| 136 |
+
# ============================================================================
|
| 137 |
+
|
| 138 |
+
with tab1:
|
| 139 |
+
st.header("🔍 Ask Questions About Your Documents")
|
| 140 |
+
|
| 141 |
+
col1, col2 = st.columns([3, 1])
|
| 142 |
+
|
| 143 |
+
with col1:
|
| 144 |
+
query = st.text_input(
|
| 145 |
+
"Enter your question (in Russian or English):",
|
| 146 |
+
placeholder="Например: Какие ключевые моменты описаны в документе?",
|
| 147 |
+
help="Ask any question about your uploaded documents"
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
with col2:
|
| 151 |
+
n_docs = st.number_input("Retrieved docs:", value=5, min_value=1, max_value=10)
|
| 152 |
+
|
| 153 |
+
if st.button("🚀 Get Answer", use_container_width=True, type="primary"):
|
| 154 |
+
try:
|
| 155 |
+
collection_info = chroma_manager.get_collection_info()
|
| 156 |
+
|
| 157 |
+
if collection_info['document_count'] == 0:
|
| 158 |
+
st.warning("⚠️ No documents loaded. Please load PDFs from the sidebar first.")
|
| 159 |
+
elif not query:
|
| 160 |
+
st.warning("⚠️ Please enter a question.")
|
| 161 |
+
else:
|
| 162 |
+
with st.spinner("🤖 Generating answer... (this may take 10-60 seconds)"):
|
| 163 |
+
result = rag_pipeline.answer_question(
|
| 164 |
+
query=query,
|
| 165 |
+
n_retrieved=n_docs,
|
| 166 |
+
max_new_tokens=512
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
# Check for errors
|
| 170 |
+
if "error" in result and result["error"]:
|
| 171 |
+
st.error(f"⚠️ {result['error']}")
|
| 172 |
+
|
| 173 |
+
# Display answer
|
| 174 |
+
st.success("✅ Answer Generated")
|
| 175 |
+
st.markdown("### Answer")
|
| 176 |
+
st.write(result['answer'])
|
| 177 |
+
|
| 178 |
+
# Display retrieved documents
|
| 179 |
+
with st.expander("📚 Retrieved Documents", expanded=False):
|
| 180 |
+
st.markdown(f"#### {result['doc_count']} Relevant Document Chunks:")
|
| 181 |
+
for idx, doc in enumerate(result['retrieved_docs'], 1):
|
| 182 |
+
with st.container():
|
| 183 |
+
col_rel, col_meta = st.columns([3, 1])
|
| 184 |
+
with col_rel:
|
| 185 |
+
st.markdown(f"**Document {idx}**")
|
| 186 |
+
with col_meta:
|
| 187 |
+
st.caption(f"Relevance: {doc['relevance_score']:.2%}")
|
| 188 |
+
|
| 189 |
+
# Truncate for display
|
| 190 |
+
preview = doc['document'][:300] + "..." if len(doc['document']) > 300 else doc['document']
|
| 191 |
+
st.write(preview)
|
| 192 |
+
if doc['metadata']:
|
| 193 |
+
st.caption(f"Source: {doc['metadata'].get('filename', 'Unknown')}")
|
| 194 |
+
|
| 195 |
+
except Exception as e:
|
| 196 |
+
st.error(f"❌ Error processing question: {e}")
|
| 197 |
+
|
| 198 |
+
# ============================================================================
|
| 199 |
+
# TAB 2: DOCUMENT SUMMARY
|
| 200 |
+
# ============================================================================
|
| 201 |
+
|
| 202 |
+
with tab2:
|
| 203 |
+
st.header("📝 Document Summary")
|
| 204 |
+
st.markdown("Generate a summary of all indexed documents")
|
| 205 |
+
|
| 206 |
+
if st.button("📊 Generate Summary of All Documents", use_container_width=True, type="primary"):
|
| 207 |
+
try:
|
| 208 |
+
collection_info = chroma_manager.get_collection_info()
|
| 209 |
+
|
| 210 |
+
if collection_info['document_count'] == 0:
|
| 211 |
+
st.warning("⚠️ No documents loaded. Please load PDFs first.")
|
| 212 |
+
else:
|
| 213 |
+
with st.spinner("🤖 Generating summary... (this may take 20-60 seconds)"):
|
| 214 |
+
summary = rag_pipeline.summarize_all_documents()
|
| 215 |
+
st.markdown("### Summary")
|
| 216 |
+
st.write(summary)
|
| 217 |
+
except Exception as e:
|
| 218 |
+
st.error(f"❌ Error generating summary: {e}")
|
| 219 |
+
|
| 220 |
+
# ============================================================================
|
| 221 |
+
# TAB 3: ABOUT
|
| 222 |
+
# ============================================================================
|
| 223 |
+
|
| 224 |
+
with tab3:
|
| 225 |
+
st.header("ℹ️ About This System")
|
| 226 |
+
|
| 227 |
+
st.markdown("""
|
| 228 |
+
### Overview
|
| 229 |
+
This is an **improved Local Multimodal RAG System** with enhanced error handling and token management.
|
| 230 |
+
|
| 231 |
+
### Key Improvements (Fixed Version)
|
| 232 |
+
✅ **Token Management**: Automatic context truncation to prevent model errors
|
| 233 |
+
✅ **Error Handling**: Comprehensive try-catch blocks throughout
|
| 234 |
+
✅ **Image Extraction**: Fixed PyMuPDF xref handling
|
| 235 |
+
✅ **Better Limits**: Resource limits on text, tables, and images
|
| 236 |
+
✅ **Performance**: Optimized for large PDFs (400+ pages)
|
| 237 |
+
✅ **Robustness**: Graceful degradation on errors
|
| 238 |
+
|
| 239 |
+
### Core Features
|
| 240 |
+
- **📄 PDF Processing**: Text, tables, and images extraction
|
| 241 |
+
- **🔍 Vector Search**: ChromaDB with CLIP embeddings
|
| 242 |
+
- **🤖 AI Generation**: Qwen2.5-VL-3B model
|
| 243 |
+
- **🌐 Russian Support**: Full support for Russian language
|
| 244 |
+
- **💾 Persistent Storage**: Local ChromaDB database
|
| 245 |
+
- **⚡ Lightweight**: Runs on consumer hardware
|
| 246 |
+
|
| 247 |
+
### Technology Stack
|
| 248 |
+
- **LLM Model**: Qwen2.5-VL-3B-Instruct
|
| 249 |
+
- **Embeddings**: CLIP (clip-vit-base-patch32)
|
| 250 |
+
- **Vector DB**: ChromaDB with persistent storage
|
| 251 |
+
- **UI**: Streamlit
|
| 252 |
+
- **PDF Tools**: pdfplumber + PyMuPDF
|
| 253 |
+
|
| 254 |
+
### System Requirements
|
| 255 |
+
- Python 3.9+
|
| 256 |
+
- RAM: 8GB minimum (12GB+ recommended)
|
| 257 |
+
- Storage: 15GB for models
|
| 258 |
+
- GPU optional (CUDA for faster inference)
|
| 259 |
+
|
| 260 |
+
### Performance
|
| 261 |
+
- Model Load: ~30 seconds
|
| 262 |
+
- Query Response (CPU): 20-60 seconds
|
| 263 |
+
- Query Response (GPU): 5-15 seconds
|
| 264 |
+
- PDF Processing: 1-2 seconds per page
|
| 265 |
+
|
| 266 |
+
### What's Fixed
|
| 267 |
+
- ✅ Token limit errors (uses chunking + truncation)
|
| 268 |
+
- ✅ Image extraction errors (proper xref handling)
|
| 269 |
+
- ✅ Memory issues (resource limits on text/tables/images)
|
| 270 |
+
- ✅ PyTorch GPU loading (fbgemm.dll issues)
|
| 271 |
+
- ✅ Error reporting (detailed error messages)
|
| 272 |
+
""")
|
| 273 |
+
|
| 274 |
+
# ============================================================================
|
| 275 |
+
# TAB 4: DATABASE MANAGEMENT
|
| 276 |
+
# ============================================================================
|
| 277 |
+
|
| 278 |
+
with tab4:
|
| 279 |
+
st.header("🛠️ Database Management")
|
| 280 |
+
|
| 281 |
+
col1, col2, col3 = st.columns(3)
|
| 282 |
+
|
| 283 |
+
with col1:
|
| 284 |
+
if st.button("ℹ️ Database Info", use_container_width=True):
|
| 285 |
+
try:
|
| 286 |
+
info = chroma_manager.get_collection_info()
|
| 287 |
+
st.json(info)
|
| 288 |
+
except Exception as e:
|
| 289 |
+
st.error(f"Error: {e}")
|
| 290 |
+
|
| 291 |
+
with col2:
|
| 292 |
+
if st.button("📋 List Documents", use_container_width=True):
|
| 293 |
+
try:
|
| 294 |
+
all_docs = chroma_manager.collection.get(include=['documents'])
|
| 295 |
+
if all_docs['ids']:
|
| 296 |
+
st.write(f"Total documents: {len(all_docs['ids'])}")
|
| 297 |
+
for idx, doc_id in enumerate(all_docs['ids'][:15], 1):
|
| 298 |
+
st.write(f"{idx}. {doc_id}")
|
| 299 |
+
if len(all_docs['ids']) > 15:
|
| 300 |
+
st.write(f"... and {len(all_docs['ids']) - 15} more")
|
| 301 |
+
else:
|
| 302 |
+
st.info("No documents in database")
|
| 303 |
+
except Exception as e:
|
| 304 |
+
st.error(f"Error: {e}")
|
| 305 |
+
|
| 306 |
+
with col3:
|
| 307 |
+
if st.button("🗑️ Clear Database", use_container_width=True):
|
| 308 |
+
try:
|
| 309 |
+
collection_info = chroma_manager.get_collection_info()
|
| 310 |
+
if collection_info['document_count'] > 0:
|
| 311 |
+
chroma_manager.clear_collection()
|
| 312 |
+
st.success("✅ Database cleared!")
|
| 313 |
+
st.rerun()
|
| 314 |
+
else:
|
| 315 |
+
st.info("Database is already empty")
|
| 316 |
+
except Exception as e:
|
| 317 |
+
st.error(f"Error: {e}")
|
| 318 |
+
|
| 319 |
+
st.divider()
|
| 320 |
+
|
| 321 |
+
st.markdown("### Quick Stats")
|
| 322 |
+
stats_col1, stats_col2 = st.columns(2)
|
| 323 |
+
|
| 324 |
+
with stats_col1:
|
| 325 |
+
st.metric("PDF Extraction Dir", "./pdf_extractions")
|
| 326 |
+
|
| 327 |
+
with stats_col2:
|
| 328 |
+
st.metric("ChromaDB Location", "./chroma_db")
|
| 329 |
+
|
| 330 |
+
# ============================================================================
|
| 331 |
+
# FOOTER
|
| 332 |
+
# ============================================================================
|
| 333 |
+
|
| 334 |
+
st.divider()
|
| 335 |
+
st.markdown("""
|
| 336 |
+
<div style='text-align: center; color: #666; font-size: 0.9rem;'>
|
| 337 |
+
Multimodal RAG System (Improved) | Qwen2.5-VL + ChromaDB + Streamlit | v1.1
|
| 338 |
+
</div>
|
| 339 |
+
""", unsafe_allow_html=True)
|
src/embedder.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================================================
|
| 2 |
+
# STEP 2: EMBEDDER MODULE
|
| 3 |
+
# Generate embeddings using CLIP and store in ChromaDB
|
| 4 |
+
# ============================================================================
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import json
|
| 8 |
+
from typing import List, Dict, Optional
|
| 9 |
+
import chromadb
|
| 10 |
+
from chromadb import Documents, EmbeddingFunction, Embeddings
|
| 11 |
+
from sentence_transformers import SentenceTransformer
|
| 12 |
+
import numpy as np
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class CLIPEmbeddingFunction(EmbeddingFunction):
|
| 16 |
+
"""Custom embedding function using CLIP model."""
|
| 17 |
+
|
| 18 |
+
def __init__(self, model_name: str = "sentence-transformers/clip-ViT-B-32"):
|
| 19 |
+
"""Initialize CLIP embedder."""
|
| 20 |
+
self.model = SentenceTransformer(model_name)
|
| 21 |
+
|
| 22 |
+
def __call__(self, input: Documents) -> Embeddings:
|
| 23 |
+
"""Generate embeddings for input documents."""
|
| 24 |
+
# Handle both text and list inputs
|
| 25 |
+
if isinstance(input, str):
|
| 26 |
+
embeddings = self.model.encode([input]).tolist()
|
| 27 |
+
else:
|
| 28 |
+
embeddings = self.model.encode(list(input)).tolist()
|
| 29 |
+
return embeddings
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class ChromaDBManager:
|
| 33 |
+
"""Manage ChromaDB vector storage with persistent data."""
|
| 34 |
+
|
| 35 |
+
def __init__(self, db_dir: str = "./chroma_db"):
|
| 36 |
+
"""Initialize ChromaDB with persistent storage."""
|
| 37 |
+
self.db_dir = db_dir
|
| 38 |
+
os.makedirs(db_dir, exist_ok=True)
|
| 39 |
+
|
| 40 |
+
# Initialize persistent client
|
| 41 |
+
self.client = chromadb.PersistentClient(path=db_dir)
|
| 42 |
+
|
| 43 |
+
# Initialize embedding function with CLIP
|
| 44 |
+
self.embedding_function = CLIPEmbeddingFunction(
|
| 45 |
+
model_name="sentence-transformers/clip-ViT-B-32"
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Get or create collection
|
| 49 |
+
self.collection = self.client.get_or_create_collection(
|
| 50 |
+
name="pdf_documents",
|
| 51 |
+
embedding_function=self.embedding_function,
|
| 52 |
+
metadata={"hnsw:space": "cosine"}
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
print(f"ChromaDB initialized. Database location: {db_dir}")
|
| 56 |
+
|
| 57 |
+
def add_documents(self, documents: List[Dict]) -> None:
|
| 58 |
+
"""Add documents to ChromaDB."""
|
| 59 |
+
if not documents:
|
| 60 |
+
print("No documents to add")
|
| 61 |
+
return
|
| 62 |
+
|
| 63 |
+
doc_ids = []
|
| 64 |
+
doc_texts = []
|
| 65 |
+
doc_metadatas = []
|
| 66 |
+
|
| 67 |
+
for idx, doc in enumerate(documents):
|
| 68 |
+
doc_id = f"doc_{doc.get('filename', 'unknown')}_{idx}"
|
| 69 |
+
doc_text = doc.get('text', '') + " " + " ".join([table[1] for table in doc.get('tables', [])])
|
| 70 |
+
|
| 71 |
+
doc_ids.append(doc_id)
|
| 72 |
+
doc_texts.append(doc_text)
|
| 73 |
+
doc_metadatas.append({
|
| 74 |
+
"filename": doc.get('filename', ''),
|
| 75 |
+
"page": str(doc.get('page', 0)),
|
| 76 |
+
"source": "pdf"
|
| 77 |
+
})
|
| 78 |
+
|
| 79 |
+
# Add to collection
|
| 80 |
+
self.collection.add(
|
| 81 |
+
ids=doc_ids,
|
| 82 |
+
documents=doc_texts,
|
| 83 |
+
metadatas=doc_metadatas
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
print(f"Added {len(documents)} documents to ChromaDB")
|
| 87 |
+
|
| 88 |
+
def search(self, query: str, n_results: int = 5) -> List[Dict]:
|
| 89 |
+
"""Search for documents similar to query."""
|
| 90 |
+
results = self.collection.query(
|
| 91 |
+
query_texts=[query],
|
| 92 |
+
n_results=n_results
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
retrieved_docs = []
|
| 96 |
+
if results['documents']:
|
| 97 |
+
for doc, distance, metadata in zip(
|
| 98 |
+
results['documents'][0],
|
| 99 |
+
results['distances'][0],
|
| 100 |
+
results['metadatas'][0]
|
| 101 |
+
):
|
| 102 |
+
retrieved_docs.append({
|
| 103 |
+
'document': doc,
|
| 104 |
+
'distance': distance,
|
| 105 |
+
'metadata': metadata,
|
| 106 |
+
'relevance_score': 1 - distance # Convert distance to similarity score
|
| 107 |
+
})
|
| 108 |
+
|
| 109 |
+
return retrieved_docs
|
| 110 |
+
|
| 111 |
+
def get_all_documents_count(self) -> int:
|
| 112 |
+
"""Get total number of documents in collection."""
|
| 113 |
+
return self.collection.count()
|
| 114 |
+
|
| 115 |
+
def clear_collection(self) -> None:
|
| 116 |
+
"""Clear all documents from collection (for reset)."""
|
| 117 |
+
self.collection.delete(where={})
|
| 118 |
+
print("Collection cleared")
|
| 119 |
+
|
| 120 |
+
def get_collection_info(self) -> Dict:
|
| 121 |
+
"""Get information about the collection."""
|
| 122 |
+
return {
|
| 123 |
+
"name": self.collection.name,
|
| 124 |
+
"document_count": self.collection.count(),
|
| 125 |
+
"metadata": self.collection.metadata
|
| 126 |
+
}
|
src/pdf_parser.py
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from typing import Dict, List, Tuple
|
| 5 |
+
import pdfplumber
|
| 6 |
+
import fitz # PyMuPDF
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import io
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class PDFParser:
|
| 12 |
+
"""Parse PDF documents and extract text, tables, and images."""
|
| 13 |
+
|
| 14 |
+
def __init__(self, extraction_dir: str = "./pdf_extractions"):
|
| 15 |
+
self.extraction_dir = extraction_dir
|
| 16 |
+
self.state_file = os.path.join(extraction_dir, "processing_state.json")
|
| 17 |
+
os.makedirs(extraction_dir, exist_ok=True)
|
| 18 |
+
self.processed_files = self._load_processing_state()
|
| 19 |
+
|
| 20 |
+
def _load_processing_state(self) -> Dict:
|
| 21 |
+
"""Load state of already processed files to avoid re-processing."""
|
| 22 |
+
if os.path.exists(self.state_file):
|
| 23 |
+
try:
|
| 24 |
+
with open(self.state_file, 'r') as f:
|
| 25 |
+
return json.load(f)
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"Warning: Could not load processing state: {e}")
|
| 28 |
+
return {}
|
| 29 |
+
return {}
|
| 30 |
+
|
| 31 |
+
def _save_processing_state(self):
|
| 32 |
+
"""Save processing state to disk."""
|
| 33 |
+
try:
|
| 34 |
+
with open(self.state_file, 'w') as f:
|
| 35 |
+
json.dump(self.processed_files, f, indent=2)
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"Warning: Could not save processing state: {e}")
|
| 38 |
+
|
| 39 |
+
def _get_file_hash(self, pdf_path: str) -> str:
|
| 40 |
+
"""Generate a simple hash for the file (file size + modification time)."""
|
| 41 |
+
try:
|
| 42 |
+
stat = os.stat(pdf_path)
|
| 43 |
+
return f"{stat.st_size}_{stat.st_mtime}"
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"Error getting file hash: {e}")
|
| 46 |
+
return "unknown"
|
| 47 |
+
|
| 48 |
+
def extract_text_with_pdfplumber(self, pdf_path: str, max_chars: int = 1000000) -> str:
|
| 49 |
+
"""Extract text from PDF using pdfplumber (handles complex layouts)."""
|
| 50 |
+
text = ""
|
| 51 |
+
char_count = 0
|
| 52 |
+
try:
|
| 53 |
+
with pdfplumber.open(pdf_path) as pdf:
|
| 54 |
+
for page_num, page in enumerate(pdf.pages, 1):
|
| 55 |
+
if char_count >= max_chars:
|
| 56 |
+
print(f"Text extraction reached maximum chars limit ({max_chars})")
|
| 57 |
+
break
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
page_text = page.extract_text()
|
| 61 |
+
if page_text:
|
| 62 |
+
# Limit per-page text to avoid token explosion
|
| 63 |
+
page_text = page_text[:50000]
|
| 64 |
+
text += f"\n--- Page {page_num} ---\n{page_text}"
|
| 65 |
+
char_count += len(page_text)
|
| 66 |
+
except Exception as e:
|
| 67 |
+
print(f"Error extracting text from page {page_num}: {e}")
|
| 68 |
+
continue
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(f"Error opening PDF with pdfplumber: {e}")
|
| 71 |
+
|
| 72 |
+
return text
|
| 73 |
+
|
| 74 |
+
def extract_tables_from_pdf(self, pdf_path: str, max_tables: int = 50) -> List[Tuple[int, str]]:
|
| 75 |
+
"""Extract tables from PDF and return as formatted text."""
|
| 76 |
+
tables = []
|
| 77 |
+
table_count = 0
|
| 78 |
+
try:
|
| 79 |
+
with pdfplumber.open(pdf_path) as pdf:
|
| 80 |
+
for page_num, page in enumerate(pdf.pages, 1):
|
| 81 |
+
if table_count >= max_tables:
|
| 82 |
+
print(f"Table extraction reached maximum tables limit ({max_tables})")
|
| 83 |
+
break
|
| 84 |
+
|
| 85 |
+
try:
|
| 86 |
+
page_tables = page.extract_tables()
|
| 87 |
+
if page_tables:
|
| 88 |
+
for table_idx, table in enumerate(page_tables):
|
| 89 |
+
# Convert table to text format
|
| 90 |
+
table_text = f"TABLE on page {page_num}:\n"
|
| 91 |
+
for row in table:
|
| 92 |
+
row_str = " | ".join([str(cell) if cell else "" for cell in row])
|
| 93 |
+
# Limit row length
|
| 94 |
+
if len(row_str) > 1000:
|
| 95 |
+
row_str = row_str[:1000] + "..."
|
| 96 |
+
table_text += row_str + "\n"
|
| 97 |
+
|
| 98 |
+
tables.append((page_num, table_text))
|
| 99 |
+
table_count += 1
|
| 100 |
+
except Exception as e:
|
| 101 |
+
print(f"Error extracting tables from page {page_num}: {e}")
|
| 102 |
+
continue
|
| 103 |
+
except Exception as e:
|
| 104 |
+
print(f"Error opening PDF for table extraction: {e}")
|
| 105 |
+
|
| 106 |
+
return tables
|
| 107 |
+
|
| 108 |
+
def extract_images_from_pdf(self, pdf_path: str, output_dir: str = None, max_images: int = 100) -> List[Tuple[int, str]]:
|
| 109 |
+
"""
|
| 110 |
+
Extract images from PDF using PyMuPDF.
|
| 111 |
+
FIXED: Properly handles xref tuples from get_images()
|
| 112 |
+
"""
|
| 113 |
+
if output_dir is None:
|
| 114 |
+
output_dir = os.path.join(self.extraction_dir, "images")
|
| 115 |
+
|
| 116 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 117 |
+
images = []
|
| 118 |
+
image_count = 0
|
| 119 |
+
|
| 120 |
+
try:
|
| 121 |
+
pdf_name = Path(pdf_path).stem
|
| 122 |
+
pdf_file = fitz.open(pdf_path)
|
| 123 |
+
|
| 124 |
+
for page_num in range(len(pdf_file)):
|
| 125 |
+
if image_count >= max_images:
|
| 126 |
+
print(f"Image extraction reached maximum images limit ({max_images})")
|
| 127 |
+
break
|
| 128 |
+
|
| 129 |
+
try:
|
| 130 |
+
page = pdf_file[page_num]
|
| 131 |
+
pix_list = page.get_images()
|
| 132 |
+
|
| 133 |
+
for image_idx, img_info in enumerate(pix_list):
|
| 134 |
+
if image_count >= max_images:
|
| 135 |
+
break
|
| 136 |
+
|
| 137 |
+
try:
|
| 138 |
+
# FIXED: Extract xref from tuple properly
|
| 139 |
+
# get_images() returns tuples: (xref, smask, width, height, ...)
|
| 140 |
+
xref = img_info[0] # Get xref as integer
|
| 141 |
+
|
| 142 |
+
# Extract image
|
| 143 |
+
base_image = pdf_file.extract_image(xref)
|
| 144 |
+
|
| 145 |
+
if base_image and "image" in base_image:
|
| 146 |
+
image_bytes = base_image["image"]
|
| 147 |
+
image_ext = base_image["ext"]
|
| 148 |
+
|
| 149 |
+
image_name = f"{pdf_name}_page{page_num+1}_img{image_idx}.{image_ext}"
|
| 150 |
+
image_path = os.path.join(output_dir, image_name)
|
| 151 |
+
|
| 152 |
+
with open(image_path, "wb") as f:
|
| 153 |
+
f.write(image_bytes)
|
| 154 |
+
|
| 155 |
+
images.append((page_num + 1, image_path))
|
| 156 |
+
image_count += 1
|
| 157 |
+
|
| 158 |
+
except TypeError as e:
|
| 159 |
+
# Handle comparison errors with tuple
|
| 160 |
+
print(f"Error with image data type on page {page_num}, image {image_idx}: {e}")
|
| 161 |
+
continue
|
| 162 |
+
except Exception as e:
|
| 163 |
+
print(f"Error extracting image {image_idx} from page {page_num}: {e}")
|
| 164 |
+
continue
|
| 165 |
+
|
| 166 |
+
except Exception as e:
|
| 167 |
+
print(f"Error processing page {page_num}: {e}")
|
| 168 |
+
continue
|
| 169 |
+
|
| 170 |
+
pdf_file.close()
|
| 171 |
+
except Exception as e:
|
| 172 |
+
print(f"Error opening PDF for image extraction: {e}")
|
| 173 |
+
|
| 174 |
+
return images
|
| 175 |
+
|
| 176 |
+
def process_pdf(self, pdf_path: str) -> Dict:
|
| 177 |
+
"""Process entire PDF and extract all content."""
|
| 178 |
+
file_hash = self._get_file_hash(pdf_path)
|
| 179 |
+
|
| 180 |
+
# Check if already processed
|
| 181 |
+
if pdf_path in self.processed_files and self.processed_files[pdf_path] == file_hash:
|
| 182 |
+
print(f"File {pdf_path} already processed. Loading cached results.")
|
| 183 |
+
return self._load_cached_results(pdf_path)
|
| 184 |
+
|
| 185 |
+
print(f"Processing PDF: {pdf_path}")
|
| 186 |
+
|
| 187 |
+
result = {
|
| 188 |
+
"pdf_path": pdf_path,
|
| 189 |
+
"filename": Path(pdf_path).name,
|
| 190 |
+
"text": self.extract_text_with_pdfplumber(pdf_path, max_chars=1000000),
|
| 191 |
+
"tables": self.extract_tables_from_pdf(pdf_path, max_tables=50),
|
| 192 |
+
"images": self.extract_images_from_pdf(pdf_path, max_images=100)
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
# Save results to cache
|
| 196 |
+
self._save_cached_results(pdf_path, result)
|
| 197 |
+
|
| 198 |
+
# Update processing state
|
| 199 |
+
self.processed_files[pdf_path] = file_hash
|
| 200 |
+
self._save_processing_state()
|
| 201 |
+
|
| 202 |
+
return result
|
| 203 |
+
|
| 204 |
+
def _save_cached_results(self, pdf_path: str, result: Dict):
|
| 205 |
+
"""Save extraction results to a JSON file."""
|
| 206 |
+
safe_name = Path(pdf_path).stem
|
| 207 |
+
cache_file = os.path.join(self.extraction_dir, f"{safe_name}_cache.json")
|
| 208 |
+
|
| 209 |
+
# Don't save image paths in cache, just metadata
|
| 210 |
+
cache_data = {
|
| 211 |
+
"pdf_path": result["pdf_path"],
|
| 212 |
+
"filename": result["filename"],
|
| 213 |
+
"text": result["text"],
|
| 214 |
+
"tables": result["tables"],
|
| 215 |
+
"image_count": len(result["images"])
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
try:
|
| 219 |
+
with open(cache_file, 'w', encoding='utf-8') as f:
|
| 220 |
+
json.dump(cache_data, f, ensure_ascii=False, indent=2)
|
| 221 |
+
except Exception as e:
|
| 222 |
+
print(f"Warning: Could not save cache: {e}")
|
| 223 |
+
|
| 224 |
+
def _load_cached_results(self, pdf_path: str) -> Dict:
|
| 225 |
+
"""Load cached extraction results."""
|
| 226 |
+
safe_name = Path(pdf_path).stem
|
| 227 |
+
cache_file = os.path.join(self.extraction_dir, f"{safe_name}_cache.json")
|
| 228 |
+
|
| 229 |
+
try:
|
| 230 |
+
with open(cache_file, 'r', encoding='utf-8') as f:
|
| 231 |
+
return json.load(f)
|
| 232 |
+
except Exception as e:
|
| 233 |
+
print(f"Error loading cache: {e}")
|
| 234 |
+
return {"text": "", "tables": [], "images": []}
|
| 235 |
+
|
| 236 |
+
def process_pdf_directory(self, pdf_dir: str) -> List[Dict]:
|
| 237 |
+
"""Process all PDFs in a directory."""
|
| 238 |
+
results = []
|
| 239 |
+
pdf_files = list(Path(pdf_dir).glob("*.pdf"))
|
| 240 |
+
|
| 241 |
+
if not pdf_files:
|
| 242 |
+
print(f"No PDF files found in {pdf_dir}")
|
| 243 |
+
return results
|
| 244 |
+
|
| 245 |
+
print(f"Found {len(pdf_files)} PDF files to process")
|
| 246 |
+
|
| 247 |
+
for idx, pdf_file in enumerate(pdf_files, 1):
|
| 248 |
+
try:
|
| 249 |
+
print(f"Processing {idx}/{len(pdf_files)}: {pdf_file.name}")
|
| 250 |
+
result = self.process_pdf(str(pdf_file))
|
| 251 |
+
results.append(result)
|
| 252 |
+
except Exception as e:
|
| 253 |
+
print(f"Error processing {pdf_file}: {e}")
|
| 254 |
+
continue
|
| 255 |
+
|
| 256 |
+
print(f"Completed processing {len(results)} PDFs")
|
| 257 |
+
return results
|
src/rag_pipeline.py
ADDED
|
@@ -0,0 +1,417 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List, Dict, Optional, Tuple
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor, AutoTokenizer
|
| 4 |
+
from qwen_vl_utils import process_vision_info
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class TokenChunker:
|
| 10 |
+
"""Handle token counting and chunking for model context limits."""
|
| 11 |
+
|
| 12 |
+
def __init__(self, model_name: str = "Qwen/Qwen2.5-VL-3B-Instruct"):
|
| 13 |
+
"""Initialize tokenizer for token counting."""
|
| 14 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 15 |
+
# Qwen2.5-VL has max context of 131,072 tokens
|
| 16 |
+
self.max_tokens = 100000 # Conservative limit (use 100K of 131K available)
|
| 17 |
+
|
| 18 |
+
def count_tokens(self, text: str) -> int:
|
| 19 |
+
"""Count tokens in text."""
|
| 20 |
+
try:
|
| 21 |
+
tokens = self.tokenizer.encode(text, add_special_tokens=False)
|
| 22 |
+
return len(tokens)
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"Error counting tokens: {e}")
|
| 25 |
+
# Rough estimate: 1 token ≈ 4 characters for English/Russian
|
| 26 |
+
return len(text) // 4
|
| 27 |
+
|
| 28 |
+
def chunk_text(self, text: str, chunk_size: int = 50000) -> List[str]:
|
| 29 |
+
"""Split text into chunks that fit within token limits."""
|
| 30 |
+
if len(text) <= chunk_size:
|
| 31 |
+
return [text]
|
| 32 |
+
|
| 33 |
+
chunks = []
|
| 34 |
+
current_chunk = ""
|
| 35 |
+
|
| 36 |
+
# Split by paragraphs first
|
| 37 |
+
paragraphs = text.split("\n\n")
|
| 38 |
+
|
| 39 |
+
for paragraph in paragraphs:
|
| 40 |
+
if len(current_chunk) + len(paragraph) < chunk_size:
|
| 41 |
+
current_chunk += paragraph + "\n\n"
|
| 42 |
+
else:
|
| 43 |
+
if current_chunk:
|
| 44 |
+
chunks.append(current_chunk.strip())
|
| 45 |
+
current_chunk = paragraph + "\n\n"
|
| 46 |
+
|
| 47 |
+
if current_chunk:
|
| 48 |
+
chunks.append(current_chunk.strip())
|
| 49 |
+
|
| 50 |
+
return chunks
|
| 51 |
+
|
| 52 |
+
def truncate_to_token_limit(self, text: str, token_limit: int = 50000) -> str:
|
| 53 |
+
"""Truncate text to fit within token limit."""
|
| 54 |
+
current_tokens = self.count_tokens(text)
|
| 55 |
+
|
| 56 |
+
if current_tokens <= token_limit:
|
| 57 |
+
return text
|
| 58 |
+
|
| 59 |
+
print(f"Text too long ({current_tokens} tokens). Truncating to {token_limit}...")
|
| 60 |
+
|
| 61 |
+
# Estimate characters per token
|
| 62 |
+
char_per_token = len(text) / current_tokens
|
| 63 |
+
target_chars = int(token_limit * char_per_token * 0.9) # 90% to be safe
|
| 64 |
+
|
| 65 |
+
truncated = text[:target_chars]
|
| 66 |
+
return truncated
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
class Qwen25VLInferencer:
|
| 70 |
+
"""Handle inference with Qwen2.5-VL-3B model - FIXED meta tensor issue."""
|
| 71 |
+
|
| 72 |
+
class Qwen25VLInferencer:
|
| 73 |
+
"""Handle inference with Qwen2.5-VL-3B model - FIXED meta tensor issue."""
|
| 74 |
+
|
| 75 |
+
def __init__(self, model_name: str = "Qwen/Qwen2.5-VL-3B-Instruct", device: str = "cuda"):
|
| 76 |
+
"""Initialize Qwen2.5-VL model with proper device handling."""
|
| 77 |
+
self.device = device if torch.cuda.is_available() else "cpu"
|
| 78 |
+
print(f"Loading Qwen2.5-VL-3B model on device: {self.device}")
|
| 79 |
+
|
| 80 |
+
try:
|
| 81 |
+
# FIXED: Load model without device_map first, then move to device
|
| 82 |
+
# This avoids the meta tensor issue
|
| 83 |
+
|
| 84 |
+
# Determine data type based on device
|
| 85 |
+
if self.device == "cuda":
|
| 86 |
+
dtype = torch.float16 # GPU: use half precision
|
| 87 |
+
else:
|
| 88 |
+
dtype = torch.float32 # CPU: use full precision
|
| 89 |
+
|
| 90 |
+
print(f"Using dtype: {dtype}")
|
| 91 |
+
|
| 92 |
+
# Load model
|
| 93 |
+
print("Loading model weights...")
|
| 94 |
+
self.model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 95 |
+
model_name,
|
| 96 |
+
torch_dtype=dtype,
|
| 97 |
+
trust_remote_code=True,
|
| 98 |
+
# IMPORTANT: Don't use device_map="auto" here - causes meta tensor issue
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
# Move to device explicitly AFTER loading
|
| 102 |
+
print(f"Moving model to {self.device}...")
|
| 103 |
+
if self.device == "cuda":
|
| 104 |
+
self.model = self.model.to("cuda")
|
| 105 |
+
else:
|
| 106 |
+
self.model = self.model.to("cpu")
|
| 107 |
+
|
| 108 |
+
# Set to evaluation mode
|
| 109 |
+
self.model.eval()
|
| 110 |
+
|
| 111 |
+
print("✅ Model loaded successfully")
|
| 112 |
+
|
| 113 |
+
except RuntimeError as e:
|
| 114 |
+
if "meta tensor" in str(e):
|
| 115 |
+
print(f"⚠️ Meta tensor error detected: {e}")
|
| 116 |
+
print("Falling back to CPU mode...")
|
| 117 |
+
self.device = "cpu"
|
| 118 |
+
|
| 119 |
+
self.model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 120 |
+
model_name,
|
| 121 |
+
torch_dtype=torch.float32,
|
| 122 |
+
trust_remote_code=True,
|
| 123 |
+
)
|
| 124 |
+
self.model = self.model.to("cpu")
|
| 125 |
+
self.model.eval()
|
| 126 |
+
print("✅ Model loaded on CPU")
|
| 127 |
+
else:
|
| 128 |
+
raise
|
| 129 |
+
|
| 130 |
+
except Exception as e:
|
| 131 |
+
print(f"❌ Error loading model: {e}")
|
| 132 |
+
print("Trying fallback CPU loading...")
|
| 133 |
+
|
| 134 |
+
self.device = "cpu"
|
| 135 |
+
self.model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 136 |
+
model_name,
|
| 137 |
+
torch_dtype=torch.float32,
|
| 138 |
+
trust_remote_code=True,
|
| 139 |
+
)
|
| 140 |
+
self.model = self.model.to("cpu")
|
| 141 |
+
self.model.eval()
|
| 142 |
+
|
| 143 |
+
# Load processor
|
| 144 |
+
print("Loading processor...")
|
| 145 |
+
self.processor = AutoProcessor.from_pretrained(
|
| 146 |
+
model_name,
|
| 147 |
+
trust_remote_code=True
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# Initialize token chunker
|
| 151 |
+
self.token_chunker = TokenChunker(model_name)
|
| 152 |
+
|
| 153 |
+
print("✅ Model initialization complete")
|
| 154 |
+
|
| 155 |
+
def _prepare_text_message(self, text: str) -> List[Dict]:
|
| 156 |
+
"""Prepare text-only message for the model."""
|
| 157 |
+
return [{"type": "text", "text": text}]
|
| 158 |
+
|
| 159 |
+
def _prepare_image_text_message(self, image_path: str, text: str) -> List[Dict]:
|
| 160 |
+
"""Prepare message with image and text."""
|
| 161 |
+
return [
|
| 162 |
+
{"type": "image", "image": image_path},
|
| 163 |
+
{"type": "text", "text": text}
|
| 164 |
+
]
|
| 165 |
+
|
| 166 |
+
def generate_answer(
|
| 167 |
+
self,
|
| 168 |
+
query: str,
|
| 169 |
+
retrieved_docs: List[Dict],
|
| 170 |
+
retrieved_images: List[str] = None,
|
| 171 |
+
max_new_tokens: int = 128
|
| 172 |
+
) -> str:
|
| 173 |
+
"""
|
| 174 |
+
Generate answer based on query and retrieved documents.
|
| 175 |
+
FIXED: Includes token chunking and context length management
|
| 176 |
+
"""
|
| 177 |
+
# Build context from retrieved documents
|
| 178 |
+
context = "КОНТЕКСТ ИЗ ДОКУМЕНТОВ:\n"
|
| 179 |
+
for doc in retrieved_docs:
|
| 180 |
+
relevance = doc.get('relevance_score', 0)
|
| 181 |
+
context += f"\n[Релевантность: {relevance:.2f}]\n{doc['document']}\n"
|
| 182 |
+
|
| 183 |
+
# FIXED: Truncate context if too long
|
| 184 |
+
context = self.token_chunker.truncate_to_token_limit(context, token_limit=50000)
|
| 185 |
+
|
| 186 |
+
# Build system prompt
|
| 187 |
+
system_prompt = "Ты помощник для анализа документов. Используй предоставленный контекст для ответа на вопросы. Отвечай на русском языке. Будь кратким и точным."
|
| 188 |
+
|
| 189 |
+
# Prepare the full query
|
| 190 |
+
full_query = f"{system_prompt}\n\n{context}\n\nВопрос: {query}\n\nОтвет:"
|
| 191 |
+
|
| 192 |
+
# FIXED: Check and limit token count
|
| 193 |
+
query_tokens = self.token_chunker.count_tokens(full_query)
|
| 194 |
+
print(f"Query token count: {query_tokens}")
|
| 195 |
+
|
| 196 |
+
if query_tokens > 100000:
|
| 197 |
+
print(f"Query exceeds token limit. Reducing context...")
|
| 198 |
+
# Keep only first 3 documents instead of all
|
| 199 |
+
context = "КОНТЕКСТ ИЗ ДОКУМЕНТОВ:\n"
|
| 200 |
+
for doc in retrieved_docs[:3]:
|
| 201 |
+
relevance = doc.get('relevance_score', 0)
|
| 202 |
+
context += f"\n[Релевантность: {relevance:.2f}]\n{doc['document']}\n"
|
| 203 |
+
|
| 204 |
+
context = self.token_chunker.truncate_to_token_limit(context, token_limit=30000)
|
| 205 |
+
full_query = f"{system_prompt}\n\n{context}\n\nВопрос: {query}\n\nОтвет:"
|
| 206 |
+
|
| 207 |
+
# Prepare messages
|
| 208 |
+
messages = self._prepare_text_message(full_query)
|
| 209 |
+
|
| 210 |
+
# If images are provided, add them
|
| 211 |
+
if retrieved_images and len(retrieved_images) > 0:
|
| 212 |
+
try:
|
| 213 |
+
image_message = self._prepare_image_text_message(
|
| 214 |
+
retrieved_images[0],
|
| 215 |
+
f"Проанализируй это изображение в контексте вопроса: {query}"
|
| 216 |
+
)
|
| 217 |
+
messages = image_message + [{"type": "text", "text": full_query}]
|
| 218 |
+
except Exception as e:
|
| 219 |
+
print(f"Warning: Could not include images: {e}")
|
| 220 |
+
|
| 221 |
+
# Process vision info if images are included
|
| 222 |
+
image_inputs = []
|
| 223 |
+
video_inputs = []
|
| 224 |
+
|
| 225 |
+
try:
|
| 226 |
+
if any(msg.get('type') == 'image' for msg in messages):
|
| 227 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
| 228 |
+
except Exception as e:
|
| 229 |
+
print(f"Warning: Could not process images: {e}")
|
| 230 |
+
|
| 231 |
+
# Prepare inputs for model
|
| 232 |
+
try:
|
| 233 |
+
inputs = self.processor(
|
| 234 |
+
text=[full_query],
|
| 235 |
+
images=image_inputs if image_inputs else None,
|
| 236 |
+
videos=video_inputs if video_inputs else None,
|
| 237 |
+
padding=True,
|
| 238 |
+
return_tensors='pt',
|
| 239 |
+
)
|
| 240 |
+
except Exception as e:
|
| 241 |
+
print(f"Error preparing inputs: {e}")
|
| 242 |
+
return f"Error preparing inputs: {e}"
|
| 243 |
+
|
| 244 |
+
# Move inputs to device
|
| 245 |
+
if self.device == "cuda":
|
| 246 |
+
inputs = inputs.to("cuda")
|
| 247 |
+
|
| 248 |
+
# Generate response with error handling
|
| 249 |
+
try:
|
| 250 |
+
with torch.no_grad():
|
| 251 |
+
generated_ids = self.model.generate(
|
| 252 |
+
**inputs,
|
| 253 |
+
max_new_tokens=min(max_new_tokens, 512), # Cap at 512
|
| 254 |
+
num_beams=1,
|
| 255 |
+
do_sample=False
|
| 256 |
+
)
|
| 257 |
+
except Exception as e:
|
| 258 |
+
print(f"Error during generation: {e}")
|
| 259 |
+
return f"Error generating response: {e}"
|
| 260 |
+
|
| 261 |
+
# Decode output
|
| 262 |
+
try:
|
| 263 |
+
generated_ids_trimmed = [
|
| 264 |
+
out_ids[len(in_ids):]
|
| 265 |
+
for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 266 |
+
]
|
| 267 |
+
|
| 268 |
+
response = self.processor.batch_decode(
|
| 269 |
+
generated_ids_trimmed,
|
| 270 |
+
skip_special_tokens=True,
|
| 271 |
+
clean_up_tokenization_spaces=False
|
| 272 |
+
)
|
| 273 |
+
|
| 274 |
+
return response[0] if response else "Could not generate response"
|
| 275 |
+
except Exception as e:
|
| 276 |
+
print(f"Error decoding response: {e}")
|
| 277 |
+
return f"Error decoding response: {e}"
|
| 278 |
+
|
| 279 |
+
def summarize_document(
|
| 280 |
+
self,
|
| 281 |
+
document_text: str,
|
| 282 |
+
max_new_tokens: int = 512
|
| 283 |
+
) -> str:
|
| 284 |
+
"""Summarize a document with token limit management."""
|
| 285 |
+
|
| 286 |
+
# FIXED: Truncate document to fit in context
|
| 287 |
+
document_text = self.token_chunker.truncate_to_token_limit(
|
| 288 |
+
document_text,
|
| 289 |
+
token_limit=40000
|
| 290 |
+
)
|
| 291 |
+
|
| 292 |
+
prompt = f"""Пожалуйста, создай подробное резюме следующего документа на русском языке.
|
| 293 |
+
|
| 294 |
+
Документ:
|
| 295 |
+
{document_text}
|
| 296 |
+
|
| 297 |
+
Резюме:"""
|
| 298 |
+
|
| 299 |
+
messages = self._prepare_text_message(prompt)
|
| 300 |
+
|
| 301 |
+
try:
|
| 302 |
+
inputs = self.processor(
|
| 303 |
+
text=[prompt],
|
| 304 |
+
padding=True,
|
| 305 |
+
return_tensors='pt',
|
| 306 |
+
)
|
| 307 |
+
|
| 308 |
+
if self.device == "cuda":
|
| 309 |
+
inputs = inputs.to("cuda")
|
| 310 |
+
|
| 311 |
+
with torch.no_grad():
|
| 312 |
+
generated_ids = self.model.generate(
|
| 313 |
+
**inputs,
|
| 314 |
+
max_new_tokens=min(max_new_tokens, 512),
|
| 315 |
+
num_beams=1,
|
| 316 |
+
do_sample=False
|
| 317 |
+
)
|
| 318 |
+
|
| 319 |
+
generated_ids_trimmed = [
|
| 320 |
+
out_ids[len(in_ids):]
|
| 321 |
+
for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 322 |
+
]
|
| 323 |
+
|
| 324 |
+
response = self.processor.batch_decode(
|
| 325 |
+
generated_ids_trimmed,
|
| 326 |
+
skip_special_tokens=True,
|
| 327 |
+
clean_up_tokenization_spaces=False
|
| 328 |
+
)
|
| 329 |
+
|
| 330 |
+
return response[0] if response else "Could not generate summary"
|
| 331 |
+
except Exception as e:
|
| 332 |
+
print(f"Error generating summary: {e}")
|
| 333 |
+
return f"Error: {e}"
|
| 334 |
+
|
| 335 |
+
|
| 336 |
+
class RAGPipeline:
|
| 337 |
+
"""Complete RAG pipeline combining retrieval and generation."""
|
| 338 |
+
|
| 339 |
+
def __init__(self, chroma_manager, device: str = "cuda"):
|
| 340 |
+
"""Initialize RAG pipeline."""
|
| 341 |
+
self.chroma_manager = chroma_manager
|
| 342 |
+
self.inferencer = Qwen25VLInferencer(device=device)
|
| 343 |
+
|
| 344 |
+
def answer_question(
|
| 345 |
+
self,
|
| 346 |
+
query: str,
|
| 347 |
+
n_retrieved: int = 5,
|
| 348 |
+
max_new_tokens: int = 512
|
| 349 |
+
) -> Dict:
|
| 350 |
+
"""
|
| 351 |
+
Answer user question using RAG pipeline.
|
| 352 |
+
1. Retrieve relevant documents
|
| 353 |
+
2. Generate answer using Qwen2.5-VL
|
| 354 |
+
"""
|
| 355 |
+
# Step 1: Retrieve
|
| 356 |
+
retrieved_docs = self.chroma_manager.search(query, n_results=n_retrieved)
|
| 357 |
+
|
| 358 |
+
if not retrieved_docs:
|
| 359 |
+
return {
|
| 360 |
+
"answer": "Не найдены релевантные документы для ответа на вопрос.",
|
| 361 |
+
"retrieved_docs": [],
|
| 362 |
+
"query": query,
|
| 363 |
+
"error": "No documents found"
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
# Extract images from retrieved results if available
|
| 367 |
+
retrieved_images = []
|
| 368 |
+
|
| 369 |
+
# Step 2: Generate
|
| 370 |
+
try:
|
| 371 |
+
answer = self.inferencer.generate_answer(
|
| 372 |
+
query=query,
|
| 373 |
+
retrieved_docs=retrieved_docs,
|
| 374 |
+
retrieved_images=retrieved_images,
|
| 375 |
+
max_new_tokens=max_new_tokens
|
| 376 |
+
)
|
| 377 |
+
except Exception as e:
|
| 378 |
+
answer = f"Error generating answer: {e}"
|
| 379 |
+
|
| 380 |
+
return {
|
| 381 |
+
"answer": answer,
|
| 382 |
+
"retrieved_docs": retrieved_docs,
|
| 383 |
+
"query": query,
|
| 384 |
+
"model": "Qwen2.5-VL-3B",
|
| 385 |
+
"doc_count": len(retrieved_docs)
|
| 386 |
+
}
|
| 387 |
+
|
| 388 |
+
def summarize_all_documents(self, max_chars: int = 100000) -> str:
|
| 389 |
+
"""Create summary of all indexed documents with token limits."""
|
| 390 |
+
collection_info = self.chroma_manager.get_collection_info()
|
| 391 |
+
doc_count = collection_info['document_count']
|
| 392 |
+
|
| 393 |
+
if doc_count == 0:
|
| 394 |
+
return "No documents in database to summarize."
|
| 395 |
+
|
| 396 |
+
# Retrieve documents
|
| 397 |
+
try:
|
| 398 |
+
all_docs = self.chroma_manager.collection.get(include=['documents'])
|
| 399 |
+
|
| 400 |
+
if not all_docs['documents']:
|
| 401 |
+
return "Could not retrieve documents for summarization."
|
| 402 |
+
|
| 403 |
+
# Combine first documents with char limit
|
| 404 |
+
combined_text = ""
|
| 405 |
+
for doc in all_docs['documents'][:10]: # Max 10 docs
|
| 406 |
+
if len(combined_text) + len(doc) < max_chars:
|
| 407 |
+
combined_text += doc + "\n\n"
|
| 408 |
+
else:
|
| 409 |
+
break
|
| 410 |
+
|
| 411 |
+
if not combined_text:
|
| 412 |
+
combined_text = all_docs['documents'][0][:max_chars]
|
| 413 |
+
|
| 414 |
+
summary = self.inferencer.summarize_document(combined_text)
|
| 415 |
+
return summary
|
| 416 |
+
except Exception as e:
|
| 417 |
+
return f"Error summarizing documents: {e}"
|