|
| 1 | +import argparse |
| 2 | +import base64 |
| 3 | +from os import getenv |
| 4 | + |
| 5 | +from dotenv import load_dotenv |
| 6 | +from openai import AzureOpenAI |
| 7 | + |
| 8 | + |
| 9 | +def init_args() -> argparse.Namespace: |
| 10 | + parser = argparse.ArgumentParser( |
| 11 | + prog="ProgramName", |
| 12 | + description="What the program does", |
| 13 | + epilog="Text at the bottom of help", |
| 14 | + ) |
| 15 | + parser.add_argument("-f", "--file") |
| 16 | + parser.add_argument("-s", "--system", default="You are a professional image analyst. Describe the image.") |
| 17 | + parser.add_argument("-p", "--prompt", default="Please describe the content of the image") |
| 18 | + return parser.parse_args() |
| 19 | + |
| 20 | + |
| 21 | +if __name__ == "__main__": |
| 22 | + # Parse .env file and set environment variables |
| 23 | + load_dotenv() |
| 24 | + |
| 25 | + # Initialize AzureOpenAI client |
| 26 | + client = AzureOpenAI( |
| 27 | + api_key=getenv("AZURE_OPENAI_API_KEY"), |
| 28 | + api_version=getenv("AZURE_OPENAI_API_VERSION"), |
| 29 | + azure_endpoint=getenv("AZURE_OPENAI_ENDPOINT"), |
| 30 | + ) |
| 31 | + |
| 32 | + args = init_args() |
| 33 | + |
| 34 | + # Read image file and encode it to base64 |
| 35 | + try: |
| 36 | + with open(args.file, "rb") as f: |
| 37 | + encoded_image = base64.b64encode(f.read()).decode() |
| 38 | + except Exception as e: |
| 39 | + print(e) |
| 40 | + exit(1) |
| 41 | + |
| 42 | + # Call completion API and get a response to user input |
| 43 | + response = client.chat.completions.create( |
| 44 | + model=getenv("AZURE_OPENAI_GPT_MODEL"), |
| 45 | + messages=[ |
| 46 | + { |
| 47 | + "role": "system", |
| 48 | + "content": args.system, |
| 49 | + }, |
| 50 | + { |
| 51 | + "role": "user", |
| 52 | + "content": [ |
| 53 | + { |
| 54 | + "type": "image_url", |
| 55 | + "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}, |
| 56 | + }, |
| 57 | + { |
| 58 | + "type": "text", |
| 59 | + "content": args.prompt, |
| 60 | + }, |
| 61 | + ], |
| 62 | + }, |
| 63 | + ], |
| 64 | + ) |
| 65 | + |
| 66 | + # Print response to console |
| 67 | + # print(response.model_dump_json(indent=2)) |
| 68 | + print(response.choices[0].message.content) |
0 commit comments