Skip to content

Python scraper based on AI

refactoring of the example and add 2 new nodes

VinciGit00f8bb32919b81
diff --git a/examples/custom_graph.py b/examples/fetch_html_node_example.py
similarity index 96%
rename from examples/custom_graph.py
rename to examples/fetch_html_node_example.py
index bca44d36..f32da32f 100644
--- a/examples/custom_graph.py
+++ b/examples/fetch_html_node_example.py
@@ -1,52 +1,52 @@
-"""
-Example of custom graph using existing nodes
-"""
-
-import os
-from dotenv import load_dotenv
-
-from langchain_openai import ChatOpenAI
-from scrapegraphai.graphs import BaseGraph
-from scrapegraphai.nodes import FetchHTMLNode, ParseHTMLNode, GenerateAnswerNode
-
-# load the environment variables
-load_dotenv()
-openai_key = os.getenv("API_KEY")
-if not openai_key:
- print("Error: OpenAI API key not found in environment variables.")
-
-# Define the configuration for the language model
-llm_config = {
- "api_key": openai_key,
- "model_name": "gpt-3.5-turbo",
- "temperature": 0,
- "streaming": True
-}
-model = ChatOpenAI(**llm_config)
-
-# define the nodes for the graph
-fetch_html_node = FetchHTMLNode("fetch_html")
-parse_document_node = ParseHTMLNode("parse_document")
-generate_answer_node = GenerateAnswerNode(model, "generate_answer")
-
-# create the graph
-graph = BaseGraph(
- nodes={
- fetch_html_node,
- parse_document_node,
- generate_answer_node
- },
- edges={
- (fetch_html_node, parse_document_node),
- (parse_document_node, generate_answer_node)
- },
- entry_point=fetch_html_node
-)
-
-# execute the graph
-inputs = {"keys": {"user_input": "What is the title of the page?", "url": "https://example.com"}}
-result = graph.execute(inputs)
-
-# get the answer from the result
-answer = result["keys"].get("answer", "No answer found.")
-print(answer)
+"""
+Example of custom graph using existing nodes
+"""
+
+import os
+from dotenv import load_dotenv
+
+from langchain_openai import ChatOpenAI
+from scrapegraphai.graphs import BaseGraph
+from scrapegraphai.nodes import FetchHTMLNode, ParseHTMLNode, GenerateAnswerNode
+
+# load the environment variables
+load_dotenv()
+openai_key = os.getenv("API_KEY")
+if not openai_key:
+ print("Error: OpenAI API key not found in environment variables.")
+
+# Define the configuration for the language model
+llm_config = {
+ "api_key": openai_key,
+ "model_name": "gpt-3.5-turbo",
+ "temperature": 0,
+ "streaming": True
+}
+model = ChatOpenAI(**llm_config)
+
+# define the nodes for the graph
+fetch_html_node = FetchHTMLNode("fetch_html")
+parse_document_node = ParseHTMLNode("parse_document")
+generate_answer_node = GenerateAnswerNode(model, "generate_answer")
+
+# create the graph
+graph = BaseGraph(
+ nodes={
+ fetch_html_node,
+ parse_document_node,
+ generate_answer_node
+ },
+ edges={
+ (fetch_html_node, parse_document_node),
+ (parse_document_node, generate_answer_node)
+ },
+ entry_point=fetch_html_node
+)
+
+# execute the graph
+inputs = {"keys": {"user_input": "What is the title of the page?", "url": "https://example.com"}}
+result = graph.execute(inputs)
+
+# get the answer from the result
+answer = result["keys"].get("answer", "No answer found.")
+print(answer)
diff --git a/examples/graph_example.py b/examples/graph_example.py
deleted file mode 100644
index 16ab3a97..00000000
--- a/examples/graph_example.py
+++ /dev/null
@@ -1,22 +0,0 @@
-"""
-Basic example for creating the node
-"""
-import os
-from dotenv import load_dotenv
-from scrapegraphai.graphs import SmartScraper
-
-load_dotenv()
-
-openai_key = os.getenv("API_KEY")
-if not openai_key:
- print("Error: OpenAI API key not found in environment variables.")
-llm_config = {
- "api_key": openai_key,
- "model_name": "gpt-3.5-turbo",
-}
-
-smart_scraper = SmartScraper("List me all the titles and project descriptions",
- "https://perinim.github.io/projects/", llm_config)
-
-answer = smart_scraper.run()
-print(answer)
diff --git a/scrapegraphai/nodes/image_to_text_node.py b/scrapegraphai/nodes/image_to_text_node.py
new file mode 100644
index 00000000..02f2b168
--- /dev/null
+++ b/scrapegraphai/nodes/image_to_text_node.py
@@ -0,0 +1,90 @@
+"""
+Module for creating the basic node
+"""
+from abc import ABC, abstractmethod
+from openai import OpenAI
+
+
+class ImageToTextNode(ABC):
+ """
+ An abstract base class for nodes in a graph-based workflow. Each node is
+ intended to perform a specific action when executed as part of the graph's
+ processing flow.
+
+ Attributes:
+ node_name (str): A unique identifier for the node.
+ node_type (str): Specifies the node's type, which influences how the
+ node interacts within the graph. Valid values are
+ "node" for standard nodes and "conditional_node" for
+ nodes that determine the flow based on conditions.
+
+ Methods:
+ execute(state): An abstract method that subclasses must implement. This
+ method should contain the logic that the node executes
+ when it is reached in the graph's flow. It takes the
+ graph's current state as input and returns the updated
+ state after execution.
+
+ Args:
+ node_name (str): The unique identifier name for the node. This name is
+ used to reference the node within the graph.
+ node_type (str): The type of the node, limited to "node" or
+ "conditional_node". This categorization helps in
+ determining the node's role and behavior within the
+ graph.
+
+ Raises:
+ ValueError: If the provided `node_type` is not one of the allowed
+ values ("node" or "conditional_node"), a ValueError is
+ raised to indicate the incorrect usage.
+ """
+
+ def __init__(self, llm, node_name: str = "ParseImageToText"):
+ """
+ Initialize the node with a unique identifier and a specified node type.
+
+ Args:
+ node_name (str): The unique identifier name for the node.
+ node_type (str): The type of the node, limited to "node" or "conditional_node".
+
+ Raises:
+ ValueError: If node_type is not "node" or "conditional_node".
+ """
+ super().__init__(node_name, "node")
+ self.llm = llm
+
+ @abstractmethod
+ def execute(self, state: dict, url: str) -> str:
+ """
+ Execute the node's logic and return the updated state.
+ Args:
+ state (dict): The current state of the graph.
+ url (str): url of the image where to
+ :return: The updated state after executing this node.
+ """
+ # Da fixare
+ client = OpenAI(api_key=self.llm.openai_api_key)
+
+ if not self.llm.model_name == "gpt-4-vision-preview":
+ raise ValueError("Model is not gpt-4-vision-preview")
+
+ response = client.chat.completions.create(
+ model=self.llm.model_name,
+ messages=[
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "What’s in this image?"},
+ {
+ "type": "image_url",
+ "image_url": {
+ "url": url,
+ },
+ },
+ ],
+ }
+ ],
+ max_tokens=300,
+ )
+
+ return response.choices[0]
diff --git a/scrapegraphai/nodes/parse_html_node.py b/scrapegraphai/nodes/parse_html_node.py
deleted file mode 100644
index 62690e48..00000000
--- a/scrapegraphai/nodes/parse_html_node.py
+++ /dev/null
@@ -1,74 +0,0 @@
-"""
-Module for parsing the HTML node
-"""
-from langchain_community.document_transformers import BeautifulSoupTransformer
-from .base_node import BaseNode
-
-
-class ParseHTMLNode(BaseNode):
- """
- A node responsible for parsing HTML content from a document using specified tags.
- It uses BeautifulSoupTransformer for parsing, providing flexibility in extracting
- specific parts of an HTML document based on the tags provided in the state.
-
- This node enhances the scraping workflow by allowing for targeted extraction of
- content, thereby optimizing the processing of large HTML documents.
-
- Attributes:
- node_name (str): The unique identifier name for the node, defaulting to "ParseHTMLNode".
- node_type (str): The type of the node, set to "node" indicating a standard operational node.
-
- Args:
- node_name (str, optional): The unique identifier name for the node.
- Defaults to "ParseHTMLNode".
-
- Methods:
- execute(state): Parses the HTML document contained within the state using
- the specified tags, if provided, and updates the state with the parsed content.
- """
-
- def __init__(self, node_name="ParseHTMLNode"):
- """
- Initializes the ParseHTMLNode with a node name.
- """
- super().__init__(node_name, "node")
-
- def execute(self, state):
- """
- Executes the node's logic to parse the HTML document based on specified tags.
- If tags are provided in the state, the document is parsed accordingly; otherwise,
- the document remains unchanged. The method updates the state with either the original
- or parsed document under the 'parsed_document' key.
-
- Args:
- state (dict): The current state of the graph, expected to contain
- 'document' within 'keys', and optionally 'tags' for targeted parsing.
-
- Returns:
- dict: The updated state with the 'parsed_document' key containing the parsed content,
- if tags were provided, or the original document otherwise.
-
- Raises:
- KeyError: If 'document' is not found in the state, indicating that the necessary
- information for parsing is missing.
- """
-
- print("---PARSE HTML DOCUMENT---")
- try:
- document = state["keys"]["document"]
- except KeyError as e:
- print(f"Error: {e} not found in state.")
- raise
-
- tags = state["keys"].get("tags", None)
-
- if not tags:
- print("No specific tags provided; returning document as is.")
- return state
-
- bs_transformer = BeautifulSoupTransformer()
- parsed_document = bs_transformer.transform_documents(
- document, tags_to_extract=tags)
- print("Document parsed with specified tags.")
- state["keys"].update({"parsed_document": parsed_document})
- return state
diff --git a/scrapegraphai/nodes/text_to_speach_node.py b/scrapegraphai/nodes/text_to_speach_node.py
index 28b53993..1018535f 100644
--- a/scrapegraphai/nodes/text_to_speach_node.py
+++ b/scrapegraphai/nodes/text_to_speach_node.py
@@ -9,10 +9,8 @@ from .base_node import BaseNode
class TextToSpeachNode(BaseNode):
"""
- A node responsible for parsing HTML content from a document using specified tags.
- It uses BeautifulSoupTransformer for parsing, providing flexibility in extracting
- specific parts of an HTML document based on the tags provided in the state.
-
+ A node responsible for parsing text content from a document using specified tags and readinf
+ it with the selected voiceq.
This node enhances the scraping workflow by allowing for targeted extraction of
content, thereby optimizing the processing of large HTML documents.
@@ -29,14 +27,14 @@ class TextToSpeachNode(BaseNode):
the specified tags, if provided, and updates the state with the parsed content.
"""
- def __init__(self, llm, node_name="ParseTextToSpeach"):
+ def __init__(self, llm, node_name: str = "ParseTextToSpeach"):
"""
Initializes the ParseHTMLNode with a node name.
"""
super().__init__(node_name, "node")
self.llm = llm
- def execute(self, state: dict, text: str, output_path: str = str, model: str = "tts-1", voice="alloy"):
+ def execute(self, state: dict, text: str, output_path: str = str, model: str = "tts-1", voice="alloy") -> dict:
"""
Executes the node's logic to parse the HTML document based on specified tags.
If tags are provided in the state, the document is parsed accordingly; otherwise,