Summary / app.py
hadheedo's picture
Update app.py
1b3a96f verified
raw
history blame
3.21 kB
import streamlit as st
from transformers import T5Tokenizer, T5ForConditionalGeneration
import torch
# Load model and tokenizer
model_path = "./saved_model"
tokenizer_path = "./saved_tokenizer"
tokenizer = T5Tokenizer.from_pretrained(tokenizer_path)
model = T5ForConditionalGeneration.from_pretrained(model_path)
# Function to generate summary
def generate_summary(text):
inputs = ["summarize: " + text]
inputs = tokenizer(inputs, max_length=1024, truncation=True, return_tensors="pt")
outputs = model.generate(inputs.input_ids.to(model.device), max_length=150, length_penalty=2.0, num_beams=4, early_stopping=True)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
# Custom CSS styling for a clean, modern interface
st.markdown("""
<style>
.main {
background-color: #f0f2f6;
background-image: linear-gradient(135deg, #e6f7ff 0%, #b3e0ff 100%);
font-family: 'Arial', sans-serif;
}
.stTextArea textarea {
border: 2px solid #0078d4;
border-radius: 12px;
padding: 15px;
font-family: 'Segoe UI', sans-serif;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 800px;
margin: 20px auto;
font-size: 16px;
}
.stTextArea textarea:focus {
border-color: #0058a3;
box-shadow: 0 0 10px #4f8bf9;
}
.stButton>button {
background-color: #29b6f6;
color: white;
border-radius: 25px;
border: none;
padding: 12px 30px;
font-size: 18px;
font-weight: bold;
box-shadow: 0 4px 12px rgba(41, 182, 246, 0.3);
transition: all 0.3s ease;
}
.stButton>button:hover {
background-color: #0288d1;
box-shadow: 0 6px 14px rgba(2, 136, 209, 0.4);
transform: translateY(-2px);
}
.stTitle {
color: #0078d4;
font-size: 2.5em;
text-align: center;
margin-bottom: 20px;
font-family: 'Segoe UI', sans-serif;
}
.summary-container {
background-color: #ffffff;
border-radius: 12px;
padding: 20px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
margin-top: 20px;
max-width: 800px;
margin: 20px auto;
}
.summary-title {
color: #0288d1;
font-weight: bold;
font-size: 1.5em;
margin-bottom: 10px;
}
.footer {
text-align: center;
margin-top: 50px;
padding: 20px;
color: #0078d4;
font-style: italic;
}
</style>
""", unsafe_allow_html=True)
# Application UI
st.title("πŸ“ Text Summarizer App")
text = st.text_area("Enter the text you want to summarize...", height=200)
if st.button("Generate Summary"):
if text:
with st.spinner("Generating summary..."):
summary = generate_summary(text)
st.markdown('<div class="summary-container"><div class="summary-title">πŸ“‹ Summary</div>' +
summary + '</div>', unsafe_allow_html=True)
else:
st.warning("⚠️ Please enter text to summarize.")
# Footer
st.markdown("""
<div class="footer">
Created with hadheedo
</div>
""", unsafe_allow_html=True)