from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from app.models.palm_report import PalmReport from app.models.uploaded_image import UploadedImage from app.services.image_service import ImageService from app.services.palm_analyzer import PalmAnalyzer class ReportService: def __init__(self) -> None: self.images = ImageService() self.analyzer = PalmAnalyzer() async def generate(self, db: AsyncSession, report_id: str) -> None: result = await db.execute(select(PalmReport).where(PalmReport.id == report_id)) report = result.scalar_one() image_result = await db.execute(select(UploadedImage).where(UploadedImage.id == report.image_id)) image = image_result.scalar_one() report.status = "processing" await db.flush() try: image_bytes = self.images.read_bytes(image.storage_key) data = await self.analyzer.analyze(image_bytes, image.content_type, report.hand_side) report.report_data = data report.status = "failed" if not data["quality_check"]["can_analyze"] else "completed" report.error_message = None if report.status == "completed" else data["quality_check"]["reason"] except Exception as exc: report.status = "failed" report.error_message = str(exc) await db.commit()