File size: 8,666 Bytes
3e772ec
8ba2581
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e772ec
 
8ba2581
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e772ec
8ba2581
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import asyncio
from pathlib import Path
import tempfile
import json
from typing import List, Dict, Any
import logging

from config import Config
from mcp_server import mcp
# Handle imports based on how the app is run
try:
    from mcp_server import mcp
    MCP_AVAILABLE = True
except ImportError:
    MCP_AVAILABLE = False
    print("⚠️ MCP server not available, running in standalone mode")

import mcp_tools

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Validate configuration on startup
try:
    Config.validate()
except ValueError as e:
    logger.error(f"Configuration error: {e}")
    print(f"⚠️ Configuration error: {e}")
    print("Please set the required API keys in your environment variables or .env file")

# Global state for search results
current_results = []

async def process_file_handler(file):
    """Handle file upload and processing"""
    if file is None:
        return "Please upload a file", "", "", None
    
    try:
        # Process the file
        result = await mcp_tools.process_local_file(file.name)
        
        if result.get("success"):
            tags_display = ", ".join(result["tags"])
            return (
                f"βœ… Successfully processed: {result['file_name']}",
                result["summary"],
                tags_display,
                gr.update(visible=True, value=create_result_card(result))
            )
        else:
            return f"❌ Error: {result.get('error', 'Unknown error')}", "", "", None
            
    except Exception as e:
        logger.error(f"Error in file handler: {str(e)}")
        return f"❌ Error: {str(e)}", "", "", None

async def process_url_handler(url):
    """Handle URL processing"""
    if not url:
        return "Please enter a URL", "", "", None
    
    try:
        # Process the URL
        result = await mcp_tools.process_web_content(url)
        
        if result.get("success"):
            tags_display = ", ".join(result["tags"])
            return (
                f"βœ… Successfully processed: {url}",
                result["summary"],
                tags_display,
                gr.update(visible=True, value=create_result_card(result))
            )
        else:
            return f"❌ Error: {result.get('error', 'Unknown error')}", "", "", None
            
    except Exception as e:
        logger.error(f"Error in URL handler: {str(e)}")
        return f"❌ Error: {str(e)}", "", "", None

async def search_handler(query):
    """Handle semantic search"""
    if not query:
        return [], "Please enter a search query"
    
    try:
        # Perform search
        results = await mcp_tools.search_knowledge_base(query, limit=10)
        
        if results:
            # Create display cards for each result
            result_cards = []
            for result in results:
                card = f"""
                ### πŸ“„ {result.get('source', 'Unknown Source')}
                **Tags:** {', '.join(result.get('tags', []))}
                
                **Summary:** {result.get('summary', 'No summary available')}
                
                **Relevance:** {result.get('relevance_score', 0):.2%}
                
                ---
                """
                result_cards.append(card)
            
            global current_results
            current_results = results
            
            return result_cards, f"Found {len(results)} results"
        else:
            return [], "No results found"
            
    except Exception as e:
        logger.error(f"Error in search: {str(e)}")
        return [], f"Error: {str(e)}"

def create_result_card(result: Dict[str, Any]) -> str:
    """Create a formatted result card"""
    return f"""
    ### πŸ“‹ Processing Complete
    
    **Document ID:** {result.get('doc_id', 'N/A')}
    
    **Source:** {result.get('file_name', result.get('url', 'Unknown'))}
    
    **Tags:** {', '.join(result.get('tags', []))}
    
    **Summary:** {result.get('summary', 'No summary available')}
    
    **Chunks Processed:** {result.get('chunks_processed', 0)}
    """

# Create Gradio interface
with gr.Blocks(title="Intelligent Content Organizer - MCP Agent") as demo:
    gr.Markdown("""
    # 🧠 Intelligent Content Organizer
    ### MCP-Powered Knowledge Management System
    
    This AI-driven system automatically organizes, enriches, and retrieves your digital content.
    Upload files or provide URLs to build your personal knowledge base with automatic tagging and semantic search.
    
    ---
    """)
    
    with gr.Tabs():
        # File Processing Tab
        with gr.TabItem("πŸ“ Process Files"):
            with gr.Row():
                with gr.Column():
                    file_input = gr.File(
                        label="Upload Document",
                        file_types=[".pdf", ".txt", ".docx", ".doc", ".html", ".md", ".csv", ".json"]
                    )
                    file_process_btn = gr.Button("Process File", variant="primary")
                
                with gr.Column():
                    file_status = gr.Textbox(label="Status", lines=1)
                    file_summary = gr.Textbox(label="Generated Summary", lines=3)
                    file_tags = gr.Textbox(label="Generated Tags", lines=1)
            
            file_result = gr.Markdown(visible=False)
        
        # URL Processing Tab
        with gr.TabItem("🌐 Process URLs"):
            with gr.Row():
                with gr.Column():
                    url_input = gr.Textbox(
                        label="Enter URL",
                        placeholder="https://example.com/article"
                    )
                    url_process_btn = gr.Button("Process URL", variant="primary")
                
                with gr.Column():
                    url_status = gr.Textbox(label="Status", lines=1)
                    url_summary = gr.Textbox(label="Generated Summary", lines=3)
                    url_tags = gr.Textbox(label="Generated Tags", lines=1)
            
            url_result = gr.Markdown(visible=False)
        
        # Search Tab
        with gr.TabItem("πŸ” Semantic Search"):
            search_input = gr.Textbox(
                label="Search Query",
                placeholder="Enter your search query...",
                lines=1
            )
            search_btn = gr.Button("Search", variant="primary")
            search_status = gr.Textbox(label="Status", lines=1)
            
            search_results = gr.Markdown(label="Search Results")
        
        # MCP Server Info Tab
        with gr.TabItem("ℹ️ MCP Server Info"):
            gr.Markdown("""
            ### MCP Server Configuration
            
            This Gradio app also functions as an MCP (Model Context Protocol) server, allowing integration with:
            - Claude Desktop
            - Cursor
            - Other MCP-compatible clients
            
            **Server Name:** intelligent-content-organizer
            
            **Available Tools:**
            - `process_file`: Process local files and extract content
            - `process_url`: Fetch and process web content
            - `semantic_search`: Search across stored documents
            - `get_document_summary`: Get detailed document information
            
            **To use as MCP server:**
            1. Add this server to your MCP client configuration
            2. Use the tools listed above to interact with your knowledge base
            3. All processed content is automatically indexed for semantic search
            
            **Tags:** mcp-server-track
            """)
    
    # Event handlers
    file_process_btn.click(
        fn=lambda x: asyncio.run(process_file_handler(x)),
        inputs=[file_input],
        outputs=[file_status, file_summary, file_tags, file_result]
    )
    
    url_process_btn.click(
        fn=lambda x: asyncio.run(process_url_handler(x)),
        inputs=[url_input],
        outputs=[url_status, url_summary, url_tags, url_result]
    )
    
    search_btn.click(
        fn=lambda x: asyncio.run(search_handler(x)),
        inputs=[search_input],
        outputs=[search_results, search_status]
    )

# Launch configuration
if __name__ == "__main__":
    # Check if running as MCP server
    import sys
    if "--mcp" in sys.argv:
        # Run as MCP server
        import asyncio
        asyncio.run(mcp.run())
    else:
        # Run as Gradio app
        demo.launch(
            server_name="0.0.0.0",
            share=False,
            show_error=True
        )