email-parser / app.py
Nikhil Singh
Labels fix
28ca0f2
raw
history blame
1.63 kB
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)
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"]), # 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
]
outputs = [
gr.outputs.Textbox(label="Subject"),
gr.outputs.Textbox(label="From"),
gr.outputs.Textbox(label="To"),
gr.outputs.Textbox(label="Date"),
gr.outputs.Textbox(label="Message ID"),
gr.outputs.Textbox(label="Headers"),
gr.outputs.Textbox(label="Attachments")
]
demo = gr.Interface(
fn=present,
inputs="text",
outputs=outputs,
title="Email Info",
description="Enter the email content below to view its details."
)
demo.launch()