Spaces:
Runtime error
Runtime error
| import os, requests | |
| from pypdf import PdfReader | |
| import streamlit as st | |
| # --- Setup --- | |
| OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") | |
| if not OPENAI_API_KEY: | |
| st.warning("β οΈ Set your OpenAI key using the next cell before generating.") | |
| OPENAI_URL = "https://api.openai.com/v1/chat/completions" | |
| MODEL = "gpt-4o-mini" | |
| MAX_PAGES, MAX_CHARS = 15, 10000 | |
| # --- UI --- | |
| st.set_page_config(page_title="StudySnap", page_icon="π", layout="centered") | |
| st.title("π StudySnap β Summaries + MCQs") | |
| tab1, tab2 = st.tabs(["Paste Text", "Upload PDF"]) | |
| length = st.radio("Output length:", ["short","medium","long"], index=1, horizontal=True) | |
| with tab1: | |
| text_input = st.text_area("Paste your notes/article here:", height=220) | |
| with tab2: | |
| pdf_file = st.file_uploader("Upload PDF (first 15 pages only)", type=["pdf"]) | |
| # --- Helpers --- | |
| def extract_pdf_text(file): | |
| r = PdfReader(file) | |
| pages = min(len(r.pages), MAX_PAGES) | |
| text = "\n".join(r.pages[i].extract_text() or "" for i in range(pages)) | |
| lines = [ln.strip() for ln in text.splitlines() if ln.strip()] | |
| return "\n".join(lines)[:MAX_CHARS] | |
| def call_openai(system, user): | |
| if not OPENAI_API_KEY: | |
| raise RuntimeError("API key missing.") | |
| headers = {"Authorization": f"Bearer {OPENAI_API_KEY}", "Content-Type": "application/json"} | |
| body = {"model": MODEL, "messages":[{"role":"system","content":system},{"role":"user","content":user}], "temperature":0.2} | |
| r = requests.post(OPENAI_URL, headers=headers, json=body, timeout=60) | |
| r.raise_for_status() | |
| return r.json()["choices"][0]["message"]["content"] | |
| # --- AI Wrappers --- | |
| def summarize(txt, length): | |
| sys = ("You are a precise study assistant. Summarize in bullet points for MBA-level students. " | |
| "short=5 bullets, medium=8β10, long=12β15.") | |
| usr = f"Length: {length}\n\nCONTENT:\n{txt[:MAX_CHARS]}" | |
| return call_openai(sys, usr) | |
| def make_mcqs(txt): | |
| sys = ("Create 10 single-answer MCQs with 4 options (AβD), plus 'Answer: X' and a short explanation. " | |
| "Keep options plausible, style professional.") | |
| usr = txt[:8000] | |
| return call_openai(sys, usr) | |
| # --- Main --- | |
| if st.button("Generate", type="primary"): | |
| if pdf_file: | |
| content = extract_pdf_text(pdf_file) | |
| title = pdf_file.name | |
| elif text_input.strip(): | |
| content = text_input.strip() | |
| title = "Pasted Text" | |
| else: | |
| st.warning("Paste text or upload a PDF first.") | |
| st.stop() | |
| if len(content) < 200: | |
| st.warning("Input too short.") | |
| st.stop() | |
| with st.spinner("Summarizing..."): | |
| try: | |
| summary = summarize(content, length) | |
| st.success("β Summary Ready!") | |
| st.markdown(summary) | |
| except Exception as e: | |
| st.error(f"Summarization failed: {e}") | |
| st.stop() | |
| st.divider() | |
| st.subheader("π§ Practice β 10 MCQs") | |
| with st.spinner("Generating questions..."): | |
| try: | |
| mcqs = make_mcqs(content) | |
| st.markdown(mcqs) | |
| except Exception as e: | |
| st.error(f"MCQ generation failed: {e}") | |
| st.caption("MVP limits: 15 pages / 10k characters. We never store your files.") | |