Spaces:
Sleeping
Sleeping
Upload 8 files
Browse files- app/__init__.py +0 -0
- app/api_utils.py +22 -0
- app/fact_game.py +11 -0
- app/flashcard_generator.py +7 -0
- app/mcq_generator.py +13 -0
- app/trivia_app.py +42 -0
- assets/style.css +7 -0
- data/mcqs.json +12 -0
app/__init__.py
ADDED
|
File without changes
|
app/api_utils.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import requests
|
| 3 |
+
import wikipedia
|
| 4 |
+
|
| 5 |
+
def get_summary(topic):
|
| 6 |
+
try:
|
| 7 |
+
return wikipedia.summary(topic, sentences=3)
|
| 8 |
+
except:
|
| 9 |
+
url = "https://en.wikibooks.org/w/api.php"
|
| 10 |
+
params = {
|
| 11 |
+
"action": "query",
|
| 12 |
+
"format": "json",
|
| 13 |
+
"prop": "extracts",
|
| 14 |
+
"titles": topic,
|
| 15 |
+
"exintro": True,
|
| 16 |
+
"explaintext": True
|
| 17 |
+
}
|
| 18 |
+
resp = requests.get(url, params=params).json()
|
| 19 |
+
pages = resp["query"]["pages"]
|
| 20 |
+
for page in pages.values():
|
| 21 |
+
return page.get("extract", None)
|
| 22 |
+
return None
|
app/fact_game.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
def get_facts(topic):
|
| 5 |
+
try:
|
| 6 |
+
resp = requests.get(f"https://en.wikipedia.org/api/rest_v1/page/summary/{topic}").json()
|
| 7 |
+
text = resp.get("extract", "")
|
| 8 |
+
facts = [s.strip() for s in text.split('.') if len(s.strip()) > 10][:5]
|
| 9 |
+
return facts
|
| 10 |
+
except:
|
| 11 |
+
return [f"No facts found for {topic}."]
|
app/flashcard_generator.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
def load_flashcards(topic):
|
| 3 |
+
# Basic example, can be replaced with Wikibooks/Wikidata
|
| 4 |
+
return [
|
| 5 |
+
{"question": f"What is {topic}?", "answer": f"{topic} is a concept worth learning."},
|
| 6 |
+
{"question": f"Why is {topic} important?", "answer": f"It is relevant to modern studies and knowledge."}
|
| 7 |
+
]
|
app/mcq_generator.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
generator = pipeline("text-generation", model="google/flan-t5-base", max_new_tokens=256)
|
| 5 |
+
|
| 6 |
+
def generate_mcq(summary):
|
| 7 |
+
prompt = f"Generate a multiple choice question (with 4 options and correct answer) based on this:\n{summary}"
|
| 8 |
+
result = generator(prompt)[0]["generated_text"]
|
| 9 |
+
lines = result.split("\n")
|
| 10 |
+
question = lines[0] if lines else "Generated Question"
|
| 11 |
+
options = lines[1:5] if len(lines) >= 5 else ["Option A", "Option B", "Option C", "Option D"]
|
| 12 |
+
answer = lines[5].split(":")[-1].strip() if len(lines) > 5 else options[0]
|
| 13 |
+
return question, options, answer
|
app/trivia_app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from app.api_utils import get_summary
|
| 4 |
+
from app.mcq_generator import generate_mcq
|
| 5 |
+
from app.flashcard_generator import load_flashcards
|
| 6 |
+
from app.fact_game import get_facts
|
| 7 |
+
|
| 8 |
+
def run_triviaverse():
|
| 9 |
+
st.title("🧠 TriviaVerse – Wikipedia-Powered Quiz Generator")
|
| 10 |
+
|
| 11 |
+
mode = st.sidebar.radio("Select Mode", ["MCQ Quiz", "Flashcards", "Fact Game"])
|
| 12 |
+
|
| 13 |
+
topic = st.text_input("Enter a topic", "Solar System")
|
| 14 |
+
|
| 15 |
+
if mode == "MCQ Quiz":
|
| 16 |
+
if st.button("Start Quiz"):
|
| 17 |
+
with st.spinner("Generating question..."):
|
| 18 |
+
summary = get_summary(topic)
|
| 19 |
+
if summary:
|
| 20 |
+
question, options, answer = generate_mcq(summary)
|
| 21 |
+
st.write("### Q:", question)
|
| 22 |
+
selected = st.radio("Choose an answer:", options)
|
| 23 |
+
if selected:
|
| 24 |
+
if selected == answer:
|
| 25 |
+
st.success("✅ Correct!")
|
| 26 |
+
else:
|
| 27 |
+
st.error(f"❌ Incorrect. Correct answer: {answer}")
|
| 28 |
+
else:
|
| 29 |
+
st.error("No summary available.")
|
| 30 |
+
|
| 31 |
+
elif mode == "Flashcards":
|
| 32 |
+
st.info("Flashcard Mode - Tap to reveal answers.")
|
| 33 |
+
flashcards = load_flashcards(topic)
|
| 34 |
+
for card in flashcards:
|
| 35 |
+
with st.expander(card["question"]):
|
| 36 |
+
st.write(card["answer"])
|
| 37 |
+
|
| 38 |
+
elif mode == "Fact Game":
|
| 39 |
+
st.info("Fact Game - Discover fast facts.")
|
| 40 |
+
facts = get_facts(topic)
|
| 41 |
+
for fact in facts:
|
| 42 |
+
st.write("•", fact)
|
assets/style.css
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
body {
|
| 2 |
+
background-color: #f0f8ff;
|
| 3 |
+
}
|
| 4 |
+
.stButton>button {
|
| 5 |
+
background-color: #007ACC;
|
| 6 |
+
color: white;
|
| 7 |
+
}
|
data/mcqs.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"question": "What is the capital of France?",
|
| 4 |
+
"options": [
|
| 5 |
+
"Paris",
|
| 6 |
+
"London",
|
| 7 |
+
"Berlin",
|
| 8 |
+
"Rome"
|
| 9 |
+
],
|
| 10 |
+
"answer": "Paris"
|
| 11 |
+
}
|
| 12 |
+
]
|