Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import mailparser
|
3 |
+
from email_reply_parser import EmailReplyParser
|
4 |
+
|
5 |
+
# Function to extract the latest email message from raw email content
|
6 |
+
def extract_latest_message(raw_email):
|
7 |
+
try:
|
8 |
+
# Parse the email using mail-parser
|
9 |
+
mail = mailparser.parse_from_string(raw_email)
|
10 |
+
|
11 |
+
# Extract the plain text body, fallback to HTML body if plain text is not available
|
12 |
+
body = mail.text_plain[0] if mail.text_plain else mail.body
|
13 |
+
|
14 |
+
# Use email-reply-parser to extract only the latest reply (remove quoted thread)
|
15 |
+
latest_reply = EmailReplyParser.parse_reply(body)
|
16 |
+
|
17 |
+
return latest_reply
|
18 |
+
except Exception as e:
|
19 |
+
return f"Error: {e}"
|
20 |
+
|
21 |
+
# Streamlit app
|
22 |
+
def main():
|
23 |
+
st.title("Email Latest Message Extractor")
|
24 |
+
|
25 |
+
st.write("""
|
26 |
+
This tool extracts the latest message from a raw MIME email and removes any quoted thread or previous messages.
|
27 |
+
Paste the raw email in MIME format in the text area below, and the tool will display the latest message.
|
28 |
+
""")
|
29 |
+
|
30 |
+
# Input field for the raw email content
|
31 |
+
raw_email = st.text_area("Paste the raw MIME email content here", height=300)
|
32 |
+
|
33 |
+
# Button to process the input
|
34 |
+
if st.button("Extract Latest Message"):
|
35 |
+
if raw_email.strip():
|
36 |
+
# Call the function to extract the latest message
|
37 |
+
latest_message = extract_latest_message(raw_email)
|
38 |
+
st.subheader("Extracted Latest Message:")
|
39 |
+
st.text_area("Latest Message", value=latest_message, height=200)
|
40 |
+
else:
|
41 |
+
st.warning("Please paste the raw MIME email content.")
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
main()
|