Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +94 -106
src/streamlit_app.py
CHANGED
|
@@ -3,59 +3,43 @@ import torch
|
|
| 3 |
from transformers import pipeline
|
| 4 |
import time
|
| 5 |
|
| 6 |
-
# Page config
|
| 7 |
st.set_page_config(
|
| 8 |
page_title="π§ Email Reply Assistant",
|
| 9 |
page_icon="π§",
|
| 10 |
-
layout="wide"
|
| 11 |
-
initial_sidebar_state="expanded"
|
| 12 |
)
|
| 13 |
|
| 14 |
-
# Custom CSS
|
| 15 |
st.markdown("""
|
| 16 |
<style>
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
</style>
|
| 22 |
""", unsafe_allow_html=True)
|
| 23 |
|
| 24 |
@st.cache_resource
|
| 25 |
def load_pipelines():
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
model="distilbert-base-uncased",
|
| 31 |
-
device=0 if torch.cuda.is_available() else -1
|
| 32 |
-
)
|
| 33 |
-
generator = pipeline(
|
| 34 |
-
"text-generation",
|
| 35 |
-
model="Kunal7370944861/Email-Writer-AI",
|
| 36 |
-
device=0 if torch.cuda.is_available() else -1
|
| 37 |
-
)
|
| 38 |
-
translator = pipeline(
|
| 39 |
-
"translation",
|
| 40 |
-
model="DDDSSS/translation_en-zh",
|
| 41 |
-
device=0 if torch.cuda.is_available() else -1
|
| 42 |
-
)
|
| 43 |
return classifier, generator, translator
|
| 44 |
|
| 45 |
-
# Load
|
| 46 |
try:
|
| 47 |
classifier, generator, translator = load_pipelines()
|
| 48 |
-
st.success("β
All 3
|
| 49 |
except Exception as e:
|
| 50 |
-
st.error(f"
|
| 51 |
st.stop()
|
| 52 |
|
| 53 |
-
|
| 54 |
-
def classify_email(text: str, classifier):
|
| 55 |
result = classifier(text[:512])[0]
|
| 56 |
return result["label"], float(result["score"])
|
| 57 |
|
| 58 |
-
def build_prompt(email_text
|
| 59 |
return f"""You are a helpful customer service agent.
|
| 60 |
Email category: {category}
|
| 61 |
|
|
@@ -65,101 +49,105 @@ Customer email:
|
|
| 65 |
Write a polite, concise reply template.
|
| 66 |
Reply:"""
|
| 67 |
|
| 68 |
-
def generate_reply(prompt
|
| 69 |
-
outputs = generator(
|
| 70 |
-
|
| 71 |
-
max_length=300,
|
| 72 |
-
num_return_sequences=1,
|
| 73 |
-
do_sample=True,
|
| 74 |
-
temperature=0.7,
|
| 75 |
-
pad_token_id=generator.tokenizer.eos_token_id
|
| 76 |
-
)
|
| 77 |
full_text = outputs[0]["generated_text"]
|
| 78 |
if "Reply:" in full_text:
|
| 79 |
return full_text.split("Reply:", 1)[-1].strip()
|
| 80 |
return full_text.replace(prompt, "").strip()
|
| 81 |
|
| 82 |
-
def translate_reply(text
|
| 83 |
-
if not text.strip():
|
| 84 |
-
|
| 85 |
-
outputs = translator(text)
|
| 86 |
-
return outputs[0]["translation_text"].strip()
|
| 87 |
|
| 88 |
-
#
|
| 89 |
st.markdown('<h1 class="main-header">π€ Email Reply Assistant</h1>', unsafe_allow_html=True)
|
| 90 |
-
st.markdown("**Classify β Generate Reply β Translate to Chinese**
|
| 91 |
|
| 92 |
-
# Sidebar
|
| 93 |
with st.sidebar:
|
| 94 |
-
st.header("
|
| 95 |
-
st.
|
| 96 |
-
**Pipeline
|
| 97 |
-
**Pipeline
|
| 98 |
-
**Pipeline 3**: `DDDSSS/translation_en-zh` β Chinese translation
|
| 99 |
-
|
| 100 |
-
**Status**: β
All models loaded
|
| 101 |
-
""")
|
| 102 |
st.markdown("---")
|
| 103 |
-
st.info("π Paste email β
|
| 104 |
|
| 105 |
-
# Main
|
| 106 |
col1, col2 = st.columns([1, 2])
|
| 107 |
|
| 108 |
with col1:
|
| 109 |
-
st.header("π¨ Input Email")
|
| 110 |
-
|
| 111 |
-
# Email input
|
| 112 |
email_text = st.text_area(
|
| 113 |
-
"Paste
|
| 114 |
-
placeholder="Subject:
|
| 115 |
-
height=
|
| 116 |
-
help="Paste complete email (subject + body)"
|
| 117 |
)
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
st.
|
|
|
|
|
|
|
| 125 |
|
| 126 |
with col2:
|
| 127 |
-
if
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
col_b.metric("Confidence", f"{score:.1%}")
|
| 136 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 137 |
-
|
| 138 |
-
# Pipeline 2: Reply Generation
|
| 139 |
-
with st.container():
|
| 140 |
-
st.markdown('<div class="pipeline-card"><h3>βοΈ Pipeline 2: English Reply</h3>', unsafe_allow_html=True)
|
| 141 |
-
prompt = build_prompt(email_text, label)
|
| 142 |
-
reply_en = generate_reply(prompt, generator)
|
| 143 |
-
st.text_area("English Reply Template", reply_en, height=150, disabled=True)
|
| 144 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 145 |
-
|
| 146 |
-
# Pipeline 3: Translation
|
| 147 |
-
with st.container():
|
| 148 |
-
st.markdown('<div class="pipeline-card"><h3>π¨π³ Pipeline 3: Chinese Translation</h3>', unsafe_allow_html=True)
|
| 149 |
-
reply_zh = translate_reply(reply_en, translator)
|
| 150 |
-
st.text_area("δΈζεε€ζ¨‘ζΏ", reply_zh, height=150, disabled=True)
|
| 151 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
else:
|
| 161 |
-
st.
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
# Footer
|
| 164 |
st.markdown("---")
|
| 165 |
-
st.markdown("*
|
|
|
|
| 3 |
from transformers import pipeline
|
| 4 |
import time
|
| 5 |
|
|
|
|
| 6 |
st.set_page_config(
|
| 7 |
page_title="π§ Email Reply Assistant",
|
| 8 |
page_icon="π§",
|
| 9 |
+
layout="wide"
|
|
|
|
| 10 |
)
|
| 11 |
|
|
|
|
| 12 |
st.markdown("""
|
| 13 |
<style>
|
| 14 |
+
.main-header {font-size: 3rem; color: #1f77b4; text-align: center;}
|
| 15 |
+
.pipeline-card {background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
|
| 16 |
+
padding: 1.5rem; border-radius: 15px; margin: 1rem 0;
|
| 17 |
+
border-left: 6px solid #1f77b4; box-shadow: 0 4px 6px rgba(0,0,0,0.1);}
|
| 18 |
+
.metric-card {background: white; padding: 1rem; border-radius: 10px; text-align: center;}
|
| 19 |
</style>
|
| 20 |
""", unsafe_allow_html=True)
|
| 21 |
|
| 22 |
@st.cache_resource
|
| 23 |
def load_pipelines():
|
| 24 |
+
with st.spinner('π Loading AI models (2-3 min)...'):
|
| 25 |
+
classifier = pipeline("text-classification", model="distilbert-base-uncased")
|
| 26 |
+
generator = pipeline("text-generation", model="Kunal7370944861/Email-Writer-AI")
|
| 27 |
+
translator = pipeline("translation", model="DDDSSS/translation_en-zh")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
return classifier, generator, translator
|
| 29 |
|
| 30 |
+
# Load models safely
|
| 31 |
try:
|
| 32 |
classifier, generator, translator = load_pipelines()
|
| 33 |
+
st.success("β
All 3 pipelines ready!")
|
| 34 |
except Exception as e:
|
| 35 |
+
st.error(f"Model loading error: {str(e)}")
|
| 36 |
st.stop()
|
| 37 |
|
| 38 |
+
def classify_email(text, classifier):
|
|
|
|
| 39 |
result = classifier(text[:512])[0]
|
| 40 |
return result["label"], float(result["score"])
|
| 41 |
|
| 42 |
+
def build_prompt(email_text, category):
|
| 43 |
return f"""You are a helpful customer service agent.
|
| 44 |
Email category: {category}
|
| 45 |
|
|
|
|
| 49 |
Write a polite, concise reply template.
|
| 50 |
Reply:"""
|
| 51 |
|
| 52 |
+
def generate_reply(prompt, generator):
|
| 53 |
+
outputs = generator(prompt, max_length=300, num_return_sequences=1,
|
| 54 |
+
do_sample=True, temperature=0.7)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
full_text = outputs[0]["generated_text"]
|
| 56 |
if "Reply:" in full_text:
|
| 57 |
return full_text.split("Reply:", 1)[-1].strip()
|
| 58 |
return full_text.replace(prompt, "").strip()
|
| 59 |
|
| 60 |
+
def translate_reply(text, translator):
|
| 61 |
+
if not text.strip(): return ""
|
| 62 |
+
return translator(text)[0]["translation_text"].strip()
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
# Header
|
| 65 |
st.markdown('<h1 class="main-header">π€ Email Reply Assistant</h1>', unsafe_allow_html=True)
|
| 66 |
+
st.markdown("**AI-powered: Classify β Generate Reply β Translate to Chinese**")
|
| 67 |
|
| 68 |
+
# Sidebar
|
| 69 |
with st.sidebar:
|
| 70 |
+
st.header("π§ Pipeline Status")
|
| 71 |
+
st.success("β
**Pipeline 1**: `distilbert-base-uncased`")
|
| 72 |
+
st.success("β
**Pipeline 2**: `Kunal7370944861/Email-Writer-AI`")
|
| 73 |
+
st.success("β
**Pipeline 3**: `DDDSSS/translation_en-zh`")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
st.markdown("---")
|
| 75 |
+
st.info("π **Paste email β Process β Copy replies!**")
|
| 76 |
|
| 77 |
+
# Main layout
|
| 78 |
col1, col2 = st.columns([1, 2])
|
| 79 |
|
| 80 |
with col1:
|
| 81 |
+
st.header("π¨ **Input Email**")
|
|
|
|
|
|
|
| 82 |
email_text = st.text_area(
|
| 83 |
+
"Paste complete email here...",
|
| 84 |
+
placeholder="Subject: Order Issue\n\nHello,\nMy package arrived damaged...",
|
| 85 |
+
height=220
|
|
|
|
| 86 |
)
|
| 87 |
|
| 88 |
+
if st.button("π **PROCESS EMAIL**", type="primary", use_container_width=True):
|
| 89 |
+
if email_text.strip():
|
| 90 |
+
st.session_state.processed = True
|
| 91 |
+
st.session_state.email = email_text
|
| 92 |
+
else:
|
| 93 |
+
st.error("β Please paste an email first!")
|
| 94 |
+
if st.button("π§Ή **CLEAR**", use_container_width=True):
|
| 95 |
+
st.rerun()
|
| 96 |
|
| 97 |
with col2:
|
| 98 |
+
if 'processed' in st.session_state and st.session_state.processed:
|
| 99 |
+
email_text = st.session_state.email
|
| 100 |
+
|
| 101 |
+
# Pipeline 1: Classification
|
| 102 |
+
with st.container():
|
| 103 |
+
st.markdown('<div class="pipeline-card">', unsafe_allow_html=True)
|
| 104 |
+
st.markdown("### π’ **Pipeline 1: Email Classification**")
|
| 105 |
+
label, score = classify_email(email_text, classifier)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
col_a, col_b = st.columns(2)
|
| 108 |
+
with col_a:
|
| 109 |
+
st.markdown(f"""
|
| 110 |
+
<div class="metric-card">
|
| 111 |
+
<h3>π·οΈ Tag</h3>
|
| 112 |
+
<h2>{label}</h2>
|
| 113 |
+
</div>
|
| 114 |
+
""", unsafe_allow_html=True)
|
| 115 |
+
with col_b:
|
| 116 |
+
st.markdown(f"""
|
| 117 |
+
<div class="metric-card">
|
| 118 |
+
<h3>π Confidence</h3>
|
| 119 |
+
<h2>{score:.1%}</h2>
|
| 120 |
+
</div>
|
| 121 |
+
""", unsafe_allow_html=True)
|
| 122 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 123 |
+
|
| 124 |
+
# Pipeline 2: English Reply
|
| 125 |
+
with st.container():
|
| 126 |
+
st.markdown('<div class="pipeline-card">', unsafe_allow_html=True)
|
| 127 |
+
st.markdown("### βοΈ **Pipeline 2: English Reply**")
|
| 128 |
+
prompt = build_prompt(email_text, label)
|
| 129 |
+
reply_en = generate_reply(prompt, generator)
|
| 130 |
+
st.text_area("**Reply Template**", reply_en, height=140, disabled=True)
|
| 131 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 132 |
+
|
| 133 |
+
# Pipeline 3: Chinese Translation
|
| 134 |
+
with st.container():
|
| 135 |
+
st.markdown('<div class="pipeline-card">', unsafe_allow_html=True)
|
| 136 |
+
st.markdown("### π¨π³ **Pipeline 3: Chinese Translation**")
|
| 137 |
+
reply_zh = translate_reply(reply_en, translator)
|
| 138 |
+
st.text_area("**δΈζεε€**", reply_zh, height=140, disabled=True)
|
| 139 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 140 |
+
|
| 141 |
+
# Download buttons
|
| 142 |
+
col_c, col_d = st.columns(2)
|
| 143 |
+
with col_c:
|
| 144 |
+
st.download_button("π₯ Download English", reply_en, "email_reply_en.txt", use_container_width=True)
|
| 145 |
+
with col_d:
|
| 146 |
+
st.download_button("π₯ Download Chinese", reply_zh, "email_reply_zh.txt", use_container_width=True)
|
| 147 |
else:
|
| 148 |
+
st.markdown('<div class="pipeline-card">', unsafe_allow_html=True)
|
| 149 |
+
st.info("π― **Paste your email above and click PROCESS**")
|
| 150 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 151 |
|
|
|
|
| 152 |
st.markdown("---")
|
| 153 |
+
st.markdown("*Powered by Streamlit + Transformers | Deployed on Hugging Face Spaces*")
|