Skip to content

Commit 350d5c1

Browse files
committed
add analyze image command
1 parent 53631f8 commit 350d5c1

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

scripts/azure_openai_operator.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from base64 import b64encode
23

34
import typer
45
from dotenv import load_dotenv
@@ -16,6 +17,11 @@
1617
logger = get_logger(__name__)
1718

1819

20+
def load_image_to_base64(image_path: str) -> str:
21+
with open(image_path, "rb") as image_file:
22+
return b64encode(image_file.read()).decode("utf-8")
23+
24+
1925
@app.command()
2026
def chat(
2127
query: str = typer.Option(
@@ -80,6 +86,63 @@ def reasoning(
8086
logger.info(f"Output: {response.content}")
8187

8288

89+
@app.command()
90+
def image(
91+
query: str = typer.Option(
92+
"Please analyze the following image and answer the question",
93+
"--query",
94+
"-q",
95+
help="Query to run with the Azure OpenAI chat model",
96+
),
97+
file_path: str = typer.Option(
98+
"./docs/images/streamlit.png",
99+
"--file",
100+
"-f",
101+
help="Path to the image file to analyze",
102+
),
103+
verbose: bool = typer.Option(
104+
False,
105+
"--verbose",
106+
"-v",
107+
help="Enable verbose output",
108+
),
109+
):
110+
# Set up logging
111+
if verbose:
112+
logger.setLevel(logging.DEBUG)
113+
114+
base64_image = load_image_to_base64(file_path)
115+
messages = {
116+
"role": "user",
117+
"content": [
118+
{
119+
"type": "text",
120+
"text": query,
121+
},
122+
{
123+
"type": "image",
124+
"source_type": "base64",
125+
"data": base64_image,
126+
"mime_type": "image/png",
127+
},
128+
],
129+
}
130+
131+
logger.info("Running...")
132+
response = AzureOpenAiWrapper().chat_model.invoke(
133+
input=[
134+
messages,
135+
],
136+
)
137+
logger.debug(
138+
response.model_dump_json(
139+
indent=2,
140+
exclude_none=True,
141+
)
142+
)
143+
logger.info(f"Output: {response.content}")
144+
145+
83146
if __name__ == "__main__":
84147
load_dotenv(
85148
override=True,

0 commit comments

Comments
 (0)