aakashdevdat commited on
Commit
e1b9af2
·
verified ·
1 Parent(s): 25a9ca2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from pypdf import PdfReader
3
+ from transformers import pipeline
4
+
5
+ # -----------------------------
6
+ # PAGE CONFIG
7
+ # -----------------------------
8
+ st.set_page_config(page_title="AI Reading Assistant", layout="wide")
9
+
10
+ st.title("📚 AI Reading Assistant")
11
+ st.write("Upload a PDF or paste text. Click a word/sentence to get explanation, meaning, and simplification.")
12
+
13
+ # -----------------------------
14
+ # LOAD MODELS (FREE HF MODELS)
15
+ # -----------------------------
16
+ @st.cache_resource
17
+ def load_models():
18
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
19
+ qa_model = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
20
+ return summarizer, qa_model
21
+
22
+ summarizer, qa_model = load_models()
23
+
24
+ # -----------------------------
25
+ # FILE UPLOAD
26
+ # -----------------------------
27
+ uploaded_file = st.file_uploader("Upload PDF", type=["pdf"])
28
+
29
+ text_data = ""
30
+
31
+ if uploaded_file:
32
+ reader = PdfReader(uploaded_file)
33
+ for page in reader.pages:
34
+ text_data += page.extract_text()
35
+
36
+ # -----------------------------
37
+ # TEXT INPUT OPTION
38
+ # -----------------------------
39
+ text_input = st.text_area("Or paste your text here:")
40
+
41
+ if text_input:
42
+ text_data = text_input
43
+
44
+ # -----------------------------
45
+ # DISPLAY TEXT
46
+ # -----------------------------
47
+ if text_data:
48
+ st.subheader("📄 Your Text")
49
+ st.write(text_data[:2000]) # limit display for performance
50
+
51
+ # -----------------------------
52
+ # SIMPLIFY TEXT
53
+ # -----------------------------
54
+ if st.button("✨ Simplify Paragraph"):
55
+ with st.spinner("Simplifying..."):
56
+ summary = summarizer(text_data[:1000], max_length=120, min_length=30, do_sample=False)
57
+ st.success(summary[0]['summary_text'])
58
+
59
+ # -----------------------------
60
+ # QUESTION ANSWERING
61
+ # -----------------------------
62
+ st.subheader("❓ Ask a Question from Text")
63
+ question = st.text_input("Enter your question:")
64
+
65
+ if question:
66
+ with st.spinner("Finding answer..."):
67
+ answer = qa_model(question=question, context=text_data[:2000])
68
+ st.success(f"Answer: {answer['answer']}")
69
+
70
+ # -----------------------------
71
+ # WORD MEANING (SIMPLE)
72
+ # -----------------------------
73
+ st.subheader("🔍 Word Explanation")
74
+ word = st.text_input("Enter a difficult word:")
75
+
76
+ if word:
77
+ explanation_prompt = f"Explain the meaning of the word '{word}' in simple English with an example."
78
+ with st.spinner("Explaining..."):
79
+ explanation = summarizer(explanation_prompt, max_length=60, min_length=20, do_sample=False)
80
+ st.success(explanation[0]['summary_text'])
81
+
82
+ else:
83
+ st.info("Upload a PDF or paste text to begin.")
84
+
85
+ # -----------------------------
86
+ # FOOTER
87
+ # -----------------------------
88
+ st.markdown("---")
89
+ st.caption("Built with ❤️ using Streamlit + Hugging Face")