SUAD_Park / src /openai_predictions.py
leo-bourrel's picture
feat: add GCS
8630bef
import os
import base64
from tqdm import tqdm
from openai_agent import Agent
agent = Agent("./prompts")
# Function to encode the image
def encode_image_from_path(image_path: str) -> str:
if not os.path.exists(image_path):
raise FileNotFoundError(f"Image not found at {image_path}")
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
def encode_image(photo_binary: bytes) -> str:
return base64.b64encode(photo_binary).decode("utf-8")
def process_agent_predictions(file_path: str, photo_binary: bytes):
base64_image = encode_image(photo_binary)
prompts = [
"built_elements",
"fauna_identification",
"human_activity",
"human_detection",
"vegetation_detection",
"water_elements",
]
responses = {}
for prompt in tqdm(prompts):
detailed_prompt = agent.read_prompt(prompt)
response = agent.image_request(
base64_image, detailed_prompt, max_tokens=300, force_json=True
)
responses[prompt] = response.as_json()
return responses