Python scraper based on AI
Python ai-crawlerai-scrapingai-searchcrawlerdata-extractionfirecrawl-alternativelarge-language-modelllmmarkdownragscrapingscraping-pythonweb-crawlerweb-crawlersweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
dev: added scrape graph example
Marco Perinidce571ccdadc
diff --git a/examples/graph_example.py b/examples/graph_example.pyindex fe29ffc1..b6548b0f 100644--- a/examples/graph_example.py+++ b/examples/graph_example.py@@ -1,42 +1,14 @@from langchain_openai import ChatOpenAI-from langchain_community.document_loaders import AsyncHtmlLoader--from yosoai.graph import BaseGraph-from yosoai.graph import GetProbableTagsNode-from yosoai.graph import ParseHTMLNode-from yosoai.graph import GenerateAnswerNode-from yosoai.graph import ConditionalNode+from yosoai.graph import SmartScraperOPENAI_API_KEY = ''--urls = ["https://perinim.github.io/projects/"]--# Load Documents-loader = AsyncHtmlLoader(urls)-docs = loader.load()-llm = ChatOpenAI(api_key=OPENAI_API_KEY, model_name="gpt-3.5-turbo", temperature=0, streaming=True)-get_probable_tags_node = GetProbableTagsNode(llm, "get_probable_tags")-parse_document_node = ParseHTMLNode("parse_document")-generate_answer_node = GenerateAnswerNode(llm, "generate_answer")-conditional_node = ConditionalNode("tags", [parse_document_node, generate_answer_node])+url = "https://perinim.github.io/projects/"+prompt = "List me all the titles and project descriptions"++smart_scraper = SmartScraper(prompt, url, llm)+answer = smart_scraper.run()-scrapeGraph = BaseGraph(- nodes={- get_probable_tags_node,- conditional_node,- parse_document_node,- generate_answer_node,- },- edges={- (get_probable_tags_node, conditional_node),- (parse_document_node, generate_answer_node)- },- entry_point=get_probable_tags_node-)+print(answer)-# To execute the graph-inputs = {"keys": {"user_input": "List me all projects and titles", "document": docs, "url": urls[0]}}-final_state = scrapeGraph.execute(inputs)-print(final_state["keys"]["answer"])\ No newline at end of filediff --git a/yosoai/graph/__init__.py b/yosoai/graph/__init__.pyindex 9c17c3a4..b4e2f459 100644--- a/yosoai/graph/__init__.py+++ b/yosoai/graph/__init__.py@@ -1,5 +1,8 @@from .base_graph import BaseGraph+from .fetch_html_node import FetchHTMLNodefrom .conditional_node import ConditionalNodefrom .get_probable_tags_node import GetProbableTagsNodefrom .generate_answer_node import GenerateAnswerNodefrom .parse_html_node import ParseHTMLNode++from .smart_scraper import SmartScraper\ No newline at end of filediff --git a/yosoai/graph/__pycache__/__init__.cpython-311.pyc b/yosoai/graph/__pycache__/__init__.cpython-311.pycdeleted file mode 100644index 7de90281..00000000Binary files a/yosoai/graph/__pycache__/__init__.cpython-311.pyc and /dev/null differdiff --git a/yosoai/graph/__pycache__/base_graph.cpython-311.pyc b/yosoai/graph/__pycache__/base_graph.cpython-311.pycdeleted file mode 100644index e096ba34..00000000Binary files a/yosoai/graph/__pycache__/base_graph.cpython-311.pyc and /dev/null differdiff --git a/yosoai/graph/__pycache__/base_node.cpython-311.pyc b/yosoai/graph/__pycache__/base_node.cpython-311.pycdeleted file mode 100644index c40e0a52..00000000Binary files a/yosoai/graph/__pycache__/base_node.cpython-311.pyc and /dev/null differdiff --git a/yosoai/graph/__pycache__/conditional_node.cpython-311.pyc b/yosoai/graph/__pycache__/conditional_node.cpython-311.pycdeleted file mode 100644index bcb4a761..00000000Binary files a/yosoai/graph/__pycache__/conditional_node.cpython-311.pyc and /dev/null differdiff --git a/yosoai/graph/__pycache__/generate_answer_node.cpython-311.pyc b/yosoai/graph/__pycache__/generate_answer_node.cpython-311.pycdeleted file mode 100644index a5329174..00000000Binary files a/yosoai/graph/__pycache__/generate_answer_node.cpython-311.pyc and /dev/null differdiff --git a/yosoai/graph/__pycache__/get_probable_tags_node.cpython-311.pyc b/yosoai/graph/__pycache__/get_probable_tags_node.cpython-311.pycdeleted file mode 100644index c98e9136..00000000Binary files a/yosoai/graph/__pycache__/get_probable_tags_node.cpython-311.pyc and /dev/null differdiff --git a/yosoai/graph/__pycache__/langgraph_example.cpython-311.pyc b/yosoai/graph/__pycache__/langgraph_example.cpython-311.pycdeleted file mode 100644index a8925ccb..00000000Binary files a/yosoai/graph/__pycache__/langgraph_example.cpython-311.pyc and /dev/null differdiff --git a/yosoai/graph/__pycache__/parse_html_node.cpython-311.pyc b/yosoai/graph/__pycache__/parse_html_node.cpython-311.pycdeleted file mode 100644index 40845be9..00000000Binary files a/yosoai/graph/__pycache__/parse_html_node.cpython-311.pyc and /dev/null differdiff --git a/yosoai/graph/fetch_html_node.py b/yosoai/graph/fetch_html_node.pynew file mode 100644index 00000000..31b1c2e0--- /dev/null+++ b/yosoai/graph/fetch_html_node.py@@ -0,0 +1,27 @@+from langchain_community.document_loaders import AsyncHtmlLoader+from .base_node import BaseNode++class FetchHTMLNode(BaseNode):+ def __init__(self, node_name, node_type="node"):+ super().__init__(node_name, node_type)++ def execute(self, state):+ """+ Fetches HTML document for the given URL and updates the state with the document.++ Args:+ state (dict): The current state of the graph, expected to contain a 'url' key.++ Returns:+ dict: The updated state with a new 'document' key containing the HTML content.+ """+ try:+ url = state["keys"]["url"]+ except KeyError as e:+ print(f"Error: {e} not found in state.")+ raise+ loader = AsyncHtmlLoader(url)+ document = loader.load()+ state["keys"]["document"] = document++ return statediff --git a/yosoai/graph/smart_scraper.py b/yosoai/graph/smart_scraper.pynew file mode 100644index 00000000..a0ad9515--- /dev/null+++ b/yosoai/graph/smart_scraper.py@@ -0,0 +1,42 @@+from langchain_openai import ChatOpenAI+from .base_graph import BaseGraph+from .fetch_html_node import FetchHTMLNode+from .conditional_node import ConditionalNode+from .get_probable_tags_node import GetProbableTagsNode+from .generate_answer_node import GenerateAnswerNode+from .parse_html_node import ParseHTMLNode++class SmartScraper:+ def __init__(self, prompt, url, llm):+ self.prompt = prompt+ self.url = url+ self.llm = llm+ self.graph = self._create_graph()++ def _create_graph(self):+ fetch_html_node = FetchHTMLNode("fetch_html")+ get_probable_tags_node = GetProbableTagsNode(self.llm, "get_probable_tags")+ parse_document_node = ParseHTMLNode("parse_document")+ generate_answer_node = GenerateAnswerNode(self.llm, "generate_answer")+ conditional_node = ConditionalNode("conditional", [parse_document_node, generate_answer_node])++ return BaseGraph(+ nodes={+ fetch_html_node,+ get_probable_tags_node,+ conditional_node,+ parse_document_node,+ generate_answer_node,+ },+ edges={+ (fetch_html_node, get_probable_tags_node),+ (get_probable_tags_node, conditional_node),+ (parse_document_node, generate_answer_node)+ },+ entry_point=fetch_html_node+ )++ def run(self):+ inputs = {"keys": {"user_input": self.prompt, "url": self.url}}+ final_state = self.graph.execute(inputs)+ return final_state["keys"].get("answer", "")