Spaces:
Sleeping
Sleeping
| import os | |
| os.environ["TRANSFORMERS_CACHE"] = "./hf_cache" | |
| import streamlit as st | |
| from transformers import pipeline | |
| from streamlit_lottie import st_lottie | |
| import requests | |
| import datetime | |
| def load_lottieurl(url): | |
| r = requests.get(url) | |
| if r.status_code != 200: | |
| return None | |
| return r.json() | |
| lottie_animation = load_lottieurl("https://assets2.lottiefiles.com/packages/lf20_w51pcehl.json") | |
| st.markdown("<h1 style='text-align: center;'>π Text Summarization App</h1>", unsafe_allow_html=True) | |
| st_lottie(lottie_animation, height=250, key="header_anim") | |
| def load_summarizer(model_name): | |
| return pipeline("summarization", model=model_name) | |
| model_map = { | |
| "BART": "facebook/bart-large-cnn", | |
| "T5": "t5-small", | |
| "PEGASUS": "google/pegasus-cnn_dailymail" | |
| } | |
| model_choice = st.selectbox("Choose Summarization Model", list(model_map.keys())) | |
| summarizer = load_summarizer(model_map[model_choice]) | |
| mode = st.radio("Choose Output Mode:", ["Paragraph", "Bullet Points", "Custom"], horizontal=True) | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| st.markdown("### βοΈ Enter Your Text") | |
| user_input = st.text_area("", height=300, placeholder="Paste your job description, article, or any long-form text here...") | |
| word_count = len(user_input.split()) | |
| st.markdown(f"**{word_count} words**") | |
| if mode != "Custom": | |
| length_label = st.radio("Summary Length", ["Short", "Medium"], horizontal=True) | |
| min_len = 40 | |
| max_len = 150 if length_label == "Short" else 300 | |
| else: | |
| st.markdown("### π οΈ Customize Summary Length") | |
| min_len = st.slider("Minimum Length", 20, 200, 50) | |
| max_len = st.slider("Maximum Length", 100, 500, 200) | |
| if st.button("β¨ Summarize", use_container_width=True): | |
| if not user_input.strip(): | |
| st.warning("Please enter text to summarize.") | |
| else: | |
| with st.spinner("Generating your summary... hang tight! β³"): | |
| try: | |
| result = summarizer(user_input, max_length=max_len, min_length=min_len, do_sample=False) | |
| summary = result[0]['summary_text'] | |
| if mode == "Bullet Points": | |
| summary = "β’ " + summary.replace(". ", ".\nβ’ ") | |
| st.session_state["summary"] = summary | |
| except Exception as e: | |
| st.error(f"β οΈ Error during summarization: {e}") | |
| import datetime # β Make sure this is at the top of your file | |
| # Right Column: Output | |
| with col2: | |
| st.markdown("### π Summary Output") | |
| if "summary" in st.session_state: | |
| st.success(st.session_state["summary"]) | |
| summary_words = len(st.session_state["summary"].split()) | |
| st.markdown(f"π {summary_words} words") | |
| # β Direct Download Button | |
| st.download_button( | |
| label="π₯ Download This Summary", | |
| data=st.session_state["summary"], | |
| file_name="summary.txt", | |
| mime="text/plain" | |
| ) | |
| # Save Options in Expander | |
| with st.expander("πΎ Save & View Summary History"): | |
| # Save to history file | |
| if st.button("β Save this summary to history"): | |
| try: | |
| with open("summary_history.txt", "a", encoding="utf-8") as f: | |
| f.write("\n" + "="*50 + "\n") | |
| f.write(f"π Timestamp: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n") | |
| f.write(f"πΉ Model Used: {model_choice}\n") | |
| f.write(f"πΈ Mode: {mode}\n") | |
| f.write(f"π Original Text:\n{user_input.strip()}\n\n") | |
| f.write(f"β Summary:\n{st.session_state['summary'].strip()}\n") | |
| f.write("="*50 + "\n\n") | |
| st.success("π Summary saved to history!") | |
| except Exception as e: | |
| st.error(f"β Failed to save summary: {e}") | |
| # View Summary History | |
| if st.checkbox("π Show Summary History"): | |
| try: | |
| with open("summary_history.txt", "r", encoding="utf-8") as f: | |
| history = f.read() | |
| st.text_area("ποΈ Summary History", value=history, height=300) | |
| except FileNotFoundError: | |
| st.info("No history found yet.") | |
| else: | |
| st.info("Your summary will appear here once generated.") | |
| st.markdown("<hr>", unsafe_allow_html=True) | |
| st.markdown("<small>Built by MULA VAMSHIπ€ using Hugging Face Transformers, Streamlit & Lottie</small>", unsafe_allow_html=True) |