Spaces:
Sleeping
Sleeping
File size: 3,304 Bytes
242da2d bc20a41 529a636 e33f38e 242da2d e33f38e 242da2d af08924 529a636 242da2d 529a636 242da2d 529a636 242da2d 529a636 242da2d 529a636 242da2d af08924 242da2d af08924 242da2d 529a636 242da2d 529a636 242da2d 529a636 242da2d 529a636 242da2d 529a636 242da2d af08924 242da2d 529a636 242da2d 529a636 af08924 242da2d |
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 |
import os
import tempfile
from datetime import datetime
from PyPDF2 import PdfReader
from docx import Document
import streamlit as st
from transformers import pipeline
# β
Page config MUST be first Streamlit command
st.set_page_config(page_title="AI Study Plan Assistant", layout="wide")
# Load models with caching
@st.cache_resource
def load_models():
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
return summarizer, qa_pipeline
summarizer, qa_pipeline = load_models()
# Extract text from supported formats
def extract_text(file):
ext = os.path.splitext(file.name)[1].lower()
if ext == ".txt":
return file.read().decode("utf-8")
elif ext == ".docx":
doc = Document(file)
return "\n".join([para.text for para in doc.paragraphs])
elif ext == ".pdf":
pdf_reader = PdfReader(file)
text = ""
for page in pdf_reader.pages:
text += page.extract_text() + "\n"
return text
else:
raise ValueError("Only .txt, .docx, and .pdf files are supported.")
# Generate study plan
def generate_plan(file, hours_per_day, exam_date, language):
try:
content = extract_text(file)
today = datetime.now().date()
exam = datetime.strptime(exam_date.strip(), "%Y-%m-%d").date()
days = (exam - today).days
if days <= 0:
return "β Exam date must be in the future."
prompt = f"Create a {days}-day study plan for this syllabus:\n{content}\nDaily study time: {hours_per_day} hours."
if language == "Urdu":
prompt += "\nTranslate the plan into Urdu."
summary = summarizer(prompt, max_length=512, min_length=150, do_sample=False)
return summary[0]['summary_text']
except Exception as e:
return f"β οΈ Error: {str(e)}"
# Ask a question
def ask_question(file, question):
try:
context = extract_text(file)
result = qa_pipeline({"context": context, "question": question})
return result['answer']
except Exception as e:
return f"β οΈ Error: {str(e)}"
# Streamlit UI
st.sidebar.title("π Study Assistant Options")
uploaded_file = st.sidebar.file_uploader("Upload syllabus (.txt, .docx, .pdf)", type=["txt", "docx", "pdf"])
study_hours = st.sidebar.number_input("Study hours per day", min_value=1, max_value=12, value=3)
exam_date = st.sidebar.text_input("Exam Date (YYYY-MM-DD)", value="2025-06-30")
language = st.sidebar.selectbox("Select Language", ["English", "Urdu"])
st.title("π§ AI Study Plan & QA Assistant")
tab1, tab2 = st.tabs(["π
Generate Study Plan", "β Ask a Question"])
with tab1:
st.subheader("Generate a Personalized Study Plan")
if uploaded_file and st.button("Generate Study Plan"):
result = generate_plan(uploaded_file, study_hours, exam_date, language)
st.text_area("Study Plan", result, height=400)
with tab2:
st.subheader("Ask Questions from Uploaded Material")
question = st.text_input("Enter your question:")
if uploaded_file and question and st.button("Get Answer"):
answer = ask_question(uploaded_file, question)
st.text_area("Answer", answer, height=200)
|