aesfsf/ai_document_analyzer
0
1import os2os.environ["TRANSFORMERS_NO_TF"] = "1"3 4import streamlit as st5import PyPDF26import docx7from textblob import TextBlob8from collections import Counter9import re10from transformers import pipeline11from wordcloud import WordCloud12import matplotlib.pyplot as plt13from io import BytesIO14from reportlab.lib.pagesizes import letter15from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, ListFlowable, ListItem16from reportlab.lib.styles import getSampleStyleSheet17from reportlab.lib import colors18from reportlab.lib.units import inch19from reportlab.lib.enums import TA_CENTER20from reportlab.lib.styles import ParagraphStyle21import spacy22 23 24st.set_page_config(25 page_title="AI Document Analyzer",26 page_icon="๐ง ",27 layout="wide"28)29 30 31 32# -----------------------------33# Load SpaCy Model (simple English model for sentence splitting)34# -----------------------------35@st.cache_resource36def load_spacy():37 nlp = spacy.blank("en")38 nlp.add_pipe("sentencizer")39 return nlp40 41 42nlp = load_spacy()43 44 45# -----------------------------46# Load Summarizer47# -----------------------------48@st.cache_resource49def load_summarizer():50 return pipeline(51 "summarization",52 model="sshleifer/distilbart-cnn-12-6"53 )54 55 56summarizer = load_summarizer()57 58 59# --------------------------------60# Extract Text61# --------------------------------62def extract_text_from_pdf(uploaded_file):63 text = ""64 reader = PyPDF2.PdfReader(uploaded_file)65 for page in reader.pages:66 ptext = page.extract_text()67 if ptext:68 text += ptext + "\n"69 return text70 71 72def extract_text_from_docx(uploaded_file):73 doc_file = docx.Document(uploaded_file)74 return "\n".join([p.text for p in doc_file.paragraphs])75 76 77def clean_text(text):78 return re.sub(r"\s+", " ", text.strip())79 80 81# --------------------------------82# Topic Segmentation83# --------------------------------84def segment_topics(text, chunk_size=300):85 sentences = list(nlp(text).sents)86 topics = []87 chunk = ""88 for s in sentences:89 if len(chunk) + len(s.text) < chunk_size:90 chunk += " " + s.text91 else:92 topics.append(chunk.strip())93 chunk = s.text94 if chunk:95 topics.append(chunk.strip())96 return topics97 98 99# --------------------------------100# Text Analysis101# --------------------------------102def analyze_text(text):103 doc = nlp(text)104 blob = TextBlob(text)105 106 sentiment = blob.sentiment.polarity107 108 stopwords = nlp.Defaults.stop_words109 words = [w.lower() for w in re.findall(r"\b\w+\b", text) if w.lower() not in stopwords]110 111 keywords = Counter(words).most_common(10)112 entities = [(ent.text, ent.label_) for ent in doc.ents]113 114 return sentiment, keywords, entities, words115 116 117# --------------------------------118# AI Prompt Generator119# --------------------------------120def create_prompt(text, mode):121 if mode == "Summarize":122 return f"Summarize this clearly:\n{text}"123 124 if mode == "Explain Like a Teacher":125 return f"Explain this like a teacher with examples:\n{text}"126 127 if mode == "Generate Quiz Questions":128 return f"Generate 5 quiz questions from this content:\n{text}"129 130 if mode == "Create Flashcards":131 return f"Make flashcards in Q:A format:\n{text}"132 133 134# --------------------------------135# Chunk-based summarizer136# --------------------------------137def split_into_chunks(text, max_words=350):138 words = text.split()139 chunks = []140 current = []141 142 for w in words:143 current.append(w)144 if len(current) >= max_words:145 chunks.append(" ".join(current))146 current = []147 148 if current:149 chunks.append(" ".join(current))150 151 return chunks152 153 154def generate_ai_output(text, mode):155 chunks = split_into_chunks(text)156 final_output = ""157 158 for chunk in chunks:159 prompt = create_prompt(chunk, mode)160 161 response = summarizer(162 prompt,163 max_length=200,164 min_length=50,165 do_sample=False166 )167 168 final_output += response[0]["summary_text"] + "\n\n"169 170 return final_output.strip()171 172 173# --------------------------------174# Word Cloud175# --------------------------------176def generate_wordcloud(words):177 wc = WordCloud(width=800, height=400, background_color="white").generate(" ".join(words))178 179 fig, ax = plt.subplots(figsize=(10, 5))180 ax.imshow(wc, interpolation="bilinear")181 ax.axis("off")182 st.pyplot(fig)183 184 buf = BytesIO()185 fig.savefig(buf, format="png")186 buf.seek(0)187 return buf188 189 190# --------------------------------191# PDF Report192# --------------------------------193def create_pdf(summary, sentiment, keywords, entities, wordcloud_buf):194 buffer = BytesIO()195 doc = SimpleDocTemplate(buffer, pagesize=letter)196 197 styles = getSampleStyleSheet()198 title = ParagraphStyle(199 "Title",200 fontSize=22,201 alignment=TA_CENTER,202 textColor=colors.HexColor("#004AAD")203 )204 205 story = [Paragraph("๐ง AI Document Analyzer Report", title), Spacer(1, 20)]206 207 story.append(Paragraph("๐ Summary", styles["Heading2"]))208 story.append(Paragraph(summary, styles["BodyText"]))209 story.append(Spacer(1, 10))210 211 story.append(Paragraph("๐ Sentiment", styles["Heading2"]))212 story.append(Paragraph(f"Polarity Score: {sentiment}", styles["BodyText"]))213 story.append(Spacer(1, 10))214 215 story.append(Paragraph("๐ Keywords", styles["Heading2"]))216 story.append(ListFlowable([ListItem(Paragraph(f"{w}: {c}", styles["BodyText"])) for w, c in keywords]))217 story.append(Spacer(1, 10))218 219 story.append(Paragraph("๐ท๏ธ Named Entities", styles["Heading2"]))220 if entities:221 story.append(ListFlowable([222 ListItem(Paragraph(f"{e} ({l})", styles["BodyText"])) for e, l in entities223 ]))224 else:225 story.append(Paragraph("No entities found.", styles["BodyText"]))226 227 story.append(Spacer(1, 12))228 229 if wordcloud_buf:230 story.append(Paragraph("โ๏ธ Word Cloud", styles["Heading2"]))231 story.append(Image(wordcloud_buf, width=5.5 * inch))232 233 doc.build(story)234 buffer.seek(0)235 return buffer236 237 238# --------------------------------239# STREAMLIT UI240# --------------------------------241 242 243 244st.markdown("Upload a document and get **AI-powered summaries, quizzes, explanations, keywords, and more.**")245 246uploaded_file = st.file_uploader("๐ Upload PDF / DOCX / TXT", type=["pdf", "docx", "txt"])247 248if uploaded_file:249 ext = uploaded_file.name.split(".")[-1].lower()250 251 with st.spinner("๐ฅ Extracting text..."):252 if ext == "pdf":253 text = extract_text_from_pdf(uploaded_file)254 elif ext == "docx":255 text = extract_text_from_docx(uploaded_file)256 else:257 text = uploaded_file.read().decode("utf-8")258 259 text = clean_text(text)260 261 if len(text) < 50:262 st.error("Not enough content to analyze.")263 st.stop()264 265 st.text_area("๐ Extracted Text", text[:1000] + "...", height=250)266 267 topics = segment_topics(text)268 269 st.markdown("### ๐งฉ Detected Topics")270 for i, t in enumerate(topics[:5]):271 st.write(f"**Topic {i+1}:** {t[:200]}...")272 273 st.markdown("### ๐๏ธ Choose AI Mode")274 mode = st.selectbox(275 "What should the AI do?",276 ["Summarize", "Explain Like a Teacher", "Generate Quiz Questions", "Create Flashcards"]277 )278 279 with st.spinner("๐ค AI Thinking..."):280 result = generate_ai_output(text, mode)281 282 st.markdown(f"### ๐ฏ AI Output โ *{mode}*")283 st.write(result)284 285 st.markdown("### ๐ Additional Analysis")286 287 sentiment, keywords, entities, words = analyze_text(text)288 289 st.write("#### ๐ Sentiment Polarity:", sentiment)290 291 st.write("#### ๐ Keywords:")292 for w, c in keywords:293 st.write(f"- {w}: {c}")294 295 st.write("#### ๐ท๏ธ Named Entities:")296 if entities:297 for e, l in entities:298 st.write(f"- {e} ({l})")299 else:300 st.write("No entities found.")301 302 st.write("#### โ๏ธ Word Cloud:")303 wordcloud_buf = generate_wordcloud(words)304 305 pdf_buf = create_pdf(result, sentiment, keywords, entities, wordcloud_buf)306 307 st.download_button(308 "๐ฅ Download PDF Report",309 pdf_buf,310 "AI_Document_Report.pdf",311 "application/pdf"312 )313 