Upload folder using huggingface_hub
Browse files- .gitignore +5 -0
- README.md +1 -1
- app.py +1241 -165
- requirements.txt +4 -1
.gitignore
CHANGED
|
@@ -165,4 +165,9 @@ cython_debug/
|
|
| 165 |
.vscode
|
| 166 |
.ruff_cache
|
| 167 |
flagged
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
*.csv
|
|
|
|
| 165 |
.vscode
|
| 166 |
.ruff_cache
|
| 167 |
flagged
|
| 168 |
+
*.csv
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
uv.lock
|
| 172 |
+
*.apkg
|
| 173 |
*.csv
|
README.md
CHANGED
|
@@ -5,7 +5,7 @@ app_file: app.py
|
|
| 5 |
requirements: requirements.txt
|
| 6 |
python: 3.12
|
| 7 |
sdk: gradio
|
| 8 |
-
sdk_version:
|
| 9 |
---
|
| 10 |
|
| 11 |
# AnkiGen - Anki Card Generator
|
|
|
|
| 5 |
requirements: requirements.txt
|
| 6 |
python: 3.12
|
| 7 |
sdk: gradio
|
| 8 |
+
sdk_version: 5.13.1
|
| 9 |
---
|
| 10 |
|
| 11 |
# AnkiGen - Anki Card Generator
|
app.py
CHANGED
|
@@ -2,6 +2,19 @@ from openai import OpenAI
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from typing import List, Optional
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
class Step(BaseModel):
|
|
@@ -31,6 +44,7 @@ class CardBack(BaseModel):
|
|
| 31 |
class Card(BaseModel):
|
| 32 |
front: CardFront
|
| 33 |
back: CardBack
|
|
|
|
| 34 |
|
| 35 |
|
| 36 |
class CardList(BaseModel):
|
|
@@ -38,235 +52,1297 @@ class CardList(BaseModel):
|
|
| 38 |
cards: List[Card]
|
| 39 |
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def structured_output_completion(
|
| 42 |
client, model, response_format, system_prompt, user_prompt
|
| 43 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
try:
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
model=model,
|
| 47 |
messages=[
|
| 48 |
{"role": "system", "content": system_prompt.strip()},
|
| 49 |
{"role": "user", "content": user_prompt.strip()},
|
| 50 |
],
|
| 51 |
-
response_format=
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
-
except Exception as e:
|
| 55 |
-
print(f"An error occurred during the API call: {e}")
|
| 56 |
-
return None
|
| 57 |
-
|
| 58 |
-
try:
|
| 59 |
if not hasattr(completion, "choices") or not completion.choices:
|
| 60 |
-
|
| 61 |
return None
|
| 62 |
|
| 63 |
first_choice = completion.choices[0]
|
| 64 |
if not hasattr(first_choice, "message"):
|
| 65 |
-
|
| 66 |
return None
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
-
|
| 76 |
-
raise
|
|
|
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
def generate_cards(
|
| 80 |
api_key_input,
|
| 81 |
subject,
|
|
|
|
| 82 |
topic_number=1,
|
| 83 |
cards_per_topic=2,
|
| 84 |
preference_prompt="assume I'm a beginner",
|
| 85 |
):
|
| 86 |
-
""
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
Parameters:
|
| 90 |
-
- subject (str): The subject to generate cards for.
|
| 91 |
-
- topic_number (int): Number of topics to generate.
|
| 92 |
-
- cards_per_topic (int): Number of cards per topic.
|
| 93 |
-
- preference_prompt (str): User preferences to consider.
|
| 94 |
-
|
| 95 |
-
Returns:
|
| 96 |
-
- List[List[str]]: A list of rows containing
|
| 97 |
-
[topic, question, answer, explanation, example].
|
| 98 |
-
"""
|
| 99 |
-
|
| 100 |
-
gr.Info("Starting process")
|
| 101 |
|
|
|
|
| 102 |
if not api_key_input:
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
|
| 111 |
-
You are an expert in {subject}, assisting the user to master the topic while
|
| 112 |
-
keeping in mind the user's preferences: {preference_prompt}.
|
| 113 |
"""
|
| 114 |
|
| 115 |
topic_prompt = f"""
|
| 116 |
-
Generate the top {topic_number} important subjects to know
|
| 117 |
-
order of ascending difficulty.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
"""
|
| 119 |
|
| 120 |
try:
|
|
|
|
| 121 |
topics_response = structured_output_completion(
|
| 122 |
-
client,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
)
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
print("Invalid topics response format.")
|
| 129 |
-
return []
|
| 130 |
-
topic_list = [
|
| 131 |
-
item for subtopic in topics_response.result for item in subtopic.result
|
| 132 |
-
][:topic_number]
|
| 133 |
-
except Exception as e:
|
| 134 |
-
raise gr.Error(f"Topic generation failed due to {e}")
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
keeping in mind the user's preferences: {preference_prompt}.
|
| 140 |
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
| 145 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
)
|
| 151 |
-
if cards is None:
|
| 152 |
-
print(f"Failed to generate cards for topic '{topic}'.")
|
| 153 |
-
continue
|
| 154 |
-
if not hasattr(cards, "topic") or not hasattr(cards, "cards"):
|
| 155 |
-
print(f"Invalid card response format for topic '{topic}'.")
|
| 156 |
-
continue
|
| 157 |
-
all_card_lists.append(cards)
|
| 158 |
-
except Exception as e:
|
| 159 |
-
print(f"An error occurred while generating cards for topic '{topic}': {e}")
|
| 160 |
-
continue
|
| 161 |
|
| 162 |
-
flattened_data = []
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
with gr.Blocks(
|
| 201 |
-
|
| 202 |
-
title="AnkiGen",
|
| 203 |
-
css="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
) as ankigen:
|
| 205 |
-
gr.
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
|
| 271 |
if __name__ == "__main__":
|
|
|
|
| 272 |
ankigen.launch(share=False, favicon_path="./favicon.ico")
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from typing import List, Optional
|
| 4 |
import gradio as gr
|
| 5 |
+
import os
|
| 6 |
+
import logging
|
| 7 |
+
from logging.handlers import RotatingFileHandler
|
| 8 |
+
import sys
|
| 9 |
+
from functools import lru_cache
|
| 10 |
+
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
|
| 11 |
+
import hashlib
|
| 12 |
+
import genanki
|
| 13 |
+
import random
|
| 14 |
+
import json
|
| 15 |
+
import tempfile
|
| 16 |
+
from pathlib import Path
|
| 17 |
+
import pandas as pd
|
| 18 |
|
| 19 |
|
| 20 |
class Step(BaseModel):
|
|
|
|
| 44 |
class Card(BaseModel):
|
| 45 |
front: CardFront
|
| 46 |
back: CardBack
|
| 47 |
+
metadata: Optional[dict] = None
|
| 48 |
|
| 49 |
|
| 50 |
class CardList(BaseModel):
|
|
|
|
| 52 |
cards: List[Card]
|
| 53 |
|
| 54 |
|
| 55 |
+
class ConceptBreakdown(BaseModel):
|
| 56 |
+
main_concept: str
|
| 57 |
+
prerequisites: List[str]
|
| 58 |
+
learning_outcomes: List[str]
|
| 59 |
+
common_misconceptions: List[str]
|
| 60 |
+
difficulty_level: str # "beginner", "intermediate", "advanced"
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class CardGeneration(BaseModel):
|
| 64 |
+
concept: str
|
| 65 |
+
thought_process: str
|
| 66 |
+
verification_steps: List[str]
|
| 67 |
+
card: Card
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
class LearningSequence(BaseModel):
|
| 71 |
+
topic: str
|
| 72 |
+
concepts: List[ConceptBreakdown]
|
| 73 |
+
cards: List[CardGeneration]
|
| 74 |
+
suggested_study_order: List[str]
|
| 75 |
+
review_recommendations: List[str]
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def setup_logging():
|
| 79 |
+
"""Configure logging to both file and console"""
|
| 80 |
+
logger = logging.getLogger('ankigen')
|
| 81 |
+
logger.setLevel(logging.DEBUG)
|
| 82 |
+
|
| 83 |
+
# Create formatters
|
| 84 |
+
detailed_formatter = logging.Formatter(
|
| 85 |
+
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 86 |
+
)
|
| 87 |
+
simple_formatter = logging.Formatter(
|
| 88 |
+
'%(levelname)s: %(message)s'
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# File handler (detailed logging)
|
| 92 |
+
file_handler = RotatingFileHandler(
|
| 93 |
+
'ankigen.log',
|
| 94 |
+
maxBytes=1024*1024, # 1MB
|
| 95 |
+
backupCount=5
|
| 96 |
+
)
|
| 97 |
+
file_handler.setLevel(logging.DEBUG)
|
| 98 |
+
file_handler.setFormatter(detailed_formatter)
|
| 99 |
+
|
| 100 |
+
# Console handler (info and above)
|
| 101 |
+
console_handler = logging.StreamHandler(sys.stdout)
|
| 102 |
+
console_handler.setLevel(logging.INFO)
|
| 103 |
+
console_handler.setFormatter(simple_formatter)
|
| 104 |
+
|
| 105 |
+
# Add handlers to logger
|
| 106 |
+
logger.addHandler(file_handler)
|
| 107 |
+
logger.addHandler(console_handler)
|
| 108 |
+
|
| 109 |
+
return logger
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
# Initialize logger
|
| 113 |
+
logger = setup_logging()
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
# Replace the caching implementation with a proper cache dictionary
|
| 117 |
+
_response_cache = {} # Global cache dictionary
|
| 118 |
+
|
| 119 |
+
@lru_cache(maxsize=100)
|
| 120 |
+
def get_cached_response(cache_key: str):
|
| 121 |
+
"""Get response from cache"""
|
| 122 |
+
return _response_cache.get(cache_key)
|
| 123 |
+
|
| 124 |
+
def set_cached_response(cache_key: str, response):
|
| 125 |
+
"""Set response in cache"""
|
| 126 |
+
_response_cache[cache_key] = response
|
| 127 |
+
|
| 128 |
+
def create_cache_key(prompt: str, model: str) -> str:
|
| 129 |
+
"""Create a unique cache key for the API request"""
|
| 130 |
+
return hashlib.md5(f"{model}:{prompt}".encode()).hexdigest()
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
# Add retry decorator for API calls
|
| 134 |
+
@retry(
|
| 135 |
+
stop=stop_after_attempt(3),
|
| 136 |
+
wait=wait_exponential(multiplier=1, min=4, max=10),
|
| 137 |
+
retry=retry_if_exception_type(Exception),
|
| 138 |
+
before_sleep=lambda retry_state: logger.warning(
|
| 139 |
+
f"Retrying API call (attempt {retry_state.attempt_number})"
|
| 140 |
+
)
|
| 141 |
+
)
|
| 142 |
def structured_output_completion(
|
| 143 |
client, model, response_format, system_prompt, user_prompt
|
| 144 |
):
|
| 145 |
+
"""Make API call with retry logic and caching"""
|
| 146 |
+
cache_key = create_cache_key(f"{system_prompt}:{user_prompt}", model)
|
| 147 |
+
cached_response = get_cached_response(cache_key)
|
| 148 |
+
|
| 149 |
+
if cached_response is not None:
|
| 150 |
+
logger.info("Using cached response")
|
| 151 |
+
return cached_response
|
| 152 |
+
|
| 153 |
try:
|
| 154 |
+
logger.debug(f"Making API call with model {model}")
|
| 155 |
+
|
| 156 |
+
# Add JSON instruction to system prompt
|
| 157 |
+
system_prompt = f"{system_prompt}\nProvide your response as a JSON object matching the specified schema."
|
| 158 |
+
|
| 159 |
+
completion = client.chat.completions.create(
|
| 160 |
model=model,
|
| 161 |
messages=[
|
| 162 |
{"role": "system", "content": system_prompt.strip()},
|
| 163 |
{"role": "user", "content": user_prompt.strip()},
|
| 164 |
],
|
| 165 |
+
response_format={"type": "json_object"},
|
| 166 |
+
temperature=0.7
|
| 167 |
)
|
| 168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
if not hasattr(completion, "choices") or not completion.choices:
|
| 170 |
+
logger.warning("No choices returned in the completion.")
|
| 171 |
return None
|
| 172 |
|
| 173 |
first_choice = completion.choices[0]
|
| 174 |
if not hasattr(first_choice, "message"):
|
| 175 |
+
logger.warning("No message found in the first choice.")
|
| 176 |
return None
|
| 177 |
|
| 178 |
+
# Parse the JSON response
|
| 179 |
+
result = json.loads(first_choice.message.content)
|
| 180 |
+
|
| 181 |
+
# Cache the successful response
|
| 182 |
+
set_cached_response(cache_key, result)
|
| 183 |
+
return result
|
| 184 |
|
| 185 |
+
except Exception as e:
|
| 186 |
+
logger.error(f"API call failed: {str(e)}", exc_info=True)
|
| 187 |
+
raise
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
def generate_cards_batch(
|
| 191 |
+
client,
|
| 192 |
+
model,
|
| 193 |
+
topic,
|
| 194 |
+
num_cards,
|
| 195 |
+
system_prompt,
|
| 196 |
+
batch_size=3
|
| 197 |
+
):
|
| 198 |
+
"""Generate a batch of cards for a topic"""
|
| 199 |
+
cards_prompt = f"""
|
| 200 |
+
Generate {num_cards} flashcards for the topic: {topic}
|
| 201 |
+
Return your response as a JSON object with the following structure:
|
| 202 |
+
{{
|
| 203 |
+
"cards": [
|
| 204 |
+
{{
|
| 205 |
+
"front": {{
|
| 206 |
+
"question": "question text"
|
| 207 |
+
}},
|
| 208 |
+
"back": {{
|
| 209 |
+
"answer": "concise answer",
|
| 210 |
+
"explanation": "detailed explanation",
|
| 211 |
+
"example": "practical example"
|
| 212 |
+
}},
|
| 213 |
+
"metadata": {{
|
| 214 |
+
"prerequisites": ["list", "of", "prerequisites"],
|
| 215 |
+
"learning_outcomes": ["list", "of", "outcomes"],
|
| 216 |
+
"misconceptions": ["list", "of", "misconceptions"],
|
| 217 |
+
"difficulty": "beginner/intermediate/advanced"
|
| 218 |
+
}}
|
| 219 |
+
}}
|
| 220 |
+
]
|
| 221 |
+
}}
|
| 222 |
+
"""
|
| 223 |
+
|
| 224 |
+
try:
|
| 225 |
+
logger.info(f"Generated learning sequence for {topic}")
|
| 226 |
+
response = structured_output_completion(
|
| 227 |
+
client,
|
| 228 |
+
model,
|
| 229 |
+
{"type": "json_object"},
|
| 230 |
+
system_prompt,
|
| 231 |
+
cards_prompt
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
if not response or "cards" not in response:
|
| 235 |
+
logger.error("Invalid cards response format")
|
| 236 |
+
raise ValueError("Failed to generate cards. Please try again.")
|
| 237 |
+
|
| 238 |
+
# Convert the JSON response into Card objects
|
| 239 |
+
cards = []
|
| 240 |
+
for card_data in response["cards"]:
|
| 241 |
+
card = Card(
|
| 242 |
+
front=CardFront(**card_data["front"]),
|
| 243 |
+
back=CardBack(**card_data["back"]),
|
| 244 |
+
metadata=card_data.get("metadata", {})
|
| 245 |
+
)
|
| 246 |
+
cards.append(card)
|
| 247 |
+
|
| 248 |
+
return cards
|
| 249 |
|
| 250 |
except Exception as e:
|
| 251 |
+
logger.error(f"Failed to generate cards batch: {str(e)}")
|
| 252 |
+
raise
|
| 253 |
+
|
| 254 |
|
| 255 |
+
# Add near the top with other constants
|
| 256 |
+
AVAILABLE_MODELS = [
|
| 257 |
+
{
|
| 258 |
+
"value": "gpt-4o-mini", # Default model
|
| 259 |
+
"label": "gpt-4o Mini (Fastest)",
|
| 260 |
+
"description": "Balanced speed and quality"
|
| 261 |
+
},
|
| 262 |
+
{
|
| 263 |
+
"value": "gpt-4o",
|
| 264 |
+
"label": "gpt-4o (Better Quality)",
|
| 265 |
+
"description": "Higher quality, slower generation"
|
| 266 |
+
},
|
| 267 |
+
{
|
| 268 |
+
"value": "o1",
|
| 269 |
+
"label": "o1 (Best Quality)",
|
| 270 |
+
"description": "Highest quality, longest generation time"
|
| 271 |
+
}
|
| 272 |
+
]
|
| 273 |
+
|
| 274 |
+
GENERATION_MODES = [
|
| 275 |
+
{
|
| 276 |
+
"value": "subject",
|
| 277 |
+
"label": "Single Subject",
|
| 278 |
+
"description": "Generate cards for a specific topic"
|
| 279 |
+
},
|
| 280 |
+
{
|
| 281 |
+
"value": "path",
|
| 282 |
+
"label": "Learning Path",
|
| 283 |
+
"description": "Break down a job description or learning goal into subjects"
|
| 284 |
+
}
|
| 285 |
+
]
|
| 286 |
|
| 287 |
def generate_cards(
|
| 288 |
api_key_input,
|
| 289 |
subject,
|
| 290 |
+
model_name="gpt-4o-mini",
|
| 291 |
topic_number=1,
|
| 292 |
cards_per_topic=2,
|
| 293 |
preference_prompt="assume I'm a beginner",
|
| 294 |
):
|
| 295 |
+
logger.info(f"Starting card generation for subject: {subject}")
|
| 296 |
+
logger.debug(f"Parameters: topics={topic_number}, cards_per_topic={cards_per_topic}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
+
# Input validation
|
| 299 |
if not api_key_input:
|
| 300 |
+
logger.warning("No API key provided")
|
| 301 |
+
raise gr.Error("OpenAI API key is required")
|
| 302 |
+
if not api_key_input.startswith("sk-"):
|
| 303 |
+
logger.warning("Invalid API key format")
|
| 304 |
+
raise gr.Error("Invalid API key format. OpenAI keys should start with 'sk-'")
|
| 305 |
+
if not subject.strip():
|
| 306 |
+
logger.warning("No subject provided")
|
| 307 |
+
raise gr.Error("Subject is required")
|
| 308 |
+
|
| 309 |
+
gr.Info("🚀 Starting card generation...")
|
| 310 |
+
|
| 311 |
+
try:
|
| 312 |
+
logger.debug("Initializing OpenAI client")
|
| 313 |
+
client = OpenAI(api_key=api_key_input)
|
| 314 |
+
except Exception as e:
|
| 315 |
+
logger.error(f"Failed to initialize OpenAI client: {str(e)}", exc_info=True)
|
| 316 |
+
raise gr.Error(f"Failed to initialize OpenAI client: {str(e)}")
|
| 317 |
|
| 318 |
+
model = model_name
|
| 319 |
+
flattened_data = []
|
| 320 |
+
total = 0
|
| 321 |
+
|
| 322 |
+
progress_tracker = gr.Progress(track_tqdm=True)
|
| 323 |
+
|
| 324 |
+
system_prompt = f"""
|
| 325 |
+
You are an expert educator in {subject}, creating an optimized learning sequence.
|
| 326 |
+
Your goal is to:
|
| 327 |
+
1. Break down the subject into logical concepts
|
| 328 |
+
2. Identify prerequisites and learning outcomes
|
| 329 |
+
3. Generate cards that build upon each other
|
| 330 |
+
4. Address and correct common misconceptions
|
| 331 |
+
5. Include verification steps to minimize hallucinations
|
| 332 |
+
6. Provide a recommended study order
|
| 333 |
|
| 334 |
+
For explanations and examples:
|
| 335 |
+
- Keep explanations in plain text
|
| 336 |
+
- Format code examples with triple backticks (```)
|
| 337 |
+
- Separate conceptual examples from code examples
|
| 338 |
+
- Use clear, concise language
|
| 339 |
|
| 340 |
+
Keep in mind the user's preferences: {preference_prompt}
|
|
|
|
|
|
|
| 341 |
"""
|
| 342 |
|
| 343 |
topic_prompt = f"""
|
| 344 |
+
Generate the top {topic_number} important subjects to know about {subject} in
|
| 345 |
+
order of ascending difficulty. Return your response as a JSON object with the following structure:
|
| 346 |
+
{{
|
| 347 |
+
"topics": [
|
| 348 |
+
{{
|
| 349 |
+
"name": "topic name",
|
| 350 |
+
"difficulty": "beginner/intermediate/advanced",
|
| 351 |
+
"description": "brief description"
|
| 352 |
+
}}
|
| 353 |
+
]
|
| 354 |
+
}}
|
| 355 |
"""
|
| 356 |
|
| 357 |
try:
|
| 358 |
+
logger.info("Generating topics...")
|
| 359 |
topics_response = structured_output_completion(
|
| 360 |
+
client,
|
| 361 |
+
model,
|
| 362 |
+
{"type": "json_object"},
|
| 363 |
+
system_prompt,
|
| 364 |
+
topic_prompt
|
| 365 |
)
|
| 366 |
+
|
| 367 |
+
if not topics_response or "topics" not in topics_response:
|
| 368 |
+
logger.error("Invalid topics response format")
|
| 369 |
+
raise gr.Error("Failed to generate topics. Please try again.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
|
| 371 |
+
topics = topics_response["topics"]
|
| 372 |
+
|
| 373 |
+
gr.Info(f"✨ Generated {len(topics)} topics successfully!")
|
|
|
|
| 374 |
|
| 375 |
+
# Generate cards for each topic
|
| 376 |
+
for i, topic in enumerate(progress_tracker.tqdm(topics, desc="Generating cards")):
|
| 377 |
+
progress_html = f"""
|
| 378 |
+
<div style="text-align: center">
|
| 379 |
+
<p>Generating cards for topic {i+1}/{len(topics)}: {topic["name"]}</p>
|
| 380 |
+
<p>Cards generated so far: {total}</p>
|
| 381 |
+
</div>
|
| 382 |
+
"""
|
| 383 |
+
|
| 384 |
+
try:
|
| 385 |
+
cards = generate_cards_batch(
|
| 386 |
+
client,
|
| 387 |
+
model,
|
| 388 |
+
topic["name"],
|
| 389 |
+
cards_per_topic,
|
| 390 |
+
system_prompt,
|
| 391 |
+
batch_size=3
|
| 392 |
+
)
|
| 393 |
+
|
| 394 |
+
if cards:
|
| 395 |
+
for card_index, card in enumerate(cards, start=1):
|
| 396 |
+
index = f"{i+1}.{card_index}"
|
| 397 |
+
metadata = card.metadata or {}
|
| 398 |
+
|
| 399 |
+
row = [
|
| 400 |
+
index,
|
| 401 |
+
topic["name"],
|
| 402 |
+
card.front.question,
|
| 403 |
+
card.back.answer,
|
| 404 |
+
card.back.explanation,
|
| 405 |
+
card.back.example,
|
| 406 |
+
metadata.get("prerequisites", []),
|
| 407 |
+
metadata.get("learning_outcomes", []),
|
| 408 |
+
metadata.get("misconceptions", []),
|
| 409 |
+
metadata.get("difficulty", "beginner")
|
| 410 |
+
]
|
| 411 |
+
flattened_data.append(row)
|
| 412 |
+
total += 1
|
| 413 |
+
|
| 414 |
+
gr.Info(f"✅ Generated {len(cards)} cards for {topic['name']}")
|
| 415 |
+
|
| 416 |
+
except Exception as e:
|
| 417 |
+
logger.error(f"Failed to generate cards for topic {topic['name']}: {str(e)}")
|
| 418 |
+
gr.Warning(f"Failed to generate cards for '{topic['name']}'")
|
| 419 |
+
continue
|
| 420 |
|
| 421 |
+
final_html = f"""
|
| 422 |
+
<div style="text-align: center">
|
| 423 |
+
<p>✅ Generation complete!</p>
|
| 424 |
+
<p>Total cards generated: {total}</p>
|
| 425 |
+
</div>
|
| 426 |
"""
|
| 427 |
+
|
| 428 |
+
# Convert to DataFrame with all columns
|
| 429 |
+
df = pd.DataFrame(
|
| 430 |
+
flattened_data,
|
| 431 |
+
columns=[
|
| 432 |
+
"Index",
|
| 433 |
+
"Topic",
|
| 434 |
+
"Question",
|
| 435 |
+
"Answer",
|
| 436 |
+
"Explanation",
|
| 437 |
+
"Example",
|
| 438 |
+
"Prerequisites",
|
| 439 |
+
"Learning_Outcomes",
|
| 440 |
+
"Common_Misconceptions",
|
| 441 |
+
"Difficulty"
|
| 442 |
+
]
|
| 443 |
+
)
|
| 444 |
+
|
| 445 |
+
return df, final_html, total
|
| 446 |
|
| 447 |
+
except Exception as e:
|
| 448 |
+
logger.error(f"Card generation failed: {str(e)}", exc_info=True)
|
| 449 |
+
raise gr.Error(f"Card generation failed: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
|
|
|
|
| 451 |
|
| 452 |
+
# Update the BASIC_MODEL definition with enhanced CSS/HTML
|
| 453 |
+
BASIC_MODEL = genanki.Model(
|
| 454 |
+
random.randrange(1 << 30, 1 << 31),
|
| 455 |
+
'AnkiGen Enhanced',
|
| 456 |
+
fields=[
|
| 457 |
+
{'name': 'Question'},
|
| 458 |
+
{'name': 'Answer'},
|
| 459 |
+
{'name': 'Explanation'},
|
| 460 |
+
{'name': 'Example'},
|
| 461 |
+
{'name': 'Prerequisites'},
|
| 462 |
+
{'name': 'Learning_Outcomes'},
|
| 463 |
+
{'name': 'Common_Misconceptions'},
|
| 464 |
+
{'name': 'Difficulty'}
|
| 465 |
+
],
|
| 466 |
+
templates=[{
|
| 467 |
+
'name': 'Card 1',
|
| 468 |
+
'qfmt': '''
|
| 469 |
+
<div class="card question-side">
|
| 470 |
+
<div class="difficulty-indicator {{Difficulty}}"></div>
|
| 471 |
+
<div class="content">
|
| 472 |
+
<div class="question">{{Question}}</div>
|
| 473 |
+
<div class="prerequisites" onclick="event.stopPropagation();">
|
| 474 |
+
<div class="prerequisites-toggle">Show Prerequisites</div>
|
| 475 |
+
<div class="prerequisites-content">{{Prerequisites}}</div>
|
| 476 |
+
</div>
|
| 477 |
+
</div>
|
| 478 |
+
</div>
|
| 479 |
+
<script>
|
| 480 |
+
document.querySelector('.prerequisites-toggle').addEventListener('click', function(e) {
|
| 481 |
+
e.stopPropagation();
|
| 482 |
+
this.parentElement.classList.toggle('show');
|
| 483 |
+
});
|
| 484 |
+
</script>
|
| 485 |
+
''',
|
| 486 |
+
'afmt': '''
|
| 487 |
+
<div class="card answer-side">
|
| 488 |
+
<div class="content">
|
| 489 |
+
<div class="question-section">
|
| 490 |
+
<div class="question">{{Question}}</div>
|
| 491 |
+
<div class="prerequisites">
|
| 492 |
+
<strong>Prerequisites:</strong> {{Prerequisites}}
|
| 493 |
+
</div>
|
| 494 |
+
</div>
|
| 495 |
+
<hr>
|
| 496 |
+
|
| 497 |
+
<div class="answer-section">
|
| 498 |
+
<h3>Answer</h3>
|
| 499 |
+
<div class="answer">{{Answer}}</div>
|
| 500 |
+
</div>
|
| 501 |
+
|
| 502 |
+
<div class="explanation-section">
|
| 503 |
+
<h3>Explanation</h3>
|
| 504 |
+
<div class="explanation-text">{{Explanation}}</div>
|
| 505 |
+
</div>
|
| 506 |
+
|
| 507 |
+
<div class="example-section">
|
| 508 |
+
<h3>Example</h3>
|
| 509 |
+
<div class="example-text"></div>
|
| 510 |
+
<pre><code>{{Example}}</code></pre>
|
| 511 |
+
</div>
|
| 512 |
+
|
| 513 |
+
<div class="metadata-section">
|
| 514 |
+
<div class="learning-outcomes">
|
| 515 |
+
<h3>Learning Outcomes</h3>
|
| 516 |
+
<div>{{Learning_Outcomes}}</div>
|
| 517 |
+
</div>
|
| 518 |
+
|
| 519 |
+
<div class="misconceptions">
|
| 520 |
+
<h3>Common Misconceptions - Debunked</h3>
|
| 521 |
+
<div>{{Common_Misconceptions}}</div>
|
| 522 |
+
</div>
|
| 523 |
+
|
| 524 |
+
<div class="difficulty">
|
| 525 |
+
<h3>Difficulty Level</h3>
|
| 526 |
+
<div>{{Difficulty}}</div>
|
| 527 |
+
</div>
|
| 528 |
+
</div>
|
| 529 |
+
</div>
|
| 530 |
+
</div>
|
| 531 |
+
''',
|
| 532 |
+
}],
|
| 533 |
+
css='''
|
| 534 |
+
/* Base styles */
|
| 535 |
+
.card {
|
| 536 |
+
font-family: 'Inter', system-ui, -apple-system, sans-serif;
|
| 537 |
+
font-size: 16px;
|
| 538 |
+
line-height: 1.6;
|
| 539 |
+
color: #1a1a1a;
|
| 540 |
+
max-width: 800px;
|
| 541 |
+
margin: 0 auto;
|
| 542 |
+
padding: 20px;
|
| 543 |
+
background: #ffffff;
|
| 544 |
+
}
|
| 545 |
+
|
| 546 |
+
@media (max-width: 768px) {
|
| 547 |
+
.card {
|
| 548 |
+
font-size: 14px;
|
| 549 |
+
padding: 15px;
|
| 550 |
+
}
|
| 551 |
+
}
|
| 552 |
+
|
| 553 |
+
/* Question side */
|
| 554 |
+
.question-side {
|
| 555 |
+
position: relative;
|
| 556 |
+
min-height: 200px;
|
| 557 |
+
}
|
| 558 |
+
|
| 559 |
+
.difficulty-indicator {
|
| 560 |
+
position: absolute;
|
| 561 |
+
top: 10px;
|
| 562 |
+
right: 10px;
|
| 563 |
+
width: 10px;
|
| 564 |
+
height: 10px;
|
| 565 |
+
border-radius: 50%;
|
| 566 |
+
}
|
| 567 |
+
|
| 568 |
+
.difficulty-indicator.beginner { background: #4ade80; }
|
| 569 |
+
.difficulty-indicator.intermediate { background: #fbbf24; }
|
| 570 |
+
.difficulty-indicator.advanced { background: #ef4444; }
|
| 571 |
+
|
| 572 |
+
.question {
|
| 573 |
+
font-size: 1.3em;
|
| 574 |
+
font-weight: 600;
|
| 575 |
+
color: #2563eb;
|
| 576 |
+
margin-bottom: 1.5em;
|
| 577 |
+
}
|
| 578 |
+
|
| 579 |
+
.prerequisites {
|
| 580 |
+
margin-top: 1em;
|
| 581 |
+
font-size: 0.9em;
|
| 582 |
+
color: #666;
|
| 583 |
+
}
|
| 584 |
+
|
| 585 |
+
.prerequisites-toggle {
|
| 586 |
+
color: #2563eb;
|
| 587 |
+
cursor: pointer;
|
| 588 |
+
text-decoration: underline;
|
| 589 |
+
}
|
| 590 |
+
|
| 591 |
+
.prerequisites-content {
|
| 592 |
+
display: none;
|
| 593 |
+
margin-top: 0.5em;
|
| 594 |
+
padding: 0.5em;
|
| 595 |
+
background: #f8fafc;
|
| 596 |
+
border-radius: 4px;
|
| 597 |
+
}
|
| 598 |
+
|
| 599 |
+
.prerequisites.show .prerequisites-content {
|
| 600 |
+
display: block;
|
| 601 |
+
}
|
| 602 |
+
|
| 603 |
+
/* Answer side */
|
| 604 |
+
.answer-section,
|
| 605 |
+
.explanation-section,
|
| 606 |
+
.example-section {
|
| 607 |
+
margin: 1.5em 0;
|
| 608 |
+
padding: 1.2em;
|
| 609 |
+
border-radius: 8px;
|
| 610 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
|
| 611 |
+
}
|
| 612 |
+
|
| 613 |
+
.answer-section {
|
| 614 |
+
background: #f0f9ff;
|
| 615 |
+
border-left: 4px solid #2563eb;
|
| 616 |
+
}
|
| 617 |
+
|
| 618 |
+
.explanation-section {
|
| 619 |
+
background: #f0fdf4;
|
| 620 |
+
border-left: 4px solid #4ade80;
|
| 621 |
+
}
|
| 622 |
+
|
| 623 |
+
.example-section {
|
| 624 |
+
background: #fff7ed;
|
| 625 |
+
border-left: 4px solid #f97316;
|
| 626 |
+
}
|
| 627 |
+
|
| 628 |
+
/* Code blocks */
|
| 629 |
+
pre code {
|
| 630 |
+
display: block;
|
| 631 |
+
padding: 1em;
|
| 632 |
+
background: #1e293b;
|
| 633 |
+
color: #e2e8f0;
|
| 634 |
+
border-radius: 6px;
|
| 635 |
+
overflow-x: auto;
|
| 636 |
+
font-family: 'Fira Code', 'Consolas', monospace;
|
| 637 |
+
font-size: 0.9em;
|
| 638 |
+
}
|
| 639 |
+
|
| 640 |
+
/* Metadata tabs */
|
| 641 |
+
.metadata-tabs {
|
| 642 |
+
margin-top: 2em;
|
| 643 |
+
border: 1px solid #e5e7eb;
|
| 644 |
+
border-radius: 8px;
|
| 645 |
+
overflow: hidden;
|
| 646 |
+
}
|
| 647 |
+
|
| 648 |
+
.tab-buttons {
|
| 649 |
+
display: flex;
|
| 650 |
+
background: #f8fafc;
|
| 651 |
+
border-bottom: 1px solid #e5e7eb;
|
| 652 |
+
}
|
| 653 |
+
|
| 654 |
+
.tab-btn {
|
| 655 |
+
flex: 1;
|
| 656 |
+
padding: 0.8em;
|
| 657 |
+
border: none;
|
| 658 |
+
background: none;
|
| 659 |
+
cursor: pointer;
|
| 660 |
+
font-weight: 500;
|
| 661 |
+
color: #64748b;
|
| 662 |
+
transition: all 0.2s;
|
| 663 |
+
}
|
| 664 |
+
|
| 665 |
+
.tab-btn:hover {
|
| 666 |
+
background: #f1f5f9;
|
| 667 |
+
}
|
| 668 |
+
|
| 669 |
+
.tab-btn.active {
|
| 670 |
+
color: #2563eb;
|
| 671 |
+
background: #fff;
|
| 672 |
+
border-bottom: 2px solid #2563eb;
|
| 673 |
+
}
|
| 674 |
+
|
| 675 |
+
.tab-content {
|
| 676 |
+
display: none;
|
| 677 |
+
padding: 1.2em;
|
| 678 |
+
}
|
| 679 |
+
|
| 680 |
+
.tab-content.active {
|
| 681 |
+
display: block;
|
| 682 |
+
}
|
| 683 |
+
|
| 684 |
+
/* Responsive design */
|
| 685 |
+
@media (max-width: 640px) {
|
| 686 |
+
.tab-buttons {
|
| 687 |
+
flex-direction: column;
|
| 688 |
+
}
|
| 689 |
+
|
| 690 |
+
.tab-btn {
|
| 691 |
+
width: 100%;
|
| 692 |
+
text-align: left;
|
| 693 |
+
padding: 0.6em;
|
| 694 |
+
}
|
| 695 |
+
|
| 696 |
+
.answer-section,
|
| 697 |
+
.explanation-section,
|
| 698 |
+
.example-section {
|
| 699 |
+
padding: 1em;
|
| 700 |
+
margin: 1em 0;
|
| 701 |
+
}
|
| 702 |
+
}
|
| 703 |
+
|
| 704 |
+
/* Animations */
|
| 705 |
+
@keyframes fadeIn {
|
| 706 |
+
from { opacity: 0; }
|
| 707 |
+
to { opacity: 1; }
|
| 708 |
+
}
|
| 709 |
+
|
| 710 |
+
.card {
|
| 711 |
+
animation: fadeIn 0.3s ease-in-out;
|
| 712 |
+
}
|
| 713 |
+
|
| 714 |
+
.tab-content.active {
|
| 715 |
+
animation: fadeIn 0.2s ease-in-out;
|
| 716 |
+
}
|
| 717 |
+
'''
|
| 718 |
+
)
|
| 719 |
+
|
| 720 |
+
# Split the export functions
|
| 721 |
+
def export_csv(data):
|
| 722 |
+
"""Export the generated cards as a CSV file"""
|
| 723 |
+
if data is None:
|
| 724 |
+
raise gr.Error("No data to export. Please generate cards first.")
|
| 725 |
+
|
| 726 |
+
if len(data) < 2: # Minimum 2 cards
|
| 727 |
+
raise gr.Error("Need at least 2 cards to export.")
|
| 728 |
|
| 729 |
+
try:
|
| 730 |
+
gr.Info("💾 Exporting to CSV...")
|
| 731 |
+
csv_path = "anki_cards.csv"
|
| 732 |
+
data.to_csv(csv_path, index=False)
|
| 733 |
+
gr.Info("✅ CSV export complete!")
|
| 734 |
+
return gr.File(value=csv_path, visible=True)
|
| 735 |
+
|
| 736 |
+
except Exception as e:
|
| 737 |
+
logger.error(f"Failed to export CSV: {str(e)}", exc_info=True)
|
| 738 |
+
raise gr.Error(f"Failed to export CSV: {str(e)}")
|
|
|
|
|
|
|
| 739 |
|
| 740 |
+
def export_deck(data, subject):
|
| 741 |
+
"""Export the generated cards as an Anki deck with pedagogical metadata"""
|
| 742 |
+
if data is None:
|
| 743 |
+
raise gr.Error("No data to export. Please generate cards first.")
|
| 744 |
+
|
| 745 |
+
if len(data) < 2: # Minimum 2 cards
|
| 746 |
+
raise gr.Error("Need at least 2 cards to export.")
|
| 747 |
+
|
| 748 |
+
try:
|
| 749 |
+
gr.Info("💾 Creating Anki deck...")
|
| 750 |
+
|
| 751 |
+
deck_id = random.randrange(1 << 30, 1 << 31)
|
| 752 |
+
deck = genanki.Deck(deck_id, f"AnkiGen - {subject}")
|
| 753 |
+
|
| 754 |
+
records = data.to_dict('records')
|
| 755 |
+
|
| 756 |
+
# Update the model to include metadata fields
|
| 757 |
+
global BASIC_MODEL
|
| 758 |
+
BASIC_MODEL = genanki.Model(
|
| 759 |
+
random.randrange(1 << 30, 1 << 31),
|
| 760 |
+
'AnkiGen Enhanced',
|
| 761 |
+
fields=[
|
| 762 |
+
{'name': 'Question'},
|
| 763 |
+
{'name': 'Answer'},
|
| 764 |
+
{'name': 'Explanation'},
|
| 765 |
+
{'name': 'Example'},
|
| 766 |
+
{'name': 'Prerequisites'},
|
| 767 |
+
{'name': 'Learning_Outcomes'},
|
| 768 |
+
{'name': 'Common_Misconceptions'},
|
| 769 |
+
{'name': 'Difficulty'}
|
| 770 |
+
],
|
| 771 |
+
templates=[{
|
| 772 |
+
'name': 'Card 1',
|
| 773 |
+
'qfmt': '''
|
| 774 |
+
<div class="card question">
|
| 775 |
+
<div class="content">{{Question}}</div>
|
| 776 |
+
<div class="prerequisites">Prerequisites: {{Prerequisites}}</div>
|
| 777 |
+
</div>
|
| 778 |
+
''',
|
| 779 |
+
'afmt': '''
|
| 780 |
+
<div class="card answer">
|
| 781 |
+
<div class="question">{{Question}}</div>
|
| 782 |
+
<hr>
|
| 783 |
+
<div class="content">
|
| 784 |
+
<div class="answer-section">
|
| 785 |
+
<h3>Answer:</h3>
|
| 786 |
+
<div>{{Answer}}</div>
|
| 787 |
+
</div>
|
| 788 |
+
|
| 789 |
+
<div class="explanation-section">
|
| 790 |
+
<h3>Explanation:</h3>
|
| 791 |
+
<div>{{Explanation}}</div>
|
| 792 |
+
</div>
|
| 793 |
+
|
| 794 |
+
<div class="example-section">
|
| 795 |
+
<h3>Example:</h3>
|
| 796 |
+
<pre><code>{{Example}}</code></pre>
|
| 797 |
+
</div>
|
| 798 |
+
|
| 799 |
+
<div class="metadata-section">
|
| 800 |
+
<h3>Prerequisites:</h3>
|
| 801 |
+
<div>{{Prerequisites}}</div>
|
| 802 |
+
|
| 803 |
+
<h3>Learning Outcomes:</h3>
|
| 804 |
+
<div>{{Learning_Outcomes}}</div>
|
| 805 |
+
|
| 806 |
+
<h3>Watch out for:</h3>
|
| 807 |
+
<div>{{Common_Misconceptions}}</div>
|
| 808 |
+
|
| 809 |
+
<h3>Difficulty Level:</h3>
|
| 810 |
+
<div>{{Difficulty}}</div>
|
| 811 |
+
</div>
|
| 812 |
+
</div>
|
| 813 |
+
</div>
|
| 814 |
+
'''
|
| 815 |
+
}],
|
| 816 |
+
css='''
|
| 817 |
+
.card {
|
| 818 |
+
font-family: 'Inter', system-ui, -apple-system, sans-serif;
|
| 819 |
+
font-size: 16px;
|
| 820 |
+
line-height: 1.6;
|
| 821 |
+
color: #1a1a1a;
|
| 822 |
+
max-width: 800px;
|
| 823 |
+
margin: 0 auto;
|
| 824 |
+
padding: 20px;
|
| 825 |
+
background: #ffffff;
|
| 826 |
+
}
|
| 827 |
+
|
| 828 |
+
.question {
|
| 829 |
+
font-size: 1.3em;
|
| 830 |
+
font-weight: 600;
|
| 831 |
+
color: #2563eb;
|
| 832 |
+
margin-bottom: 1.5em;
|
| 833 |
+
}
|
| 834 |
+
|
| 835 |
+
.prerequisites {
|
| 836 |
+
font-size: 0.9em;
|
| 837 |
+
color: #666;
|
| 838 |
+
margin-top: 1em;
|
| 839 |
+
font-style: italic;
|
| 840 |
+
}
|
| 841 |
+
|
| 842 |
+
.answer-section,
|
| 843 |
+
.explanation-section,
|
| 844 |
+
.example-section {
|
| 845 |
+
margin: 1.5em 0;
|
| 846 |
+
padding: 1.2em;
|
| 847 |
+
border-radius: 8px;
|
| 848 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
|
| 849 |
+
}
|
| 850 |
+
|
| 851 |
+
.answer-section {
|
| 852 |
+
background: #f0f9ff;
|
| 853 |
+
border-left: 4px solid #2563eb;
|
| 854 |
+
}
|
| 855 |
+
|
| 856 |
+
.explanation-section {
|
| 857 |
+
background: #f0fdf4;
|
| 858 |
+
border-left: 4px solid #4ade80;
|
| 859 |
+
}
|
| 860 |
+
|
| 861 |
+
.example-section {
|
| 862 |
+
background: #fff7ed;
|
| 863 |
+
border-left: 4px solid #f97316;
|
| 864 |
+
}
|
| 865 |
+
|
| 866 |
+
.metadata-section {
|
| 867 |
+
background: #f8f9fa;
|
| 868 |
+
padding: 1em;
|
| 869 |
+
border-radius: 6px;
|
| 870 |
+
margin: 1em 0;
|
| 871 |
+
}
|
| 872 |
+
|
| 873 |
+
pre code {
|
| 874 |
+
display: block;
|
| 875 |
+
padding: 1em;
|
| 876 |
+
background: #1e293b;
|
| 877 |
+
color: #e2e8f0;
|
| 878 |
+
border-radius: 6px;
|
| 879 |
+
overflow-x: auto;
|
| 880 |
+
font-family: 'Fira Code', 'Consolas', monospace;
|
| 881 |
+
font-size: 0.9em;
|
| 882 |
+
}
|
| 883 |
+
'''
|
| 884 |
+
)
|
| 885 |
+
|
| 886 |
+
# Add notes to the deck
|
| 887 |
+
for record in records:
|
| 888 |
+
note = genanki.Note(
|
| 889 |
+
model=BASIC_MODEL,
|
| 890 |
+
fields=[
|
| 891 |
+
str(record['Question']),
|
| 892 |
+
str(record['Answer']),
|
| 893 |
+
str(record['Explanation']),
|
| 894 |
+
str(record['Example']),
|
| 895 |
+
str(record['Prerequisites']),
|
| 896 |
+
str(record['Learning_Outcomes']),
|
| 897 |
+
str(record['Common_Misconceptions']),
|
| 898 |
+
str(record['Difficulty'])
|
| 899 |
+
]
|
| 900 |
+
)
|
| 901 |
+
deck.add_note(note)
|
| 902 |
+
|
| 903 |
+
# Create a temporary directory for the package
|
| 904 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 905 |
+
output_path = Path(temp_dir) / "anki_deck.apkg"
|
| 906 |
+
package = genanki.Package(deck)
|
| 907 |
+
package.write_to_file(output_path)
|
| 908 |
+
|
| 909 |
+
# Copy to a more permanent location
|
| 910 |
+
final_path = "anki_deck.apkg"
|
| 911 |
+
with open(output_path, 'rb') as src, open(final_path, 'wb') as dst:
|
| 912 |
+
dst.write(src.read())
|
| 913 |
+
|
| 914 |
+
gr.Info("✅ Anki deck export complete!")
|
| 915 |
+
return gr.File(value=final_path, visible=True)
|
| 916 |
+
|
| 917 |
+
except Exception as e:
|
| 918 |
+
logger.error(f"Failed to export Anki deck: {str(e)}", exc_info=True)
|
| 919 |
+
raise gr.Error(f"Failed to export Anki deck: {str(e)}")
|
| 920 |
|
| 921 |
|
| 922 |
+
# Add this near the top where we define our CSS
|
| 923 |
+
js_storage = """
|
| 924 |
+
async () => {
|
| 925 |
+
// Load decks from localStorage
|
| 926 |
+
const loadDecks = () => {
|
| 927 |
+
const decks = localStorage.getItem('ankigen_decks');
|
| 928 |
+
return decks ? JSON.parse(decks) : [];
|
| 929 |
+
};
|
| 930 |
|
| 931 |
+
// Save decks to localStorage
|
| 932 |
+
const saveDecks = (decks) => {
|
| 933 |
+
localStorage.setItem('ankigen_decks', JSON.stringify(decks));
|
| 934 |
+
};
|
| 935 |
|
| 936 |
+
// Add methods to window for Gradio to access
|
| 937 |
+
window.loadStoredDecks = loadDecks;
|
| 938 |
+
window.saveStoredDecks = saveDecks;
|
| 939 |
+
|
| 940 |
+
// Initial load
|
| 941 |
+
return loadDecks();
|
| 942 |
+
}
|
| 943 |
+
"""
|
| 944 |
|
| 945 |
+
# Create a custom theme
|
| 946 |
+
custom_theme = gr.themes.Soft().set(
|
| 947 |
+
body_background_fill="*background_fill_secondary",
|
| 948 |
+
block_background_fill="*background_fill_primary",
|
| 949 |
+
block_border_width="0",
|
| 950 |
+
button_primary_background_fill="*primary_500",
|
| 951 |
+
button_primary_text_color="white",
|
| 952 |
+
)
|
| 953 |
+
|
| 954 |
+
def analyze_learning_path(api_key, description, model):
|
| 955 |
+
"""Analyze a job description or learning goal to create a structured learning path"""
|
| 956 |
+
|
| 957 |
+
try:
|
| 958 |
+
client = OpenAI(api_key=api_key)
|
| 959 |
+
except Exception as e:
|
| 960 |
+
logger.error(f"Failed to initialize OpenAI client: {str(e)}")
|
| 961 |
+
raise gr.Error(f"Failed to initialize OpenAI client: {str(e)}")
|
| 962 |
+
|
| 963 |
+
system_prompt = """You are an expert curriculum designer and educational consultant.
|
| 964 |
+
Your task is to analyze learning goals and create structured, achievable learning paths.
|
| 965 |
+
Break down complex topics into manageable subjects, identify prerequisites,
|
| 966 |
+
and suggest practical projects that reinforce learning.
|
| 967 |
+
Focus on creating a logical progression that builds upon previous knowledge."""
|
| 968 |
+
|
| 969 |
+
path_prompt = f"""
|
| 970 |
+
Analyze this description and create a structured learning path.
|
| 971 |
+
Return your analysis as a JSON object with the following structure:
|
| 972 |
+
{{
|
| 973 |
+
"subjects": [
|
| 974 |
+
{{
|
| 975 |
+
"Subject": "name of the subject",
|
| 976 |
+
"Prerequisites": "required prior knowledge",
|
| 977 |
+
"Time Estimate": "estimated time to learn"
|
| 978 |
+
}}
|
| 979 |
+
],
|
| 980 |
+
"learning_order": "recommended sequence of study",
|
| 981 |
+
"projects": "suggested practical projects"
|
| 982 |
+
}}
|
| 983 |
+
|
| 984 |
+
Description to analyze:
|
| 985 |
+
{description}
|
| 986 |
+
"""
|
| 987 |
+
|
| 988 |
+
try:
|
| 989 |
+
response = structured_output_completion(
|
| 990 |
+
client,
|
| 991 |
+
model,
|
| 992 |
+
{"type": "json_object"},
|
| 993 |
+
system_prompt,
|
| 994 |
+
path_prompt
|
| 995 |
+
)
|
| 996 |
+
|
| 997 |
+
# Format the response for the UI
|
| 998 |
+
subjects_df = pd.DataFrame(response["subjects"])
|
| 999 |
+
learning_order_text = f"### Recommended Learning Order\n{response['learning_order']}"
|
| 1000 |
+
projects_text = f"### Suggested Projects\n{response['projects']}"
|
| 1001 |
+
|
| 1002 |
+
return subjects_df, learning_order_text, projects_text
|
| 1003 |
+
|
| 1004 |
+
except Exception as e:
|
| 1005 |
+
logger.error(f"Failed to analyze learning path: {str(e)}")
|
| 1006 |
+
raise gr.Error(f"Failed to analyze learning path: {str(e)}")
|
| 1007 |
|
| 1008 |
with gr.Blocks(
|
| 1009 |
+
theme=custom_theme,
|
| 1010 |
+
title="AnkiGen",
|
| 1011 |
+
css="""
|
| 1012 |
+
#footer {display:none !important}
|
| 1013 |
+
.tall-dataframe {height: 800px !important}
|
| 1014 |
+
.contain {max-width: 1200px; margin: auto;}
|
| 1015 |
+
.output-cards {border-radius: 8px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1);}
|
| 1016 |
+
.hint-text {font-size: 0.9em; color: #666; margin-top: 4px;}
|
| 1017 |
+
""",
|
| 1018 |
+
js=js_storage, # Add the JavaScript
|
| 1019 |
) as ankigen:
|
| 1020 |
+
with gr.Column(elem_classes="contain"):
|
| 1021 |
+
gr.Markdown("# 📚 AnkiGen - Advanced Anki Card Generator")
|
| 1022 |
+
gr.Markdown("""
|
| 1023 |
+
#### Generate comprehensive Anki flashcards using AI.
|
| 1024 |
+
""")
|
| 1025 |
|
| 1026 |
+
with gr.Row():
|
| 1027 |
+
with gr.Column(scale=1):
|
| 1028 |
+
gr.Markdown("### Configuration")
|
| 1029 |
+
|
| 1030 |
+
# Add mode selection
|
| 1031 |
+
generation_mode = gr.Radio(
|
| 1032 |
+
choices=[
|
| 1033 |
+
"subject",
|
| 1034 |
+
"path"
|
| 1035 |
+
],
|
| 1036 |
+
value="subject",
|
| 1037 |
+
label="Generation Mode",
|
| 1038 |
+
info="Choose how you want to generate content"
|
| 1039 |
+
)
|
| 1040 |
+
|
| 1041 |
+
# Create containers for different modes
|
| 1042 |
+
with gr.Group() as subject_mode:
|
| 1043 |
+
subject = gr.Textbox(
|
| 1044 |
+
label="Subject",
|
| 1045 |
+
placeholder="Enter the subject, e.g., 'Basic SQL Concepts'",
|
| 1046 |
+
info="The topic you want to generate flashcards for"
|
| 1047 |
+
)
|
| 1048 |
+
|
| 1049 |
+
with gr.Group(visible=False) as path_mode:
|
| 1050 |
+
description = gr.Textbox(
|
| 1051 |
+
label="Learning Goal",
|
| 1052 |
+
placeholder="Paste a job description or describe what you want to learn...",
|
| 1053 |
+
info="We'll break this down into learnable subjects",
|
| 1054 |
+
lines=5
|
| 1055 |
+
)
|
| 1056 |
+
analyze_button = gr.Button("Analyze & Break Down", variant="secondary")
|
| 1057 |
+
|
| 1058 |
+
# Common settings
|
| 1059 |
+
api_key_input = gr.Textbox(
|
| 1060 |
+
label="OpenAI API Key",
|
| 1061 |
+
type="password",
|
| 1062 |
+
placeholder="Enter your OpenAI API key",
|
| 1063 |
+
value=os.getenv("OPENAI_API_KEY", ""),
|
| 1064 |
+
info="Your OpenAI API key starting with 'sk-'"
|
| 1065 |
+
)
|
| 1066 |
+
|
| 1067 |
+
# Generation Button
|
| 1068 |
+
generate_button = gr.Button("Generate Cards", variant="primary")
|
| 1069 |
|
| 1070 |
+
# Advanced Settings in Accordion
|
| 1071 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 1072 |
+
model_choice = gr.Dropdown(
|
| 1073 |
+
choices=[
|
| 1074 |
+
"gpt-4o-mini",
|
| 1075 |
+
"gpt-4o",
|
| 1076 |
+
"o1"
|
| 1077 |
+
],
|
| 1078 |
+
value="gpt-4o-mini",
|
| 1079 |
+
label="Model Selection",
|
| 1080 |
+
info="Select the AI model to use for generation"
|
| 1081 |
+
)
|
| 1082 |
+
|
| 1083 |
+
# Add tooltip/description for models
|
| 1084 |
+
model_info = gr.Markdown("""
|
| 1085 |
+
**Model Information:**
|
| 1086 |
+
- **gpt-4o-mini**: Fastest option, good for most use cases
|
| 1087 |
+
- **gpt-4o**: Better quality, takes longer to generate
|
| 1088 |
+
- **o1**: Highest quality, longest generation time
|
| 1089 |
+
""")
|
| 1090 |
+
|
| 1091 |
+
topic_number = gr.Slider(
|
| 1092 |
+
label="Number of Topics",
|
| 1093 |
+
minimum=2,
|
| 1094 |
+
maximum=20,
|
| 1095 |
+
step=1,
|
| 1096 |
+
value=2,
|
| 1097 |
+
info="How many distinct topics to cover within the subject",
|
| 1098 |
+
)
|
| 1099 |
+
cards_per_topic = gr.Slider(
|
| 1100 |
+
label="Cards per Topic",
|
| 1101 |
+
minimum=2,
|
| 1102 |
+
maximum=30,
|
| 1103 |
+
step=1,
|
| 1104 |
+
value=3,
|
| 1105 |
+
info="How many flashcards to generate for each topic",
|
| 1106 |
+
)
|
| 1107 |
+
preference_prompt = gr.Textbox(
|
| 1108 |
+
label="Learning Preferences",
|
| 1109 |
+
placeholder="e.g., 'Assume I'm a beginner' or 'Focus on practical examples'",
|
| 1110 |
+
info="Customize how the content is presented",
|
| 1111 |
+
lines=3,
|
| 1112 |
+
)
|
| 1113 |
+
|
| 1114 |
+
# Right column - add a new container for learning path results
|
| 1115 |
+
with gr.Column(scale=2):
|
| 1116 |
+
with gr.Group(visible=False) as path_results:
|
| 1117 |
+
gr.Markdown("### Learning Path Analysis")
|
| 1118 |
+
subjects_list = gr.Dataframe(
|
| 1119 |
+
headers=["Subject", "Prerequisites", "Time Estimate"],
|
| 1120 |
+
label="Recommended Subjects",
|
| 1121 |
+
interactive=False
|
| 1122 |
+
)
|
| 1123 |
+
learning_order = gr.Markdown("### Recommended Learning Order")
|
| 1124 |
+
projects = gr.Markdown("### Suggested Projects")
|
| 1125 |
+
|
| 1126 |
+
# Replace generate_selected with use_subjects
|
| 1127 |
+
use_subjects = gr.Button(
|
| 1128 |
+
"Use These Subjects ℹ️", # Added info emoji to button text
|
| 1129 |
+
variant="primary"
|
| 1130 |
+
)
|
| 1131 |
+
gr.Markdown(
|
| 1132 |
+
"*Click to copy subjects to main input for card generation*",
|
| 1133 |
+
elem_classes="hint-text"
|
| 1134 |
+
)
|
| 1135 |
+
|
| 1136 |
+
# Existing output components
|
| 1137 |
+
with gr.Group() as cards_output:
|
| 1138 |
+
gr.Markdown("### Generated Cards")
|
| 1139 |
+
|
| 1140 |
+
# Output Format Documentation
|
| 1141 |
+
with gr.Accordion("Output Format", open=True):
|
| 1142 |
+
gr.Markdown("""
|
| 1143 |
+
The generated cards include:
|
| 1144 |
+
|
| 1145 |
+
* **Index**: Unique identifier for each card
|
| 1146 |
+
* **Topic**: The specific subtopic within your subject
|
| 1147 |
+
* **Question**: Clear, focused question for the flashcard front
|
| 1148 |
+
* **Answer**: Concise core answer
|
| 1149 |
+
* **Explanation**: Detailed conceptual explanation
|
| 1150 |
+
* **Example**: Practical implementation or code example
|
| 1151 |
+
* **Prerequisites**: Required knowledge for this concept
|
| 1152 |
+
* **Learning Outcomes**: What you should understand after mastering this card
|
| 1153 |
+
* **Common Misconceptions**: Incorrect assumptions debunked with explanations
|
| 1154 |
+
* **Difficulty**: Concept complexity level for optimal study sequencing
|
| 1155 |
+
|
| 1156 |
+
Export options:
|
| 1157 |
+
- **CSV**: Raw data for custom processing
|
| 1158 |
+
- **Anki Deck**: Ready-to-use deck with formatted cards and metadata
|
| 1159 |
+
""")
|
| 1160 |
+
|
| 1161 |
+
# Add near the output format documentation
|
| 1162 |
+
with gr.Accordion("Example Card Format", open=False):
|
| 1163 |
+
gr.Code(
|
| 1164 |
+
label="Example Card",
|
| 1165 |
+
value='''
|
| 1166 |
+
{
|
| 1167 |
+
"front": {
|
| 1168 |
+
"question": "What is a PRIMARY KEY constraint in SQL?"
|
| 1169 |
+
},
|
| 1170 |
+
"back": {
|
| 1171 |
+
"answer": "A PRIMARY KEY constraint uniquely identifies each record in a table",
|
| 1172 |
+
"explanation": "A primary key serves as a unique identifier for each row in a database table. It enforces data integrity by ensuring that:\n1. Each value is unique\n2. No null values are allowed\n3. The value remains stable over time\n\nThis is fundamental for:\n- Establishing relationships between tables\n- Maintaining data consistency\n- Efficient data retrieval",
|
| 1173 |
+
"example": "-- Creating a table with a primary key\nCREATE TABLE Users (\n user_id INT PRIMARY KEY,\n username VARCHAR(50) NOT NULL,\n email VARCHAR(100) UNIQUE\n);"
|
| 1174 |
+
},
|
| 1175 |
+
"metadata": {
|
| 1176 |
+
"prerequisites": ["Basic SQL table concepts", "Understanding of data types"],
|
| 1177 |
+
"learning_outcomes": ["Understand the purpose and importance of primary keys", "Know how to create and use primary keys"],
|
| 1178 |
+
"common_misconceptions": [
|
| 1179 |
+
"❌ Misconception: Primary keys must always be single columns\n✓ Reality: Primary keys can be composite (multiple columns)",
|
| 1180 |
+
"❌ Misconception: Primary keys must be integers\n✓ Reality: Any data type that ensures uniqueness can be used"
|
| 1181 |
],
|
| 1182 |
+
"difficulty": "beginner"
|
| 1183 |
+
}
|
| 1184 |
+
}
|
| 1185 |
+
''',
|
| 1186 |
+
language="json"
|
| 1187 |
+
)
|
| 1188 |
+
|
| 1189 |
+
# Dataframe Output
|
| 1190 |
+
output = gr.Dataframe(
|
| 1191 |
+
headers=[
|
| 1192 |
+
"Index",
|
| 1193 |
+
"Topic",
|
| 1194 |
+
"Question",
|
| 1195 |
+
"Answer",
|
| 1196 |
+
"Explanation",
|
| 1197 |
+
"Example",
|
| 1198 |
+
"Prerequisites",
|
| 1199 |
+
"Learning_Outcomes",
|
| 1200 |
+
"Common_Misconceptions",
|
| 1201 |
+
"Difficulty"
|
| 1202 |
+
],
|
| 1203 |
+
interactive=True,
|
| 1204 |
+
elem_classes="tall-dataframe",
|
| 1205 |
+
wrap=True,
|
| 1206 |
+
column_widths=[50, 100, 200, 200, 250, 200, 150, 150, 150, 100],
|
| 1207 |
+
)
|
| 1208 |
+
|
| 1209 |
+
# Export Controls
|
| 1210 |
+
with gr.Row():
|
| 1211 |
+
with gr.Column():
|
| 1212 |
+
gr.Markdown("### Export Options")
|
| 1213 |
+
with gr.Row():
|
| 1214 |
+
export_csv_button = gr.Button("Export to CSV", variant="secondary")
|
| 1215 |
+
export_anki_button = gr.Button("Export to Anki Deck", variant="secondary")
|
| 1216 |
+
download_csv = gr.File(label="Download CSV", interactive=False, visible=False)
|
| 1217 |
+
download_anki = gr.File(label="Download Anki Deck", interactive=False, visible=False)
|
| 1218 |
+
|
| 1219 |
+
# Add near the top of the Blocks
|
| 1220 |
+
with gr.Row():
|
| 1221 |
+
progress = gr.HTML(visible=False)
|
| 1222 |
+
total_cards = gr.Number(label="Total Cards Generated", value=0, visible=False)
|
| 1223 |
+
|
| 1224 |
+
# Add JavaScript to handle mode switching
|
| 1225 |
+
def update_mode_visibility(mode):
|
| 1226 |
+
"""Update component visibility based on selected mode and clear values"""
|
| 1227 |
+
is_subject = (mode == "subject")
|
| 1228 |
+
is_path = (mode == "path")
|
| 1229 |
+
|
| 1230 |
+
# Clear values when switching modes
|
| 1231 |
+
if is_path:
|
| 1232 |
+
subject.value = "" # Clear subject when switching to path mode
|
| 1233 |
+
else:
|
| 1234 |
+
description.value = "" # Clear description when switching to subject mode
|
| 1235 |
+
|
| 1236 |
+
return {
|
| 1237 |
+
subject_mode: gr.update(visible=is_subject),
|
| 1238 |
+
path_mode: gr.update(visible=is_path),
|
| 1239 |
+
path_results: gr.update(visible=is_path),
|
| 1240 |
+
cards_output: gr.update(visible=not is_path),
|
| 1241 |
+
subject: gr.update(value="") if is_path else gr.update(),
|
| 1242 |
+
description: gr.update(value="") if not is_path else gr.update(),
|
| 1243 |
+
output: gr.update(value=None), # Clear previous output
|
| 1244 |
+
progress: gr.update(value="", visible=False),
|
| 1245 |
+
total_cards: gr.update(value=0, visible=False)
|
| 1246 |
+
}
|
| 1247 |
+
|
| 1248 |
+
# Update the mode switching handler to include all components that need clearing
|
| 1249 |
+
generation_mode.change(
|
| 1250 |
+
fn=update_mode_visibility,
|
| 1251 |
+
inputs=[generation_mode],
|
| 1252 |
+
outputs=[
|
| 1253 |
+
subject_mode,
|
| 1254 |
+
path_mode,
|
| 1255 |
+
path_results,
|
| 1256 |
+
cards_output,
|
| 1257 |
+
subject,
|
| 1258 |
+
description,
|
| 1259 |
+
output,
|
| 1260 |
+
progress,
|
| 1261 |
+
total_cards
|
| 1262 |
+
]
|
| 1263 |
+
)
|
| 1264 |
+
|
| 1265 |
+
# Add handler for path analysis
|
| 1266 |
+
analyze_button.click(
|
| 1267 |
+
fn=analyze_learning_path,
|
| 1268 |
+
inputs=[api_key_input, description, model_choice],
|
| 1269 |
+
outputs=[subjects_list, learning_order, projects]
|
| 1270 |
+
)
|
| 1271 |
+
|
| 1272 |
+
# Add this function to handle copying subjects to main input
|
| 1273 |
+
def use_selected_subjects(subjects_df, topic_number):
|
| 1274 |
+
"""Copy selected subjects to main input and switch to subject mode"""
|
| 1275 |
+
if subjects_df is None or subjects_df.empty:
|
| 1276 |
+
raise gr.Error("No subjects available to copy")
|
| 1277 |
+
|
| 1278 |
+
# Get all subjects and join them
|
| 1279 |
+
subjects = subjects_df["Subject"].tolist()
|
| 1280 |
+
combined_subject = ", ".join(subjects)
|
| 1281 |
+
|
| 1282 |
+
# Calculate reasonable number of topics based on number of subjects
|
| 1283 |
+
suggested_topics = min(len(subjects) + 2, 20) # Add 2 for related concepts, cap at 20
|
| 1284 |
+
|
| 1285 |
+
# Return updates for individual components instead of groups
|
| 1286 |
+
return (
|
| 1287 |
+
"subject", # generation_mode value
|
| 1288 |
+
gr.update(visible=True), # subject textbox visibility
|
| 1289 |
+
gr.update(visible=False), # description textbox visibility
|
| 1290 |
+
gr.update(visible=False), # subjects_list visibility
|
| 1291 |
+
gr.update(visible=False), # learning_order visibility
|
| 1292 |
+
gr.update(visible=False), # projects visibility
|
| 1293 |
+
gr.update(visible=True), # output visibility
|
| 1294 |
+
combined_subject, # subject value
|
| 1295 |
+
suggested_topics, # topic_number value
|
| 1296 |
+
"Focus on connections between these subjects and their practical applications" # preference_prompt
|
| 1297 |
+
)
|
| 1298 |
+
|
| 1299 |
+
# Update the click handler to match the new outputs
|
| 1300 |
+
use_subjects.click(
|
| 1301 |
+
fn=use_selected_subjects,
|
| 1302 |
+
inputs=[subjects_list, topic_number],
|
| 1303 |
+
outputs=[
|
| 1304 |
+
generation_mode,
|
| 1305 |
+
subject, # Individual components instead of groups
|
| 1306 |
+
description,
|
| 1307 |
+
subjects_list,
|
| 1308 |
+
learning_order,
|
| 1309 |
+
projects,
|
| 1310 |
+
output,
|
| 1311 |
+
subject,
|
| 1312 |
+
topic_number,
|
| 1313 |
+
preference_prompt
|
| 1314 |
+
]
|
| 1315 |
+
)
|
| 1316 |
|
| 1317 |
+
# Simplified event handlers
|
| 1318 |
+
generate_button.click(
|
| 1319 |
+
fn=generate_cards,
|
| 1320 |
+
inputs=[
|
| 1321 |
+
api_key_input,
|
| 1322 |
+
subject,
|
| 1323 |
+
model_choice, # Add model selection
|
| 1324 |
+
topic_number,
|
| 1325 |
+
cards_per_topic,
|
| 1326 |
+
preference_prompt,
|
| 1327 |
+
],
|
| 1328 |
+
outputs=[output, progress, total_cards],
|
| 1329 |
+
show_progress=True,
|
| 1330 |
+
)
|
| 1331 |
+
|
| 1332 |
+
export_csv_button.click(
|
| 1333 |
+
fn=export_csv,
|
| 1334 |
+
inputs=[output],
|
| 1335 |
+
outputs=download_csv,
|
| 1336 |
+
show_progress="full",
|
| 1337 |
+
)
|
| 1338 |
+
|
| 1339 |
+
export_anki_button.click(
|
| 1340 |
+
fn=export_deck,
|
| 1341 |
+
inputs=[output, subject],
|
| 1342 |
+
outputs=download_anki,
|
| 1343 |
+
show_progress="full",
|
| 1344 |
+
)
|
| 1345 |
|
| 1346 |
if __name__ == "__main__":
|
| 1347 |
+
logger.info("Starting AnkiGen application")
|
| 1348 |
ankigen.launch(share=False, favicon_path="./favicon.ico")
|
requirements.txt
CHANGED
|
@@ -1,2 +1,5 @@
|
|
| 1 |
gradio
|
| 2 |
-
openai
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
+
openai
|
| 3 |
+
tenacity>=8.2.3
|
| 4 |
+
genanki>=0.13.0
|
| 5 |
+
pandas>=2.0.0
|