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import FPDF
|
| 6 |
+
import tempfile
|
| 7 |
+
import os
|
| 8 |
+
import requests
|
| 9 |
+
|
| 10 |
+
# ------------- PAGE SETUP -------------
|
| 11 |
+
st.set_page_config(page_title="Visual Summarizer", page_icon="π§ ", layout="centered")
|
| 12 |
+
|
| 13 |
+
# ------------- THEME TOGGLE -------------
|
| 14 |
+
theme = st.sidebar.radio("π Theme", ["Light", "Dark"])
|
| 15 |
+
summary_type = st.sidebar.selectbox("π Summary Length", ["Brief", "Standard", "Detailed"])
|
| 16 |
+
translate_to = st.sidebar.selectbox("π Translate Summary To", ["None", "Urdu", "French", "Spanish"])
|
| 17 |
+
|
| 18 |
+
if theme == "Dark":
|
| 19 |
+
bg_color = "#212121"
|
| 20 |
+
text_color = "white"
|
| 21 |
+
card_bg = "#424242"
|
| 22 |
+
else:
|
| 23 |
+
bg_color = "#f7fafd"
|
| 24 |
+
text_color = "#333"
|
| 25 |
+
card_bg = "#e3f2fd"
|
| 26 |
+
|
| 27 |
+
st.markdown(f"""
|
| 28 |
+
<style>
|
| 29 |
+
body {{ background-color: {bg_color}; color: {text_color}; }}
|
| 30 |
+
.summary-card {{
|
| 31 |
+
background-color: {card_bg};
|
| 32 |
+
padding: 15px;
|
| 33 |
+
border-radius: 10px;
|
| 34 |
+
box-shadow: 2px 2px 6px #aaa;
|
| 35 |
+
color: {text_color};
|
| 36 |
+
}}
|
| 37 |
+
.title {{ text-align: center; font-size: 32px; color: #4A148C; }}
|
| 38 |
+
.subtitle {{ text-align: center; color: gray; }}
|
| 39 |
+
</style>
|
| 40 |
+
""", unsafe_allow_html=True)
|
| 41 |
+
|
| 42 |
+
# ------------- LOAD SUMMARIZER -------------
|
| 43 |
+
@st.cache_resource
|
| 44 |
+
def load_model():
|
| 45 |
+
return pipeline("summarization", model="facebook/bart-large-cnn")
|
| 46 |
+
|
| 47 |
+
summarizer = load_model()
|
| 48 |
+
|
| 49 |
+
# ------------- HEADER -------------
|
| 50 |
+
st.markdown("<h1 class='title'>π§ Visual Text Summarizer & Learner</h1>", unsafe_allow_html=True)
|
| 51 |
+
st.markdown("<p class='subtitle'>Summarize, visualize, download, translate, and even explore YouTube to learn more!</p>", unsafe_allow_html=True)
|
| 52 |
+
st.markdown("---")
|
| 53 |
+
|
| 54 |
+
# ------------- INPUT SECTION -------------
|
| 55 |
+
text_input = st.text_area("π₯ Paste your text here (or upload a file below):", height=250)
|
| 56 |
+
uploaded_file = st.file_uploader("π Upload a .txt or .pdf file", type=["txt", "pdf"])
|
| 57 |
+
|
| 58 |
+
def extract_text(file):
|
| 59 |
+
if file.name.endswith(".txt"):
|
| 60 |
+
return file.read().decode("utf-8")
|
| 61 |
+
elif file.name.endswith(".pdf"):
|
| 62 |
+
with pdfplumber.open(file) as pdf:
|
| 63 |
+
return "\n".join([page.extract_text() for page in pdf.pages if page.extract_text()])
|
| 64 |
+
return ""
|
| 65 |
+
|
| 66 |
+
input_text = extract_text(uploaded_file) if uploaded_file else text_input
|
| 67 |
+
|
| 68 |
+
# ------------- SUMMARY LENGTH CONFIG -------------
|
| 69 |
+
if summary_type == "Brief":
|
| 70 |
+
max_len, min_len = 60, 20
|
| 71 |
+
elif summary_type == "Detailed":
|
| 72 |
+
max_len, min_len = 200, 50
|
| 73 |
+
else:
|
| 74 |
+
max_len, min_len = 130, 30
|
| 75 |
+
|
| 76 |
+
# ------------- TRANSLATION FUNCTION -------------
|
| 77 |
+
def translate_text(text, lang_code):
|
| 78 |
+
try:
|
| 79 |
+
url = "https://translate.googleapis.com/translate_a/single"
|
| 80 |
+
params = {
|
| 81 |
+
"client": "gtx",
|
| 82 |
+
"sl": "en",
|
| 83 |
+
"tl": lang_code,
|
| 84 |
+
"dt": "t",
|
| 85 |
+
"q": text
|
| 86 |
+
}
|
| 87 |
+
response = requests.get(url, params=params)
|
| 88 |
+
translated = response.json()[0]
|
| 89 |
+
return ''.join([i[0] for i in translated])
|
| 90 |
+
except:
|
| 91 |
+
return "β οΈ Translation failed."
|
| 92 |
+
|
| 93 |
+
# ------------- MAIN LOGIC -------------
|
| 94 |
+
if st.button("β¨ Summarize & Visualize"):
|
| 95 |
+
if len(input_text.strip()) < 20:
|
| 96 |
+
st.warning("β οΈ Please provide more content.")
|
| 97 |
+
else:
|
| 98 |
+
with st.spinner("π Summarizing..."):
|
| 99 |
+
summary = summarizer(input_text, max_length=max_len, min_length=min_len, do_sample=False)[0]["summary_text"]
|
| 100 |
+
|
| 101 |
+
# ----- Summary Display -----
|
| 102 |
+
st.markdown("### βοΈ Summary:")
|
| 103 |
+
st.markdown(f"<div class='summary-card'>{summary}</div>", unsafe_allow_html=True)
|
| 104 |
+
|
| 105 |
+
# ----- Visual Diagram -----
|
| 106 |
+
st.markdown("### π§ Visual Mind Map:")
|
| 107 |
+
dot = Digraph()
|
| 108 |
+
dot.node("Summary", "Summary", shape="box", style="filled", color="skyblue")
|
| 109 |
+
|
| 110 |
+
for i, sent in enumerate(summary.split(". ")[:6]):
|
| 111 |
+
dot.node(f"node{i}", sent.strip(), shape="ellipse", style="filled", color="lightgreen")
|
| 112 |
+
dot.edge("Summary", f"node{i}")
|
| 113 |
+
|
| 114 |
+
st.graphviz_chart(dot)
|
| 115 |
+
|
| 116 |
+
# ----- PDF Export -----
|
| 117 |
+
pdf = FPDF()
|
| 118 |
+
pdf.add_page()
|
| 119 |
+
pdf.set_font("Arial", size=12)
|
| 120 |
+
pdf.multi_cell(0, 10, "Summary Report\n", align="C")
|
| 121 |
+
pdf.set_font("Arial", size=11)
|
| 122 |
+
pdf.multi_cell(0, 10, summary)
|
| 123 |
+
|
| 124 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_pdf:
|
| 125 |
+
pdf.output(tmp_pdf.name)
|
| 126 |
+
with open(tmp_pdf.name, "rb") as f:
|
| 127 |
+
st.download_button("π Download as PDF", f, file_name="summary.pdf", mime="application/pdf")
|
| 128 |
+
os.remove(tmp_pdf.name)
|
| 129 |
+
|
| 130 |
+
# ----- Translation -----
|
| 131 |
+
if translate_to != "None":
|
| 132 |
+
lang_map = {"Urdu": "ur", "French": "fr", "Spanish": "es"}
|
| 133 |
+
translated = translate_text(summary, lang_map[translate_to])
|
| 134 |
+
st.markdown(f"### π Translated Summary ({translate_to}):")
|
| 135 |
+
st.success(translated)
|
| 136 |
+
|
| 137 |
+
# ----- YouTube Video Search -----
|
| 138 |
+
st.markdown("### πΊ Learn More on YouTube:")
|
| 139 |
+
query = summary.split(".")[0]
|
| 140 |
+
youtube_link = f"https://www.youtube.com/results?search_query={query.replace(' ', '+')}"
|
| 141 |
+
st.markdown(f"[π Click to search YouTube for: `{query}`]({youtube_link})")
|
| 142 |
+
|
| 143 |
+
# ------------- FOOTER -------------
|
| 144 |
+
st.markdown("---")
|
| 145 |
+
st.markdown("<p style='text-align:center; color:gray;'>Made with β€οΈ by a 4th Semester Software Engineer π</p>", unsafe_allow_html=True)
|