Spaces:
Runtime error
Runtime error
File size: 2,177 Bytes
411ca77 |
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 |
# input : data_path
import os
from pdfparser_hq import encode_image
from config import openai_api
from openai import OpenAI
def transcribe_all(data_path,
name_of_raw_transcripts = "transcript_raw.txt"):
imgs = []
client = OpenAI(api_key=openai_api)
transcripts = []
system_prompt = """
You will be given a reciept that could be handwritten or properly formated. Your goal is to transcribe what is written in JSON following this format:
{
"name_of_supplier" : X,
"amount" : X,
"currency": X,
"date" : DD/MM/YYYY
}
Make sure you provide the total amount and the correct dates, handwritten ones might be tricky. This will be used to reconcile with banking transactions.
"""
for root, dirs, files in os.walk(data_path):
for file in files:
if file.endswith('.png'):
print(os.path.join(root, file))
imgs.append({"path": os.path.join(root, file)})
pass
for i, img in enumerate(imgs):
filename = img["path"]
base64_image = encode_image(img["path"])
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "system",
"content": [
{
"type": "text",
"text": system_prompt
}
]
},
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}",
}
}
]
}
],
temperature=1,
max_tokens=1877,
top_p=1,
response_format={"type": "json_object"},
frequency_penalty=0,
presence_penalty=0
).choices[0].message.content
transcripts.append({"path": filename,"filename" : f"P{i+1}.png", "content": response})
print(f"done transcribing transcript: {i+1}/{len(imgs)}")
with open(f"{data_path}/{name_of_raw_transcripts}", 'w') as file:
file.write(str(transcripts)) |