|
1 | 1 | import logging
|
| 2 | +from base64 import b64encode |
2 | 3 |
|
3 | 4 | import typer
|
4 | 5 | from dotenv import load_dotenv
|
|
16 | 17 | logger = get_logger(__name__)
|
17 | 18 |
|
18 | 19 |
|
| 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 | + |
19 | 25 | @app.command()
|
20 | 26 | def chat(
|
21 | 27 | query: str = typer.Option(
|
@@ -80,6 +86,63 @@ def reasoning(
|
80 | 86 | logger.info(f"Output: {response.content}")
|
81 | 87 |
|
82 | 88 |
|
| 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 | + |
83 | 146 | if __name__ == "__main__":
|
84 | 147 | load_dotenv(
|
85 | 148 | override=True,
|
|
0 commit comments