Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
os.environ["TRANSFORMERS_CACHE"] = "./hf_cache"
|
| 3 |
|
|
@@ -6,7 +7,9 @@ from transformers import pipeline
|
|
| 6 |
from streamlit_lottie import st_lottie
|
| 7 |
import requests
|
| 8 |
import datetime
|
|
|
|
| 9 |
|
|
|
|
| 10 |
def load_lottieurl(url):
|
| 11 |
r = requests.get(url)
|
| 12 |
if r.status_code != 200:
|
|
@@ -15,9 +18,11 @@ def load_lottieurl(url):
|
|
| 15 |
|
| 16 |
lottie_animation = load_lottieurl("https://assets2.lottiefiles.com/packages/lf20_w51pcehl.json")
|
| 17 |
|
|
|
|
| 18 |
st.markdown("<h1 style='text-align: center;'>๐ Text Summarization App</h1>", unsafe_allow_html=True)
|
| 19 |
st_lottie(lottie_animation, height=250, key="header_anim")
|
| 20 |
|
|
|
|
| 21 |
@st.cache_resource(show_spinner="๐ Loading summarization model...")
|
| 22 |
def load_summarizer(model_name):
|
| 23 |
return pipeline("summarization", model=model_name)
|
|
@@ -28,34 +33,44 @@ model_map = {
|
|
| 28 |
"PEGASUS": "google/pegasus-cnn_dailymail"
|
| 29 |
}
|
| 30 |
|
| 31 |
-
model_choice = st.selectbox("Choose Summarization Model", list(model_map.keys()))
|
| 32 |
summarizer = load_summarizer(model_map[model_choice])
|
| 33 |
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
col1, col2 = st.columns(2)
|
| 37 |
|
| 38 |
with col1:
|
| 39 |
-
st.markdown("### โ๏ธ Enter
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
word_count = len(user_input.split())
|
| 43 |
-
st.markdown(f"
|
| 44 |
|
|
|
|
| 45 |
if mode != "Custom":
|
| 46 |
-
length_label = st.radio("Summary Length", ["Short", "Medium"], horizontal=True)
|
| 47 |
min_len = 40
|
| 48 |
max_len = 150 if length_label == "Short" else 300
|
| 49 |
else:
|
| 50 |
-
st.markdown("###
|
| 51 |
min_len = st.slider("Minimum Length", 20, 200, 50)
|
| 52 |
max_len = st.slider("Maximum Length", 100, 500, 200)
|
| 53 |
|
|
|
|
| 54 |
if st.button("โจ Summarize", use_container_width=True):
|
| 55 |
if not user_input.strip():
|
| 56 |
-
st.warning("Please enter text to summarize.")
|
| 57 |
else:
|
| 58 |
-
with st.spinner("Generating your summary... hang tight! โณ"):
|
| 59 |
try:
|
| 60 |
result = summarizer(user_input, max_length=max_len, min_length=min_len, do_sample=False)
|
| 61 |
summary = result[0]['summary_text']
|
|
@@ -68,9 +83,7 @@ with col1:
|
|
| 68 |
except Exception as e:
|
| 69 |
st.error(f"โ ๏ธ Error during summarization: {e}")
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
# Right Column: Output
|
| 74 |
with col2:
|
| 75 |
st.markdown("### ๐ Summary Output")
|
| 76 |
|
|
@@ -79,17 +92,16 @@ with col2:
|
|
| 79 |
summary_words = len(st.session_state["summary"].split())
|
| 80 |
st.markdown(f"๐ {summary_words} words")
|
| 81 |
|
| 82 |
-
#
|
| 83 |
st.download_button(
|
| 84 |
-
label="๐ฅ Download This Summary",
|
| 85 |
data=st.session_state["summary"],
|
| 86 |
file_name="summary.txt",
|
| 87 |
mime="text/plain"
|
| 88 |
)
|
| 89 |
|
| 90 |
-
# Save
|
| 91 |
with st.expander("๐พ Save & View Summary History"):
|
| 92 |
-
# Save to history file
|
| 93 |
if st.button("โ
Save this summary to history"):
|
| 94 |
try:
|
| 95 |
with open("summary_history.txt", "a", encoding="utf-8") as f:
|
|
@@ -104,16 +116,42 @@ with col2:
|
|
| 104 |
except Exception as e:
|
| 105 |
st.error(f"โ Failed to save summary: {e}")
|
| 106 |
|
| 107 |
-
# View Summary History
|
| 108 |
if st.checkbox("๐ Show Summary History"):
|
| 109 |
try:
|
| 110 |
with open("summary_history.txt", "r", encoding="utf-8") as f:
|
| 111 |
history = f.read()
|
| 112 |
st.text_area("๐๏ธ Summary History", value=history, height=300)
|
| 113 |
except FileNotFoundError:
|
| 114 |
-
st.info("No history found yet.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
else:
|
| 116 |
-
st.info("Your summary will appear here once generated.")
|
| 117 |
|
|
|
|
| 118 |
st.markdown("<hr>", unsafe_allow_html=True)
|
| 119 |
-
st.markdown(
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ------------------------๐ง ENVIRONMENT SETUP ------------------------
|
| 2 |
import os
|
| 3 |
os.environ["TRANSFORMERS_CACHE"] = "./hf_cache"
|
| 4 |
|
|
|
|
| 7 |
from streamlit_lottie import st_lottie
|
| 8 |
import requests
|
| 9 |
import datetime
|
| 10 |
+
import pandas as pd
|
| 11 |
|
| 12 |
+
# ------------------------๐๏ธ LOAD LOTTIE ANIMATION ------------------------
|
| 13 |
def load_lottieurl(url):
|
| 14 |
r = requests.get(url)
|
| 15 |
if r.status_code != 200:
|
|
|
|
| 18 |
|
| 19 |
lottie_animation = load_lottieurl("https://assets2.lottiefiles.com/packages/lf20_w51pcehl.json")
|
| 20 |
|
| 21 |
+
# ------------------------๐ APP TITLE & HEADER ------------------------
|
| 22 |
st.markdown("<h1 style='text-align: center;'>๐ Text Summarization App</h1>", unsafe_allow_html=True)
|
| 23 |
st_lottie(lottie_animation, height=250, key="header_anim")
|
| 24 |
|
| 25 |
+
# ------------------------๐ LOAD SUMMARIZATION MODELS ------------------------
|
| 26 |
@st.cache_resource(show_spinner="๐ Loading summarization model...")
|
| 27 |
def load_summarizer(model_name):
|
| 28 |
return pipeline("summarization", model=model_name)
|
|
|
|
| 33 |
"PEGASUS": "google/pegasus-cnn_dailymail"
|
| 34 |
}
|
| 35 |
|
| 36 |
+
model_choice = st.selectbox("๐ Choose Summarization Model", list(model_map.keys()))
|
| 37 |
summarizer = load_summarizer(model_map[model_choice])
|
| 38 |
|
| 39 |
+
# ------------------------๐ ๏ธ USER INPUT & CONTROLS ------------------------
|
| 40 |
+
mode = st.radio("๐ค Choose Output Mode:", ["Paragraph", "Bullet Points", "Custom"], horizontal=True)
|
| 41 |
|
| 42 |
col1, col2 = st.columns(2)
|
| 43 |
|
| 44 |
with col1:
|
| 45 |
+
st.markdown("### โ๏ธ Enter Text or Upload File")
|
| 46 |
+
|
| 47 |
+
uploaded_file = st.file_uploader("๐ Upload .txt file", type=["txt"])
|
| 48 |
+
|
| 49 |
+
if uploaded_file is not None:
|
| 50 |
+
user_input = uploaded_file.read().decode("utf-8")
|
| 51 |
+
st.text_area("๐ Uploaded Text Preview", value=user_input, height=200)
|
| 52 |
+
else:
|
| 53 |
+
user_input = st.text_area("", height=300, placeholder="Paste your job description, article, or any long-form text here...")
|
| 54 |
|
| 55 |
word_count = len(user_input.split())
|
| 56 |
+
st.markdown(f"**๐งฎ {word_count} words**")
|
| 57 |
|
| 58 |
+
# ๐ง Summary length control
|
| 59 |
if mode != "Custom":
|
| 60 |
+
length_label = st.radio("๐ Summary Length", ["Short", "Medium"], horizontal=True)
|
| 61 |
min_len = 40
|
| 62 |
max_len = 150 if length_label == "Short" else 300
|
| 63 |
else:
|
| 64 |
+
st.markdown("### ๐๏ธ Customize Summary Length")
|
| 65 |
min_len = st.slider("Minimum Length", 20, 200, 50)
|
| 66 |
max_len = st.slider("Maximum Length", 100, 500, 200)
|
| 67 |
|
| 68 |
+
# โจ Generate Summary
|
| 69 |
if st.button("โจ Summarize", use_container_width=True):
|
| 70 |
if not user_input.strip():
|
| 71 |
+
st.warning("โ ๏ธ Please enter text to summarize.")
|
| 72 |
else:
|
| 73 |
+
with st.spinner("๐ Generating your summary... hang tight! โณ"):
|
| 74 |
try:
|
| 75 |
result = summarizer(user_input, max_length=max_len, min_length=min_len, do_sample=False)
|
| 76 |
summary = result[0]['summary_text']
|
|
|
|
| 83 |
except Exception as e:
|
| 84 |
st.error(f"โ ๏ธ Error during summarization: {e}")
|
| 85 |
|
| 86 |
+
# ------------------------๐ SUMMARY OUTPUT & HISTORY ------------------------
|
|
|
|
|
|
|
| 87 |
with col2:
|
| 88 |
st.markdown("### ๐ Summary Output")
|
| 89 |
|
|
|
|
| 92 |
summary_words = len(st.session_state["summary"].split())
|
| 93 |
st.markdown(f"๐ {summary_words} words")
|
| 94 |
|
| 95 |
+
# ๐ฅ Download Summary as TXT
|
| 96 |
st.download_button(
|
| 97 |
+
label="๐ฅ Download This Summary (TXT)",
|
| 98 |
data=st.session_state["summary"],
|
| 99 |
file_name="summary.txt",
|
| 100 |
mime="text/plain"
|
| 101 |
)
|
| 102 |
|
| 103 |
+
# ๐พ Save to Summary History
|
| 104 |
with st.expander("๐พ Save & View Summary History"):
|
|
|
|
| 105 |
if st.button("โ
Save this summary to history"):
|
| 106 |
try:
|
| 107 |
with open("summary_history.txt", "a", encoding="utf-8") as f:
|
|
|
|
| 116 |
except Exception as e:
|
| 117 |
st.error(f"โ Failed to save summary: {e}")
|
| 118 |
|
| 119 |
+
# ๐ View Summary History
|
| 120 |
if st.checkbox("๐ Show Summary History"):
|
| 121 |
try:
|
| 122 |
with open("summary_history.txt", "r", encoding="utf-8") as f:
|
| 123 |
history = f.read()
|
| 124 |
st.text_area("๐๏ธ Summary History", value=history, height=300)
|
| 125 |
except FileNotFoundError:
|
| 126 |
+
st.info("โน๏ธ No history found yet.")
|
| 127 |
+
|
| 128 |
+
# ๐ Export as CSV
|
| 129 |
+
if st.button("โฌ๏ธ Export History as CSV"):
|
| 130 |
+
try:
|
| 131 |
+
summaries = []
|
| 132 |
+
with open("summary_history.txt", "r", encoding="utf-8") as f:
|
| 133 |
+
lines = f.read().split("="*50)
|
| 134 |
+
for entry in lines:
|
| 135 |
+
if "๐ Timestamp" in entry:
|
| 136 |
+
lines_dict = {
|
| 137 |
+
"Timestamp": entry.split("๐ Timestamp: ")[1].split("\n")[0].strip(),
|
| 138 |
+
"Model": entry.split("๐น Model Used: ")[1].split("\n")[0].strip(),
|
| 139 |
+
"Mode": entry.split("๐ธ Mode: ")[1].split("\n")[0].strip(),
|
| 140 |
+
"Original_Text": entry.split("๐ Original Text:\n")[1].split("\n\n")[0].strip(),
|
| 141 |
+
"Summary": entry.split("โ
Summary:\n")[1].strip()
|
| 142 |
+
}
|
| 143 |
+
summaries.append(lines_dict)
|
| 144 |
+
df = pd.DataFrame(summaries)
|
| 145 |
+
csv = df.to_csv(index=False).encode('utf-8')
|
| 146 |
+
st.download_button("๐ Download CSV File", csv, "summary_history.csv", "text/csv")
|
| 147 |
+
except Exception as e:
|
| 148 |
+
st.error(f"โ Failed to export as CSV: {e}")
|
| 149 |
else:
|
| 150 |
+
st.info("โน๏ธ Your summary will appear here once generated.")
|
| 151 |
|
| 152 |
+
# ------------------------๐ FOOTER ------------------------
|
| 153 |
st.markdown("<hr>", unsafe_allow_html=True)
|
| 154 |
+
st.markdown(
|
| 155 |
+
"<small>๐ Built by <b>MULA VAMSHI๐ค</b> using Hugging Face Transformers, Streamlit & Lottie</small>",
|
| 156 |
+
unsafe_allow_html=True
|
| 157 |
+
)
|