Skip to content

Python scraper based on AI

add new tests and refactoring of code

VinciGit00a84931518259
diff --git a/.gitignore b/.gitignore
index 356aa143..45329238 100644
--- a/.gitignore
+++ b/.gitignore
@@ -25,4 +25,5 @@ venv/
# exclude pdf, mp3
*.pdf
*.mp3
-*.sqlite
\ No newline at end of file
+*.sqlite
+examples/graph_examples/ScrapeGraphAI_generated_graph
diff --git a/examples/graph_examples/graph_from_text_example.py b/examples/graph_examples/graph_from_text_example.py
index 12c3f092..0b6733e9 100644
--- a/examples/graph_examples/graph_from_text_example.py
+++ b/examples/graph_examples/graph_from_text_example.py
@@ -28,25 +28,26 @@ with open(file_path, "r", encoding="utf-8") as file:
# define the nodes for the graph
-fetch_html_node = FetchTextNode("load_html_from_text")
-parse_document_node = ParseNode(doc_type="text", chunks_size=4000, node_name="parse_document")
+fetch_text_node = FetchTextNode("load_html_from_text")
+parse_document_node = ParseNode(
+ doc_type="text", chunks_size=4000, node_name="parse_document")
rag_node = RAGNode(model, "rag")
generate_answer_node = GenerateAnswerNode(model, "generate_answer")
# create the graph
graph = BaseGraph(
nodes={
- fetch_html_node,
+ fetch_text_node,
parse_document_node,
rag_node,
generate_answer_node
},
edges={
- (fetch_html_node, parse_document_node),
+ (fetch_text_node, parse_document_node),
(parse_document_node, rag_node),
(rag_node, generate_answer_node)
},
- entry_point=fetch_html_node
+ entry_point=fetch_text_node
)
# execute the graph
diff --git a/scrapegraphai/nodes/fetch_text_node.py b/scrapegraphai/nodes/fetch_text_node.py
index 529a7824..3942eb0a 100644
--- a/scrapegraphai/nodes/fetch_text_node.py
+++ b/scrapegraphai/nodes/fetch_text_node.py
@@ -1,8 +1,9 @@
""" 
Module for FetchTextNode
"""
-from .base_node import BaseNode
from langchain_core.documents import Document
+from .base_node import BaseNode
+
class FetchTextNode(BaseNode):
"""
diff --git a/tests/graphs/graph_from_text_test.py b/tests/graphs/graph_from_text_test.py
new file mode 100644
index 00000000..5c4154a8
--- /dev/null
+++ b/tests/graphs/graph_from_text_test.py
@@ -0,0 +1,93 @@
+"""
+Module for testing the graph_from_text_example
+"""
+import os
+import unittest
+from unittest.mock import patch
+from scrapegraphai.models import OpenAI
+from scrapegraphai.graphs import BaseGraph
+from scrapegraphai.nodes import FetchTextNode, ParseNode, RAGNode, GenerateAnswerNode
+
+
+class TestCustomGraph(unittest.TestCase):
+ """
+ Test suite for custom graph functionality.
+ """
+
+ @patch.object(OpenAI, 'generate')
+ def test_answer_generation(self, mock_generate):
+ """
+ Test answer generation functionality.
+
+ Args:
+ mock_generate (MagicMock): Mocked generate method of OpenAI class.
+ """
+ # Mock responses from the RAG and OpenAI models
+ mock_generate.side_effect = [
+ ("Here's the answer from the RAG model."),
+ ("The final refined answer.")
+ ]
+
+ # Sample text data
+ test_text = "This is a sample text for testing. It contains some information."
+
+ # Prepare a temporary file
+ with open('text_example.txt', 'w') as f:
+ f.write(test_text)
+
+ # Retrieve OpenAI API key from environment variables
+ openai_key = os.getenv("OPENAI_APIKEY")
+
+ # Check if the key was found
+ if not openai_key:
+ raise ValueError(
+ "OPENAI_APIKEY environment variable not found. Please set it before running.")
+
+ # LLM configuration
+ llm_config = {
+ "api_key": openai_key,
+ "model_name": "gpt-3.5-turbo",
+ "temperature": 0,
+ "streaming": True
+ }
+
+ # Create the OpenAI model instance
+ model = OpenAI(llm_config)
+
+ # Set up graph structure
+ fetch_text_node = FetchTextNode("load_html_from_text")
+ parse_document_node = ParseNode(
+ doc_type="text", chunks_size=20, node_name="parse_document")
+ rag_node = RAGNode(model, "rag")
+ generate_answer_node = GenerateAnswerNode(model, "generate_answer")
+
+ graph = BaseGraph(
+ nodes={
+ fetch_text_node,
+ parse_document_node,
+ rag_node,
+ generate_answer_node
+ },
+ edges={
+ (fetch_text_node, parse_document_node),
+ (parse_document_node, rag_node),
+ (rag_node, generate_answer_node)
+ },
+ entry_point=fetch_text_node
+ )
+
+ # Execute the graph
+ inputs = {"user_input": "What is the information in this text?",
+ "text": 'text_example.txt'}
+ result = graph.execute(inputs)
+
+ # Assert the expected answer
+ answer = result.get("answer", "No answer found.")
+ self.assertEqual(answer, "The final refined answer.")
+
+ # Clean up
+ os.remove('text_example.txt')
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tests/graphs/results.json b/tests/graphs/results.json
deleted file mode 100644
index 15980f90..00000000
--- a/tests/graphs/results.json
+++ /dev/null
@@ -1,62 +0,0 @@
-[
- {
- "imageUrl": "https://picsum.photos/200/300?random=1",
- "name": "T-shirt with Owl Print",
- "description": "Comfortable cotton t-shirt featuring an owl print.",
- "price": "€20.00"
- },
- {
- "imageUrl": "https://picsum.photos/200/300?random=2",
- "name": "Classic Leather Wallet",
- "description": "Genuine leather wallet with multiple compartments.",
- "price": "€35.00"
- },
- {
- "imageUrl": "https://picsum.photos/200/300?random=3",
- "name": "Canvas Backpack",
- "description": "Spacious canvas backpack perfect for daily use.",
- "price": "€45.00"
- },
- {
- "imageUrl": "https://picsum.photos/200/300?random=4",
- "name": "Stainless Steel Water Bottle",
- "description": "Durable stainless steel water bottle, eco-friendly.",
- "price": "€15.00"
- },
- {
- "imageUrl": "https://picsum.photos/200/300?random=5",
- "name": "Wooden Desk Organizer",
- "description": "Handcrafted wooden desk organizer with compartments.",
- "price": "€25.00"
- },
- {
- "imageUrl": "https://picsum.photos/200/300?random=6",
- "name": "Ceramic Coffee Mug",
- "description": "Stylish ceramic coffee mug, microwave safe.",
- "price": "€10.00"
- },
- {
- "imageUrl": "https://picsum.photos/200/300?random=7",
- "name": "Smartphone Stand",
- "description": "Adjustable smartphone stand for hands-free viewing.",
- "price": "€8.00"
- },
- {
- "imageUrl": "https://picsum.photos/200/300?random=8",
- "name": "Soft Cotton Bath Towel",
- "description": "Luxuriously soft cotton bath towel for spa-like comfort.",
- "price": "€18.00"
- },
- {
- "imageUrl": "https://picsum.photos/200/300?random=9",
- "name": "Wireless Bluetooth Earphones",
- "description": "High-quality wireless Bluetooth earphones with noise cancellation.",
- "price": "€50.00"
- },
- {
- "imageUrl": "https://picsum.photos/200/300?random=10",
- "name": "Glass Fruit Bowl",
- "description": "Elegant glass fruit bowl to display fruits beautifully.",
- "price": "€30.00"
- }
-]
diff --git a/tests/graphs/smart_scraper_test.py b/tests/graphs/smart_scraper_test.py
index 70702feb..6bb56755 100644
--- a/tests/graphs/smart_scraper_test.py
+++ b/tests/graphs/smart_scraper_test.py
@@ -1,3 +1,6 @@
+"""
+Module for testing SmartScraper class
+"""
import os
import unittest
from dotenv import load_dotenv
diff --git a/tests/graphs/speech_summary_test.py b/tests/graphs/speech_summary_test.py
index 15e190e6..79503f2c 100644
--- a/tests/graphs/speech_summary_test.py
+++ b/tests/graphs/speech_summary_test.py
@@ -1,3 +1,6 @@
+"""
+Module for testing SpeechSummaryGraph
+"""
import os
import unittest
from dotenv import load_dotenv
diff --git a/tests/graphs/text_example.txt b/tests/graphs/text_example.txt
new file mode 100644
index 00000000..c76d93df
--- /dev/null
+++ b/tests/graphs/text_example.txt
@@ -0,0 +1 @@
+This is a sample text for testing. It contains some information.
\ No newline at end of file