Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,18 +8,17 @@ from transformers import pipeline
|
|
| 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.
|
| 12 |
|
| 13 |
# -----------------------------
|
| 14 |
-
# LOAD
|
| 15 |
# -----------------------------
|
| 16 |
@st.cache_resource
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return summarizer, qa_model
|
| 21 |
|
| 22 |
-
|
| 23 |
|
| 24 |
# -----------------------------
|
| 25 |
# FILE UPLOAD
|
|
@@ -31,10 +30,11 @@ text_data = ""
|
|
| 31 |
if uploaded_file:
|
| 32 |
reader = PdfReader(uploaded_file)
|
| 33 |
for page in reader.pages:
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
# -----------------------------
|
| 37 |
-
# TEXT INPUT
|
| 38 |
# -----------------------------
|
| 39 |
text_input = st.text_area("Or paste your text here:")
|
| 40 |
|
|
@@ -42,42 +42,54 @@ if text_input:
|
|
| 42 |
text_data = text_input
|
| 43 |
|
| 44 |
# -----------------------------
|
| 45 |
-
#
|
| 46 |
# -----------------------------
|
| 47 |
if text_data:
|
| 48 |
-
|
| 49 |
-
st.
|
|
|
|
| 50 |
|
| 51 |
# -----------------------------
|
| 52 |
# SIMPLIFY TEXT
|
| 53 |
# -----------------------------
|
| 54 |
if st.button("β¨ Simplify Paragraph"):
|
| 55 |
with st.spinner("Simplifying..."):
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# -----------------------------
|
| 60 |
# QUESTION ANSWERING
|
| 61 |
# -----------------------------
|
| 62 |
-
st.subheader("β Ask a Question
|
| 63 |
question = st.text_input("Enter your question:")
|
| 64 |
|
| 65 |
if question:
|
| 66 |
-
with st.spinner("
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
# -----------------------------
|
| 71 |
-
# WORD
|
| 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 |
-
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
else:
|
| 83 |
st.info("Upload a PDF or paste text to begin.")
|
|
@@ -86,4 +98,4 @@ else:
|
|
| 86 |
# FOOTER
|
| 87 |
# -----------------------------
|
| 88 |
st.markdown("---")
|
| 89 |
-
st.caption("Built with β€οΈ using Streamlit + Hugging Face")
|
|
|
|
| 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. Get simple explanations, summaries, and answers.")
|
| 12 |
|
| 13 |
# -----------------------------
|
| 14 |
+
# LOAD MODEL (LIGHT + STABLE)
|
| 15 |
# -----------------------------
|
| 16 |
@st.cache_resource
|
| 17 |
+
def load_model():
|
| 18 |
+
generator = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 19 |
+
return generator
|
|
|
|
| 20 |
|
| 21 |
+
generator = load_model()
|
| 22 |
|
| 23 |
# -----------------------------
|
| 24 |
# FILE UPLOAD
|
|
|
|
| 30 |
if uploaded_file:
|
| 31 |
reader = PdfReader(uploaded_file)
|
| 32 |
for page in reader.pages:
|
| 33 |
+
if page.extract_text():
|
| 34 |
+
text_data += page.extract_text()
|
| 35 |
|
| 36 |
# -----------------------------
|
| 37 |
+
# TEXT INPUT
|
| 38 |
# -----------------------------
|
| 39 |
text_input = st.text_area("Or paste your text here:")
|
| 40 |
|
|
|
|
| 42 |
text_data = text_input
|
| 43 |
|
| 44 |
# -----------------------------
|
| 45 |
+
# MAIN FUNCTIONALITY
|
| 46 |
# -----------------------------
|
| 47 |
if text_data:
|
| 48 |
+
|
| 49 |
+
st.subheader("π Your Text Preview")
|
| 50 |
+
st.write(text_data[:1500])
|
| 51 |
|
| 52 |
# -----------------------------
|
| 53 |
# SIMPLIFY TEXT
|
| 54 |
# -----------------------------
|
| 55 |
if st.button("β¨ Simplify Paragraph"):
|
| 56 |
with st.spinner("Simplifying..."):
|
| 57 |
+
prompt = f"Explain this in very simple English:\n{text_data[:500]}"
|
| 58 |
+
response = generator(prompt, max_length=150)
|
| 59 |
+
st.success(response[0]['generated_text'])
|
| 60 |
+
|
| 61 |
+
# -----------------------------
|
| 62 |
+
# SUMMARIZE TEXT
|
| 63 |
+
# -----------------------------
|
| 64 |
+
if st.button("π Summarize Text"):
|
| 65 |
+
with st.spinner("Summarizing..."):
|
| 66 |
+
prompt = f"Summarize this text:\n{text_data[:500]}"
|
| 67 |
+
response = generator(prompt, max_length=120)
|
| 68 |
+
st.success(response[0]['generated_text'])
|
| 69 |
|
| 70 |
# -----------------------------
|
| 71 |
# QUESTION ANSWERING
|
| 72 |
# -----------------------------
|
| 73 |
+
st.subheader("β Ask a Question")
|
| 74 |
question = st.text_input("Enter your question:")
|
| 75 |
|
| 76 |
if question:
|
| 77 |
+
with st.spinner("Thinking..."):
|
| 78 |
+
prompt = f"Answer the question based on the text below:\n\nText:\n{text_data[:700]}\n\nQuestion:\n{question}"
|
| 79 |
+
response = generator(prompt, max_length=120)
|
| 80 |
+
st.success(response[0]['generated_text'])
|
| 81 |
|
| 82 |
# -----------------------------
|
| 83 |
+
# WORD EXPLANATION
|
| 84 |
# -----------------------------
|
| 85 |
st.subheader("π Word Explanation")
|
| 86 |
word = st.text_input("Enter a difficult word:")
|
| 87 |
|
| 88 |
if word:
|
|
|
|
| 89 |
with st.spinner("Explaining..."):
|
| 90 |
+
prompt = f"Explain the word '{word}' in simple English and give an example."
|
| 91 |
+
response = generator(prompt, max_length=80)
|
| 92 |
+
st.success(response[0]['generated_text'])
|
| 93 |
|
| 94 |
else:
|
| 95 |
st.info("Upload a PDF or paste text to begin.")
|
|
|
|
| 98 |
# FOOTER
|
| 99 |
# -----------------------------
|
| 100 |
st.markdown("---")
|
| 101 |
+
st.caption("Built with β€οΈ using Streamlit + Hugging Face (FLAN-T5)")
|