Skip to content

Commit

Permalink
Update index.qmd
Browse files Browse the repository at this point in the history
  • Loading branch information
awellis committed Nov 28, 2024
1 parent 7826e28 commit 371edb3
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions projects/anki/example/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ execute:
cache: true
---

First, we import the required libraries:

```{python}
import os
import csv
Expand All @@ -15,11 +17,15 @@ from pydantic import BaseModel, Field
from typing import List
```

Initialize the OpenAI client using environment variables:

```{python}
load_dotenv()
client = OpenAI()
```

Define our data models using Pydantic for type safety and validation:

```{python}
class AnkiFlashcard(BaseModel):
"""
Expand All @@ -31,6 +37,8 @@ class AnkiFlashcard(BaseModel):
tags: List[str] = Field(..., description="List of tags associated with the flashcard")
```

Create a model to represent a complete deck of flashcards:

```{python}
class AnkiDeck(BaseModel):
"""
Expand All @@ -41,6 +49,8 @@ class AnkiDeck(BaseModel):
deck_name: str = Field(..., description="Name of the Anki deck")
```

Define the main function that generates flashcards using GPT-4:

```{python}
def generate_structured_flashcards(text: str,
deck_name: str,
Expand Down Expand Up @@ -87,13 +97,16 @@ def generate_structured_flashcards(text: str,
return completion.choices[0].message.parsed
```

Load our sample text about the Romantic era from a markdown file:

```{python}
#| echo: false
with open("romantic-essay.md", "r") as file:
romantic_text = file.read()
```

Display the content of the romantic text (hidden by default):

:::{.callout-note collapse=true title="`romantic_text`"}
## The Romantic Era: Emotion Unleashed (1810-1910)

Expand Down Expand Up @@ -138,27 +151,35 @@ The Romantic period gradually gave way to various modern movements. The increasi
The Romantic era's emphasis on emotional expression, its technical innovations, and its expansion of musical possibilities created a legacy that continues to influence musicians today. The period's great works remain central to the concert repertoire, beloved for their emotional depth and expressive power.
:::


```{python}
#| echo: false
#| eval: false
print(romantic_text)
```

Generate a deck of 10 flashcards about the Romantic era:

```{python}
romantic_deck = generate_structured_flashcards(romantic_text,
"Romanticism",
num_cards=10)
```

Display the complete deck object:

```{python}
romantic_deck
```

Show the deck in JSON format:

```{python}
print(romantic_deck.model_dump_json())
```

Print each flashcard in a readable format:

```{python}
for card in romantic_deck.cards:
print(f"Question: {card.question}")
Expand All @@ -167,6 +188,8 @@ for card in romantic_deck.cards:
print("-" * 20)
```

Export the flashcards to a CSV file for importing into Anki:

```{python}
os.makedirs('assets/flashcards', exist_ok=True)
Expand Down

0 comments on commit 371edb3

Please sign in to comment.