File size: 6,961 Bytes
435b1b4
fa1621b
 
 
 
 
 
 
 
435b1b4
fa1621b
435b1b4
fa1621b
 
 
 
 
 
 
 
435b1b4
fa1621b
 
 
435b1b4
fa1621b
 
 
 
 
 
 
 
 
 
435b1b4
fa1621b
 
435b1b4
 
fa1621b
 
 
 
435b1b4
 
 
 
 
 
 
 
 
fa1621b
 
435b1b4
fa1621b
435b1b4
fa1621b
435b1b4
fa1621b
 
 
435b1b4
fa1621b
 
 
435b1b4
fa1621b
 
435b1b4
fa1621b
435b1b4
fa1621b
 
 
 
 
 
 
 
 
 
 
 
435b1b4
fa1621b
 
 
 
 
 
 
 
435b1b4
fa1621b
435b1b4
fa1621b
 
 
 
 
435b1b4
fa1621b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
435b1b4
fa1621b
 
 
 
 
 
435b1b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa1621b
435b1b4
fa1621b
435b1b4
fa1621b
435b1b4
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# ------------------------๐Ÿ”ง ENVIRONMENT SETUP ------------------------
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
import pandas as pd

# ------------------------๐ŸŽž๏ธ LOAD LOTTIE ANIMATION ------------------------
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")

# ------------------------๐Ÿ“Œ APP TITLE & HEADER ------------------------
st.markdown("<h1 style='text-align: center;'>๐Ÿ“ Text Summarization App</h1>", unsafe_allow_html=True)
st_lottie(lottie_animation, height=250, key="header_anim")

# ------------------------๐Ÿš€ LOAD SUMMARIZATION MODELS ------------------------
@st.cache_resource(show_spinner="๐Ÿ”„ Loading summarization model...")
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])

# ------------------------๐Ÿ› ๏ธ USER INPUT & CONTROLS ------------------------
mode = st.radio("๐Ÿ“ค Choose Output Mode:", ["Paragraph", "Bullet Points", "Custom"], horizontal=True)

col1, col2 = st.columns(2)

with col1:
    st.markdown("### โœ๏ธ Enter Text or Upload File")

    uploaded_file = st.file_uploader("๐Ÿ“‚ Upload .txt file", type=["txt"])

    if uploaded_file is not None:
        user_input = uploaded_file.read().decode("utf-8")
        st.text_area("๐Ÿ“ƒ Uploaded Text Preview", value=user_input, height=200)
    else:
        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**")

    # ๐Ÿ”ง Summary length control
    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)

    # โœจ Generate Summary
    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}")

# ------------------------๐Ÿ“„ SUMMARY OUTPUT & HISTORY ------------------------
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")

        # ๐Ÿ“ฅ Download Summary as TXT
        st.download_button(
            label="๐Ÿ“ฅ Download This Summary (TXT)",
            data=st.session_state["summary"],
            file_name="summary.txt",
            mime="text/plain"
        )

        # ๐Ÿ’พ Save to Summary History
        with st.expander("๐Ÿ’พ Save & View Summary History"):
            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.")

            # ๐Ÿ“Š Export as CSV
            if st.button("โฌ‡๏ธ Export History as CSV"):
                try:
                    summaries = []
                    with open("summary_history.txt", "r", encoding="utf-8") as f:
                        lines = f.read().split("="*50)
                        for entry in lines:
                            if "๐Ÿ•’ Timestamp" in entry:
                                lines_dict = {
                                    "Timestamp": entry.split("๐Ÿ•’ Timestamp: ")[1].split("\n")[0].strip(),
                                    "Model": entry.split("๐Ÿ”น Model Used: ")[1].split("\n")[0].strip(),
                                    "Mode": entry.split("๐Ÿ”ธ Mode: ")[1].split("\n")[0].strip(),
                                    "Original_Text": entry.split("๐Ÿ“ Original Text:\n")[1].split("\n\n")[0].strip(),
                                    "Summary": entry.split("โœ… Summary:\n")[1].strip()
                                }
                                summaries.append(lines_dict)
                    df = pd.DataFrame(summaries)
                    csv = df.to_csv(index=False).encode('utf-8')
                    st.download_button("๐Ÿ“„ Download CSV File", csv, "summary_history.csv", "text/csv")
                except Exception as e:
                    st.error(f"โŒ Failed to export as CSV: {e}")
    else:
        st.info("โ„น๏ธ Your summary will appear here once generated.")

# ------------------------๐Ÿ”š FOOTER ------------------------
st.markdown("<hr>", unsafe_allow_html=True)
st.markdown(
    "<small>๐Ÿš€ Built by <b>MULA VAMSHI๐Ÿค</b> using Hugging Face Transformers, Streamlit & Lottie</small>",
    unsafe_allow_html=True
)