File size: 2,912 Bytes
7ae78bd
 
ed5ec6a
 
127f2f2
7ae78bd
ed5ec6a
3f47633
 
 
 
127f2f2
ed5ec6a
 
 
127f2f2
ed5ec6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127f2f2
 
 
 
 
 
 
 
 
e183e6b
127f2f2
 
 
 
 
 
 
 
ed5ec6a
 
 
 
7ae78bd
 
 
 
3f47633
7ae78bd
 
3f47633
 
7ae78bd
 
3f47633
 
7ae78bd
 
ed5ec6a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import asyncio

from fastapi import UploadFile, File

from ocr.api.message.schemas import CreateMessageRequest
from ocr.api.openai_requests import generate_report, generate_changes
from ocr.api.report import report_router
from ocr.api.report.db_requests import (get_all_reports_obj,
                                        delete_all_reports,
                                        get_report_obj_by_id,
                                        save_report_obj,
                                        get_last_report_obj, change_report_report_obj, change_changes_report_obj)
from ocr.api.report.dto import Paging
from ocr.api.report.model import ReportModel
from ocr.api.report.schemas import AllReportResponse
from ocr.api.utils import divide_images, clean_response, extract_text_from_images
from ocr.core.wrappers import OcrResponseWrapper


@report_router.get('/all')
async def get_all_reports() -> OcrResponseWrapper[AllReportResponse]:
    reports = await get_all_reports_obj()
    response = AllReportResponse(
        paging=Paging(pageSize=len(reports), pageIndex=0, totalCount=len(reports)),
        data=reports
    )
    return OcrResponseWrapper(data=response)


@report_router.delete('/all')
async def delete_all_report() -> OcrResponseWrapper:
    await delete_all_reports()
    return OcrResponseWrapper()


@report_router.get('/{reportId}')
async def get_report(reportId: str) -> OcrResponseWrapper[ReportModel]:
    report = await get_report_obj_by_id(reportId)
    return OcrResponseWrapper(data=report)


@report_router.patch('/{reportId}/report')
async def change_report_report(
        reportId: str,
        data: CreateMessageRequest
) -> OcrResponseWrapper[ReportModel]:
    report = await change_report_report_obj(reportId, data)
    return OcrResponseWrapper(data=report)


@report_router.patch('/{reportId}/changes')
async def change_report_changes(
        reportId: str,
        data: CreateMessageRequest
) -> OcrResponseWrapper[ReportModel]:
    report = await change_changes_report_obj(reportId, data)
    return OcrResponseWrapper(data=report)


@report_router.post('')
async def create_report(
        file: UploadFile = File(...),
) -> OcrResponseWrapper[ReportModel]:
    try:
        last_report, contents = await asyncio.gather(get_last_report_obj(), file.read())
        report, changes = None, None
        images = divide_images(contents)
        text_content = extract_text_from_images(images)
        if last_report:
            report, changes = await asyncio.gather(
                generate_report(text_content),
                generate_changes(text_content, last_report.report)
            )
        else:
            report = await generate_report(text_content)
        report = await save_report_obj(clean_response(report), clean_response(changes), text_content, file.filename)
    finally:
        await file.close()
    return OcrResponseWrapper(data=report)