File size: 18,150 Bytes
422e708
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8a714e
 
422e708
d8a714e
 
 
 
422e708
 
 
d8a714e
 
422e708
 
 
 
 
d8a714e
422e708
 
 
d8a714e
 
 
 
 
422e708
 
 
 
 
 
 
 
d8a714e
422e708
 
 
d8a714e
 
 
 
 
422e708
 
 
 
 
 
 
 
 
d8a714e
 
 
 
422e708
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8a714e
 
 
422e708
 
 
 
 
 
d8a714e
 
422e708
 
d8a714e
422e708
 
 
 
d8a714e
422e708
 
 
 
d8a714e
422e708
 
d8a714e
422e708
 
 
 
 
 
 
d8a714e
422e708
 
 
 
 
 
 
d8a714e
422e708
 
 
 
 
 
 
d8a714e
422e708
 
 
 
 
 
 
d8a714e
422e708
 
 
 
 
 
 
d8a714e
422e708
 
 
 
 
 
 
d8a714e
422e708
 
 
 
 
 
 
d8a714e
422e708
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
from pymongo import MongoClient
from typing import List, Dict, Any, Optional, Tuple
from datetime import datetime
import json
import re

class ConversationAnalysisUI:
    """Gradio UI for displaying conversation analysis results."""
    
    def __init__(self):
        # Use keshavchhaparia MongoDB instance (same as RAG system)
        self.mongodb_uri = "mongodb+srv://keshavchhaparia:bUSBXeVCGWDyQhDG@saaslabs.awtivxf.mongodb.net/"
        self.database_name = "second_brain_course"
        self.collection_name = "test_intercom_data"
        
        self.setup_mongodb()
        self.setup_ui()
    
    def setup_mongodb(self):
        """Initialize MongoDB connection."""
        try:
            self.client = MongoClient(self.mongodb_uri)
            self.db = self.client[self.database_name]
            self.collection = self.db[self.collection_name]
            print(f"βœ… Connected to MongoDB: {self.database_name}.{self.collection_name}")
        except Exception as e:
            print(f"❌ MongoDB connection failed: {e}")
            raise
    
    def load_conversations(self, 
                          quality_min: float = 0.0, 
                          quality_max: float = 1.0,
                          sentiment: str = "All",
                          search_text: str = "",
                          limit: int = 100) -> pd.DataFrame:
        """Load and filter conversations."""
        try:
            # Build query
            query = {
                'conversation_analysis': {'$exists': True, '$ne': None},
                'content_quality_score': {'$gte': quality_min, '$lte': quality_max}
            }
            
            # Add sentiment filter
            if sentiment != "All":
                query['conversation_analysis.aggregated_marketing_insights.quotes.sentiment'] = sentiment
            
            # Add text search
            if search_text:
                query['$or'] = [
                    {'content': {'$regex': search_text, '$options': 'i'}},
                    {'conversation_analysis.aggregated_contextual_summary': {'$regex': search_text, '$options': 'i'}}
                ]
            
            # Fetch documents
            docs = list(self.collection.find(query).limit(limit))
            
            # Convert to DataFrame
            data = []
            seen_conversation_ids = set()
            
            for doc in docs:
                conversation_id = doc.get('metadata', {}).get('properties', {}).get('conversation_id', 'N/A')
                
                # Skip duplicates
                if conversation_id in seen_conversation_ids:
                    continue
                seen_conversation_ids.add(conversation_id)
                
                analysis = doc.get('conversation_analysis', {})
                insights = analysis.get('aggregated_marketing_insights', {})
                quotes = insights.get('quotes', [])
                
                # Extract primary sentiment
                primary_sentiment = quotes[0].get('sentiment', 'Unknown') if quotes else 'Unknown'
                
                # Format date
                created_at = analysis.get('created_at', '')
                if isinstance(created_at, str):
                    try:
                        # Parse and format date
                        dt = datetime.fromisoformat(created_at.replace('Z', '+00:00'))
                        formatted_date = dt.strftime('%b %d, %Y %H:%M')
                    except:
                        formatted_date = created_at
                elif hasattr(created_at, 'strftime'):
                    formatted_date = created_at.strftime('%b %d, %Y %H:%M')
                else:
                    formatted_date = str(created_at)
                
                # Get full summary without truncation
                full_summary = analysis.get('aggregated_contextual_summary', 'No summary available')
                
                # Get a simple insights summary for the table
                marketing_insights = analysis.get('aggregated_marketing_insights', {})
                insights_count = 0
                
                if isinstance(marketing_insights, dict):
                    quotes_count = len(marketing_insights.get('quotes', []))
                    findings_count = len(marketing_insights.get('key_findings', []))
                    insights_count = quotes_count + findings_count
                
                insights_text = f"{insights_count} insights available" if insights_count > 0 else "No insights available"
                
                data.append({
                    'conversation_id': conversation_id,
                    'quality_score': round(doc.get('content_quality_score', 0.0), 2),
                    'sentiment': primary_sentiment,
                    'summary': full_summary,
                    'insights': insights_text,
                    'date': formatted_date
                })
            
            return pd.DataFrame(data)
            
        except Exception as e:
            print(f"❌ Error loading conversations: {e}")
            return pd.DataFrame()
    
    def get_conversation_details(self, conversation_id: str) -> str:
        """Get detailed analysis for a specific conversation."""
        try:
            doc = self.collection.find_one({
                'metadata.properties.conversation_id': conversation_id,
                'conversation_analysis': {'$exists': True}
            })
            
            if not doc:
                return "<p>❌ Conversation not found</p>"
            
            analysis = doc.get('conversation_analysis', {})
            insights = analysis.get('aggregated_marketing_insights', {})
            
            # Format the HTML content
            html_content = f"""
            <div class="conversation-details" style="background-color: white; color: #333; padding: 20px;">
                <h3 style="color: #333; background-color: white;">πŸ“„ Conversation Analysis: {conversation_id}</h3>
                
                <div class="section" style="background-color: white; color: #333; border: 1px solid #e0e0e0; border-radius: 8px; padding: 15px; margin: 20px 0;">
                    <h4 style="color: #333; background-color: white;">πŸ“ Summary (Contextual Summary)</h4>
                    <div class="content-box" style="background-color: #f8f9fa; color: #333; padding: 15px; border-radius: 5px; border: 1px solid #dee2e6; margin: 10px 0;">
                        <p style="color: #333; background-color: transparent;">{analysis.get('aggregated_contextual_summary', 'No summary available')}</p>
                    </div>
                </div>
                
                <div class="section" style="background-color: white; color: #333; border: 1px solid #e0e0e0; border-radius: 8px; padding: 15px; margin: 20px 0;">
                    <h4 style="color: #333; background-color: white;">πŸ’‘ Insights</h4>
            """
            
            # Add quotes
            quotes = insights.get('quotes', [])
            if quotes:
                html_content += "<h5 style='color: #333; background-color: white;'>πŸ“ Key Quotes:</h5><ul style='color: #333; background-color: white;'>"
                for i, quote in enumerate(quotes, 1):
                    sentiment_class = f"sentiment-{quote.get('sentiment', 'neutral').lower()}"
                    html_content += f"""
                    <li style='color: #333; background-color: white;'>
                        <div class="quote-item" style='background-color: #f8f9fa; color: #333; padding: 10px; border-radius: 5px; border-left: 4px solid #007bff; margin: 10px 0;'>
                            <p style='color: #333; background-color: transparent;'><strong>Quote {i}:</strong> "{quote.get('quote', '')}"</p>
                            <p style='color: #333; background-color: transparent;'><strong>Context:</strong> {quote.get('context', '')}</p>
                            <p style='color: #333; background-color: transparent;'><strong>Sentiment:</strong> <span class="{sentiment_class}">{quote.get('sentiment', 'Unknown')}</span></p>
                        </div>
                    </li>
                    """
                html_content += "</ul>"
            
            # Add key findings
            findings = insights.get('key_findings', [])
            if findings:
                html_content += "<h5 style='color: #333; background-color: white;'>πŸ” Key Findings:</h5><ul style='color: #333; background-color: white;'>"
                for i, finding in enumerate(findings, 1):
                    impact_class = f"impact-{finding.get('impact', 'medium').lower()}"
                    html_content += f"""
                    <li style='color: #333; background-color: white;'>
                        <div class="finding-item" style='background-color: #f8f9fa; color: #333; padding: 10px; border-radius: 5px; border-left: 4px solid #007bff; margin: 10px 0;'>
                            <p style='color: #333; background-color: transparent;'><strong>Finding {i}:</strong> {finding.get('finding', '')}</p>
                            <p style='color: #333; background-color: transparent;'><strong>Evidence:</strong> {finding.get('evidence', '')}</p>
                            <p style='color: #333; background-color: transparent;'><strong>Impact:</strong> <span class="{impact_class}">{finding.get('impact', 'Unknown')}</span></p>
                        </div>
                    </li>
                    """
                html_content += "</ul>"
            
            # Add follow-up email
            follow_up_email = analysis.get('follow_up_email', '')
            if follow_up_email:
                html_content += f"""
                <div class="section" style="background-color: white; color: #333; border: 1px solid #e0e0e0; border-radius: 8px; padding: 15px; margin: 20px 0;">
                    <h4 style="color: #333; background-color: white;">πŸ“§ Follow-up Email</h4>
                    <div class="content-box" style="background-color: #f8f9fa; color: #333; padding: 15px; border-radius: 5px; border: 1px solid #dee2e6; margin: 10px 0;">
                        <pre style="color: #333; background-color: transparent; white-space: pre-wrap; font-family: monospace;">{follow_up_email}</pre>
                    </div>
                </div>
                """
            
            html_content += "</div>"
            
            return html_content
            
        except Exception as e:
            return f"<p>❌ Error loading conversation details: {e}</p>"
    
    def setup_ui(self):
        """Setup the Gradio interface."""
        with gr.Blocks(
            title="Conversation Analysis Dashboard",
            theme=gr.themes.Soft(),
            css="""
            .conversation-details {
                max-width: 100%;
                padding: 20px;
                font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
                background-color: white;
                color: #333;
            }
            .section {
                margin: 20px 0;
                padding: 15px;
                border: 1px solid #e0e0e0;
                border-radius: 8px;
                background-color: #ffffff;
                color: #333;
            }
            .content-box {
                background-color: #f8f9fa;
                padding: 15px;
                border-radius: 5px;
                border: 1px solid #dee2e6;
                margin: 10px 0;
                color: #333;
            }
            .quote-item, .finding-item {
                margin: 10px 0;
                padding: 10px;
                background-color: #f8f9fa;
                border-radius: 5px;
                border-left: 4px solid #007bff;
                color: #333;
            }
            .sentiment-positive { 
                background-color: #d4edda; 
                color: #155724; 
                padding: 2px 8px;
                border-radius: 4px;
                font-weight: bold;
                display: inline-block;
            }
            .sentiment-negative { 
                background-color: #f8d7da; 
                color: #721c24; 
                padding: 2px 8px;
                border-radius: 4px;
                font-weight: bold;
                display: inline-block;
            }
            .sentiment-neutral { 
                background-color: #d1ecf1; 
                color: #0c5460; 
                padding: 2px 8px;
                border-radius: 4px;
                font-weight: bold;
                display: inline-block;
            }
            .sentiment-confused { 
                background-color: #fff3cd; 
                color: #856404; 
                padding: 2px 8px;
                border-radius: 4px;
                font-weight: bold;
                display: inline-block;
            }
            .impact-high { 
                background-color: #f8d7da; 
                color: #721c24; 
                padding: 2px 8px;
                border-radius: 4px;
                font-weight: bold;
                display: inline-block;
            }
            .impact-medium { 
                background-color: #fff3cd; 
                color: #856404; 
                padding: 2px 8px;
                border-radius: 4px;
                font-weight: bold;
                display: inline-block;
            }
            .impact-low { 
                background-color: #d4edda; 
                color: #155724; 
                padding: 2px 8px;
                border-radius: 4px;
                font-weight: bold;
                display: inline-block;
            }
            .quality-high { color: #28a745; font-weight: bold; }
            .quality-medium { color: #ffc107; font-weight: bold; }
            .quality-low { color: #dc3545; font-weight: bold; }
            """
        ) as self.interface:
            
            gr.Markdown("# 🎯 Conversation Analysis Dashboard")
            gr.Markdown("Analyze customer conversations with AI-powered insights, summaries, and follow-up emails.")
            
            # Filters
            with gr.Row():
                with gr.Column(scale=2):
                    quality_range = gr.Slider(
                        minimum=0.0, maximum=1.0, value=[0.0, 1.0],
                        label="Quality Score Range", step=0.01
                    )
                with gr.Column(scale=1):
                    sentiment_filter = gr.Dropdown(
                        choices=["All", "Positive", "Negative", "Neutral", "Confused"],
                        value="All", label="Sentiment Filter"
                    )
                with gr.Column(scale=1):
                    search_text = gr.Textbox(
                        placeholder="Search conversations...", label="Search"
                    )
                with gr.Column(scale=1):
                    refresh_btn = gr.Button("πŸ”„ Refresh", variant="primary")
            
            # Main table
            with gr.Row():
                conversations_df = gr.Dataframe(
                    headers=["Conversation ID", "Quality", "Sentiment", "Summary", "Insights Count", "Date"],
                    datatype=["str", "number", "str", "str", "str", "str"],
                    interactive=False,
                    label="Conversations",
                    wrap=True,  # Enable text wrapping
                    max_height=600  # Set max height for scrolling
                )
            
            # Detail view
            with gr.Row():
                with gr.Column():
                    detail_view = gr.HTML(
                        value="<p>Select a conversation from the table above to view detailed analysis</p>",
                        label="Conversation Details"
                    )
            
            # Event handlers
            def refresh_data(quality_range, sentiment, search):
                if isinstance(quality_range, (list, tuple)) and len(quality_range) == 2:
                    quality_min, quality_max = quality_range
                else:
                    quality_min, quality_max = 0.0, 1.0
                df = self.load_conversations(quality_min, quality_max, sentiment, search, limit=1000)
                return df
            
            def on_table_select(evt: gr.SelectData):
                if evt.index[0] is not None:
                    try:
                        # Get the conversation ID from the selected row
                        # We need to get the current dataframe from the table
                        current_df = self.load_conversations()
                        if not current_df.empty and evt.index[0] < len(current_df):
                            conversation_id = current_df.iloc[evt.index[0]]['conversation_id']
                            return self.get_conversation_details(conversation_id)
                        else:
                            return "<p>Please refresh the data first</p>"
                    except Exception as e:
                        return f"<p>Error: {e}</p>"
                return "<p>Please select a conversation from the table</p>"
            
            refresh_btn.click(
                fn=refresh_data,
                inputs=[quality_range, sentiment_filter, search_text],
                outputs=[conversations_df]
            )
            
            conversations_df.select(
                fn=on_table_select,
                outputs=[detail_view]
            )
            
            # Load initial data when the page loads
            def load_initial_data():
                return self.load_conversations(limit=1000)  # Load more conversations
            
            # Set initial data using the interface's load event
            self.interface.load(load_initial_data, outputs=[conversations_df])
    
    def launch(self, **kwargs):
        """Launch the Gradio interface."""
        self.interface.launch(**kwargs)