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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import os
import sys
import gradio as gr
import json
import base64
import logging
from pathlib import Path
import uuid
import re
from datetime import datetime
from typing import Dict, List, Optional, Tuple
import pandas as pd

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

# Fix the model path for HF Space deployment
if os.path.exists('final_optimized_model'):
    # Running in HF Space
    MODEL_PATH = 'final_optimized_model'
else:
    # Running locally
    MODEL_PATH = os.path.join(Path(__file__).parent.parent, 'final_optimized_model')

# Import the ML components
sys.path.insert(0, str(Path(__file__).parent))
if os.path.exists('ml_suite'):
    # Override the config to use local model path
    import ml_suite.config as config
    config.FINE_TUNED_MODEL_DIR = MODEL_PATH
    
    from ml_suite.predictor import initialize_predictor, get_ai_prediction_for_email, is_predictor_ready, get_model_status

# Initialize the predictor once when the app starts
logger.info(f"Initializing AI model from {MODEL_PATH}...")
if 'ml_suite' in sys.modules:
    initialize_predictor(logger)
    model_ready = is_predictor_ready()
    logger.info(f"Model initialization status: {'Ready' if model_ready else 'Failed'}")
else:
    model_ready = False
    logger.error("ML suite not found")

# Store session data
session_data = {}

def create_session():
    """Create a new session"""
    session_id = str(uuid.uuid4())
    session_data[session_id] = {
        'emails': [],
        'scan_history': [],
        'settings': {
            'ai_enabled': True,
            'confidence_threshold': 0.5
        }
    }
    return session_id

def parse_email_batch(email_text):
    """Parse batch email input"""
    emails = []
    current_email = {'subject': '', 'body': '', 'sender': ''}
    
    lines = email_text.strip().split('\n')
    current_section = None
    
    for line in lines:
        line = line.strip()
        
        if line.lower().startswith('---'):  # Email separator
            if current_email['subject'] or current_email['body']:
                emails.append(current_email)
                current_email = {'subject': '', 'body': '', 'sender': ''}
                current_section = None
        elif line.lower().startswith('from:'):
            current_email['sender'] = line[5:].strip()
            current_section = 'sender'
        elif line.lower().startswith('subject:'):
            current_email['subject'] = line[8:].strip()
            current_section = 'subject'
        elif line.lower().startswith('body:'):
            current_section = 'body'
        elif line and current_section == 'body':
            current_email['body'] += line + '\n'
        elif line and current_section == 'subject' and not line.lower().startswith(('from:', 'body:')):
            current_email['subject'] += ' ' + line
    
    # Add last email
    if current_email['subject'] or current_email['body']:
        emails.append(current_email)
    
    return emails

def classify_email(email_data):
    """Classify a single email"""
    if not model_ready:
        return {
            'prediction': 'error',
            'confidence': 0,
            'error': 'Model not ready'
        }
    
    try:
        # Prepare email data for predictor
        email_for_prediction = {
            'snippet': email_data.get('body', '')[:200],
            'subject': email_data.get('subject', ''),
            'body': email_data.get('body', ''),
            'sender': email_data.get('sender', 'unknown@example.com'),
            'id': str(uuid.uuid4())
        }
        
        result = get_ai_prediction_for_email(email_for_prediction)
        return result
    except Exception as e:
        logger.error(f"Classification error: {str(e)}")
        return {
            'prediction': 'error',
            'confidence': 0,
            'error': str(e)
        }

def scan_emails(session_id, email_batch_text, ai_enabled, confidence_threshold):
    """Scan a batch of emails"""
    if session_id not in session_data:
        session_id = create_session()
    
    session = session_data[session_id]
    session['settings']['ai_enabled'] = ai_enabled
    session['settings']['confidence_threshold'] = confidence_threshold
    
    # Parse emails
    emails = parse_email_batch(email_batch_text)
    
    if not emails:
        return "No valid emails found in input.", None, session_id
    
    results = []
    unsubscribe_count = 0
    important_count = 0
    
    for email in emails:
        if ai_enabled and model_ready:
            classification = classify_email(email)
            prediction = classification.get('prediction', 'unknown')
            confidence = classification.get('confidence', 0)
            
            if confidence >= confidence_threshold:
                if prediction == 'unsubscribe':
                    unsubscribe_count += 1
                    status = "βœ… Unsubscribe"
                else:
                    important_count += 1
                    status = "⚠️ Important"
            else:
                status = "❓ Uncertain"
        else:
            prediction = 'not_analyzed'
            confidence = 0
            status = "⏭️ Skipped (AI disabled)"
        
        result = {
            'subject': email.get('subject', 'No subject'),
            'sender': email.get('sender', 'Unknown'),
            'prediction': prediction,
            'confidence': confidence,
            'status': status,
            'body_preview': email.get('body', '')[:100] + '...' if len(email.get('body', '')) > 100 else email.get('body', '')
        }
        results.append(result)
        session['emails'].append(result)
    
    # Create summary
    summary = f"""
## Scan Results

**Total Emails Scanned:** {len(results)}
**Unsubscribe Confirmations:** {unsubscribe_count}
**Important Emails:** {important_count}
**Uncertain:** {len(results) - unsubscribe_count - important_count}

### Detailed Results:
"""
    
    for i, result in enumerate(results, 1):
        summary += f"\n**{i}. {result['subject']}**\n"
        summary += f"- From: {result['sender']}\n"
        summary += f"- Status: {result['status']}\n"
        if ai_enabled and result['confidence'] > 0:
            summary += f"- Confidence: {result['confidence']:.2%}\n"
        summary += f"- Preview: {result['body_preview']}\n"
    
    # Create DataFrame for display
    df_data = []
    for r in results:
        df_data.append({
            'Subject': r['subject'],
            'From': r['sender'],
            'Status': r['status'],
            'Confidence': f"{r['confidence']:.2%}" if r['confidence'] > 0 else "N/A",
            'Preview': r['body_preview'][:50] + '...'
        })
    
    df = pd.DataFrame(df_data) if df_data else None
    
    # Add to scan history
    session['scan_history'].append({
        'timestamp': datetime.now().isoformat(),
        'count': len(results),
        'unsubscribe': unsubscribe_count,
        'important': important_count
    })
    
    return summary, df, session_id

def get_statistics(session_id):
    """Get session statistics"""
    if session_id not in session_data:
        return "No session data available."
    
    session = session_data[session_id]
    total_scans = len(session['scan_history'])
    total_emails = sum(scan['count'] for scan in session['scan_history'])
    total_unsubscribe = sum(scan['unsubscribe'] for scan in session['scan_history'])
    total_important = sum(scan['important'] for scan in session['scan_history'])
    
    stats = f"""
## Session Statistics

**Total Scans:** {total_scans}
**Total Emails Processed:** {total_emails}
**Unsubscribe Emails Found:** {total_unsubscribe}
**Important Emails Protected:** {total_important}

### Model Information:
- **Model:** DeBERTa-v3-small
- **Training Samples:** 20,000
- **Accuracy:** 100% on test set
- **Status:** {'🟒 Ready' if model_ready else 'πŸ”΄ Not Available'}
"""
    return stats

# Create Gradio interface
with gr.Blocks(title="Gmail Unsubscriber - Full Web Version", theme=gr.themes.Soft()) as demo:
    session_state = gr.State(create_session())
    
    gr.Markdown("""
    # πŸ“§ Gmail Unsubscriber - Web Version
    
    This is a web-based version of the Gmail Unsubscriber application that uses AI to classify emails as unsubscribe confirmations or important emails.
    
    **Note:** This web version demonstrates the AI classification capabilities. For full Gmail integration with OAuth, please use the desktop version.
    """)
    
    with gr.Tabs():
        with gr.TabItem("πŸ“Š Email Scanner"):
            gr.Markdown("### Batch Email Classification")
            
            with gr.Row():
                with gr.Column(scale=2):
                    email_input = gr.Textbox(
                        lines=15,
                        placeholder="""Paste multiple emails here. Format each email as:

From: sender@example.com
Subject: Your subscription has been cancelled
Body:
We're sorry to see you go! Your subscription has been cancelled.

---

From: bank@example.com
Subject: Important: Security Alert
Body:
We detected unusual activity on your account. Please review immediately.

---

(Continue with more emails...)""",
                        label="Email Batch Input"
                    )
                
                with gr.Column(scale=1):
                    ai_enabled = gr.Checkbox(value=True, label="Enable AI Classification")
                    confidence_threshold = gr.Slider(
                        minimum=0.1,
                        maximum=0.9,
                        value=0.5,
                        step=0.1,
                        label="Confidence Threshold"
                    )
                    scan_btn = gr.Button("πŸ” Scan Emails", variant="primary", size="lg")
            
            scan_output = gr.Markdown()
            results_table = gr.DataFrame(label="Scan Results")
        
        with gr.TabItem("πŸ“ˆ Statistics"):
            stats_output = gr.Markdown()
            refresh_stats_btn = gr.Button("πŸ”„ Refresh Statistics")
        
        with gr.TabItem("πŸ§ͺ Test Single Email"):
            gr.Markdown("### Test AI Classification on a Single Email")
            
            with gr.Row():
                with gr.Column():
                    test_subject = gr.Textbox(label="Subject", placeholder="Your subscription has been cancelled")
                    test_sender = gr.Textbox(label="From", placeholder="noreply@example.com")
                    test_body = gr.Textbox(
                        lines=5,
                        label="Body",
                        placeholder="We're sorry to see you go! Your subscription has been successfully cancelled."
                    )
                    test_btn = gr.Button("πŸ€– Classify", variant="primary")
                
                with gr.Column():
                    test_output = gr.Markdown()
        
        with gr.TabItem("ℹ️ About"):
            gr.Markdown("""
            ## About Gmail Unsubscriber
            
            This application uses a fine-tuned DeBERTa-v3-small model to classify emails automatically.
            
            ### Features:
            - πŸ€– AI-powered email classification
            - πŸ“Š Batch processing capabilities
            - πŸ“ˆ Real-time statistics
            - 🎯 Adjustable confidence thresholds
            
            ### Model Performance:
            - **Accuracy:** 100% on test set
            - **F1 Score:** 1.0 for both classes
            - **Model Size:** 552MB
            - **Training Data:** 20,000 email samples
            
            ### Desktop Version Features (Not available in web):
            - Gmail OAuth integration
            - Automatic email fetching
            - One-click unsubscribe
            - Email archiving
            - Persistent user settings
            """)
    
    # Event handlers
    def test_single_email(subject, sender, body):
        if not subject and not body:
            return "Please enter email content to test."
        
        email_data = {
            'subject': subject,
            'sender': sender,
            'body': body
        }
        
        result = classify_email(email_data)
        
        if result.get('error'):
            return f"❌ Error: {result['error']}"
        
        prediction = result.get('prediction', 'unknown')
        confidence = result.get('confidence', 0)
        
        if prediction == 'unsubscribe':
            emoji = "βœ…"
            description = "This appears to be an unsubscribe confirmation."
        elif prediction == 'important':
            emoji = "⚠️"
            description = "This appears to be an important email."
        else:
            emoji = "❓"
            description = "Unable to classify with confidence."
        
        output = f"""
### Classification Result

{emoji} **{prediction.upper()}**

**Confidence:** {confidence:.2%}

{description}
"""
        return output
    
    # Connect event handlers
    scan_btn.click(
        fn=scan_emails,
        inputs=[session_state, email_input, ai_enabled, confidence_threshold],
        outputs=[scan_output, results_table, session_state]
    )
    
    refresh_stats_btn.click(
        fn=get_statistics,
        inputs=[session_state],
        outputs=[stats_output]
    )
    
    test_btn.click(
        fn=test_single_email,
        inputs=[test_subject, test_sender, test_body],
        outputs=[test_output]
    )
    
    # Load initial statistics
    demo.load(
        fn=get_statistics,
        inputs=[session_state],
        outputs=[stats_output]
    )

if __name__ == "__main__":
    demo.launch()