| 5 | |
| 6 | |
| 7 | def build_tool(config) -> Tool: |
| 8 | tool = Tool( |
| 9 | "Image Explainer", |
| 10 | "Tool that adds the capability to explain images.", |
| 11 | name_for_model="Image Explainer", |
| 12 | description_for_model=( |
| 13 | "An Image Captioning Tool: Use this tool to generate a detailed caption " |
| 14 | "for an image. The input can be an image file of any format, and " |
| 15 | "the output will be a text description that covers every detail of the image." |
| 16 | ), |
| 17 | logo_url="https://scenex.jina.ai/SceneX%20-%20Light.svg", |
| 18 | contact_email="hello@contact.com", |
| 19 | legal_info_url="hello@legal.com" |
| 20 | ) |
| 21 | |
| 22 | scenex_api_key = config["subscription_key"] |
| 23 | scenex_api_url: str = ( |
| 24 | "https://us-central1-causal-diffusion.cloudfunctions.net/describe" |
| 25 | ) |
| 26 | |
| 27 | |
| 28 | @tool.get("/describe_image") |
| 29 | def describe_image(image : str): |
| 30 | '''Get the text description of an image. |
| 31 | ''' |
| 32 | headers = { |
| 33 | "x-api-key": f"token {scenex_api_key}", |
| 34 | "content-type": "application/json", |
| 35 | } |
| 36 | payload = { |
| 37 | "data": [ |
| 38 | { |
| 39 | "image": image, |
| 40 | "algorithm": "Ember", |
| 41 | "languages": ["en"], |
| 42 | } |
| 43 | ] |
| 44 | } |
| 45 | response = requests.post(scenex_api_url, headers=headers, json=payload) |
| 46 | response.raise_for_status() |
| 47 | result = response.json().get("result", []) |
| 48 | img = result[0] if result else {} |
| 49 | description = img.get("text", "") |
| 50 | if not description: |
| 51 | return "No description found." |
| 52 | |
| 53 | return description |
| 54 | |
| 55 | return tool |