File size: 3,372 Bytes
ef71843
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
102
103
104
import streamlit as st
from pypdf import PdfReader
from transformers import pipeline

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

st.title("πŸŽ“ AI Study Companion")
st.write("Your all-in-one AI tool for reading, understanding, and practicing.")

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

generator = load_model()

# -----------------------------
# INPUT SECTION
# -----------------------------
uploaded_file = st.file_uploader("πŸ“„ Upload PDF", type=["pdf"])
text_input = st.text_area("✍️ Or paste your text here:")

text_data = ""

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

if text_input:
    text_data = text_input

# -----------------------------
# MAIN APP
# -----------------------------
if text_data:

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

    # -----------------------------
    # FEATURE SELECTION
    # -----------------------------
    option = st.selectbox(
        "Choose what you want to do:",
        ["Simplify Text", "Summarize", "Ask Question", "Generate Quiz"]
    )

    # -----------------------------
    # SIMPLIFY
    # -----------------------------
    if option == "Simplify Text":
        if st.button("✨ Simplify"):
            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
    # -----------------------------
    elif option == "Summarize":
        if st.button("πŸ“ Summarize"):
            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'])

    # -----------------------------
    # Q&A
    # -----------------------------
    elif option == "Ask 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'])

    # -----------------------------
    # QUIZ GENERATOR
    # -----------------------------
    elif option == "Generate Quiz":
        if st.button("🧠 Generate Quiz"):
            with st.spinner("Creating quiz..."):
                prompt = f"Create 5 multiple choice questions (MCQs) with answers from this text:\n{text_data[:500]}"
                response = generator(prompt, max_length=300)
                st.success(response[0]['generated_text'])

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

# -----------------------------
# FOOTER
# -----------------------------
st.markdown("---")
st.caption("πŸš€ Built with Streamlit + Hugging Face | AI Study Companion")