Spaces:
Sleeping
Sleeping
| # 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 | |