Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +89 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, requests
|
| 2 |
+
from pypdf import PdfReader
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
# --- Setup ---
|
| 6 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 7 |
+
if not OPENAI_API_KEY:
|
| 8 |
+
st.warning("β οΈ Set your OpenAI key using the next cell before generating.")
|
| 9 |
+
OPENAI_URL = "https://api.openai.com/v1/chat/completions"
|
| 10 |
+
MODEL = "gpt-4o-mini"
|
| 11 |
+
MAX_PAGES, MAX_CHARS = 15, 10000
|
| 12 |
+
|
| 13 |
+
# --- UI ---
|
| 14 |
+
st.set_page_config(page_title="StudySnap", page_icon="π", layout="centered")
|
| 15 |
+
st.title("π StudySnap β Summaries + MCQs")
|
| 16 |
+
tab1, tab2 = st.tabs(["Paste Text", "Upload PDF"])
|
| 17 |
+
length = st.radio("Output length:", ["short","medium","long"], index=1, horizontal=True)
|
| 18 |
+
|
| 19 |
+
with tab1:
|
| 20 |
+
text_input = st.text_area("Paste your notes/article here:", height=220)
|
| 21 |
+
|
| 22 |
+
with tab2:
|
| 23 |
+
pdf_file = st.file_uploader("Upload PDF (first 15 pages only)", type=["pdf"])
|
| 24 |
+
|
| 25 |
+
# --- Helpers ---
|
| 26 |
+
def extract_pdf_text(file):
|
| 27 |
+
r = PdfReader(file)
|
| 28 |
+
pages = min(len(r.pages), MAX_PAGES)
|
| 29 |
+
text = "\n".join(r.pages[i].extract_text() or "" for i in range(pages))
|
| 30 |
+
lines = [ln.strip() for ln in text.splitlines() if ln.strip()]
|
| 31 |
+
return "\n".join(lines)[:MAX_CHARS]
|
| 32 |
+
|
| 33 |
+
def call_openai(system, user):
|
| 34 |
+
if not OPENAI_API_KEY:
|
| 35 |
+
raise RuntimeError("API key missing.")
|
| 36 |
+
headers = {"Authorization": f"Bearer {OPENAI_API_KEY}", "Content-Type": "application/json"}
|
| 37 |
+
body = {"model": MODEL, "messages":[{"role":"system","content":system},{"role":"user","content":user}], "temperature":0.2}
|
| 38 |
+
r = requests.post(OPENAI_URL, headers=headers, json=body, timeout=60)
|
| 39 |
+
r.raise_for_status()
|
| 40 |
+
return r.json()["choices"][0]["message"]["content"]
|
| 41 |
+
|
| 42 |
+
# --- AI Wrappers ---
|
| 43 |
+
def summarize(txt, length):
|
| 44 |
+
sys = ("You are a precise study assistant. Summarize in bullet points for MBA-level students. "
|
| 45 |
+
"short=5 bullets, medium=8β10, long=12β15.")
|
| 46 |
+
usr = f"Length: {length}\n\nCONTENT:\n{txt[:MAX_CHARS]}"
|
| 47 |
+
return call_openai(sys, usr)
|
| 48 |
+
|
| 49 |
+
def make_mcqs(txt):
|
| 50 |
+
sys = ("Create 10 single-answer MCQs with 4 options (AβD), plus 'Answer: X' and a short explanation. "
|
| 51 |
+
"Keep options plausible, style professional.")
|
| 52 |
+
usr = txt[:8000]
|
| 53 |
+
return call_openai(sys, usr)
|
| 54 |
+
|
| 55 |
+
# --- Main ---
|
| 56 |
+
if st.button("Generate", type="primary"):
|
| 57 |
+
if pdf_file:
|
| 58 |
+
content = extract_pdf_text(pdf_file)
|
| 59 |
+
title = pdf_file.name
|
| 60 |
+
elif text_input.strip():
|
| 61 |
+
content = text_input.strip()
|
| 62 |
+
title = "Pasted Text"
|
| 63 |
+
else:
|
| 64 |
+
st.warning("Paste text or upload a PDF first.")
|
| 65 |
+
st.stop()
|
| 66 |
+
|
| 67 |
+
if len(content) < 200:
|
| 68 |
+
st.warning("Input too short.")
|
| 69 |
+
st.stop()
|
| 70 |
+
|
| 71 |
+
with st.spinner("Summarizing..."):
|
| 72 |
+
try:
|
| 73 |
+
summary = summarize(content, length)
|
| 74 |
+
st.success("β
Summary Ready!")
|
| 75 |
+
st.markdown(summary)
|
| 76 |
+
except Exception as e:
|
| 77 |
+
st.error(f"Summarization failed: {e}")
|
| 78 |
+
st.stop()
|
| 79 |
+
|
| 80 |
+
st.divider()
|
| 81 |
+
st.subheader("π§ Practice β 10 MCQs")
|
| 82 |
+
with st.spinner("Generating questions..."):
|
| 83 |
+
try:
|
| 84 |
+
mcqs = make_mcqs(content)
|
| 85 |
+
st.markdown(mcqs)
|
| 86 |
+
except Exception as e:
|
| 87 |
+
st.error(f"MCQ generation failed: {e}")
|
| 88 |
+
|
| 89 |
+
st.caption("MVP limits: 15 pages / 10k characters. We never store your files.")
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit>=1.36
|
| 2 |
+
pypdf>=4.2
|
| 3 |
+
python-docx>=1.1
|
| 4 |
+
requests>=2.32
|