File size: 1,696 Bytes
e67043b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import requests
import json
from ..tool import Tool
import os


def build_tool(config) -> Tool:
    tool = Tool(
        "Image Explainer",
        "Tool that adds the capability to explain images.",
        name_for_model="Image Explainer",
        description_for_model=(
            "An Image Captioning Tool: Use this tool to generate a detailed caption "
            "for an image. The input can be an image file of any format, and "
            "the output will be a text description that covers every detail of the image."
        ),
        logo_url="https://scenex.jina.ai/SceneX%20-%20Light.svg",
        contact_email="[email protected]",
        legal_info_url="[email protected]",
    )

    scenex_api_key = config["subscription_key"]
    scenex_api_url: str = (
        "https://us-central1-causal-diffusion.cloudfunctions.net/describe"
    )

    @tool.get("/describe_image")
    def describe_image(image: str):
        """Get the text description of an image."""
        headers = {
            "x-api-key": f"token {scenex_api_key}",
            "content-type": "application/json",
        }
        payload = {
            "data": [
                {
                    "image": image,
                    "algorithm": "Ember",
                    "languages": ["en"],
                }
            ]
        }
        response = requests.post(scenex_api_url, headers=headers, json=payload)
        response.raise_for_status()
        result = response.json().get("result", [])
        img = result[0] if result else {}
        description = img.get("text", "")
        if not description:
            return "No description found."

        return description

    return tool