-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from edx/generate-anki-cards
Feat: Generate anki cards from openai response
- Loading branch information
Showing
11 changed files
with
320 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
""" | ||
Implementation of AnkiConnect to create cards | ||
""" | ||
from flashcards.apps.cards.cardgen import cards_from_block_id | ||
import genanki | ||
|
||
|
||
def create_anki_cards(openai_data): | ||
""" | ||
Generates anki cards from data | ||
""" | ||
my_model = genanki.Model( | ||
1607392319, | ||
'Simple Model', | ||
fields=[ | ||
{'name': 'Question'}, | ||
{'name': 'Answer'}, | ||
], | ||
templates=[ | ||
{ | ||
'name': 'Card 1', | ||
'qfmt': '{{Question}}', | ||
'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}', | ||
}, | ||
]) | ||
|
||
my_deck = genanki.Deck( | ||
2059400111, | ||
'Demo Deck 1') | ||
|
||
rows = openai_data.split('\n') | ||
for row in rows: | ||
question, answer = row.split(',', 1) | ||
|
||
my_note = genanki.Note( | ||
model=my_model, | ||
fields=[question, answer]) | ||
|
||
my_deck.add_note(my_note) | ||
|
||
genanki.Package(my_deck).write_to_file('demo_output.apkg') | ||
|
||
|
||
def main(): | ||
""" | ||
Master function that gets a response from openai and passes the result to Anki | ||
""" | ||
result = cards_from_block_id('course-v1:edX+DemoX+Demo_Course', | ||
'block-v1:edX+DemoX+Demo_Course+type@vertical+block@867dddb6f55d410caaa9c1eb9c6743ec') | ||
# TODO: Insert some kind of data validation here to make sure openai sent back something nice | ||
result = result.replace('\t', '') | ||
create_anki_cards(result) | ||
|
||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.