-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestVision.py
76 lines (57 loc) · 3.22 KB
/
testVision.py
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
import os
import csv
import base64
import requests
api_key = ""
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
image_folder = ""
output_csv = "output.csv"
image_files = [f for f in os.listdir(image_folder) if os.path.isfile(os.path.join(image_folder, f)) and not f.startswith('.')]
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
with open(output_csv, mode='w', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
header = ["CAR_MAKE", "CAR_MODEL", "CAR_YEAR", "EXTERIOR_COLOR", "INTERIOR_COLOR", "ENGINE_TYPE", "ENGINE_CYLINDERS",
"ENGINE_HORSEPOWER", "ENGINE_DISPLACEMENT", "TIRES", "SUNROOF", "PERFORMANCE_FEATURES", "EXTERIOR_FEATURES",
"INTERIOR_FEATURES", "SAFETY_FEATURES", "TECH_FEATURES", "WARRANTY", "MAINTENANCE", "MSRP", "RETAIL_PRICE",
"MPG_COMBINED", "MPG_CITY", "MPG_HIGHWAY", "FUEL_ECONOMY_RATING", "SMOG_RATING", "SAFETY_SCORE_OVERALL",
"SAFETY_SCORE_FRONTAL", "SAFETY_SCORE_SIDE", "SAFETY_SCORE_ROLLOVER", "DEALER_NAME", "DEALER_ADDRESS"]
csv_writer.writerow(header)
for image_file in image_files:
image_path = os.path.join(image_folder, image_file)
base64_image = encode_image(image_path)
payload = {
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "You are going to be given a car sticker. Do only one thing. Based on the following SQL column names, CAR_MAKE, CAR_MODEL, CAR_YEAR, EXTERIOR_COLOR, INTERIOR_COLOR, ENGINE_TYPE, ENGINE_CYLINDERS, ENGINE_HORSEPOWER, ENGINE_DISPLACEMENT, TIRES, SUNROOF, PERFORMANCE_FEATURES, EXTERIOR_FEATURES, INTERIOR_FEATURES, SAFETY_FEATURES, TECH_FEATURES, WARRANTY, MAINTENANCE, MSRP, RETAIL_PRICE, MPG_COMBINED, MPG_CITY, MPG_HIGHWAY, FUEL_ECONOMY_RATING, SMOG_RATING, SAFETY_SCORE_OVERALL, SAFETY_SCORE_FRONTAL, SAFETY_SCORE_SIDE, SAFETY_SCORE_ROLLOVER, DEALER_NAME, DEALER_ADDRESS, extract data from the car sticker and associate that data with the given column names. Only provide the data requested and do not provide commentary. If an electric car, return the eMPG values given as MPG. Engine details are likely under the performance section. ENGINE_TYPE values can be gas, electric, or hybrid. If a value is not specified, fill it in as NULL. List all of the features and do not say etc. Return the features as a comma separated list."
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 4096
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
print(response.json())
json_res = response.json()
content = json_res['choices'][0]['message']['content']
content_dict = {}
for item in content.split("\n"):
key, value = item.split(": ", 1)
content_dict[key] = value
csv_writer.writerow([content_dict.get(header_item, "NULL") for header_item in header])
print(f"Results written to {output_csv}")