language-learning-app/api/app/tasks/create_summary_article.py

47 lines
1.7 KiB
Python
Raw Permalink Normal View History

import logging
import uuid
from sqlalchemy.ext.asyncio import AsyncSession
from app.outbound.postgres.repositories.article_repository import (
PostgresArticleRepository,
)
from ..config import settings
from ..domain.services.summarise_service import SummariseService
from ..outbound.anthropic.anthropic_client import AnthropicClient
from ..outbound.deepgram.deepgram_client import LocalDeepgramClient
from ..outbound.deepl.deepl_client import DeepLClient
from ..outbound.gemini.gemini_client import GeminiClient
from ..outbound.postgres.database import AsyncSessionLocal
from ..outbound.spacy.spacy_client import SpacyClient
from .app import procrastinate_app
logger = logging.getLogger(__name__)
def _make_summarise_service(db: AsyncSession) -> SummariseService:
return SummariseService(
anthropic_client=AnthropicClient.new(settings.anthropic_api_key),
deepgram_client=LocalDeepgramClient(settings.deepgram_api_key),
deepl_client=DeepLClient(settings.deepl_api_key),
gemini_client=GeminiClient(settings.gemini_api_key),
spacy_client=SpacyClient(),
article_repository=PostgresArticleRepository(db),
)
@procrastinate_app.task(queue="default")
async def create_summary_article(
article_id: str, target_language: str, complexity_level: str, input_text: str
) -> None:
print(f"Starting summarisation task for article_id={article_id}")
async with AsyncSessionLocal() as db:
print("Session opened, calling summarise service...")
await _make_summarise_service(db).summarise_article(
article_id=uuid.UUID(article_id),
target_language=target_language,
complexity_level=complexity_level,
input_text=input_text,
)