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) 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, "Cleaned Body": cleaned_text } return [email_info[key] for key in email_info] with gr.Blocks() as demo: gr.Markdown("## Email Info") gr.Markdown("Enter the email content below to view its details.") with gr.Row(): with gr.Column(): input_text = gr.Textbox(label="Email Content") output_texts = [ gr.Textbox(label="Subject"), gr.Textbox(label="From"), gr.Textbox(label="To"), gr.Textbox(label="Date"), gr.Textbox(label="Message ID"), gr.Textbox(label="Headers"), gr.Textbox(label="Attachments"), gr.Textbox(label="Cleaned Body", visible=False) # Initially hidden ] button = gr.Button("Process Email") with gr.Column(): output_cleaned_text = gr.Textbox(label="Cleaned Email Body") button.click( present, inputs=input_text, outputs=output_texts + [output_cleaned_text] ) demo.launch()