Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,135 +1,702 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def __init__(self):
|
|
|
|
|
|
|
| 7 |
self.documents = {}
|
| 8 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
-
#
|
| 18 |
-
|
| 19 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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
- β
Source attribution
|
| 131 |
-
- β
Multi-format support
|
| 132 |
-
""")
|
| 133 |
-
|
| 134 |
-
# Launch
|
| 135 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
import shutil
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from typing import List, Dict, Any, Optional
|
| 7 |
+
import logging
|
| 8 |
+
import uuid
|
| 9 |
+
import json
|
| 10 |
+
from datetime import datetime
|
| 11 |
|
| 12 |
+
# Configure logging
|
| 13 |
+
logging.basicConfig(level=logging.INFO)
|
| 14 |
+
logger = logging.getLogger(__name__)
|
| 15 |
+
|
| 16 |
+
# Core AgenticRAG imports with fallbacks
|
| 17 |
+
try:
|
| 18 |
+
from smolagents import CodeAgent, GradioUI, HfApiModel, tool, Tool
|
| 19 |
+
from smolagents.tools import DuckDuckGoSearchTool
|
| 20 |
+
SMOLAGENTS_AVAILABLE = True
|
| 21 |
+
except ImportError:
|
| 22 |
+
logger.warning("smolagents not available - using fallback implementation")
|
| 23 |
+
SMOLAGENTS_AVAILABLE = False
|
| 24 |
+
|
| 25 |
+
# Enterprise RAG stack imports
|
| 26 |
+
try:
|
| 27 |
+
# Vector store and embeddings (MTEB leaderboard models)
|
| 28 |
+
from sentence_transformers import SentenceTransformer
|
| 29 |
+
import chromadb
|
| 30 |
+
from chromadb.config import Settings
|
| 31 |
+
|
| 32 |
+
# Document processing
|
| 33 |
+
from unstructured.partition.auto import partition
|
| 34 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 35 |
+
from langchain.docstore.document import Document
|
| 36 |
+
|
| 37 |
+
# Data processing
|
| 38 |
+
import pandas as pd
|
| 39 |
+
import numpy as np
|
| 40 |
+
|
| 41 |
+
# Web search and APIs
|
| 42 |
+
import requests
|
| 43 |
+
from duckduckgo_search import DDGS
|
| 44 |
+
|
| 45 |
+
ENTERPRISE_DEPS_AVAILABLE = True
|
| 46 |
+
logger.info("β
Enterprise dependencies loaded")
|
| 47 |
+
|
| 48 |
+
except ImportError as e:
|
| 49 |
+
ENTERPRISE_DEPS_AVAILABLE = False
|
| 50 |
+
logger.warning(f"Enterprise dependencies missing: {e}")
|
| 51 |
+
|
| 52 |
+
class EnterpriseDocumentRetriever(Tool):
|
| 53 |
+
"""
|
| 54 |
+
Enterprise-grade document retrieval tool using ChromaDB and MTEB models
|
| 55 |
+
Following AgenticRAG architecture patterns
|
| 56 |
+
"""
|
| 57 |
+
name = "document_retriever"
|
| 58 |
+
description = """
|
| 59 |
+
Retrieves relevant documents from the enterprise knowledge base using semantic similarity.
|
| 60 |
+
Uses state-of-the-art embeddings from MTEB leaderboard for high accuracy retrieval.
|
| 61 |
+
"""
|
| 62 |
+
inputs = {
|
| 63 |
+
"query": {
|
| 64 |
+
"type": "string",
|
| 65 |
+
"description": "The search query. Should be semantically close to target documents."
|
| 66 |
+
},
|
| 67 |
+
"max_results": {
|
| 68 |
+
"type": "integer",
|
| 69 |
+
"description": "Maximum number of documents to retrieve (default: 5)"
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
output_type = "string"
|
| 73 |
+
|
| 74 |
def __init__(self):
|
| 75 |
+
super().__init__()
|
| 76 |
+
self.setup_complete = False
|
| 77 |
self.documents = {}
|
| 78 |
+
self.collection = None
|
| 79 |
+
self.embedding_model = None
|
| 80 |
+
self.session_id = str(uuid.uuid4())
|
| 81 |
+
|
| 82 |
+
if ENTERPRISE_DEPS_AVAILABLE:
|
| 83 |
+
self._initialize_system()
|
| 84 |
|
| 85 |
+
def _initialize_system(self):
|
| 86 |
+
"""Initialize ChromaDB and MTEB embedding model"""
|
| 87 |
+
try:
|
| 88 |
+
# Initialize ChromaDB with persistence
|
| 89 |
+
self.chroma_client = chromadb.PersistentClient(
|
| 90 |
+
path="./enterprise_vectordb",
|
| 91 |
+
settings=Settings(
|
| 92 |
+
anonymized_telemetry=False,
|
| 93 |
+
allow_reset=True
|
| 94 |
+
)
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Create enterprise collection
|
| 98 |
+
self.collection = self.chroma_client.get_or_create_collection(
|
| 99 |
+
name="enterprise_documents",
|
| 100 |
+
metadata={"description": "Enterprise RAG knowledge base"}
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# Initialize MTEB leaderboard embedding model
|
| 104 |
+
embedding_models = [
|
| 105 |
+
"BAAI/bge-base-en-v1.5", # Top MTEB model
|
| 106 |
+
"sentence-transformers/all-MiniLM-L6-v2", # Fallback
|
| 107 |
+
"sentence-transformers/all-mpnet-base-v2" # Alternative
|
| 108 |
+
]
|
| 109 |
+
|
| 110 |
+
for model_name in embedding_models:
|
| 111 |
+
try:
|
| 112 |
+
self.embedding_model = SentenceTransformer(model_name)
|
| 113 |
+
logger.info(f"β
Loaded embedding model: {model_name}")
|
| 114 |
+
break
|
| 115 |
+
except Exception as e:
|
| 116 |
+
logger.warning(f"Failed to load {model_name}: {e}")
|
| 117 |
+
continue
|
| 118 |
+
|
| 119 |
+
if self.embedding_model:
|
| 120 |
+
self.setup_complete = True
|
| 121 |
+
logger.info("β
Enterprise retrieval system initialized")
|
| 122 |
+
else:
|
| 123 |
+
raise Exception("No embedding model could be loaded")
|
| 124 |
+
|
| 125 |
+
except Exception as e:
|
| 126 |
+
logger.error(f"β Failed to initialize retrieval system: {e}")
|
| 127 |
+
self.setup_complete = False
|
| 128 |
+
|
| 129 |
+
def add_documents(self, files: List[str]) -> Dict[str, Any]:
|
| 130 |
+
"""Process and add documents to vector store"""
|
| 131 |
+
if not self.setup_complete:
|
| 132 |
+
return {"success": False, "error": "System not initialized"}
|
| 133 |
|
| 134 |
+
results = {
|
| 135 |
+
"processed": 0,
|
| 136 |
+
"total_chunks": 0,
|
| 137 |
+
"errors": [],
|
| 138 |
+
"documents": []
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
for file_path in files:
|
| 142 |
try:
|
| 143 |
+
# Extract text using unstructured
|
| 144 |
+
elements = partition(filename=file_path)
|
| 145 |
+
text_content = "\n\n".join([str(element) for element in elements])
|
| 146 |
+
|
| 147 |
+
if len(text_content.strip()) < 100:
|
| 148 |
+
results["errors"].append(f"{Path(file_path).name}: No substantial content")
|
| 149 |
+
continue
|
| 150 |
+
|
| 151 |
+
# Advanced chunking
|
| 152 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 153 |
+
chunk_size=512,
|
| 154 |
+
chunk_overlap=50,
|
| 155 |
+
separators=["\n\n", "\n", ". ", " ", ""]
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
chunks = text_splitter.split_text(text_content)
|
| 159 |
+
|
| 160 |
+
if chunks:
|
| 161 |
+
# Generate embeddings
|
| 162 |
+
embeddings = self.embedding_model.encode(chunks).tolist()
|
| 163 |
+
|
| 164 |
+
# Prepare metadata
|
| 165 |
+
metadatas = []
|
| 166 |
+
ids = []
|
| 167 |
+
for i, chunk in enumerate(chunks):
|
| 168 |
+
chunk_id = f"{Path(file_path).name}_{i}_{uuid.uuid4().hex[:8]}"
|
| 169 |
+
ids.append(chunk_id)
|
| 170 |
+
metadatas.append({
|
| 171 |
+
"filename": Path(file_path).name,
|
| 172 |
+
"chunk_index": i,
|
| 173 |
+
"file_path": file_path,
|
| 174 |
+
"chunk_size": len(chunk),
|
| 175 |
+
"session_id": self.session_id,
|
| 176 |
+
"added_at": datetime.now().isoformat()
|
| 177 |
+
})
|
| 178 |
+
|
| 179 |
+
# Add to ChromaDB
|
| 180 |
+
self.collection.add(
|
| 181 |
+
documents=chunks,
|
| 182 |
+
embeddings=embeddings,
|
| 183 |
+
metadatas=metadatas,
|
| 184 |
+
ids=ids
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
results["processed"] += 1
|
| 188 |
+
results["total_chunks"] += len(chunks)
|
| 189 |
+
results["documents"].append({
|
| 190 |
+
"filename": Path(file_path).name,
|
| 191 |
+
"chunks": len(chunks),
|
| 192 |
+
"size": len(text_content)
|
| 193 |
+
})
|
| 194 |
+
|
| 195 |
+
logger.info(f"β
Processed {Path(file_path).name}: {len(chunks)} chunks")
|
| 196 |
+
|
| 197 |
except Exception as e:
|
| 198 |
+
results["errors"].append(f"{Path(file_path).name}: {str(e)}")
|
| 199 |
+
logger.error(f"Error processing {file_path}: {e}")
|
| 200 |
+
|
| 201 |
+
return results
|
| 202 |
+
|
| 203 |
+
def forward(self, query: str, max_results: int = 5) -> str:
|
| 204 |
+
"""Retrieve relevant documents using semantic search"""
|
| 205 |
+
if not self.setup_complete:
|
| 206 |
+
return "β Document retrieval system not available. Please check configuration."
|
| 207 |
+
|
| 208 |
+
try:
|
| 209 |
+
# Generate query embedding
|
| 210 |
+
query_embedding = self.embedding_model.encode([query]).tolist()[0]
|
| 211 |
+
|
| 212 |
+
# Search ChromaDB
|
| 213 |
+
results = self.collection.query(
|
| 214 |
+
query_embeddings=[query_embedding],
|
| 215 |
+
n_results=max_results,
|
| 216 |
+
include=["documents", "metadatas", "distances"]
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
if not results["documents"] or not results["documents"][0]:
|
| 220 |
+
return f"No relevant documents found for query: '{query}'"
|
| 221 |
+
|
| 222 |
+
# Format results
|
| 223 |
+
formatted_results = []
|
| 224 |
+
for i, (doc, metadata, distance) in enumerate(zip(
|
| 225 |
+
results["documents"][0],
|
| 226 |
+
results["metadatas"][0],
|
| 227 |
+
results["distances"][0]
|
| 228 |
+
)):
|
| 229 |
+
similarity = 1 - distance
|
| 230 |
+
if similarity > 0.3: # Similarity threshold
|
| 231 |
+
formatted_results.append({
|
| 232 |
+
"content": doc,
|
| 233 |
+
"filename": metadata.get("filename", "Unknown"),
|
| 234 |
+
"similarity": similarity,
|
| 235 |
+
"rank": i + 1
|
| 236 |
+
})
|
| 237 |
+
|
| 238 |
+
if not formatted_results:
|
| 239 |
+
return f"No sufficiently relevant documents found for query: '{query}'"
|
| 240 |
+
|
| 241 |
+
# Create response
|
| 242 |
+
response = f"π **Retrieved {len(formatted_results)} relevant documents for: '{query}'**\n\n"
|
| 243 |
+
|
| 244 |
+
for result in formatted_results:
|
| 245 |
+
content = result["content"]
|
| 246 |
+
if len(content) > 400:
|
| 247 |
+
content = content[:400] + "..."
|
| 248 |
+
|
| 249 |
+
response += f"**π {result['filename']}** (Similarity: {result['similarity']:.3f})\n"
|
| 250 |
+
response += f"{content}\n\n---\n\n"
|
| 251 |
+
|
| 252 |
+
return response
|
| 253 |
+
|
| 254 |
+
except Exception as e:
|
| 255 |
+
logger.error(f"Retrieval error: {e}")
|
| 256 |
+
return f"β Error during document retrieval: {str(e)}"
|
| 257 |
+
|
| 258 |
+
class EnterpriseWebSearchTool(Tool):
|
| 259 |
+
"""Advanced web search tool for current information"""
|
| 260 |
+
name = "web_search"
|
| 261 |
+
description = "Search the web for current information and recent developments"
|
| 262 |
+
inputs = {
|
| 263 |
+
"query": {
|
| 264 |
+
"type": "string",
|
| 265 |
+
"description": "The search query"
|
| 266 |
+
}
|
| 267 |
+
}
|
| 268 |
+
output_type = "string"
|
| 269 |
+
|
| 270 |
+
def forward(self, query: str) -> str:
|
| 271 |
+
try:
|
| 272 |
+
with DDGS() as ddgs:
|
| 273 |
+
results = list(ddgs.text(query, max_results=5))
|
| 274 |
+
|
| 275 |
+
if not results:
|
| 276 |
+
return f"No web search results found for: {query}"
|
| 277 |
+
|
| 278 |
+
response = f"π **Web search results for: '{query}'**\n\n"
|
| 279 |
+
|
| 280 |
+
for i, result in enumerate(results, 1):
|
| 281 |
+
title = result.get('title', 'No title')
|
| 282 |
+
snippet = result.get('body', 'No description')
|
| 283 |
+
url = result.get('href', 'No URL')
|
| 284 |
+
|
| 285 |
+
if len(snippet) > 200:
|
| 286 |
+
snippet = snippet[:200] + "..."
|
| 287 |
+
|
| 288 |
+
response += f"**{i}. {title}**\n"
|
| 289 |
+
response += f"{snippet}\n"
|
| 290 |
+
response += f"π {url}\n\n---\n\n"
|
| 291 |
+
|
| 292 |
+
return response
|
| 293 |
+
|
| 294 |
+
except Exception as e:
|
| 295 |
+
return f"β Web search error: {str(e)}"
|
| 296 |
+
|
| 297 |
+
class WeatherTool(Tool):
|
| 298 |
+
"""Weather information tool"""
|
| 299 |
+
name = "weather_info"
|
| 300 |
+
description = "Get current weather information for any location"
|
| 301 |
+
inputs = {
|
| 302 |
+
"location": {
|
| 303 |
+
"type": "string",
|
| 304 |
+
"description": "Location to get weather for"
|
| 305 |
+
}
|
| 306 |
+
}
|
| 307 |
+
output_type = "string"
|
| 308 |
+
|
| 309 |
+
def forward(self, location: str) -> str:
|
| 310 |
+
# Mock weather data for demo
|
| 311 |
+
return f"""
|
| 312 |
+
π€οΈ **Weather for {location}**
|
| 313 |
+
Temperature: 22Β°C (72Β°F)
|
| 314 |
+
Condition: Partly Cloudy
|
| 315 |
+
Humidity: 65%
|
| 316 |
+
Wind: 8 mph NW
|
| 317 |
+
|
| 318 |
+
*Note: This is demo weather data. Connect to a real weather API for production use.*
|
| 319 |
+
"""
|
| 320 |
+
|
| 321 |
+
class EnterpriseRAGAgent:
|
| 322 |
+
"""
|
| 323 |
+
Main Enterprise RAG Agent using AgenticRAG architecture
|
| 324 |
+
"""
|
| 325 |
+
|
| 326 |
+
def __init__(self):
|
| 327 |
+
self.document_retriever = EnterpriseDocumentRetriever()
|
| 328 |
+
self.web_search_tool = EnterpriseWebSearchTool()
|
| 329 |
+
self.weather_tool = WeatherTool()
|
| 330 |
|
| 331 |
+
# Initialize agent based on available dependencies
|
| 332 |
+
if SMOLAGENTS_AVAILABLE:
|
| 333 |
+
self._init_smolagents()
|
| 334 |
+
else:
|
| 335 |
+
self._init_fallback_agent()
|
| 336 |
|
| 337 |
+
def _init_smolagents(self):
|
| 338 |
+
"""Initialize with smolagents (preferred)"""
|
| 339 |
+
try:
|
| 340 |
+
# Use HfApiModel for best results (Facebook RAG, DataGemma models)
|
| 341 |
+
model = HfApiModel(
|
| 342 |
+
model_id="microsoft/DialoGPT-medium", # Fallback model
|
| 343 |
+
token=os.getenv("HF_TOKEN")
|
| 344 |
+
)
|
| 345 |
+
|
| 346 |
+
self.agent = CodeAgent(
|
| 347 |
+
model=model,
|
| 348 |
+
tools=[
|
| 349 |
+
self.document_retriever,
|
| 350 |
+
self.web_search_tool,
|
| 351 |
+
self.weather_tool
|
| 352 |
+
],
|
| 353 |
+
add_base_tools=True,
|
| 354 |
+
planning_interval=3 # Enable planning
|
| 355 |
+
)
|
| 356 |
+
|
| 357 |
+
self.agent_type = "smolagents"
|
| 358 |
+
logger.info("β
Initialized smolagents CodeAgent")
|
| 359 |
+
|
| 360 |
+
except Exception as e:
|
| 361 |
+
logger.error(f"Failed to initialize smolagents: {e}")
|
| 362 |
+
self._init_fallback_agent()
|
| 363 |
+
|
| 364 |
+
def _init_fallback_agent(self):
|
| 365 |
+
"""Fallback agent implementation"""
|
| 366 |
+
self.agent_type = "fallback"
|
| 367 |
+
logger.info("β
Initialized fallback agent")
|
| 368 |
+
|
| 369 |
+
def process_documents(self, files):
|
| 370 |
+
"""Process uploaded documents"""
|
| 371 |
+
if not files:
|
| 372 |
+
return "β No files provided for processing"
|
| 373 |
|
| 374 |
+
file_paths = [file.name for file in files]
|
| 375 |
+
results = self.document_retriever.add_documents(file_paths)
|
| 376 |
|
| 377 |
+
if results["processed"] == 0:
|
| 378 |
+
return f"β No documents were processed successfully.\nErrors: {results['errors']}"
|
|
|
|
| 379 |
|
| 380 |
+
response = f"""
|
| 381 |
+
β
**Document Processing Complete**
|
| 382 |
+
|
| 383 |
+
π **Results Summary:**
|
| 384 |
+
β’ **Processed:** {results['processed']} documents
|
| 385 |
+
β’ **Total chunks:** {results['total_chunks']} searchable segments
|
| 386 |
+
β’ **Processing method:** Unstructured + ChromaDB + MTEB embeddings
|
| 387 |
+
|
| 388 |
+
π **Processed Documents:**
|
| 389 |
+
"""
|
| 390 |
|
| 391 |
+
for doc in results["documents"]:
|
| 392 |
+
response += f"β’ **{doc['filename']}** - {doc['chunks']} chunks ({doc['size']:,} characters)\n"
|
| 393 |
|
| 394 |
+
if results["errors"]:
|
| 395 |
+
response += f"\nβ οΈ **Errors ({len(results['errors'])}):**\n"
|
| 396 |
+
for error in results["errors"][:3]:
|
| 397 |
+
response += f"β’ {error}\n"
|
| 398 |
|
| 399 |
return response
|
| 400 |
|
| 401 |
+
def query(self, message: str, history: List = None) -> str:
|
| 402 |
+
"""Process user query through the agent"""
|
| 403 |
+
if not message.strip():
|
| 404 |
+
return "Please provide a question or query."
|
| 405 |
+
|
| 406 |
+
try:
|
| 407 |
+
if self.agent_type == "smolagents":
|
| 408 |
+
# Use smolagents CodeAgent
|
| 409 |
+
enhanced_query = f"""
|
| 410 |
+
You are an enterprise AI assistant with access to multiple information sources.
|
| 411 |
+
|
| 412 |
+
User Query: {message}
|
| 413 |
+
|
| 414 |
+
Use your available tools strategically:
|
| 415 |
+
1. For questions about uploaded documents, use the document_retriever tool
|
| 416 |
+
2. For current events or recent information, use the web_search tool
|
| 417 |
+
3. For weather queries, use the weather_info tool
|
| 418 |
+
4. Combine multiple sources when appropriate
|
| 419 |
+
|
| 420 |
+
Provide comprehensive, well-sourced answers with citations.
|
| 421 |
+
"""
|
| 422 |
+
|
| 423 |
+
response = self.agent.run(enhanced_query)
|
| 424 |
+
return response
|
| 425 |
+
|
| 426 |
+
else:
|
| 427 |
+
# Fallback implementation
|
| 428 |
+
return self._fallback_query(message)
|
| 429 |
+
|
| 430 |
+
except Exception as e:
|
| 431 |
+
logger.error(f"Query processing error: {e}")
|
| 432 |
+
return f"β Error processing query: {str(e)}"
|
| 433 |
+
|
| 434 |
+
def _fallback_query(self, message: str) -> str:
|
| 435 |
+
"""Fallback query processing"""
|
| 436 |
+
# Simple routing logic
|
| 437 |
+
if any(word in message.lower() for word in ['document', 'file', 'upload', 'pdf']):
|
| 438 |
+
return self.document_retriever.forward(message)
|
| 439 |
+
elif any(word in message.lower() for word in ['weather', 'temperature', 'forecast']):
|
| 440 |
+
return self.weather_tool.forward("New York") # Default location
|
| 441 |
+
elif any(word in message.lower() for word in ['search', 'current', 'recent', 'news']):
|
| 442 |
+
return self.web_search_tool.forward(message)
|
| 443 |
+
else:
|
| 444 |
+
# Try document retrieval first
|
| 445 |
+
doc_results = self.document_retriever.forward(message)
|
| 446 |
+
if "No relevant documents" not in doc_results:
|
| 447 |
+
return doc_results
|
| 448 |
+
else:
|
| 449 |
+
return self.web_search_tool.forward(message)
|
| 450 |
+
|
| 451 |
+
def get_system_status(self) -> str:
|
| 452 |
+
"""Get comprehensive system status"""
|
| 453 |
+
try:
|
| 454 |
+
doc_count = self.document_retriever.collection.count() if self.document_retriever.collection else 0
|
| 455 |
+
except:
|
| 456 |
+
doc_count = 0
|
| 457 |
+
|
| 458 |
return f"""
|
| 459 |
+
π€ **Enterprise AgenticRAG System Status**
|
| 460 |
|
| 461 |
+
**Agent Type:** {self.agent_type.title()}
|
| 462 |
+
**Dependencies:** {"β
Full" if ENTERPRISE_DEPS_AVAILABLE else "β οΈ Limited"}
|
| 463 |
+
**Document Store:** {doc_count} chunks indexed
|
| 464 |
+
**Vector DB:** {"β
ChromaDB Active" if self.document_retriever.setup_complete else "β Not Available"}
|
| 465 |
+
**Embedding Model:** {"β
MTEB Model Loaded" if self.document_retriever.embedding_model else "β Not Available"}
|
| 466 |
+
|
| 467 |
+
**Available Tools:**
|
| 468 |
+
β’ π Document Retrieval (ChromaDB + MTEB)
|
| 469 |
+
β’ π Web Search (DuckDuckGo)
|
| 470 |
+
β’ π€οΈ Weather Information
|
| 471 |
+
β’ π§ Agentic Planning & Reasoning
|
| 472 |
+
|
| 473 |
+
**Enterprise Features:**
|
| 474 |
+
β’ Multi-format document processing
|
| 475 |
+
β’ Semantic similarity search
|
| 476 |
+
β’ Agent-based query routing
|
| 477 |
+
β’ Source attribution
|
| 478 |
+
β’ Real-time information access
|
| 479 |
"""
|
| 480 |
|
| 481 |
+
# Initialize the enterprise RAG system
|
| 482 |
+
enterprise_rag = EnterpriseRAGAgent()
|
| 483 |
+
|
| 484 |
+
def upload_and_process(files):
|
| 485 |
+
"""Handle document upload and processing"""
|
| 486 |
+
return enterprise_rag.process_documents(files)
|
| 487 |
+
|
| 488 |
+
def chat_with_agent(message, history):
|
| 489 |
+
"""Handle chat interactions"""
|
| 490 |
+
return enterprise_rag.query(message, history)
|
| 491 |
+
|
| 492 |
+
def get_status():
|
| 493 |
+
"""Get system status"""
|
| 494 |
+
return enterprise_rag.get_system_status()
|
| 495 |
+
|
| 496 |
+
# Create Gradio interface
|
| 497 |
+
def create_interface():
|
| 498 |
+
"""Create the enterprise Gradio interface"""
|
| 499 |
+
|
| 500 |
+
custom_css = """
|
| 501 |
+
.enterprise-header {
|
| 502 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 503 |
+
color: white;
|
| 504 |
+
padding: 2rem;
|
| 505 |
+
border-radius: 15px;
|
| 506 |
+
text-align: center;
|
| 507 |
+
margin-bottom: 2rem;
|
| 508 |
+
}
|
| 509 |
+
.status-panel {
|
| 510 |
+
background: #f8f9fa;
|
| 511 |
+
border: 2px solid #e9ecef;
|
| 512 |
+
border-radius: 10px;
|
| 513 |
+
padding: 1.5rem;
|
| 514 |
+
}
|
| 515 |
+
"""
|
| 516 |
+
|
| 517 |
+
with gr.Blocks(
|
| 518 |
+
title="Enterprise AgenticRAG System",
|
| 519 |
+
theme=gr.themes.Soft(),
|
| 520 |
+
css=custom_css
|
| 521 |
+
) as interface:
|
| 522 |
+
|
| 523 |
+
# Header
|
| 524 |
+
gr.HTML("""
|
| 525 |
+
<div class="enterprise-header">
|
| 526 |
+
<h1>π Enterprise AgenticRAG System</h1>
|
| 527 |
+
<p>Production-grade Retrieval-Augmented Generation with Agent Planning</p>
|
| 528 |
+
<p><strong>ChromaDB β’ MTEB Embeddings β’ Multi-Tool Reasoning β’ Real-time Search</strong></p>
|
| 529 |
+
</div>
|
| 530 |
+
""")
|
| 531 |
+
|
| 532 |
+
with gr.Row():
|
| 533 |
+
# Main content
|
| 534 |
+
with gr.Column(scale=3):
|
| 535 |
|
| 536 |
+
with gr.Tab("π Document Processing"):
|
| 537 |
+
gr.Markdown("""
|
| 538 |
+
### Enterprise Document Processing
|
| 539 |
+
**Advanced pipeline:** Unstructured extraction β Semantic chunking β ChromaDB indexing β MTEB embeddings
|
| 540 |
+
""")
|
| 541 |
+
|
| 542 |
+
file_upload = gr.File(
|
| 543 |
+
file_count="multiple",
|
| 544 |
+
file_types=[".pdf", ".docx", ".txt", ".md", ".html", ".json"],
|
| 545 |
+
label="Upload Enterprise Documents",
|
| 546 |
+
height=150
|
| 547 |
+
)
|
| 548 |
+
|
| 549 |
+
process_btn = gr.Button("βοΈ Process Documents", variant="primary", size="lg")
|
| 550 |
+
processing_results = gr.Markdown(label="Processing Results")
|
| 551 |
+
|
| 552 |
+
process_btn.click(
|
| 553 |
+
fn=upload_and_process,
|
| 554 |
+
inputs=[file_upload],
|
| 555 |
+
outputs=[processing_results]
|
| 556 |
+
)
|
| 557 |
+
|
| 558 |
+
with gr.Tab("π€ Agentic Chat"):
|
| 559 |
+
gr.Markdown("""
|
| 560 |
+
### Chat with Enterprise Agent
|
| 561 |
+
**Intelligent routing:** Document retrieval β’ Web search β’ Multi-step reasoning β’ Source attribution
|
| 562 |
+
""")
|
| 563 |
+
|
| 564 |
+
if SMOLAGENTS_AVAILABLE and enterprise_rag.agent_type == "smolagents":
|
| 565 |
+
# Use GradioUI for smolagents
|
| 566 |
+
try:
|
| 567 |
+
gradio_ui = GradioUI(enterprise_rag.agent)
|
| 568 |
+
gradio_ui.render()
|
| 569 |
+
except:
|
| 570 |
+
# Fallback to ChatInterface
|
| 571 |
+
gr.ChatInterface(
|
| 572 |
+
fn=chat_with_agent,
|
| 573 |
+
title="Enterprise Agent Chat",
|
| 574 |
+
examples=[
|
| 575 |
+
"What information do you have about Jimmy?",
|
| 576 |
+
"Search for recent AI developments",
|
| 577 |
+
"Analyze the uploaded documents",
|
| 578 |
+
"What's the weather in London?",
|
| 579 |
+
"Compare information across multiple sources"
|
| 580 |
+
]
|
| 581 |
+
)
|
| 582 |
+
else:
|
| 583 |
+
# Fallback ChatInterface
|
| 584 |
+
gr.ChatInterface(
|
| 585 |
+
fn=chat_with_agent,
|
| 586 |
+
title="Enterprise Agent Chat",
|
| 587 |
+
examples=[
|
| 588 |
+
"What information do you have about Jimmy?",
|
| 589 |
+
"Search for recent AI developments",
|
| 590 |
+
"Analyze the uploaded documents",
|
| 591 |
+
"What's the weather in London?",
|
| 592 |
+
"Compare information across multiple sources"
|
| 593 |
+
]
|
| 594 |
+
)
|
| 595 |
+
|
| 596 |
+
with gr.Tab("π API Integration"):
|
| 597 |
+
gr.Markdown("""
|
| 598 |
+
### Enterprise API Access
|
| 599 |
+
|
| 600 |
+
**REST Endpoint:** `/api/v1/query`
|
| 601 |
+
|
| 602 |
+
**Request:**
|
| 603 |
+
```json
|
| 604 |
+
{
|
| 605 |
+
"query": "Your question here",
|
| 606 |
+
"max_results": 5,
|
| 607 |
+
"use_web_search": true
|
| 608 |
+
}
|
| 609 |
+
```
|
| 610 |
+
|
| 611 |
+
**Response:**
|
| 612 |
+
```json
|
| 613 |
+
{
|
| 614 |
+
"answer": "Agent response",
|
| 615 |
+
"sources": [{"type": "document", "filename": "doc.pdf"}],
|
| 616 |
+
"processing_time": 1.23,
|
| 617 |
+
"agent_steps": ["retrieve", "analyze", "synthesize"]
|
| 618 |
+
}
|
| 619 |
+
```
|
| 620 |
+
|
| 621 |
+
**Authentication:** Set `ENTERPRISE_API_KEY` environment variable
|
| 622 |
+
""")
|
| 623 |
|
| 624 |
+
# Sidebar
|
| 625 |
+
with gr.Column(scale=1):
|
| 626 |
+
|
| 627 |
+
with gr.Group():
|
| 628 |
+
gr.Markdown("### π System Status")
|
| 629 |
+
status_display = gr.Markdown(
|
| 630 |
+
value=get_status(),
|
| 631 |
+
elem_classes=["status-panel"]
|
| 632 |
+
)
|
| 633 |
+
refresh_btn = gr.Button("π Refresh Status", size="sm")
|
| 634 |
+
refresh_btn.click(fn=get_status, outputs=[status_display])
|
| 635 |
+
|
| 636 |
+
with gr.Group():
|
| 637 |
+
gr.Markdown("""
|
| 638 |
+
### π― Enterprise Architecture
|
| 639 |
+
|
| 640 |
+
**Agent Framework:**
|
| 641 |
+
β’ smolagents CodeAgent
|
| 642 |
+
β’ Multi-tool orchestration
|
| 643 |
+
β’ Planning & reasoning
|
| 644 |
+
|
| 645 |
+
**Vector Database:**
|
| 646 |
+
β’ ChromaDB persistence
|
| 647 |
+
β’ MTEB embeddings
|
| 648 |
+
β’ Semantic similarity
|
| 649 |
+
|
| 650 |
+
**Document Processing:**
|
| 651 |
+
β’ Unstructured extraction
|
| 652 |
+
β’ Intelligent chunking
|
| 653 |
+
β’ Multi-format support
|
| 654 |
+
|
| 655 |
+
**Real-time Data:**
|
| 656 |
+
β’ Web search integration
|
| 657 |
+
β’ Current information
|
| 658 |
+
β’ Source attribution
|
| 659 |
+
""")
|
| 660 |
+
|
| 661 |
+
with gr.Group():
|
| 662 |
+
gr.Markdown("""
|
| 663 |
+
### π‘ Usage Guide
|
| 664 |
+
|
| 665 |
+
**1. Upload Documents**
|
| 666 |
+
β’ PDF, DOCX, TXT, HTML, JSON
|
| 667 |
+
β’ Automatic text extraction
|
| 668 |
+
β’ Semantic indexing
|
| 669 |
+
|
| 670 |
+
**2. Ask Questions**
|
| 671 |
+
β’ Natural language queries
|
| 672 |
+
β’ Multi-source answers
|
| 673 |
+
β’ Cited responses
|
| 674 |
+
|
| 675 |
+
**3. Agent Features**
|
| 676 |
+
β’ Intelligent tool selection
|
| 677 |
+
β’ Multi-step reasoning
|
| 678 |
+
β’ Context awareness
|
| 679 |
+
β’ Source verification
|
| 680 |
+
""")
|
| 681 |
|
| 682 |
+
# Footer
|
| 683 |
+
gr.HTML("""
|
| 684 |
+
<div style="text-align: center; margin-top: 2rem; padding: 1.5rem; background: #f1f3f4; border-radius: 10px;">
|
| 685 |
+
<p><strong>Enterprise AgenticRAG System</strong> β’ Built on Hugging Face Enterprise Stack</p>
|
| 686 |
+
<p>π’ smolagents β’ ποΈ ChromaDB β’ π§ MTEB Embeddings β’ π Multi-source Intelligence</p>
|
| 687 |
+
</div>
|
| 688 |
+
""")
|
| 689 |
+
|
| 690 |
+
return interface
|
| 691 |
+
|
| 692 |
+
# Launch the application
|
| 693 |
+
if __name__ == "__main__":
|
| 694 |
+
demo = create_interface()
|
| 695 |
+
demo.queue(max_size=20)
|
| 696 |
+
demo.launch(
|
| 697 |
+
share=False,
|
| 698 |
+
server_name="0.0.0.0",
|
| 699 |
+
server_port=7860,
|
| 700 |
+
show_error=True,
|
| 701 |
+
show_api=True
|
| 702 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|