Skip to content

Commit 3f56f05

Browse files
authored
Merge pull request #44 from VinciGit00/fix_image_to_text_node
Fix image to text node, it was bugged
2 parents 2a1d4ae + 3269b4a commit 3f56f05

13 files changed

+41
-78
lines changed

examples/ScrapeGraphAI_generated_graph

Lines changed: 0 additions & 19 deletions
This file was deleted.

examples/graph_builder_example.py

Lines changed: 0 additions & 31 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

examples/scrape_plain_text.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
from dotenv import load_dotenv
77
from scrapegraphai.graphs import SmartScraperGraph
8+
from scrapegraphai.utils import convert_to_csv, convert_to_json
89

910
load_dotenv()
1011
openai_key = os.getenv("OPENAI_APIKEY")
@@ -19,7 +20,7 @@
1920

2021

2122
# It could be also a http request using the request model
22-
text = open('plain_html_example.txt', 'r', encoding="utf-8")
23+
text = open('inputs/plain_html_example.txt', 'r', encoding="utf-8")
2324

2425
# Create the SmartScraperGraph instance
2526
smart_scraper_graph = SmartScraperGraph(
@@ -32,6 +33,5 @@
3233
print(result)
3334

3435
# Save to json or csv
35-
onvert_to_csv(result, "result")
36+
convert_to_csv(result, "result")
3637
convert_to_json(result, "result")
37-

scrapegraphai/graphs/smart_scraper_graph.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ def _create_llm(self, llm_config: dict):
7171
return OpenAI(llm_params)
7272
elif "gemini" in llm_params["model"]:
7373
return Gemini(llm_params)
74-
else:
75-
raise ValueError("Model not supported")
74+
raise ValueError("Model not supported")
7675

7776
def _create_graph(self):
7877
"""

scrapegraphai/models/gemini.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
"""
2+
Gemini module configuration
3+
"""
14
from langchain_google_genai import ChatGoogleGenerativeAI
25

36

scrapegraphai/nodes/base_node.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class BaseNode(ABC):
4040
raised to indicate the incorrect usage.
4141
"""
4242

43-
def __init__(self, node_name: str, node_type: str, input: str, output: List[str], min_input_len: int = 1, model_config: Optional[dict] = None):
43+
def __init__(self, node_name: str, node_type: str, input: str, output: List[str],
44+
min_input_len: int = 1, model_config: Optional[dict] = None):
4445
"""
4546
Initialize the node with a unique identifier and a specified node type.
4647
@@ -73,7 +74,9 @@ def execute(self, state: dict) -> dict:
7374
pass
7475

7576
def get_input_keys(self, state: dict) -> List[str]:
76-
# Use the _parse_input_keys method to identify which state keys are needed based on the input attribute
77+
"""Use the _parse_input_keys method to identify which state keys are
78+
needed based on the input attribute
79+
"""
7780
try:
7881
input_keys = self._parse_input_keys(state, self.input)
7982
self._validate_input_keys(input_keys)
Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
"""
1+
"""
22
Module for the ImageToTextNode class.
33
"""
4-
4+
from typing import List
55
from .base_node import BaseNode
66

77

@@ -10,34 +10,43 @@ class ImageToTextNode(BaseNode):
1010
A class representing a node that processes an image and returns the text description.
1111
1212
Attributes:
13-
llm (OpenAIImageToText): An instance of the OpenAIImageToText class.
13+
llm_model (OpenAIImageToText): An instance of the OpenAIImageToText class.
1414
1515
Methods:
1616
execute(state, url): Execute the node's logic and return the updated state.
1717
"""
1818

19-
def __init__(self, llm, node_name: str):
19+
def __init__(self, input: str, output: List[str], model_config: dict,
20+
node_name: str = "ImageToText"):
2021
"""
2122
Initializes an instance of the ImageToTextNode class.
2223
2324
Args:
24-
llm (OpenAIImageToText): An instance of the OpenAIImageToText class.
25-
node_name (str): name of the node
25+
input (str): The input for the node.
26+
output (List[str]): The output of the node.
27+
model_config (dict): Configuration for the model.
28+
node_name (str): Name of the node.
2629
"""
27-
super().__init__(node_name, "node")
28-
self.llm = llm
30+
super().__init__(node_name, "node", input, output, 1, model_config)
31+
self.llm_model = model_config["llm_model"]
2932

30-
def execute(self, state: dict, url: str) -> dict:
33+
def execute(self, state: dict) -> dict:
3134
"""
3235
Execute the node's logic and return the updated state.
36+
3337
Args:
3438
state (dict): The current state of the graph.
35-
url (str): url of the image where to
36-
:return: The updated state after executing this node.
37-
"""
3839
40+
Returns:
41+
dict: The updated state after executing this node.
42+
"""
3943
print("---GENERATING TEXT FROM IMAGE---")
40-
text_answer = self.llm.run(url)
44+
input_keys = self.get_input_keys(state)
45+
46+
input_data = [state[key] for key in input_keys]
47+
url = input_data[0]
48+
49+
text_answer = self.llm_model.run(url)
4150

4251
state.update({"image_text": text_answer})
4352
return state

scrapegraphai/utils/parse_state_keys.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
def parse_expression(expression, state: dict):
88
"""
99
Function for parsing the expressions
10+
Args:
11+
state (dict): state to elaborate
1012
"""
1113
# Check for empty expression
1214
if not expression:
@@ -69,14 +71,14 @@ def evaluate_expression(expression):
6971
'|'.join(sub_result) + expression[end+1:]
7072
return evaluate_simple_expression(expression)
7173

72-
result = evaluate_expression(expression)
74+
temp_result = evaluate_expression(expression)
7375

74-
if not result:
76+
if not temp_result:
7577
raise ValueError("No state keys matched the expression.")
7678

7779
# Remove redundant state keys from the result, without changing their order
7880
final_result = []
79-
for key in result:
81+
for key in temp_result:
8082
if key not in final_result:
8183
final_result.append(key)
8284

scrapegraphai/utils/remover.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ def remover(html_content: str) -> str:
1818

1919
soup = BeautifulSoup(html_content, 'html.parser')
2020

21-
# Estrai il titolo
2221
title_tag = soup.find('title')
2322
title = title_tag.get_text() if title_tag else ""
2423

25-
# Rimuovi i tag <script> in tutto il documento
2624
[script.extract() for script in soup.find_all('script')]
2725

28-
# Estrai il corpo del documento
2926
body_content = soup.find('body')
3027
body = str(body_content) if body_content else ""
3128

scrapegraphai/utils/save_audio_from_bytes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""
22
This utility function saves the byte response as an audio file.
33
"""
4-
54
from pathlib import Path
5+
from typing import Union
66

77

8-
def save_audio_from_bytes(byte_response, output_path):
8+
def save_audio_from_bytes(byte_response: bytes, output_path: Union[str, Path]) -> None:
99
"""
1010
Saves the byte response as an audio file.
1111

0 commit comments

Comments
 (0)