File size: 3,162 Bytes
e1b9af2
 
 
 
 
 
 
 
 
 
6f3b6ef
e1b9af2
 
6f3b6ef
e1b9af2
 
6f3b6ef
 
 
e1b9af2
6f3b6ef
e1b9af2
 
 
 
 
 
 
 
 
 
 
6f3b6ef
 
e1b9af2
 
6f3b6ef
e1b9af2
 
 
 
 
 
 
6f3b6ef
e1b9af2
 
6f3b6ef
 
 
e1b9af2
 
 
 
 
 
6f3b6ef
 
 
 
 
 
 
 
 
 
 
 
e1b9af2
 
 
 
6f3b6ef
e1b9af2
 
 
6f3b6ef
 
 
 
e1b9af2
 
6f3b6ef
e1b9af2
 
 
 
 
 
6f3b6ef
 
 
e1b9af2
 
 
 
 
 
 
 
6f3b6ef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import streamlit as st
from pypdf import PdfReader
from transformers import pipeline

# -----------------------------
# PAGE CONFIG
# -----------------------------
st.set_page_config(page_title="AI Reading Assistant", layout="wide")

st.title("πŸ“š AI Reading Assistant")
st.write("Upload a PDF or paste text. Get simple explanations, summaries, and answers.")

# -----------------------------
# LOAD MODEL (LIGHT + STABLE)
# -----------------------------
@st.cache_resource
def load_model():
    generator = pipeline("text2text-generation", model="google/flan-t5-base")
    return generator

generator = load_model()

# -----------------------------
# FILE UPLOAD
# -----------------------------
uploaded_file = st.file_uploader("Upload PDF", type=["pdf"])

text_data = ""

if uploaded_file:
    reader = PdfReader(uploaded_file)
    for page in reader.pages:
        if page.extract_text():
            text_data += page.extract_text()

# -----------------------------
# TEXT INPUT
# -----------------------------
text_input = st.text_area("Or paste your text here:")

if text_input:
    text_data = text_input

# -----------------------------
# MAIN FUNCTIONALITY
# -----------------------------
if text_data:

    st.subheader("πŸ“„ Your Text Preview")
    st.write(text_data[:1500])

    # -----------------------------
    # SIMPLIFY TEXT
    # -----------------------------
    if st.button("✨ Simplify Paragraph"):
        with st.spinner("Simplifying..."):
            prompt = f"Explain this in very simple English:\n{text_data[:500]}"
            response = generator(prompt, max_length=150)
            st.success(response[0]['generated_text'])

    # -----------------------------
    # SUMMARIZE TEXT
    # -----------------------------
    if st.button("πŸ“ Summarize Text"):
        with st.spinner("Summarizing..."):
            prompt = f"Summarize this text:\n{text_data[:500]}"
            response = generator(prompt, max_length=120)
            st.success(response[0]['generated_text'])

    # -----------------------------
    # QUESTION ANSWERING
    # -----------------------------
    st.subheader("❓ Ask a Question")
    question = st.text_input("Enter your question:")

    if question:
        with st.spinner("Thinking..."):
            prompt = f"Answer the question based on the text below:\n\nText:\n{text_data[:700]}\n\nQuestion:\n{question}"
            response = generator(prompt, max_length=120)
            st.success(response[0]['generated_text'])

    # -----------------------------
    # WORD EXPLANATION
    # -----------------------------
    st.subheader("πŸ” Word Explanation")
    word = st.text_input("Enter a difficult word:")

    if word:
        with st.spinner("Explaining..."):
            prompt = f"Explain the word '{word}' in simple English and give an example."
            response = generator(prompt, max_length=80)
            st.success(response[0]['generated_text'])

else:
    st.info("Upload a PDF or paste text to begin.")

# -----------------------------
# FOOTER
# -----------------------------
st.markdown("---")
st.caption("Built with ❀️ using Streamlit + Hugging Face (FLAN-T5)")