Spaces:
Running
Running
Istvan-Adem
commited on
Commit
·
0e48a80
1
Parent(s):
2cace27
fix
Browse files- ocr/api/message/openai_request.py +14 -25
- ocr/api/message/utils.py +33 -11
- ocr/api/message/views.py +10 -3
ocr/api/message/openai_request.py
CHANGED
@@ -1,28 +1,17 @@
|
|
1 |
-
import asyncio
|
2 |
-
import io
|
3 |
-
|
4 |
-
from starlette.datastructures import UploadFile
|
5 |
-
|
6 |
from ocr.api.message.prompts import OCRPrompts
|
7 |
-
from ocr.
|
8 |
-
from ocr.core.config import settings
|
9 |
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
)
|
25 |
-
run = await settings.OPENAI_CLIENT.beta.threads.runs.create_and_poll(
|
26 |
-
assistant_id=settings.ASSISTANT_ID, thread_id=thread.id, instructions=OCRPrompts.generate_general_answer
|
27 |
-
)
|
28 |
-
return await clean_assistant_response(thread.id, run.id)
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from ocr.api.message.prompts import OCRPrompts
|
2 |
+
from ocr.core.wrappers import openai_wrapper
|
|
|
3 |
|
4 |
|
5 |
+
@openai_wrapper(model='gpt-4o-mini')
|
6 |
+
async def generate_report(request_content: list[dict]):
|
7 |
+
messages = [
|
8 |
+
{
|
9 |
+
"role": "system",
|
10 |
+
"content": OCRPrompts.generate_general_answer
|
11 |
+
},
|
12 |
+
{
|
13 |
+
"role": "user",
|
14 |
+
"content": request_content
|
15 |
+
}
|
16 |
+
]
|
17 |
+
return messages
|
|
|
|
|
|
|
|
|
|
ocr/api/message/utils.py
CHANGED
@@ -1,18 +1,40 @@
|
|
|
|
|
|
1 |
import re
|
2 |
|
3 |
-
from
|
4 |
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
try:
|
15 |
-
|
16 |
except Exception as e:
|
17 |
pass
|
18 |
-
return
|
|
|
1 |
+
import base64
|
2 |
+
import io
|
3 |
import re
|
4 |
|
5 |
+
from pdf2image import convert_from_bytes
|
6 |
|
7 |
|
8 |
+
def divide_images(contents: bytes) -> list[bytes]:
|
9 |
+
images = convert_from_bytes(contents, dpi=250)
|
10 |
+
image_bytes_list = []
|
11 |
+
for image in images:
|
12 |
+
img_byte_array = io.BytesIO()
|
13 |
+
image.save(img_byte_array, format='PNG')
|
14 |
+
img_byte_array.seek(0)
|
15 |
+
image_bytes_list.append(img_byte_array.read())
|
16 |
+
return image_bytes_list
|
17 |
+
|
18 |
+
|
19 |
+
def prepare_request_content(images: list[bytes]):
|
20 |
+
content = [
|
21 |
+
{"type": "text", "text": "Generate a report on the attached document"},
|
22 |
+
*[
|
23 |
+
{
|
24 |
+
"type": "image_url",
|
25 |
+
"image_url": {
|
26 |
+
"url": f"data:image/jpeg;base64,{base64.b64encode(image).decode('utf-8')}",
|
27 |
+
},
|
28 |
+
}
|
29 |
+
for image in images
|
30 |
+
]
|
31 |
+
]
|
32 |
+
return content
|
33 |
+
|
34 |
+
|
35 |
+
def clean_response(text: str) -> str:
|
36 |
try:
|
37 |
+
text = re.search(r'```markdown\s*(.*?)\s*```', text, re.DOTALL).group(1)
|
38 |
except Exception as e:
|
39 |
pass
|
40 |
+
return text
|
ocr/api/message/views.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
from fastapi import File, UploadFile
|
2 |
|
3 |
from ocr.api.message import ocr_router
|
4 |
-
from ocr.api.message.openai_request import
|
5 |
from ocr.api.message.schemas import OcrResponse
|
|
|
6 |
from ocr.core.wrappers import OcrResponseWrapper
|
7 |
|
8 |
|
@@ -10,5 +11,11 @@ from ocr.core.wrappers import OcrResponseWrapper
|
|
10 |
async def get_all_chat_messages(
|
11 |
file: UploadFile = File(...)
|
12 |
) -> OcrResponseWrapper[OcrResponse]:
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import File, UploadFile
|
2 |
|
3 |
from ocr.api.message import ocr_router
|
4 |
+
from ocr.api.message.openai_request import generate_report
|
5 |
from ocr.api.message.schemas import OcrResponse
|
6 |
+
from ocr.api.message.utils import divide_images, prepare_request_content, clean_response
|
7 |
from ocr.core.wrappers import OcrResponseWrapper
|
8 |
|
9 |
|
|
|
11 |
async def get_all_chat_messages(
|
12 |
file: UploadFile = File(...)
|
13 |
) -> OcrResponseWrapper[OcrResponse]:
|
14 |
+
try:
|
15 |
+
contents = await file.read()
|
16 |
+
images = divide_images(contents)
|
17 |
+
request_content = prepare_request_content(images)
|
18 |
+
response = await generate_report(request_content)
|
19 |
+
return OcrResponseWrapper(data=OcrResponse(text=clean_response(response)))
|
20 |
+
finally:
|
21 |
+
await file.close()
|