-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgToICS_CLI.py
86 lines (76 loc) · 2.76 KB
/
imgToICS_CLI.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
77
78
79
80
81
82
83
84
85
86
import os
import sys
import base64
import requests
from dotenv import load_dotenv
args = sys.argv
if len(args) != 3:
print("Usage: imgToICS_CLI.py input_file output_location")
sys.exit(1)
input_file = args[1]
output_dir = args[2]
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
verbose = False # change to true if you want to print the output of the .ics file
image_path = input_file
base64_image = encode_image(image_path)
promptString = 'Review the image. Take in all information related to the event and Output ONLY the raw text of a .ICS file about the calendar event. Make sure to include Created-By-Ryan-Majd in the PRODID. Also, the year is 2024.'
load_dotenv()
KEY = ''
KEY = os.getenv('OPENAI_API_KEY')
if not image_path:
raise ValueError("No image path specified.")
if not os.path.exists(image_path):
raise FileNotFoundError("The specified image path does not exist.")
if not KEY:
raise ValueError(
"API key and/or .env file is missing. Make sure to provide a valid API key in this format: OPENAI_API_KEY = 'APIKEYHERE'")
elif KEY == '':
print("API key is empty. Make sure to provide a valid API key in a .env file.")
if not output_dir:
raise RuntimeError("Output directory is not specified.")
if not os.access(output_dir, os.W_OK):
raise PermissionError("Output exit_directory is not writable.")
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {KEY}"
}
# Load the environment variables
payload = {
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": promptString
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 300
}
response = requests.post(
"https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
ics_content = response.json()['choices'][0]['message']['content']
begin_index = ics_content.find("BEGIN:VCALENDAR")
end_index = ics_content.find("END:VCALENDAR") + len("END:VCALENDAR")
# Print the stripped content
stripped_ics_content = ics_content[begin_index:end_index]
begin_index = ics_content.find("SUMMARY:") + len("SUMMARY:")
end_index = ics_content.find("\n", begin_index)
eventTitle = ics_content[begin_index:end_index].strip()
if verbose:
print(stripped_ics_content)
file_path = os.path.join(output_dir, f'{eventTitle}.ics')
with open(file_path, 'w') as file:
file.write(stripped_ics_content)
print(f"✅ Response saved to {file_path}")