-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethodVisionOSL.py
More file actions
87 lines (75 loc) · 3.04 KB
/
Copy pathmethodVisionOSL.py
File metadata and controls
87 lines (75 loc) · 3.04 KB
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
81
82
83
84
85
86
87
import os
import base64
import requests
import csv
import glob
import pandas as pd
from prompts import OPENAI_API_KEY, SYSTEM_PROMPT, get_user_prompt, USER_PROMPT_EXAMPLE
from Other.text_extract import extract_after_backslash
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
def read_example_csv(csv_path):
with open(csv_path, newline="") as csvfile:
reader = csv.reader(csvfile)
return [row for row in reader]
def method_vision_osl(
output_folder, example_image_path, example_csv_path, transcript_name
):
example_csv_data = read_example_csv(example_csv_path)
example_csv_text = "\n".join([", ".join(row) for row in example_csv_data])
image_files = glob.glob(os.path.join(output_folder, "*.png"))
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENAI_API_KEY}",
}
for image_path in image_files:
payload = {
"model": "gpt-4o-2024-05-13",
"messages": [
SYSTEM_PROMPT,
{
"role": "user",
"content": USER_PROMPT_EXAMPLE.format(
example_csv_text=example_csv_text
),
},
{
"role": "user",
"content": "This is an example of an image:",
"name": "example_image",
"type": "image",
"image_url": "https://transcriptmiha.s3.us-east-2.amazonaws.com/input_ex.png",
},
{
"role": "user",
"content": "Below is the image you must process:",
"name": "process_image",
"type": "image",
"image_url": f"https://transcriptmiha.s3.us-east-2.amazonaws.com/{image_path}",
},
],
"max_tokens": 400,
}
response = requests.post(
"https://api.openai.com/v1/chat/completions", headers=headers, json=payload
)
try:
response_data = response.json()
except ValueError:
print(f"Error: Unable to decode JSON response for image {image_path}")
print(response.text)
continue
if "choices" not in response_data:
print(f"Error: 'choices' not in response for image {image_path}")
print(response_data)
continue
csv_content = response_data["choices"][0]["message"]["content"]
csv_lines = csv_content.split("\n")
csv_data = [line.split(", ") for line in csv_lines if line.strip()]
csv_file_name = os.path.splitext(os.path.basename(image_path))[0] + ".csv"
csv_file_path = os.path.join(output_folder, csv_file_name)
with open(csv_file_path, "w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerows(csv_data)
print(f"CSV file '{csv_file_path}' has been created and populated.")