MCPcopy Create free account
hub / github.com/ScrapeGraphAI/Scrapegraph-ai / ImageToTextNode

Class ImageToTextNode

scrapegraphai/nodes/image_to_text_node.py:12–88  ·  view source on GitHub ↗

Retrieve images from a list of URLs and return a description of the images using an image-to-text model. Attributes: llm_model: An instance of the language model client used for image-to-text conversion. verbose (bool): A flag indicating whether to show print statements

Source from the content-addressed store, hash-verified

10
11
12class ImageToTextNode(BaseNode):
13 """
14 Retrieve images from a list of URLs and return a description of
15 the images using an image-to-text model.
16
17 Attributes:
18 llm_model: An instance of the language model client used for image-to-text conversion.
19 verbose (bool): A flag indicating whether to show print statements during execution.
20
21 Args:
22 input (str): Boolean expression defining the input keys needed from the state.
23 output (List[str]): List of output keys to be updated in the state.
24 node_config (dict): Additional configuration for the node.
25 node_name (str): The unique identifier name for the node, defaulting to "ImageToText".
26 """
27
28 def __init__(
29 self,
30 input: str,
31 output: List[str],
32 node_config: Optional[dict] = None,
33 node_name: str = "ImageToText",
34 ):
35 super().__init__(node_name, "node", input, output, 1, node_config)
36
37 self.llm_model = node_config["llm_model"]
38 self.verbose = (
39 False if node_config is None else node_config.get("verbose", False)
40 )
41 self.max_images = 5 if node_config is None else node_config.get("max_images", 5)
42
43 def execute(self, state: dict) -> dict:
44 """
45 Generate text from an image using an image-to-text model. The method retrieves the image
46 from the list of URLs provided in the state and returns the extracted text.
47
48 Args:
49 state (dict): The current state of the graph. The input keys will be used to fetch the
50 correct data types from the state.
51
52 Returns:
53 dict: The updated state with the input key containing the text extracted from the image.
54 """
55
56 self.logger.info(f"--- Executing {self.node_name} Node ---")
57
58 input_keys = self.get_input_keys(state)
59 input_data = [state[key] for key in input_keys]
60 urls = input_data[0]
61
62 if isinstance(urls, str):
63 urls = [urls]
64 elif len(urls) == 0:
65 return state.update({self.output[0]: []})
66
67 if self.max_images < 1:
68 return state.update({self.output[0]: []})
69

Callers 1

_create_graphMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected