email-parser / app.py
Nikhil Singh
more cleaning
5777a9a
raw
history blame
1.59 kB
import gradio as gr
import re
from mailparser import parse_from_string
from bs4 import BeautifulSoup
def accept_mail(email_content):
email = parse_from_string(email_content)
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 remove_special_characters(text):
pattern = r'[=_-]+'
cleaned_text = re.sub(pattern, '', text)
return cleaned_text
def present(email_content):
email = accept_mail(email_content)
cleaned_text = clean_email(email)
further_cleaned_text = remove_special_characters(cleaned_text)
email_info = {
"Subject": email.subject,
"From": email.from_,
"To": email.to,
"Date": email.date,
# "Message ID": email.message_id,
# "Headers": str(email.headers),
"Cleaned Body": further_cleaned_text
}
return [email_info[key] for key in email_info]
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="Cleaned Body")
],
title="Email Info",
description="Enter the email content below to view its details."
)
demo.launch()