File size: 2,200 Bytes
8efe659
2fe2a42
13ac7c2
2fe2a42
13ac7c2
2fe2a42
13ac7c2
 
 
 
 
 
 
 
 
28ca0f2
 
01fa41d
13ac7c2
56c79b1
 
 
 
 
 
 
 
01fa41d
13ac7c2
01fa41d
 
 
 
 
 
 
 
 
 
13ac7c2
2fe2a42
01fa41d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13ac7c2
 
 
01fa41d
13ac7c2
01fa41d
 
13ac7c2
56c79b1
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
import gradio as gr
from mailparser import parse_from_string
from bs4 import BeautifulSoup

def accept_mail(name):
    email = parse_from_string(name)
    return email

def clean_email(email):
    soup = BeautifulSoup(email.body, 'html.parser')
    for tag in soup.find_all(['style', 'link']):
        tag.decompose()
    cleaned_text = ' '.join(soup.get_text(separator=' ').split())
    return cleaned_text

def present(email_content):
    email = accept_mail(email_content)
    cleaned_text = clean_email(email)  # Call clean_email to get cleaned text
    email_info = {
        "Subject": email.subject,
        "From": email.from_,
        "To": email.to,
        "Date": email.date,
        "Message ID": email.message_id,
        "Headers": email.headers,
        "Attachments": email.attachments
    }
    # Outputs are now organized as a list to match the structure for grouped outputs
    return [
        [
            email_info["Subject"], 
            str(email_info["From"]),  # Convert list to string for display
            str(email_info["To"]),    # Convert list to string for display
            email_info["Date"], 
            email_info["Message ID"], 
            str(email_info["Headers"]),  # Convert dictionary to string for display
            str(email_info["Attachments"])  # Convert list to string for display
        ],
        cleaned_text  # Cleaned text is the second major block
    ]

outputs = [
    gr.components.Group(
        components=[
            gr.components.Textbox(label="Subject"),
            gr.components.Textbox(label="From"),
            gr.components.Textbox(label="To"),
            gr.components.Textbox(label="Date"),
            gr.components.Textbox(label="Message ID"),
            gr.components.Textbox(label="Headers"),
            gr.components.Textbox(label="Attachments")
        ],
        label="Email Details"
    ),
    gr.components.Textbox(label="Cleaned Text")
]

demo = gr.Interface(
    fn=present, 
    inputs="text", 
    outputs=outputs, 
    title="Email Info", 
    description="Enter the email content below to view its details.",
    layout="horizontal"  # Arrange the major output groups horizontally
)
demo.launch()