Commit ·
b1cdbdb
1
Parent(s): 5067f71
Add tool visual_qa and update requirements.txt
Browse files- requirements.txt +1 -0
- tools/visual_qa.py +92 -0
requirements.txt
CHANGED
|
@@ -13,3 +13,4 @@ beautifulsoup4
|
|
| 13 |
youtube-transcript-api
|
| 14 |
pathvalidate
|
| 15 |
serpapi
|
|
|
|
|
|
| 13 |
youtube-transcript-api
|
| 14 |
pathvalidate
|
| 15 |
serpapi
|
| 16 |
+
pillow
|
tools/visual_qa.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import mimetypes
|
| 3 |
+
import os
|
| 4 |
+
import PIL.Image
|
| 5 |
+
import requests
|
| 6 |
+
import uuid
|
| 7 |
+
|
| 8 |
+
from smolagents import tool
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# Function to encode the image
|
| 12 |
+
def encode_image(image_path):
|
| 13 |
+
if image_path.startswith("http"):
|
| 14 |
+
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
|
| 15 |
+
request_kwargs = {
|
| 16 |
+
"headers": {"User-Agent": user_agent},
|
| 17 |
+
"stream": True,
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
# Send a HTTP request to the URL
|
| 21 |
+
response = requests.get(image_path, **request_kwargs)
|
| 22 |
+
response.raise_for_status()
|
| 23 |
+
content_type = response.headers.get("content-type", "")
|
| 24 |
+
|
| 25 |
+
extension = mimetypes.guess_extension(content_type)
|
| 26 |
+
if extension is None:
|
| 27 |
+
extension = ".download"
|
| 28 |
+
|
| 29 |
+
fname = str(uuid.uuid4()) + extension
|
| 30 |
+
download_path = os.path.abspath(os.path.join("downloads", fname))
|
| 31 |
+
|
| 32 |
+
with open(download_path, "wb") as fh:
|
| 33 |
+
for chunk in response.iter_content(chunk_size=512):
|
| 34 |
+
fh.write(chunk)
|
| 35 |
+
|
| 36 |
+
image_path = download_path
|
| 37 |
+
|
| 38 |
+
with open(image_path, "rb") as image_file:
|
| 39 |
+
return base64.b64encode(image_file.read()).decode("utf-8")
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def resize_image(image_path):
|
| 43 |
+
img = PIL.Image.open(image_path)
|
| 44 |
+
width, height = img.size
|
| 45 |
+
img = img.resize((int(width / 2), int(height / 2)))
|
| 46 |
+
new_image_path = f"resized_{image_path}"
|
| 47 |
+
img.save(new_image_path)
|
| 48 |
+
return new_image_path
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@tool
|
| 52 |
+
def visualizer(image_path: str, question: str | None = None) -> str:
|
| 53 |
+
"""A tool that can answer questions about attached images.
|
| 54 |
+
|
| 55 |
+
Args:
|
| 56 |
+
image_path: The path to the image on which to answer the question. This should be a local path to downloaded image.
|
| 57 |
+
question: The question to answer.
|
| 58 |
+
"""
|
| 59 |
+
add_note = False
|
| 60 |
+
if not question:
|
| 61 |
+
add_note = True
|
| 62 |
+
question = "Please write a detailed caption for this image."
|
| 63 |
+
if not isinstance(image_path, str):
|
| 64 |
+
raise Exception("You should provide at least `image_path` string argument to this tool!")
|
| 65 |
+
|
| 66 |
+
mime_type, _ = mimetypes.guess_type(image_path)
|
| 67 |
+
base64_image = encode_image(image_path)
|
| 68 |
+
|
| 69 |
+
payload = {
|
| 70 |
+
"model": "gpt-4o",
|
| 71 |
+
"messages": [
|
| 72 |
+
{
|
| 73 |
+
"role": "user",
|
| 74 |
+
"content": [
|
| 75 |
+
{"type": "text", "text": question},
|
| 76 |
+
{"type": "image_url", "image_url": {"url": f"data:{mime_type};base64,{base64_image}"}},
|
| 77 |
+
],
|
| 78 |
+
}
|
| 79 |
+
],
|
| 80 |
+
"max_tokens": 1000,
|
| 81 |
+
}
|
| 82 |
+
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {os.getenv('OPENAI_API_KEY')}"}
|
| 83 |
+
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
| 84 |
+
try:
|
| 85 |
+
output = response.json()["choices"][0]["message"]["content"]
|
| 86 |
+
except Exception:
|
| 87 |
+
raise Exception(f"Response format unexpected: {response.json()}")
|
| 88 |
+
|
| 89 |
+
if add_note:
|
| 90 |
+
output = f"You did not provide a particular question, so here is a detailed caption for the image: {output}"
|
| 91 |
+
|
| 92 |
+
return output
|