Skip to content

Commit 47d3a63

Browse files
committed
fix: continue session on another device, Fixes #21
1 parent b3ec208 commit 47d3a63

File tree

13 files changed

+377
-359
lines changed

13 files changed

+377
-359
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,3 @@
33
[![fuzzy-couscous](https://img.shields.io/badge/built%20with-fuzzy--couscous-success)](https://github.com/Tobi-De/fuzzy-couscous)
44
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
55
[![linting: pylint](https://img.shields.io/badge/linting-pylint-yellowgreen)](https://github.com/PyCQA/pylint)
6-
7-

config/settings.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@
205205
)
206206
SERVER_EMAIL = env("DJANGO_SERVER_EMAIL", default=DEFAULT_FROM_EMAIL)
207207

208+
CACHES = {
209+
"default": {
210+
"BACKEND": "django.core.cache.backends.db.DatabaseCache",
211+
"LOCATION": "leerming_cache_table",
212+
}
213+
}
214+
208215
if DJANGO_ENV == "production":
209216
import sentry_sdk
210217
from sentry_sdk.integrations.django import DjangoIntegration

docker/entrypoint.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
python manage.py compress
44
python manage.py collectstatic --noinput
55
python manage.py migrate
6+
python manage.py createcachetable
67
python manage.py installwatson
78
python manage.py makesuperuser
89
#gunicorn config.wsgi --config="docker/gunicorn.conf.py"

leerming/flashcards/forms.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ def clean(self):
120120

121121

122122
class LLMFlashCard(forms.ModelForm):
123+
card_type = forms.CharField(label="", widget=forms.HiddenInput())
123124

124-
card_type = forms.CharField(label="",widget=forms.HiddenInput())
125125
class Meta:
126126
model = FlashCard
127127
fields = ("question", "answer", "card_type")
@@ -144,5 +144,3 @@ def clean(self):
144144
),
145145
)
146146
return cleaned_data
147-
148-

leerming/flashcards/llm_utils/__init__.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22
from dataclasses import dataclass
33
from functools import cached_property
44

5-
65
from django.http import HttpRequest
76
from langchain.llms import OpenAI
87

98
from ..models import FlashCard
10-
from .prompts import (
11-
clozed_delete_template,
12-
front_back_template,
13-
pipe_separated_list_output_parser,
14-
double_pipe_separated_list_output_parser,
15-
ParsedCard,
16-
)
9+
from .prompts import clozed_delete_template
10+
from .prompts import double_pipe_separated_list_output_parser
11+
from .prompts import front_back_template
12+
from .prompts import ParsedCard
13+
from .prompts import pipe_separated_list_output_parser
1714

1815

1916
LLM = OpenAI()
@@ -64,7 +61,9 @@ def make_flashcards_from(
6461
"min_result": max_result,
6562
}
6663
)
67-
return [LLMFlashCard(**c, card_type=card_type, topic_id=topic_id) for c in parsed_cards]
64+
return [
65+
LLMFlashCard(**c, card_type=card_type, topic_id=topic_id) for c in parsed_cards
66+
]
6867

6968

7069
def save_llm_flashcards_to_session(

leerming/flashcards/llm_utils/prompts.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import re
22
import uuid
3+
from typing import TypedDict
4+
35
from langchain.prompts import PromptTemplate
46
from langchain.schema import BaseOutputParser
5-
from typing import TypedDict
67

78

89
"""
@@ -72,26 +73,27 @@
7273

7374

7475
class ParsedCard(TypedDict):
75-
id:str
76-
question:str
76+
id: str
77+
question: str
7778
answer: str
7879

7980

8081
class PipeSeparatedListOutputParser(BaseOutputParser):
8182
"""Parse the output of an LLM call to a comma-separated list."""
8283

83-
84-
def parse(self, text: str)->list[ParsedCard]:
84+
def parse(self, text: str) -> list[ParsedCard]:
8585
"""Parse the output of an LLM call."""
86-
statements = text.strip().split("|")
86+
statements = text.strip().split("|")
8787
flashcards = []
8888
for statement in statements:
89-
statement, clozed_deletions = self.extract_clozed_deletions(statement).values()
89+
statement, clozed_deletions = self.extract_clozed_deletions(
90+
statement
91+
).values()
9092
flashcards.extend(
9193
{
92-
"id":str(uuid.uuid4()),
93-
"question":statement,
94-
"answer":cz,
94+
"id": str(uuid.uuid4()),
95+
"question": statement,
96+
"answer": cz,
9597
}
9698
for cz in clozed_deletions
9799
)
@@ -103,25 +105,31 @@ def extract_clozed_deletions(statement):
103105
cloze_matches = re.findall(cloze_pattern, statement)
104106
cleaned_statement = re.sub(r"{c\d+::(.*?)}", r"\1", statement)
105107

106-
return {"statement": cleaned_statement.strip(), "clozed_deletions": cloze_matches}
107-
108+
return {
109+
"statement": cleaned_statement.strip(),
110+
"clozed_deletions": cloze_matches,
111+
}
112+
113+
108114
pipe_separated_list_output_parser = PipeSeparatedListOutputParser()
109-
110-
class DoublePipeSeparatedListOutputParser(BaseOutputParser):
111115

112-
def parse(self, text: str)->list[ParsedCard]:
116+
117+
class DoublePipeSeparatedListOutputParser(BaseOutputParser):
118+
def parse(self, text: str) -> list[ParsedCard]:
113119
result = text.strip().split("||")
114-
print(result)
115120
flashcards = []
116121
for qa in result:
117122
try:
118-
flashcards.append({
119-
"id":str(uuid.uuid4()),
120-
"question":qa.split("|")[0],
121-
"answer":qa.split("|")[1],
122-
})
123+
flashcards.append(
124+
{
125+
"id": str(uuid.uuid4()),
126+
"question": qa.split("|")[0],
127+
"answer": qa.split("|")[1],
128+
}
129+
)
123130
except IndexError:
124131
continue
125132
return flashcards
126-
127-
double_pipe_separated_list_output_parser = DoublePipeSeparatedListOutputParser()
133+
134+
135+
double_pipe_separated_list_output_parser = DoublePipeSeparatedListOutputParser()

leerming/flashcards/views.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ def edit_llm_flashcard(request: HttpRequest, id: str):
132132
flashcard = [flashcard for flashcard in flashcards if flashcard.id == id][0]
133133
form = LLMFlashCard(
134134
request.POST or None,
135-
initial={"question": flashcard.question, "answer": flashcard.answer, "card_type": flashcard.card_type},
135+
initial={
136+
"question": flashcard.question,
137+
"answer": flashcard.answer,
138+
"card_type": flashcard.card_type,
139+
},
136140
)
137141
if request.POST and form.is_valid():
138142
flashcard.question = form.cleaned_data["question"]

0 commit comments

Comments
 (0)