Spaces:
Sleeping
Sleeping
File size: 26,196 Bytes
1b4bf2d |
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 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 |
import gradio as gr
import os
import tempfile
import uuid
from datetime import datetime
from typing import List, Dict, Any, Optional
import json
import asyncio
from dataclasses import dataclass, asdict
import logging
# Document processing imports
import PyPDF2
import pandas as pd
from docx import Document
from pptx import Presentation
import markdown
# ML/AI imports
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from langchain.schema import Document as LCDocument
from huggingface_hub import InferenceClient
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# MCP Message Structure
@dataclass
class MCPMessage:
sender: str
receiver: str
type: str
trace_id: str
payload: Dict[str, Any]
timestamp: str = None
def __post_init__(self):
if self.timestamp is None:
self.timestamp = datetime.now().isoformat()
def to_dict(self):
return asdict(self)
# MCP Communication Layer
class MCPCommunicator:
def __init__(self):
self.message_queue = asyncio.Queue()
self.subscribers = {}
async def send_message(self, message: MCPMessage):
logger.info(f"MCP: {message.sender} -> {message.receiver}: {message.type}")
await self.message_queue.put(message)
async def receive_message(self, agent_name: str) -> MCPMessage:
while True:
message = await self.message_queue.get()
if message.receiver == agent_name:
return message
# Re-queue if not for this agent
await self.message_queue.put(message)
# Global MCP instance
mcp = MCPCommunicator()
# Base Agent Class
class BaseAgent:
def __init__(self, name: str):
self.name = name
self.mcp = mcp
async def send_mcp_message(self, receiver: str, msg_type: str, payload: Dict[str, Any], trace_id: str):
message = MCPMessage(
sender=self.name,
receiver=receiver,
type=msg_type,
trace_id=trace_id,
payload=payload
)
await self.mcp.send_message(message)
async def receive_mcp_message(self) -> MCPMessage:
return await self.mcp.receive_message(self.name)
# Document Ingestion Agent
class IngestionAgent(BaseAgent):
def __init__(self):
super().__init__("IngestionAgent")
self.text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
length_function=len,
)
def parse_pdf(self, file_path: str) -> str:
"""Parse PDF file and extract text"""
try:
with open(file_path, 'rb') as file:
pdf_reader = PyPDF2.PdfReader(file)
text = ""
for page in pdf_reader.pages:
text += page.extract_text() + "\n"
return text
except Exception as e:
logger.error(f"Error parsing PDF: {e}")
return ""
def parse_docx(self, file_path: str) -> str:
"""Parse DOCX file and extract text"""
try:
doc = Document(file_path)
text = ""
for paragraph in doc.paragraphs:
text += paragraph.text + "\n"
return text
except Exception as e:
logger.error(f"Error parsing DOCX: {e}")
return ""
def parse_pptx(self, file_path: str) -> str:
"""Parse PPTX file and extract text"""
try:
prs = Presentation(file_path)
text = ""
for slide_num, slide in enumerate(prs.slides, 1):
text += f"Slide {slide_num}:\n"
for shape in slide.shapes:
if hasattr(shape, "text"):
text += shape.text + "\n"
text += "\n"
return text
except Exception as e:
logger.error(f"Error parsing PPTX: {e}")
return ""
def parse_csv(self, file_path: str) -> str:
"""Parse CSV file and convert to text"""
try:
df = pd.read_csv(file_path)
return df.to_string()
except Exception as e:
logger.error(f"Error parsing CSV: {e}")
return ""
def parse_txt_md(self, file_path: str) -> str:
"""Parse TXT or MD file"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# If markdown, convert to plain text
if file_path.lower().endswith('.md'):
content = markdown.markdown(content)
return content
except Exception as e:
logger.error(f"Error parsing TXT/MD: {e}")
return ""
async def process_documents(self, files: List[str], trace_id: str) -> List[LCDocument]:
"""Process uploaded documents and return chunked documents"""
all_documents = []
for file_path in files:
file_ext = os.path.splitext(file_path)[1].lower()
filename = os.path.basename(file_path)
# Parse based on file extension
if file_ext == '.pdf':
content = self.parse_pdf(file_path)
elif file_ext == '.docx':
content = self.parse_docx(file_path)
elif file_ext == '.pptx':
content = self.parse_pptx(file_path)
elif file_ext == '.csv':
content = self.parse_csv(file_path)
elif file_ext in ['.txt', '.md']:
content = self.parse_txt_md(file_path)
else:
logger.warning(f"Unsupported file type: {file_ext}")
continue
if content.strip():
# Split content into chunks
chunks = self.text_splitter.split_text(content)
# Create LangChain documents
for i, chunk in enumerate(chunks):
doc = LCDocument(
page_content=chunk,
metadata={
"source": filename,
"chunk_id": i,
"file_type": file_ext
}
)
all_documents.append(doc)
return all_documents
# Retrieval Agent
class RetrievalAgent(BaseAgent):
def __init__(self):
super().__init__("RetrievalAgent")
self.embeddings = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2"
)
self.vector_store = None
async def create_vector_store(self, documents: List[LCDocument], trace_id: str):
"""Create vector store from documents"""
try:
if documents:
self.vector_store = FAISS.from_documents(documents, self.embeddings)
logger.info(f"Created vector store with {len(documents)} documents")
else:
logger.warning("No documents to create vector store")
except Exception as e:
logger.error(f"Error creating vector store: {e}")
async def retrieve_relevant_chunks(self, query: str, k: int = 5, trace_id: str = None) -> List[Dict]:
"""Retrieve relevant chunks for a query"""
if not self.vector_store:
return []
try:
# Similarity search
docs = self.vector_store.similarity_search(query, k=k)
# Format results
results = []
for doc in docs:
results.append({
"content": doc.page_content,
"source": doc.metadata.get("source", "Unknown"),
"chunk_id": doc.metadata.get("chunk_id", 0),
"file_type": doc.metadata.get("file_type", "Unknown")
})
return results
except Exception as e:
logger.error(f"Error retrieving chunks: {e}")
return []
# LLM Response Agent
class LLMResponseAgent(BaseAgent):
def __init__(self, hf_token: str = None):
super().__init__("LLMResponseAgent")
self.client = InferenceClient(
model="meta-llama/Llama-3.1-8B-Instruct",
token=hf_token
)
def format_prompt(self, query: str, context_chunks: List[Dict]) -> str:
"""Format prompt with context and query"""
context_text = "\n\n".join([
f"Source: {chunk['source']}\nContent: {chunk['content']}"
for chunk in context_chunks
])
prompt = f"""Based on the following context from uploaded documents, please answer the user's question.
Context:
{context_text}
Question: {query}
Please provide a comprehensive answer based on the context above. If the context doesn't contain enough information to fully answer the question, please mention what information is available and what might be missing.
Answer:"""
return prompt
async def generate_response(self, query: str, context_chunks: List[Dict], trace_id: str) -> str:
"""Generate response using LLM"""
try:
prompt = self.format_prompt(query, context_chunks)
# Generate response using HuggingFace Inference
response = self.client.text_generation(
prompt,
max_new_tokens=512,
temperature=0.7,
do_sample=True,
return_full_text=False
)
return response
except Exception as e:
logger.error(f"Error generating response: {e}")
return f"I apologize, but I encountered an error while generating the response: {str(e)}"
# Coordinator Agent
class CoordinatorAgent(BaseAgent):
def __init__(self, hf_token: str = None):
super().__init__("CoordinatorAgent")
self.ingestion_agent = IngestionAgent()
self.retrieval_agent = RetrievalAgent()
self.llm_agent = LLMResponseAgent(hf_token)
self.documents_processed = False
async def process_documents(self, files: List[str]) -> str:
"""Orchestrate document processing"""
trace_id = str(uuid.uuid4())
try:
# Step 1: Ingestion
await self.send_mcp_message(
"IngestionAgent",
"DOCUMENT_INGESTION_REQUEST",
{"files": files},
trace_id
)
documents = await self.ingestion_agent.process_documents(files, trace_id)
await self.send_mcp_message(
"RetrievalAgent",
"VECTOR_STORE_CREATE_REQUEST",
{"documents": len(documents)},
trace_id
)
# Step 2: Create vector store
await self.retrieval_agent.create_vector_store(documents, trace_id)
self.documents_processed = True
return f"Successfully processed {len(documents)} document chunks from {len(files)} files."
except Exception as e:
logger.error(f"Error in document processing: {e}")
return f"Error processing documents: {str(e)}"
async def answer_query(self, query: str) -> tuple[str, List[Dict]]:
"""Orchestrate query answering"""
if not self.documents_processed:
return "Please upload and process documents first.", []
trace_id = str(uuid.uuid4())
try:
# Step 1: Retrieval
await self.send_mcp_message(
"RetrievalAgent",
"RETRIEVAL_REQUEST",
{"query": query},
trace_id
)
context_chunks = await self.retrieval_agent.retrieve_relevant_chunks(query, k=5, trace_id=trace_id)
# Step 2: LLM Response
await self.send_mcp_message(
"LLMResponseAgent",
"LLM_GENERATION_REQUEST",
{"query": query, "context_chunks": len(context_chunks)},
trace_id
)
response = await self.llm_agent.generate_response(query, context_chunks, trace_id)
return response, context_chunks
except Exception as e:
logger.error(f"Error in query processing: {e}")
return f"Error processing query: {str(e)}", []
# Global coordinator instance
coordinator = None
def initialize_app(hf_token):
"""Initialize the application with HuggingFace token"""
global coordinator
coordinator = CoordinatorAgent(hf_token)
return "β
Application initialized successfully!"
async def process_files(files):
"""Process uploaded files"""
if not coordinator:
return "β Please set your HuggingFace token first!"
if not files:
return "β Please upload at least one file."
# Save uploaded files to temporary directory
file_paths = []
for file in files:
temp_path = os.path.join(tempfile.gettempdir(), file.name)
with open(temp_path, 'wb') as f:
f.write(file.read())
file_paths.append(temp_path)
result = await coordinator.process_documents(file_paths)
# Cleanup temporary files
for path in file_paths:
try:
os.remove(path)
except:
pass
return result
async def answer_question(query, history):
"""Answer user question"""
if not coordinator:
return "β Please set your HuggingFace token first!"
if not query.strip():
return "β Please enter a question."
response, context_chunks = await coordinator.answer_query(query)
# Format response with sources
if context_chunks:
sources = "\n\n**Sources:**\n"
for i, chunk in enumerate(context_chunks[:3], 1): # Show top 3 sources
sources += f"{i}. {chunk['source']} (Chunk {chunk['chunk_id']})\n"
response += sources
return response
# Custom CSS
custom_css = """
/* Main container styling */
.gradio-container {
max-width: 1200px !important;
margin: 0 auto !important;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
}
/* Header styling */
.header-container {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
color: white !important;
padding: 2rem !important;
border-radius: 15px !important;
margin-bottom: 2rem !important;
text-align: center !important;
box-shadow: 0 8px 32px rgba(0,0,0,0.1) !important;
}
.header-title {
font-size: 2.5rem !important;
font-weight: 700 !important;
margin-bottom: 0.5rem !important;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3) !important;
}
.header-subtitle {
font-size: 1.2rem !important;
opacity: 0.9 !important;
font-weight: 300 !important;
}
/* Tab styling */
.tab-nav {
background: white !important;
border-radius: 12px !important;
box-shadow: 0 4px 20px rgba(0,0,0,0.08) !important;
padding: 0.5rem !important;
margin-bottom: 1rem !important;
}
/* Card styling */
.setup-card, .upload-card, .chat-card {
background: white !important;
border-radius: 15px !important;
padding: 2rem !important;
box-shadow: 0 4px 20px rgba(0,0,0,0.08) !important;
border: 1px solid #e1e5e9 !important;
margin-bottom: 1.5rem !important;
}
/* Button styling */
.primary-button {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
color: white !important;
border: none !important;
border-radius: 10px !important;
padding: 0.75rem 2rem !important;
font-weight: 600 !important;
transition: all 0.3s ease !important;
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3) !important;
}
.primary-button:hover {
transform: translateY(-2px) !important;
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4) !important;
}
/* Chat interface styling */
.chat-container {
max-height: 600px !important;
overflow-y: auto !important;
background: #f8f9fa !important;
border-radius: 15px !important;
padding: 1rem !important;
border: 1px solid #e1e5e9 !important;
}
/* Input styling */
.input-container input, .input-container textarea {
border: 2px solid #e1e5e9 !important;
border-radius: 10px !important;
padding: 0.75rem 1rem !important;
font-size: 1rem !important;
transition: border-color 0.3s ease !important;
}
.input-container input:focus, .input-container textarea:focus {
border-color: #667eea !important;
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1) !important;
outline: none !important;
}
/* Status indicators */
.status-success {
color: #28a745 !important;
background: #d4edda !important;
padding: 0.75rem 1rem !important;
border-radius: 8px !important;
border: 1px solid #c3e6cb !important;
margin: 1rem 0 !important;
}
.status-error {
color: #dc3545 !important;
background: #f8d7da !important;
padding: 0.75rem 1rem !important;
border-radius: 8px !important;
border: 1px solid #f5c6cb !important;
margin: 1rem 0 !important;
}
/* File upload styling */
.file-upload {
border: 2px dashed #667eea !important;
border-radius: 15px !important;
padding: 2rem !important;
text-align: center !important;
background: #f8f9ff !important;
transition: all 0.3s ease !important;
}
.file-upload:hover {
border-color: #764ba2 !important;
background: #f0f4ff !important;
}
/* Architecture diagram container */
.architecture-container {
background: white !important;
border-radius: 15px !important;
padding: 2rem !important;
margin: 1rem 0 !important;
box-shadow: 0 4px 20px rgba(0,0,0,0.08) !important;
text-align: center !important;
}
/* Responsive design */
@media (max-width: 768px) {
.header-title {
font-size: 2rem !important;
}
.setup-card, .upload-card, .chat-card {
padding: 1.5rem !important;
}
}
/* Animation for loading states */
@keyframes pulse {
0% { opacity: 1; }
50% { opacity: 0.5; }
100% { opacity: 1; }
}
.loading {
animation: pulse 1.5s ease-in-out infinite !important;
}
"""
# Create Gradio Interface
def create_interface():
with gr.Blocks(css=custom_css, title="π€ Agentic RAG Chatbot") as demo:
gr.HTML("""
<div class="header-container">
<h1 class="header-title">π€ Agentic RAG Chatbot</h1>
<p class="header-subtitle">Multi-Format Document QA using Model Context Protocol (MCP)</p>
</div>
""")
with gr.Tabs() as tabs:
# Setup Tab
with gr.TabItem("βοΈ Setup", elem_classes=["tab-nav"]):
gr.HTML("""
<div class="setup-card">
<h3>π Configuration</h3>
<p>Enter your HuggingFace token to get started. This token is used to access the Llama-3.1-8B-Instruct model.</p>
</div>
""")
with gr.Row():
hf_token_input = gr.Textbox(
label="HuggingFace Token",
placeholder="hf_xxxxxxxxxxxxxxxxxxxxxxxxx",
type="password",
elem_classes=["input-container"]
)
with gr.Row():
init_button = gr.Button(
"Initialize Application",
variant="primary",
elem_classes=["primary-button"]
)
init_status = gr.Textbox(
label="Status",
interactive=False,
elem_classes=["input-container"]
)
# Upload Tab
with gr.TabItem("π Upload Documents", elem_classes=["tab-nav"]):
gr.HTML("""
<div class="upload-card">
<h3>π Document Upload</h3>
<p>Upload your documents in any supported format: PDF, DOCX, PPTX, CSV, TXT, or Markdown.</p>
</div>
""")
file_upload = gr.File(
label="Choose Files",
file_count="multiple",
file_types=[".pdf", ".docx", ".pptx", ".csv", ".txt", ".md"],
elem_classes=["file-upload"]
)
upload_button = gr.Button(
"Process Documents",
variant="primary",
elem_classes=["primary-button"]
)
upload_status = gr.Textbox(
label="Processing Status",
interactive=False,
elem_classes=["input-container"]
)
# Chat Tab
with gr.TabItem("π¬ Chat", elem_classes=["tab-nav"]):
gr.HTML("""
<div class="chat-card">
<h3>π¨οΈ Ask Questions</h3>
<p>Ask questions about your uploaded documents. The AI will provide answers based on the document content.</p>
</div>
""")
chatbot = gr.Chatbot(
label="Conversation",
height=400,
elem_classes=["chat-container"]
)
with gr.Row():
query_input = gr.Textbox(
label="Your Question",
placeholder="What are the key findings in the document?",
elem_classes=["input-container"]
)
ask_button = gr.Button(
"Ask",
variant="primary",
elem_classes=["primary-button"]
)
gr.Examples(
examples=[
"What are the main topics covered in the documents?",
"Can you summarize the key findings?",
"What are the important metrics mentioned?",
"What recommendations are provided?",
],
inputs=query_input,
label="Example Questions"
)
# Architecture Tab
with gr.TabItem("ποΈ Architecture", elem_classes=["tab-nav"]):
gr.HTML("""
<div class="architecture-container">
<h3>ποΈ System Architecture</h3>
<p>This system uses an agentic architecture with Model Context Protocol (MCP) for inter-agent communication.</p>
</div>
""")
gr.Markdown("""
## π Agent Flow Diagram
```
User Upload β CoordinatorAgent β IngestionAgent β RetrievalAgent β LLMResponseAgent
β β β β β
Documents MCP Messages Text Chunks Vector Store Final Response
```
## π€ Agent Descriptions
- **CoordinatorAgent**: Orchestrates the entire workflow and manages MCP communication
- **IngestionAgent**: Parses and preprocesses documents (PDF, DOCX, PPTX, CSV, TXT, MD)
- **RetrievalAgent**: Handles embeddings and semantic retrieval using FAISS
- **LLMResponseAgent**: Generates final responses using Llama-3.1-8B-Instruct
## π Tech Stack
- **Frontend**: Gradio with custom CSS
- **LLM**: Meta Llama-3.1-8B-Instruct (via HuggingFace Inference)
- **Embeddings**: sentence-transformers/all-MiniLM-L6-v2
- **Vector Store**: FAISS
- **Document Processing**: PyPDF2, python-docx, python-pptx, pandas
- **Framework**: LangChain for document handling
## π¨ MCP Message Example
```json
{
"sender": "RetrievalAgent",
"receiver": "LLMResponseAgent",
"type": "RETRIEVAL_RESULT",
"trace_id": "rag-457",
"payload": {
"retrieved_context": ["Revenue increased by 25%", "Q1 KPIs exceeded targets"],
"query": "What were the Q1 KPIs?"
},
"timestamp": "2025-07-21T10:30:00Z"
}
```
""")
# Event handlers
init_button.click(
fn=initialize_app,
inputs=[hf_token_input],
outputs=[init_status]
)
upload_button.click(
fn=process_files,
inputs=[file_upload],
outputs=[upload_status]
)
ask_button.click(
fn=answer_question,
inputs=[query_input, chatbot],
outputs=[chatbot]
)
query_input.submit(
fn=answer_question,
inputs=[query_input, chatbot],
outputs=[chatbot]
)
return demo
if __name__ == "__main__":
demo = create_interface()
demo.launch(
share=True,
server_name="0.0.0.0",
server_port=7860,
show_api=False
) |