-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
303 lines (243 loc) · 10.6 KB
/
main.py
File metadata and controls
303 lines (243 loc) · 10.6 KB
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
#!/usr/bin/env python3
"""
Main Entry Point for Multimodal RAG System
Provides CLI interface and coordinates all system components
"""
import argparse
import logging
import sys
from pathlib import Path
import yaml
from typing import Dict, Any
# Import system components
from src.document_processor import DocumentProcessor
from src.image_processor import ImageProcessor
from src.audio_processor import AudioProcessor
from src.vector_store import VectorStore
from src.retrieval_engine import RetrievalEngine
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('multimodal_rag.log'),
logging.StreamHandler(sys.stdout)
]
)
logger = logging.getLogger(__name__)
class MultimodalRAGSystem:
"""Main system coordinator for multimodal RAG"""
def __init__(self, config_path: str = "config.yaml"):
"""Initialize the multimodal RAG system"""
self.config = self._load_config(config_path)
self._initialize_components()
def _load_config(self, config_path: str) -> Dict[str, Any]:
"""Load configuration from YAML file"""
try:
with open(config_path, 'r') as f:
config = yaml.safe_load(f)
logger.info(f"Loaded configuration from {config_path}")
return config
except FileNotFoundError:
logger.error(f"Configuration file not found: {config_path}")
sys.exit(1)
except yaml.YAMLError as e:
logger.error(f"Error parsing configuration file: {e}")
sys.exit(1)
def _initialize_components(self):
"""Initialize all system components"""
try:
logger.info("Initializing multimodal RAG system components...")
# Initialize vector store
self.vector_store = VectorStore(self.config)
logger.info("✓ Vector store initialized")
# Initialize processors
self.doc_processor = DocumentProcessor(self.config)
self.img_processor = ImageProcessor(self.config)
self.audio_processor = AudioProcessor(self.config)
logger.info("✓ Document, image, and audio processors initialized")
# Initialize retrieval engine
self.retrieval_engine = RetrievalEngine(self.config, self.vector_store)
logger.info("✓ Retrieval engine initialized")
logger.info("🚀 Multimodal RAG system ready!")
except Exception as e:
logger.error(f"Failed to initialize system: {e}")
sys.exit(1)
def process_files(self, directory_path: str, file_types: list = None):
"""Process all files in a directory"""
directory = Path(directory_path)
if not directory.exists():
logger.error(f"Directory not found: {directory_path}")
return
logger.info(f"Processing files in: {directory_path}")
total_chunks = 0
# Process documents
if not file_types or 'documents' in file_types:
try:
doc_chunks = self.doc_processor.batch_process(str(directory))
if doc_chunks:
self.vector_store.add_documents(doc_chunks)
total_chunks += len(doc_chunks)
logger.info(f"✓ Processed {len(doc_chunks)} document chunks")
except Exception as e:
logger.error(f"Document processing failed: {e}")
# Process images
if not file_types or 'images' in file_types:
try:
img_chunks = self.img_processor.batch_process(str(directory))
if img_chunks:
self.vector_store.add_documents(img_chunks)
total_chunks += len(img_chunks)
logger.info(f"✓ Processed {len(img_chunks)} image chunks")
except Exception as e:
logger.error(f"Image processing failed: {e}")
# Process audio
if not file_types or 'audio' in file_types:
try:
audio_chunks = self.audio_processor.batch_process(str(directory))
if audio_chunks:
self.vector_store.add_documents(audio_chunks)
total_chunks += len(audio_chunks)
logger.info(f"✓ Processed {len(audio_chunks)} audio chunks")
except Exception as e:
logger.error(f"Audio processing failed: {e}")
logger.info(f"🎉 Processing complete! Total chunks: {total_chunks}")
def search(self, query: str, search_type: str = "text", top_k: int = 5):
"""Perform search and display results"""
try:
logger.info(f"Searching: '{query}' (type: {search_type})")
results = self.retrieval_engine.search(
query=query,
search_type=search_type,
top_k=top_k
)
if not results:
print("No results found.")
return
print(f"\\n🔍 Found {len(results)} results:\\n")
for i, result in enumerate(results, 1):
metadata = result.get('metadata', {})
content = result.get('content', '')
print(f"[{i}] {metadata.get('file_name', 'Unknown')} "
f"(Score: {result['similarity_score']:.3f})")
print(f" Type: {metadata.get('modality', 'unknown')} | "
f"Format: {metadata.get('file_type', 'unknown')}")
# Show content preview
preview = content[:200] + "..." if len(content) > 200 else content
print(f" Content: {preview}")
print()
except Exception as e:
logger.error(f"Search failed: {e}")
def chat(self):
"""Start interactive chat session"""
print("\\n💬 Multimodal RAG Chat Interface")
print("Type your questions or 'quit' to exit.")
print("-" * 50)
while True:
try:
query = input("\\nYou: ").strip()
if query.lower() in ['quit', 'exit', 'q']:
print("Goodbye!")
break
if not query:
continue
print("\\nAssistant: ", end="")
# Generate response
response = self.retrieval_engine.generate_response(
query=query,
top_k=self.config['search']['top_k']
)
print(response)
except KeyboardInterrupt:
print("\\n\\nGoodbye!")
break
except Exception as e:
print(f"Error: {e}")
def get_system_stats(self):
"""Display system statistics"""
try:
stats = self.vector_store.get_statistics()
print("\\n📊 System Statistics:")
print("-" * 30)
print(f"Total Documents: {stats['total_documents']}")
print(f"Vector Store: {stats['store_type']}")
print(f"Collection: {stats['collection_name']}")
print(f"Embedding Dimension: {stats['embedding_dimension']}")
print("\\nBy Modality:")
for modality, count in stats['modality_distribution'].items():
print(f" • {modality}: {count}")
print("\\nBy File Type:")
for file_type, count in stats['file_type_distribution'].items():
print(f" • {file_type}: {count}")
print(f"\\nStorage Path: {stats['storage_path']}")
except Exception as e:
logger.error(f"Failed to get stats: {e}")
def main():
"""Main CLI entry point"""
parser = argparse.ArgumentParser(
description="Multimodal RAG System - Offline semantic search across documents, images, and audio"
)
parser.add_argument(
"--config",
default="config.yaml",
help="Path to configuration file"
)
subparsers = parser.add_subparsers(dest='command', help='Available commands')
# Process command
process_parser = subparsers.add_parser('process', help='Process files')
process_parser.add_argument('directory', help='Directory to process')
process_parser.add_argument(
'--types',
nargs='+',
choices=['documents', 'images', 'audio'],
help='File types to process (default: all)'
)
# Search command
search_parser = subparsers.add_parser('search', help='Search for content')
search_parser.add_argument('query', help='Search query')
search_parser.add_argument(
'--type',
default='text',
choices=['text', 'image', 'hybrid'],
help='Search type'
)
search_parser.add_argument(
'--top-k',
type=int,
default=5,
help='Number of results to return'
)
# Chat command
chat_parser = subparsers.add_parser('chat', help='Start interactive chat')
# Stats command
stats_parser = subparsers.add_parser('stats', help='Show system statistics')
# UI command
ui_parser = subparsers.add_parser('ui', help='Launch web interface')
args = parser.parse_args()
if not args.command:
parser.print_help()
return
# Initialize system
rag_system = MultimodalRAGSystem(args.config)
# Execute command
if args.command == 'process':
rag_system.process_files(args.directory, args.types)
elif args.command == 'search':
rag_system.search(args.query, args.type, args.top_k)
elif args.command == 'chat':
rag_system.chat()
elif args.command == 'stats':
rag_system.get_system_stats()
elif args.command == 'ui':
# Launch Streamlit UI
import subprocess
import sys
try:
subprocess.run([
sys.executable, "-m", "streamlit", "run",
"src/ui_interface.py", "--server.port", "8501"
])
except KeyboardInterrupt:
print("\\nShutting down web interface...")
if __name__ == "__main__":
main()