Spaces:
Sleeping
Sleeping
File size: 4,071 Bytes
5775a1b | 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | # modules/flashcard_generator.py
"""Enhanced Flashcard Generator Module"""
from typing import Dict, List, Optional
from modules.api_utils import (
fetch_wikipedia_summary,
fetch_wikibooks_content,
fetch_wikipedia_categories,
fetch_related_topics,
)
import random
def generate_flashcard_types(topic: str, difficulty: str) -> Dict:
"""Generate different types of flashcards based on difficulty"""
summary_data = fetch_wikipedia_summary(topic)
if not summary_data:
return {"error": "Topic not found", "status": False}
title = summary_data.get("title", topic)
extract = summary_data.get("extract", "")
description = summary_data.get("description", "")
# Also try to get Wikibooks content for educational material
wikibooks_content = fetch_wikibooks_content(topic)
flashcard_types = {
"Easy": {
"front": f"What is {title}?",
"back": extract[:200] + "..." if len(extract) > 200 else extract,
"type": "definition",
},
"Medium": {
"front": f"Explain the key concepts of {title}",
"back": extract[:300] + "..." if len(extract) > 300 else extract,
"type": "conceptual",
},
"Hard": {
"front": f"How does {title} relate to its field and what are its applications?",
"back": extract
+ (
f"\n\nAdditional context: {wikibooks_content[:200]}"
if wikibooks_content
else ""
),
"type": "analytical",
},
}
flashcard = flashcard_types.get(difficulty, flashcard_types["Easy"])
# Add categories as tags
categories = fetch_wikipedia_categories(title)
return {
"front": flashcard["front"],
"back": flashcard["back"],
"hint": description,
"topic": title,
"difficulty": difficulty,
"type": flashcard["type"],
"tags": categories[:3] if categories else [],
"status": True,
}
def generate_flashcard(topic: str, difficulty: str = "Medium") -> Dict:
"""Generate an enhanced flashcard for a given topic"""
return generate_flashcard_types(topic, difficulty)
def generate_flashcard_deck(
topics: List[str], difficulty: str = "Medium"
) -> List[Dict]:
"""Generate a deck of flashcards with progress tracking"""
deck = []
for i, topic in enumerate(topics):
card = generate_flashcard(topic, difficulty)
if card.get("status"):
card["card_number"] = i + 1
card["total_cards"] = len(topics)
deck.append(card)
return deck
def generate_smart_deck(
main_topic: str, deck_size: int = 10, difficulty: str = "Medium"
) -> List[Dict]:
"""Generate a smart deck with related topics"""
# Start with the main topic
topics = [main_topic]
# Add related topics
related = fetch_related_topics(main_topic, deck_size - 1)
topics.extend(related)
# Generate the deck
deck = generate_flashcard_deck(topics[:deck_size], difficulty)
# Shuffle for variety
random.shuffle(deck)
# Re-number the cards
for i, card in enumerate(deck):
card["card_number"] = i + 1
return deck
def generate_review_card(topic: str, previous_performance: Dict) -> Dict:
"""Generate a review flashcard based on previous performance"""
# Adjust difficulty based on performance
if previous_performance.get("success_rate", 0) < 0.5:
difficulty = "Easy"
elif previous_performance.get("success_rate", 0) > 0.8:
difficulty = "Hard"
else:
difficulty = "Medium"
card = generate_flashcard(topic, difficulty)
if card.get("status"):
card["is_review"] = True
card["previous_attempts"] = previous_performance.get("attempts", 0)
card["previous_success_rate"] = previous_performance.get("success_rate", 0)
return card
|