File size: 10,545 Bytes
422c3d1
 
 
 
 
9515a7b
422c3d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9515a7b
 
422c3d1
 
 
 
 
 
 
 
9515a7b
422c3d1
 
 
 
9515a7b
422c3d1
9515a7b
422c3d1
 
 
9515a7b
422c3d1
 
 
 
 
 
 
 
 
9515a7b
422c3d1
 
 
 
 
 
 
 
 
9515a7b
422c3d1
 
 
 
 
 
9515a7b
422c3d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9515a7b
 
 
422c3d1
 
 
 
 
 
 
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
"""
DocMind - Gradio Chat Interface
Multi-agent research assistant for arXiv papers
"""

import gradio as gr
from retriever import PaperRetriever
from agents import DocMindOrchestrator
from fetch_arxiv_data import ArxivFetcher
import os


class DocMindApp:
    def __init__(self):
        self.retriever = None
        self.orchestrator = None
        self.setup_system()

    def setup_system(self):
        """Initialize retriever and load index"""
        print("Initializing DocMind...")

        # Initialize retriever
        self.retriever = PaperRetriever()

        # Try to load existing index
        if not self.retriever.load_index():
            print("No index found. Building new index...")
            fetcher = ArxivFetcher()
            papers = fetcher.load_papers("arxiv_papers.json")

            if papers:
                self.retriever.build_index(papers)
                self.retriever.save_index()
                print(f"Index built with {len(papers)} papers")
            else:
                print("⚠️ Warning: No papers found. Please run fetch_arxiv_data.py first")
                return

        # Initialize orchestrator
        self.orchestrator = DocMindOrchestrator(self.retriever)
        print("DocMind ready!")

    def chat(
            self,
            message: str,
            history: list,
            num_papers: int = 5,
            show_agent_logs: bool = True
    ) -> str:
        """
        Process chat message

        Args:
            message: User query
            history: Chat history (not used in current version)
            num_papers: Number of papers to include in response
            show_agent_logs: Whether to show agent processing logs

        Returns:
            Response string
        """
        if not self.orchestrator:
            return "⚠️ System not initialized. Please run fetch_arxiv_data.py to download papers first."

        if not message.strip():
            return "Please enter a question about research papers."

        try:
            # Process query through agent pipeline
            response = self.orchestrator.process_query(
                message,
                top_k=num_papers * 2,  # Retrieve more, filter to top N
                max_papers_in_response=num_papers
            )

            return response

        except Exception as e:
            return f"❌ Error processing query: {str(e)}\n\nPlease try rephrasing your question."


def create_interface():
    """Create Gradio chat interface"""

    app = DocMindApp()

    # Custom CSS for better styling
    css = """
    .gradio-container {
        font-family: 'Inter', 'Segoe UI', sans-serif;
        max-width: 1400px !important;
    }

    /* Header styling */
    h1 {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        background-clip: text;
        font-weight: 700;
        font-size: 2.5em !important;
        margin-bottom: 0.5em;
    }

    /* Chat area improvements */
    .message-wrap {
        padding: 1.2em !important;
        margin: 0.8em 0 !important;
        border-radius: 12px !important;
        line-height: 1.6;
    }

    /* User message */
    .message-wrap.user {
        background: linear-gradient(135deg, #667eea15 0%, #764ba215 100%) !important;
        border-left: 3px solid #667eea;
    }

    /* Bot message */
    .message-wrap.bot {
        background: #f8f9fa !important;
        border-left: 3px solid #28a745;
    }

    /* Input area */
    .input-text textarea {
        border-radius: 12px !important;
        border: 2px solid #e0e0e0 !important;
        font-size: 1.05em !important;
    }

    .input-text textarea:focus {
        border-color: #667eea !important;
        box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1) !important;
    }

    /* Buttons */
    .btn-primary {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
        border: none !important;
        border-radius: 10px !important;
        padding: 0.8em 2em !important;
        font-weight: 600 !important;
        transition: transform 0.2s !important;
    }

    .btn-primary:hover {
        transform: translateY(-2px) !important;
        box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4) !important;
    }

    /* Settings panel */
    .settings-panel {
        background: #f8f9fa;
        border-radius: 12px;
        padding: 1.5em;
    }

    /* Slider */
    input[type="range"] {
        accent-color: #667eea !important;
    }

    /* Example buttons */
    .examples button {
        border-radius: 8px !important;
        border: 2px solid #e0e0e0 !important;
        padding: 0.7em 1em !important;
        transition: all 0.2s !important;
    }

    .examples button:hover {
        border-color: #667eea !important;
        background: #667eea10 !important;
    }

    /* Code blocks in responses */
    code {
        background: #f4f4f4;
        padding: 0.2em 0.4em;
        border-radius: 4px;
        font-family: 'Courier New', monospace;
    }

    /* Remove footer */
    footer {
        display: none !important;
    }

    /* Improve markdown rendering */
    .markdown-body h2 {
        color: #667eea;
        border-bottom: 2px solid #667eea;
        padding-bottom: 0.3em;
        margin-top: 1.5em;
    }

    .markdown-body h3 {
        color: #764ba2;
        margin-top: 1.2em;
    }

    /* Better list styling */
    .markdown-body ul {
        line-height: 1.8;
    }

    .markdown-body li {
        margin: 0.5em 0;
    }
    """

    # Example queries
    examples = [
        "What are the latest methods for improving diffusion models?",
        "Summarize recent work on RLHF vs DPO for language model alignment",
        "What are the main challenges in scaling transformer models?",
        "Tell me about recent advances in vision transformers",
        "What's new in retrieval-augmented generation (RAG)?",
    ]

    with gr.Blocks(css=css, title="DocMind - arXiv Research Assistant", theme=gr.themes.Soft()) as demo:
        gr.Markdown(
            """
            # 🧠 DocMind: Multi-Agent Research Assistant

            Ask questions about recent AI/ML research papers from arXiv. DocMind uses a 4-agent pipeline to retrieve, read, critique, and synthesize answers.

            **Agent Pipeline:** πŸ” Retriever β†’ πŸ“– Reader β†’ πŸ”Ž Critic β†’ ✨ Synthesizer
            """
        )

        with gr.Row():
            with gr.Column(scale=7):
                chatbot = gr.Chatbot(
                    label="Research Chat",
                    height=550,
                    type="messages",
                    avatar_images=(None, "🧠"),
                    bubble_full_width=False
                )

                with gr.Row():
                    msg = gr.Textbox(
                        label="",
                        placeholder="Ask about recent research papers... (e.g., 'What are the latest methods for improving diffusion models?')",
                        lines=2,
                        scale=9,
                        show_label=False
                    )
                    submit = gr.Button("Send", variant="primary", scale=1, size="lg")

                with gr.Accordion("πŸ’‘ Example Questions", open=False):
                    gr.Examples(
                        examples=examples,
                        inputs=msg,
                        label=""
                    )

            with gr.Column(scale=3):
                with gr.Group():
                    gr.Markdown("### βš™οΈ Settings")

                    num_papers = gr.Slider(
                        minimum=1,
                        maximum=10,
                        value=5,
                        step=1,
                        label="Papers to Include",
                        info="More papers = more comprehensive, but slower"
                    )

                    show_logs = gr.Checkbox(
                        label="Show Agent Logs",
                        value=False,
                        info="Display processing steps"
                    )

                    clear = gr.Button("πŸ—‘οΈ Clear Chat", variant="secondary", size="sm")

                gr.Markdown(
                    """
                    ---
                    ### πŸ“Š About

                    **How it works:**
                    1. πŸ” **Retriever** finds relevant papers
                    2. πŸ“– **Reader** summarizes each paper  
                    3. πŸ”Ž **Critic** filters low-quality results
                    4. ✨ **Synthesizer** creates final answer

                    **Data Source:** arXiv papers (AI/ML/CS)

                    **Technology:** 
                    - FAISS for semantic search
                    - Sentence Transformers for embeddings
                    - 100 recent papers indexed
                    """
                )

        # Chat interaction
        def respond(message, history, num_papers_val, show_logs_val):
            if not message.strip():
                return history

            # Add user message
            history.append({"role": "user", "content": message})

            # Get bot response
            bot_response = app.chat(message, history, num_papers_val, show_logs_val)

            # Add bot message
            history.append({"role": "assistant", "content": bot_response})

            return history

        def clear_chat():
            return []

        # Event handlers
        submit.click(
            respond,
            inputs=[msg, chatbot, num_papers, show_logs],
            outputs=[chatbot]
        ).then(
            lambda: "",
            outputs=[msg]
        )

        msg.submit(
            respond,
            inputs=[msg, chatbot, num_papers, show_logs],
            outputs=[chatbot]
        ).then(
            lambda: "",
            outputs=[msg]
        )

        clear.click(clear_chat, outputs=[chatbot])

        gr.Markdown(
            """
            <div style='text-align: center; margin-top: 2em; padding: 1em; color: #666;'>
            <small>Built with FAISS, Sentence Transformers, and Gradio β€’ Powered by arXiv API</small>
            </div>
            """
        )

    return demo


if __name__ == "__main__":
    demo = create_interface()
    demo.launch(
        share=False,
        server_name="127.0.0.1",  # localhost instead of 0.0.0.0
        server_port=7860,
        show_error=True
    )