File size: 16,640 Bytes
3e1d0f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from config import logger, CUSTOM_CSS, ReasoningMode, AppConfig, ModelConfig
from core import AdvancedReasoner, PromptEngine

# Initialize system
reasoner = AdvancedReasoner()

def get_metrics_html() -> str:
    """Generate enhanced metrics HTML"""
    m = reasoner.metrics
    cache_stats = reasoner.cache.get_stats()
    status = '<span class="status-active">Active</span>' if m.tokens_used > 0 else 'Ready'
    
    return f"""<div class="metrics-card">
    <strong>Inference:</strong> {m.inference_time:.2f}s<br>
    <strong>Avg Time:</strong> {m.avg_response_time:.2f}s<br>
    <strong>Speed:</strong> {m.tokens_per_second:.1f} tok/s<br>
    <strong>Reasoning:</strong> {m.reasoning_depth} steps<br>
    <strong>Corrections:</strong> {m.self_corrections}<br>
    <strong>Confidence:</strong> {m.confidence_score:.1f}%<br>
    <strong>Total:</strong> {m.total_conversations}<br>
    <strong>Tokens:</strong> {m.tokens_used:,}<br>
    <strong>Peak:</strong> {m.peak_tokens}<br>
    <strong>Cache:</strong> {cache_stats['hit_rate']}% hit rate<br>
    <strong>Status:</strong> {status}<br>
    <strong>Session:</strong> {reasoner.session_id[:8]}...
    </div>"""

def get_empty_analytics_html() -> str:
    """Generate empty analytics HTML"""
    return """<div class="analytics-panel">
    <h3>No data yet</h3>
    <p>Start a conversation to see analytics!</p>
    </div>"""

def create_ui() -> gr.Blocks:
    """Create enhanced Gradio interface"""
    
    with gr.Blocks(
        theme=gr.themes.Soft(
            primary_hue=AppConfig.THEME_PRIMARY,
            secondary_hue=AppConfig.THEME_SECONDARY,
            font=gr.themes.GoogleFont("Inter")
        ),
        css=CUSTOM_CSS,
        title="Advanced AI Reasoning System Pro"
    ) as demo:
        
        gr.HTML("""
        <div class="research-header">
            <h1>Advanced AI Reasoning System Pro</h1>
            <p><strong>Enhanced Implementation:</strong> Tree of Thoughts + Constitutional AI + Multi-Agent Validation + Caching + Rate Limiting</p>
            <div style="margin-top: 1rem;">
                <span class="badge">Yao et al. 2023 - Tree of Thoughts</span>
                <span class="badge">Bai et al. 2022 - Constitutional AI</span>
                <span class="badge">Enhanced with 6 Reasoning Modes</span>
                <span class="badge">Performance Optimized</span>
            </div>
        </div>
        """)
        
        with gr.Tabs():
            # Main Chat Tab
            with gr.Tab("Reasoning Workspace"):
                with gr.Row():
                    with gr.Column(scale=3):
                        chatbot = gr.Chatbot(
                            label="Reasoning Workspace",
                            height=550,
                            show_copy_button=True,
                            type="messages",
                            avatar_images=(
                                "https://api.dicebear.com/7.x/avataaars/svg?seed=User",
                                "https://api.dicebear.com/7.x/bottts/svg?seed=AI"
                            )
                        )
                        
                        msg = gr.Textbox(
                            placeholder="Enter your complex problem or research question... (Max 10,000 characters)",
                            label="Query Input",
                            lines=3,
                            max_lines=10
                        )
                        
                        with gr.Row():
                            submit_btn = gr.Button("Process", variant="primary", scale=2)
                            clear_btn = gr.Button("Clear", scale=1)
                            pdf_btn = gr.Button("Download PDF", scale=1)
                    
                    with gr.Column(scale=1):
                        gr.Markdown("### Configuration")
                        
                        reasoning_mode = gr.Radio(
                            choices=[mode.value for mode in ReasoningMode],
                            value=ReasoningMode.TREE_OF_THOUGHTS.value,
                            label="Reasoning Method",
                            info="Select the reasoning strategy"
                        )
                        
                        prompt_template = gr.Dropdown(
                            choices=list(PromptEngine.TEMPLATES.keys()),
                            value="Custom",
                            label="Prompt Template",
                            info="Pre-built prompt templates"
                        )
                        
                        enable_critique = gr.Checkbox(
                            label="Enable Self-Critique",
                            value=True,
                            info="Add validation phase"
                        )
                        
                        use_cache = gr.Checkbox(
                            label="Use Cache",
                            value=True,
                            info="Cache responses for speed"
                        )
                        
                        model = gr.Dropdown(
                            choices=[m.model_id for m in ModelConfig],
                            value=ModelConfig.LLAMA_70B.model_id,
                            label="Model",
                            info="Select AI model"
                        )
                        
                        with gr.Accordion("Advanced Settings", open=False):
                            temperature = gr.Slider(
                                AppConfig.MIN_TEMPERATURE, 
                                AppConfig.MAX_TEMPERATURE, 
                                value=AppConfig.DEFAULT_TEMPERATURE, 
                                step=0.1,
                                label="Temperature",
                                info="Higher = more creative"
                            )
                            max_tokens = gr.Slider(
                                AppConfig.MIN_TOKENS, 
                                8000, 
                                value=AppConfig.DEFAULT_MAX_TOKENS, 
                                step=500,
                                label="Max Tokens",
                                info="Maximum response length"
                            )
                        
                        gr.Markdown("### Live Metrics")
                        metrics_display = gr.Markdown(value=get_metrics_html())
                        
                        with gr.Accordion("Info", open=False):
                            gr.Markdown(f"""
                            **Session ID:** `{reasoner.session_id}`  
                            **Cache Size:** {AppConfig.CACHE_SIZE}  
                            **Rate Limit:** {AppConfig.RATE_LIMIT_REQUESTS} req/{AppConfig.RATE_LIMIT_WINDOW}s  
                            **Max History:** {AppConfig.MAX_HISTORY_LENGTH} messages
                            """)
            
            # Export Tab
            with gr.Tab("Export & History"):
                gr.Markdown("### Export Conversation History")
                
                with gr.Row():
                    export_format = gr.Radio(
                        choices=["json", "markdown", "txt", "pdf"],
                        value="markdown",
                        label="Export Format"
                    )
                    include_meta = gr.Checkbox(
                        label="Include Metadata",
                        value=True
                    )
                
                export_btn = gr.Button("Export Now", variant="primary")
                export_output = gr.Code(label="Exported Data", language="markdown", lines=20)
                download_file = gr.File(label="Download File")
                
                gr.Markdown("---")
                gr.Markdown("### Search Conversations")
                
                with gr.Row():
                    search_input = gr.Textbox(
                        placeholder="Enter keyword to search...", 
                        scale=3,
                        label="Search Query"
                    )
                    search_btn = gr.Button("Search", scale=1)
                
                search_results = gr.Markdown("No results yet. Enter a keyword and click Search.")
                
                gr.Markdown("---")
                gr.Markdown("### Conversation History")
                history_stats = gr.Markdown("Loading...")
            
            # Analytics Tab
            with gr.Tab("Analytics & Insights"):
                refresh_btn = gr.Button("Refresh Analytics", variant="primary", size="lg")
                
                with gr.Row():
                    with gr.Column():
                        gr.Markdown("### Performance Metrics")
                        analytics_display = gr.Markdown(get_empty_analytics_html())
                    
                    with gr.Column():
                        gr.Markdown("### Cache Statistics")
                        cache_display = gr.Markdown("No cache data yet.")
                
                gr.Markdown("---")
                gr.Markdown("### Usage Distribution")
                
                with gr.Row():
                    model_dist = gr.Markdown("**Model Usage:** No data")
                    mode_dist = gr.Markdown("**Mode Usage:** No data")
            
            # Settings Tab
            with gr.Tab("Settings"):
                gr.Markdown("### Application Settings")
                
                gr.Markdown(f"""
                **Current Configuration:**
                
                | Setting | Value |
                |---------|-------|
                | Max History Length | {AppConfig.MAX_HISTORY_LENGTH} |
                | Max Conversation Storage | {AppConfig.MAX_CONVERSATION_STORAGE} |
                | Cache Size | {AppConfig.CACHE_SIZE} |
                | Cache TTL | {AppConfig.CACHE_TTL}s |
                | Rate Limit | {AppConfig.RATE_LIMIT_REQUESTS} requests per {AppConfig.RATE_LIMIT_WINDOW}s |
                | Request Timeout | {AppConfig.REQUEST_TIMEOUT}s |
                | Max Retries | {AppConfig.MAX_RETRIES} |
                | Export Directory | `{AppConfig.EXPORT_DIR}` |
                | Backup Directory | `{AppConfig.BACKUP_DIR}` |
                """)
                
                clear_cache_btn = gr.Button("Clear Cache", variant="stop")
                cache_status = gr.Markdown("")
        
        # Define pdf_file_output BEFORE event handlers
        pdf_file_output = gr.File(visible=False)
        
        # Event handlers
        def process_message(message, history, mode, critique, model_name, temp, tokens, template, cache):
            if not message.strip():
                return history, get_metrics_html()
            
            history = history or []
            mode_enum = ReasoningMode(mode)
            
            history.append({"role": "user", "content": message})
            yield history, get_metrics_html()
            
            history.append({"role": "assistant", "content": ""})
            
            for response in reasoner.generate_response(
                message, history[:-1], model_name, mode_enum, 
                critique, temp, tokens, template, cache
            ):
                history[-1]["content"] = response
                yield history, get_metrics_html()
        
        def reset_chat():
            reasoner.clear_history()
            return [], get_metrics_html()
        
        def export_conv(format_type, include_metadata):
            content, filename = reasoner.export_conversation(format_type, include_metadata)
            return content, filename
        
        def download_chat_pdf():
            """Download current chat as PDF"""
            pdf_file = reasoner.export_current_chat_pdf()
            if pdf_file:
                return pdf_file
            return None
        
        def search_conv(keyword):
            if not keyword.strip():
                return "Please enter a search keyword."
            
            results = reasoner.search_conversations(keyword)
            if not results:
                return f"No results found for '{keyword}'."
            
            output = f"### Found {len(results)} result(s) for '{keyword}'\n\n"
            for idx, entry in results[:10]:
                output += f"**{idx + 1}.** {entry.timestamp} | {entry.model}\n"
                output += f"**User:** {entry.user_message[:100]}...\n\n"
            
            if len(results) > 10:
                output += f"\n*Showing first 10 of {len(results)} results*"
            
            return output
        
        def refresh_analytics():
            analytics = reasoner.get_analytics()
            if not analytics:
                return get_empty_analytics_html(), "No cache data.", "No data", "No data"
            
            analytics_html = f"""<div class="analytics-panel">
            <h3>Session Analytics</h3>
            <p><strong>Session ID:</strong> {analytics['session_id']}</p>
            <p><strong>Total Conversations:</strong> {analytics['total_conversations']}</p>
            <p><strong>Total Tokens:</strong> {analytics['total_tokens']:,}</p>
            <p><strong>Total Time:</strong> {analytics['total_time']:.1f}s</p>
            <p><strong>Avg Time:</strong> {analytics['avg_inference_time']:.2f}s</p>
            <p><strong>Peak Tokens:</strong> {analytics['peak_tokens']}</p>
            <p><strong>Most Used Model:</strong> {analytics['most_used_model']}</p>
            <p><strong>Most Used Mode:</strong> {analytics['most_used_mode']}</p>
            <p><strong>Errors:</strong> {analytics['error_count']}</p>
            </div>"""
            
            cache_html = f"""**Cache Performance:**
            - Hits: {analytics['cache_hits']}
            - Misses: {analytics['cache_misses']}
            - Total: {analytics['cache_hits'] + analytics['cache_misses']}
            """
            
            model_dist_html = f"**Model Usage:** {analytics['most_used_model']}"
            mode_dist_html = f"**Mode Usage:** {analytics['most_used_mode']}"
            
            return analytics_html, cache_html, model_dist_html, mode_dist_html
        
        def update_history_stats():
            count = len(reasoner.conversation_history)
            if count == 0:
                return "No conversations yet."
            
            return f"""**Total Conversations:** {count}  
            **Session:** {reasoner.session_id[:8]}..."""
        
        def clear_cache_action():
            reasoner.cache.clear()
            return "Cache cleared successfully!"
        
        # Connect events
        submit_btn.click(
            process_message,
            [msg, chatbot, reasoning_mode, enable_critique, model, temperature, max_tokens, prompt_template, use_cache],
            [chatbot, metrics_display]
        ).then(lambda: "", None, msg)
        
        msg.submit(
            process_message,
            [msg, chatbot, reasoning_mode, enable_critique, model, temperature, max_tokens, prompt_template, use_cache],
            [chatbot, metrics_display]
        ).then(lambda: "", None, msg)
        
        clear_btn.click(reset_chat, None, [chatbot, metrics_display])
        
        # PDF Download button
        pdf_btn.click(download_chat_pdf, None, pdf_file_output)
        
        export_btn.click(export_conv, [export_format, include_meta], [export_output, download_file])
        search_btn.click(search_conv, search_input, search_results)
        refresh_btn.click(
            refresh_analytics, 
            None, 
            [analytics_display, cache_display, model_dist, mode_dist]
        )
        clear_cache_btn.click(clear_cache_action, None, cache_status)
        
        # Update history stats on load
        demo.load(update_history_stats, None, history_stats)
    
    return demo

if __name__ == "__main__":
    try:
        logger.info("="*60)
        logger.info("Starting Advanced AI Reasoning System Pro...")
        logger.info(f"Session ID: {reasoner.session_id}")
        logger.info("="*60)
        
        demo = create_ui()
        demo.launch(
            share=False,
            server_name="0.0.0.0",
            server_port=7860,
            show_error=True,
            show_api=False,
            favicon_path=None
        )
    except Exception as e:
        logger.critical(f"Failed to start application: {e}", exc_info=True)
        raise