import tempfile import os import uuid import genanki import random from google import genai def generate_anki_deck(notes_text): client = genai.Client() # Advanced logic prompt to strictly force the AI to return parseable data arrays extraction_prompt = f""" You are an expert educational assistant designed to extract spaced-repetition flashcards from study notes. Extract key facts, concepts, and definitions from the provided study notes. Format your response STRICTLY as a list of Question and Answer pairs. You MUST separate the question and the answer with ':::' and place each pair on a new line. DO NOT include any markdown formatting, numbers, bullet points, explanations, or introductory text. Example correct format: What is the powerhouse of the cell?:::The mitochondria. Who wrote Hamlet?:::William Shakespeare. Here are the study notes to extract flashcards from: {notes_text} """ try: response = client.models.generate_content( model="gemini-2.5-flash", contents=extraction_prompt ) raw_output = response.text.strip() # Parse the output robustly cards = [] for line in raw_output.split('\n'): if ':::' in line: parts = line.split(':::') if len(parts) == 2: q = parts[0].strip().replace('*', '').replace('#', '') a = parts[1].strip().replace('*', '').replace('#', '') if q and a: cards.append((q, a)) if not cards: return None # Genanki IDs must be unique numerical identifiers deck_id = random.randrange(1 << 30, 1 << 31) model_id = random.randrange(1 << 30, 1 << 31) # Create the Anki Model (Note Type HTML Structure) anki_model = genanki.Model( model_id, 'Simple QA Model', fields=[ {'name': 'Question'}, {'name': 'Answer'}, ], templates=[ { 'name': 'Card 1', 'qfmt': '