from typing import Dict, Any, Optional, List from sqlalchemy import select, text from datetime import datetime import logging logger = logging.getLogger(__name__) class TradeCorpus: def __init__(self): self._ready = False async def record( self, source_text: str, target_text: str, task_type: str, provider: str, source_lang: Optional[str] = None, target_lang: Optional[str] = None, quality_score: float = 0.5, user_edited: bool = False, metadata: Optional[Dict] = None, ): try: from app.database import AsyncSessionLocal from app.models.corpus import CorpusEntry async with AsyncSessionLocal() as session: entry = CorpusEntry( source_text=source_text[:2000], target_text=target_text[:2000], source_lang=source_lang, target_lang=target_lang, task_type=task_type, provider_used=provider, quality_score=quality_score, user_edited=user_edited, metadata=metadata or {}, ) session.add(entry) await session.commit() except Exception as e: logger.warning(f"Failed to record corpus entry: {e}") async def find_similar(self, text: str, task_type: str, top_k: int = 3) -> List[Dict[str, Any]]: try: from app.database import AsyncSessionLocal from app.models.corpus import CorpusEntry async with AsyncSessionLocal() as session: result = await session.execute( select(CorpusEntry) .where(CorpusEntry.task_type == task_type) .where(CorpusEntry.quality_score >= 0.6) .order_by(CorpusEntry.quality_score.desc()) .limit(top_k) ) entries = result.scalars().all() return [ { "source": e.source_text, "target": e.target_text, "score": e.quality_score, } for e in entries ] except Exception as e: logger.warning(f"Corpus search failed: {e}") return [] async def rate_entry(self, entry_id: str, rating: int): try: from app.database import AsyncSessionLocal from app.models.corpus import CorpusEntry async with AsyncSessionLocal() as session: result = await session.execute( select(CorpusEntry).where(CorpusEntry.id == entry_id) ) entry = result.scalar_one_or_none() if entry: entry.user_rating = rating entry.quality_score = rating / 5.0 await session.commit() except Exception as e: logger.warning(f"Failed to rate corpus entry: {e}")