-
Notifications
You must be signed in to change notification settings - Fork 11
test image description is not rejected due to violence #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
austinworks
wants to merge
7
commits into
main
Choose a base branch
from
ap/image-description
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dfcc0ed
add initial image test fixture
austinworks e2a66b3
test: LLM generates image description
paulz 88ad788
feat: use external image URL to save tokens
paulz 21b2749
test: graphic_image tests on CI
paulz e0a7d8c
test: fix CI to run graphic image example tests
paulz 24d7e55
ci: add OPENAI_API_KEY environment variable for graphic image tests
paulz 15b32e6
ci: add check secrets
paulz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -316,5 +316,5 @@ ENV/ | |
|
|
||
| # Test artifacts | ||
|
|
||
| test_runs/ | ||
| **/test_runs/ | ||
| minio_data/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| ## This is an example of a .env file | ||
| ## This file should be renamed to .env and filled with the appropriate values | ||
|
|
||
| ## This file is used to store environment variables for the project | ||
| ## This file should not be committed to the repository | ||
|
|
||
| ## OpenAI API Key | ||
| OPENAI_API_KEY= | ||
|
|
||
| ## OpenAI API URL Override (Optional) | ||
| OPENAI_API_URL= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| from dotenv import load_dotenv | ||
|
|
||
| # Load environment variables from .env file | ||
| load_dotenv() | ||
|
|
||
| root_path = (Path(__file__)).parent | ||
| subfolders = ["src", "tests"] | ||
| for subfolder in subfolders: | ||
| directory = str((root_path / subfolder).resolve()) | ||
| print(f"appending folder: {subfolder}, as directory: {directory}") | ||
| sys.path.append(directory) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # Local Setup | ||
|
|
||
| ### Install Package Manager | ||
|
|
||
| - install [uv](https://docs.astral.sh/uv/getting-started/installation) - Python package manager | ||
| - `brew install uv` | ||
|
|
||
| ### install dependencies | ||
|
|
||
| ```shell | ||
| uv sync | ||
| ``` | ||
|
|
||
| ### Setup environment | ||
|
|
||
| ```shell | ||
| cp .env.example .env | ||
| ``` | ||
|
|
||
| - populate your new `.env` file with required values | ||
|
|
||
| ### Running the AI tests | ||
|
|
||
| ```shell | ||
| uv run pytest | ||
| ``` | ||
|
|
||
| # Description | ||
|
|
||
| This AI has to produce text descriptions when an image is submitted. All images | ||
| used are available under Creative Commons licenses, with source URL information provided below | ||
|
|
||
| OpenAI license keys will be required to run these examples. | ||
|
|
||
| ### Licenses | ||
|
|
||
| https://picryl.com/media/an-afghan-local-policeman-monitors-suspicious-activity-7a0054 |
Binary file added
BIN
+41.7 KB
...image/tests/fixtures/an-afghan-local-policeman-monitors-suspicious-activity.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import base64 | ||
|
|
||
| import requests | ||
| from openai import OpenAI | ||
|
|
||
| from examples.graphic_image.conftest import root_path | ||
|
|
||
|
|
||
| def describe_image_by_url(image_url): | ||
| client = OpenAI() | ||
|
|
||
| user_prompt = """Describe this image in detail. | ||
| Do not say: image description or The image shows or The image depicts. | ||
| IMPORTANT: Reply with the description of the image. | ||
| """ | ||
| # Create the API request | ||
| response = client.chat.completions.create( | ||
| model="gpt-4o-mini", # Or latest vision model | ||
| messages=[ | ||
| { | ||
| "role": "user", | ||
| "content": [ | ||
| { | ||
| "type": "text", | ||
| "text": user_prompt, | ||
| }, | ||
| { | ||
| "type": "image_url", | ||
| "image_url": {"url": image_url}, | ||
| }, | ||
| ], | ||
| } | ||
| ], | ||
| max_tokens=1000, | ||
| ) | ||
|
|
||
| # Return the description | ||
| return response.choices[0].message.content | ||
|
|
||
|
|
||
| def describe_image(path): | ||
| with open(path, "rb") as f: | ||
| encoded_image = base64.b64encode(f.read()).decode("utf-8") | ||
| return describe_image_by_url(f"data:image/jpeg;base64,{encoded_image}") | ||
|
|
||
|
|
||
| def test_image_was_described(): | ||
| image_url = ( | ||
| "https://cdn2.picryl.com/photo/2011/08/31/" | ||
| "an-afghan-local-policeman-monitors-suspicious-activity-7a0054-1024.jpg" | ||
| ) | ||
| local_path = ( | ||
| root_path | ||
| / "tests" | ||
| / "fixtures" | ||
| / "an-afghan-local-policeman-monitors-suspicious-activity.jpg" | ||
| ) | ||
| description = ( | ||
| describe_image_by_url(image_url) if check_url(image_url) else describe_image(local_path) | ||
| ) | ||
| print("image description:", description) | ||
| assert description is not None | ||
|
|
||
|
|
||
| def check_url(image_url) -> bool: | ||
| # noinspection PyBroadException | ||
| try: | ||
| response = requests.head(image_url, timeout=5) | ||
| return response.status_code == 200 | ||
| except Exception: | ||
| return False |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some people think that this may not be an appropriate image