Skip to content

Commit

Permalink
optimized memory usage for kairyou
Browse files Browse the repository at this point in the history
  • Loading branch information
Bikatr7 committed Oct 24, 2024
1 parent 0f704c1 commit ac047c5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fastapi==0.110.3
kairyou==1.6.5
kairyou==1.6.6
uvicorn==0.30.0
easytl==0.4.9
aiofiles==23.2.1
Expand Down
14 changes: 11 additions & 3 deletions backend/routes/kairyou.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from auth.util import check_internal_request

from util import get_backend_url
from util import get_backend_url, KairyouCache

from sqlalchemy.orm import Session
from sqlalchemy import update
Expand Down Expand Up @@ -60,8 +60,16 @@ async def kairyou(request_data:KairyouRequest, request:Request, db: Session = De
try:
replacements_json = await asyncio.to_thread(json.loads, replacements_json)

preprocessed_text, preprocessing_log, error_log = await asyncio.to_thread(Kairyou.preprocess, text_to_preprocess, replacements_json)
should_save_memory = KairyouCache.should_save_memory()

preprocessed_text, preprocessing_log, error_log = await asyncio.to_thread(
Kairyou.preprocess,
text_to_preprocess,
replacements_json,
should_save_memory
)

KairyouCache.update_last_used()

return JSONResponse(
status_code=status.HTTP_200_OK,
Expand All @@ -76,7 +84,7 @@ async def kairyou(request_data:KairyouRequest, request:Request, db: Session = De
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={
"message": "You have an invalid replacement json file. Please see https://github.com/Bikatr7/Kairyou?tab=readme-ov-file#kairyou for usage."
"message": "You have an invalid replacement json file. Please see https://github.com/Bikatr7/Kairyou?tab=readme-ov-file#kairyou for what the replacement json file should look like."
}
)

Expand Down
16 changes: 16 additions & 0 deletions backend/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## license that can be found in the LICENSE file.

from constants import ENVIRONMENT
from datetime import datetime, timedelta

async def get_backend_url() -> str:

Expand All @@ -25,3 +26,18 @@ async def get_frontend_url() -> str:
return "http://localhost:5173"

return "https://kakusui.org"

class KairyouCache:
_last_used: datetime | None = None

@classmethod
def update_last_used(cls):
cls._last_used = datetime.now()

@classmethod
def should_save_memory(cls) -> bool:
if cls._last_used is None:
return True

time_since_last_use = datetime.now() - cls._last_used
return time_since_last_use > timedelta(seconds=30)

0 comments on commit ac047c5

Please sign in to comment.