Spaces:
Running
Running
quiz2.py
#11
by
g-sloka
- opened
quiz2.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import PyPDF2
|
3 |
+
import nltk
|
4 |
+
import re
|
5 |
+
import networkx as nx
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
from io import BytesIO
|
8 |
+
import random
|
9 |
+
|
10 |
+
nltk.download("punkt", quiet=True)
|
11 |
+
|
12 |
+
def extract_text_from_pdf(pdf_file):
|
13 |
+
"""Extract text from uploaded PDF"""
|
14 |
+
reader = PyPDF2.PdfReader(pdf_file)
|
15 |
+
text = ""
|
16 |
+
for page in reader.pages:
|
17 |
+
text += page.extract_text() + " "
|
18 |
+
return text
|
19 |
+
|
20 |
+
def summarize_text(text, num_sentences=5):
|
21 |
+
"""Simple bullet point summary (frequency-based)"""
|
22 |
+
sentences = nltk.sent_tokenize(text)
|
23 |
+
words = nltk.word_tokenize(text.lower())
|
24 |
+
freq = nltk.FreqDist(words)
|
25 |
+
ranked_sentences = sorted(
|
26 |
+
sentences, key=lambda s: sum(freq[w] for w in nltk.word_tokenize(s.lower()) if w in freq), reverse=True
|
27 |
+
)
|
28 |
+
return ranked_sentences[:num_sentences]
|
29 |
+
|
30 |
+
def generate_mindmap(text):
|
31 |
+
"""Generate a simple mindmap graph of keywords"""
|
32 |
+
words = re.findall(r'\b[a-zA-Z]{4,}\b', text.lower())
|
33 |
+
freq = nltk.FreqDist(words)
|
34 |
+
common_words = [w for w, _ in freq.most_common(10)]
|
35 |
+
|
36 |
+
G = nx.Graph()
|
37 |
+
for word in common_words:
|
38 |
+
G.add_node(word)
|
39 |
+
for i in range(len(common_words)):
|
40 |
+
for j in range(i+1, len(common_words)):
|
41 |
+
if random.random() > 0.5: # Random connections for visualization
|
42 |
+
G.add_edge(common_words[i], common_words[j])
|
43 |
+
|
44 |
+
# Plot the mindmap
|
45 |
+
fig, ax = plt.subplots(figsize=(6, 6))
|
46 |
+
nx.draw(G, with_labels=True, node_color="lightblue", node_size=2000, font_size=10, ax=ax)
|
47 |
+
return fig
|
48 |
+
|
49 |
+
def generate_quiz(text, num_q=5):
|
50 |
+
"""Generate quiz questions from text"""
|
51 |
+
sentences = nltk.sent_tokenize(text)
|
52 |
+
quiz = []
|
53 |
+
for _ in range(min(num_q, len(sentences))):
|
54 |
+
sent = random.choice(sentences)
|
55 |
+
words = sent.split()
|
56 |
+
if len(words) > 5:
|
57 |
+
blank_idx = random.randint(0, len(words)-1)
|
58 |
+
answer = words[blank_idx]
|
59 |
+
words[blank_idx] = "____"
|
60 |
+
quiz.append((" ".join(words), answer))
|
61 |
+
return quiz
|
62 |
+
|
63 |
+
|
64 |
+
st.title("π StudyMate: AI-Powered Study Assistant")
|
65 |
+
st.write("Upload a PDF to get **summaries, mindmaps, and quiz questions** π―")
|
66 |
+
|
67 |
+
pdf_file = st.file_uploader("Upload PDF", type=["pdf"])
|
68 |
+
|
69 |
+
if pdf_file:
|
70 |
+
text = extract_text_from_pdf(pdf_file)
|
71 |
+
|
72 |
+
st.subheader("π Summary (Bullet Points)")
|
73 |
+
summary = summarize_text(text)
|
74 |
+
for s in summary:
|
75 |
+
st.markdown(f"- {s}")
|
76 |
+
|
77 |
+
st.subheader("π§ Mindmap")
|
78 |
+
fig = generate_mindmap(text)
|
79 |
+
st.pyplot(fig)
|
80 |
+
|
81 |
+
st.subheader("β Quiz Questions")
|
82 |
+
quiz = generate_quiz(text)
|
83 |
+
for idx, (q, ans) in enumerate(quiz, 1):
|
84 |
+
st.markdown(f"**Q{idx}.** {q}")
|
85 |
+
with st.expander("Show Answer"):
|
86 |
+
st.write(ans)
|