Spaces:
Sleeping
Sleeping
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) # Get the cleaned text of the email | |
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 | |
} | |
return [ | |
email_info["Subject"], | |
str(email_info["From"]), | |
str(email_info["To"]), | |
email_info["Date"], | |
email_info["Message ID"], | |
str(email_info["Headers"]), | |
str(email_info["Attachments"]), | |
cleaned_text | |
] | |
demo = gr.Interface( | |
fn=present, | |
inputs="text", | |
outputs=[ | |
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"), | |
gr.components.Textbox(label="Cleaned Text") | |
], | |
title="Email Info", | |
description="Enter the email content below to view its details.", | |
layout="horizontal" # Arrange the output components horizontally | |
) | |
demo.launch() | |