File size: 3,120 Bytes
1ae86a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Utility functions for the AI Notes Summarizer application
"""

import logging
import streamlit as st
from typing import Optional
import re

def setup_logging():
    """Setup logging configuration"""
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        handlers=[
            logging.FileHandler('app.log'),
            logging.StreamHandler()
        ]
    )
    return logging.getLogger(__name__)

def validate_input(text: str, min_length: int = 50) -> bool:
    """
    Validate input text
    
    Args:
        text: Input text to validate
        min_length: Minimum required length
        
    Returns:
        bool: True if valid, False otherwise
    """
    if not text or not text.strip():
        st.error("Please provide some text content")
        return False
    
    if len(text.strip()) < min_length:
        st.error(f"Text is too short. Please provide at least {min_length} characters.")
        return False
    
    return True

def clean_text(text: str) -> str:
    """
    Clean and normalize text content
    
    Args:
        text: Raw text content
        
    Returns:
        str: Cleaned text
    """
    if not text:
        return ""
    
    # Remove excessive whitespace
    text = re.sub(r'\s+', ' ', text)
    
    # Remove special characters but keep punctuation
    text = re.sub(r'[^\w\s\.\,\!\?\;\:\-\(\)\[\]\"\'\/]', ' ', text)
    
    # Clean up multiple spaces
    text = ' '.join(text.split())
    
    return text.strip()

def format_file_size(size_bytes: int) -> str:
    """
    Format file size in human readable format
    
    Args:
        size_bytes: Size in bytes
        
    Returns:
        str: Formatted size string
    """
    if size_bytes < 1024:
        return f"{size_bytes} B"
    elif size_bytes < 1024 * 1024:
        return f"{size_bytes / 1024:.1f} KB"
    else:
        return f"{size_bytes / (1024 * 1024):.1f} MB"

def display_summary_stats(original_text: str, summary: str):
    """
    Display statistics about the summarization
    
    Args:
        original_text: Original input text
        summary: Generated summary
    """
    original_words = len(original_text.split())
    summary_words = len(summary.split())
    compression_ratio = (1 - summary_words / original_words) * 100 if original_words > 0 else 0
    
    col1, col2, col3 = st.columns(3)
    
    with col1:
        st.metric("Original Words", f"{original_words:,}")
    
    with col2:
        st.metric("Summary Words", f"{summary_words:,}")
    
    with col3:
        st.metric("Compression", f"{compression_ratio:.1f}%")

def create_download_link(content: str, filename: str = "summary.txt") -> str:
    """
    Create a download link for the summary
    
    Args:
        content: Content to download
        filename: Name of the file
        
    Returns:
        str: Download link HTML
    """
    import base64
    
    b64 = base64.b64encode(content.encode()).decode()
    href = f'<a href="data:text/plain;base64,{b64}" download="{filename}">Download Summary</a>'
    return href