Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 Study Companion", layout="wide")
|
| 9 |
+
|
| 10 |
+
st.title("π AI Study Companion")
|
| 11 |
+
st.write("Your all-in-one AI tool for reading, understanding, and practicing.")
|
| 12 |
+
|
| 13 |
+
# -----------------------------
|
| 14 |
+
# LOAD MODEL
|
| 15 |
+
# -----------------------------
|
| 16 |
+
@st.cache_resource
|
| 17 |
+
def load_model():
|
| 18 |
+
return pipeline("text2text-generation", model="google/flan-t5-base")
|
| 19 |
+
|
| 20 |
+
generator = load_model()
|
| 21 |
+
|
| 22 |
+
# -----------------------------
|
| 23 |
+
# INPUT SECTION
|
| 24 |
+
# -----------------------------
|
| 25 |
+
uploaded_file = st.file_uploader("π Upload PDF", type=["pdf"])
|
| 26 |
+
text_input = st.text_area("βοΈ Or paste your text here:")
|
| 27 |
+
|
| 28 |
+
text_data = ""
|
| 29 |
+
|
| 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 |
+
if text_input:
|
| 37 |
+
text_data = text_input
|
| 38 |
+
|
| 39 |
+
# -----------------------------
|
| 40 |
+
# MAIN APP
|
| 41 |
+
# -----------------------------
|
| 42 |
+
if text_data:
|
| 43 |
+
|
| 44 |
+
st.subheader("π Text Preview")
|
| 45 |
+
st.write(text_data[:1500])
|
| 46 |
+
|
| 47 |
+
# -----------------------------
|
| 48 |
+
# FEATURE SELECTION
|
| 49 |
+
# -----------------------------
|
| 50 |
+
option = st.selectbox(
|
| 51 |
+
"Choose what you want to do:",
|
| 52 |
+
["Simplify Text", "Summarize", "Ask Question", "Generate Quiz"]
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# -----------------------------
|
| 56 |
+
# SIMPLIFY
|
| 57 |
+
# -----------------------------
|
| 58 |
+
if option == "Simplify Text":
|
| 59 |
+
if st.button("β¨ Simplify"):
|
| 60 |
+
with st.spinner("Simplifying..."):
|
| 61 |
+
prompt = f"Explain this in very simple English:\n{text_data[:500]}"
|
| 62 |
+
response = generator(prompt, max_length=150)
|
| 63 |
+
st.success(response[0]['generated_text'])
|
| 64 |
+
|
| 65 |
+
# -----------------------------
|
| 66 |
+
# SUMMARIZE
|
| 67 |
+
# -----------------------------
|
| 68 |
+
elif option == "Summarize":
|
| 69 |
+
if st.button("π Summarize"):
|
| 70 |
+
with st.spinner("Summarizing..."):
|
| 71 |
+
prompt = f"Summarize this text:\n{text_data[:500]}"
|
| 72 |
+
response = generator(prompt, max_length=120)
|
| 73 |
+
st.success(response[0]['generated_text'])
|
| 74 |
+
|
| 75 |
+
# -----------------------------
|
| 76 |
+
# Q&A
|
| 77 |
+
# -----------------------------
|
| 78 |
+
elif option == "Ask Question":
|
| 79 |
+
question = st.text_input("Enter your question:")
|
| 80 |
+
|
| 81 |
+
if question:
|
| 82 |
+
with st.spinner("Thinking..."):
|
| 83 |
+
prompt = f"Answer the question based on the text below:\n\nText:\n{text_data[:700]}\n\nQuestion:\n{question}"
|
| 84 |
+
response = generator(prompt, max_length=120)
|
| 85 |
+
st.success(response[0]['generated_text'])
|
| 86 |
+
|
| 87 |
+
# -----------------------------
|
| 88 |
+
# QUIZ GENERATOR
|
| 89 |
+
# -----------------------------
|
| 90 |
+
elif option == "Generate Quiz":
|
| 91 |
+
if st.button("π§ Generate Quiz"):
|
| 92 |
+
with st.spinner("Creating quiz..."):
|
| 93 |
+
prompt = f"Create 5 multiple choice questions (MCQs) with answers from this text:\n{text_data[:500]}"
|
| 94 |
+
response = generator(prompt, max_length=300)
|
| 95 |
+
st.success(response[0]['generated_text'])
|
| 96 |
+
|
| 97 |
+
else:
|
| 98 |
+
st.info("Upload a PDF or paste text to start.")
|
| 99 |
+
|
| 100 |
+
# -----------------------------
|
| 101 |
+
# FOOTER
|
| 102 |
+
# -----------------------------
|
| 103 |
+
st.markdown("---")
|
| 104 |
+
st.caption("π Built with Streamlit + Hugging Face | AI Study Companion")
|