Skip to content

Commit 906fea9

Browse files
committed
add examples + module documentation
1 parent 39e130b commit 906fea9

File tree

6 files changed

+120
-1
lines changed

6 files changed

+120
-1
lines changed

examples/direct use/Readme.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
For having interacting examples visit the following collab notebook:
2+
[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1sEZBonBMGP44CtO6GQTwAlL0BGJXjtfd?usp=sharing)

examples/direct use/custom_graph.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
"""
3+
Example of custom graph using existing nodes
4+
"""
5+
from scrapegraphai.nodes import FetchHTMLNode, ParseHTMLNode, GenerateAnswerNode
6+
from scrapegraphai.graphs import BaseGraph
7+
from scrapegraphai.models import OpenAI
8+
from scrapegraphai.helpers import nodes_metadata
9+
10+
OPENAI_API_KEY = "YOUR_API_KEY"
11+
12+
# check available nodes
13+
14+
nodes_metadata.keys()
15+
16+
# to get more information about a node
17+
print(nodes_metadata['ImageToTextNode'])
18+
19+
# Define the configuration for the language model
20+
llm_config = {
21+
"api_key": OPENAI_API_KEY,
22+
"model_name": "gpt-3.5-turbo",
23+
"temperature": 0,
24+
"streaming": True
25+
}
26+
model = OpenAI(llm_config)
27+
28+
# define the nodes for the graph
29+
fetch_html_node = FetchHTMLNode("fetch_html")
30+
parse_document_node = ParseHTMLNode("parse_document")
31+
generate_answer_node = GenerateAnswerNode(model, "generate_answer")
32+
33+
# create the graph
34+
graph = BaseGraph(
35+
nodes={
36+
fetch_html_node,
37+
parse_document_node,
38+
generate_answer_node
39+
},
40+
edges={
41+
(fetch_html_node, parse_document_node),
42+
(parse_document_node, generate_answer_node)
43+
},
44+
entry_point=fetch_html_node
45+
)
46+
47+
# execute the graph
48+
inputs = {"user_input": "What is the title of the page?",
49+
"url": "https://example.com"}
50+
result = graph.execute(inputs)
51+
52+
# get the answer from the result
53+
answer = result.get("answer", "No answer found.")
54+
print(answer)

examples/direct use/graph_builder.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Example of graph building
3+
"""
4+
from scrapegraphai.builders import GraphBuilder
5+
OPENAI_API_KEY = "YOUR_API_KEY"
6+
7+
8+
llm_config = {
9+
"api_key": OPENAI_API_KEY,
10+
"model_name": "gpt-3.5-turbo",
11+
"temperature": 0,
12+
"streaming": True
13+
}
14+
15+
# Example usage of GraphBuilder
16+
USER_PROMPT = "Extract the news and generate a text summary with a voiceover."
17+
graph_builder = GraphBuilder(USER_PROMPT, llm_config)
18+
graph_json = graph_builder.build_graph()
19+
20+
# Convert the resulting JSON to Graphviz format
21+
graphviz_graph = graph_builder.convert_json_to_graphviz(graph_json)
22+
23+
print(graph_json)
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Basic example of scraping pipeline using SmartScraper
3+
"""
4+
from scrapegraphai.graphs import SmartScraperGraph
5+
OPENAI_API_KEY = "YOUR_API_KEY"
6+
7+
8+
llm_config = {
9+
"api_key": OPENAI_API_KEY,
10+
"model_name": "gpt-3.5-turbo",
11+
}
12+
13+
smart_scraper_graph = SmartScraperGraph("List me all the titles and project descriptions",
14+
"https://perinim.github.io/projects/", llm_config)
15+
16+
answer = smart_scraper_graph.run()
17+
print(answer["projects"][0])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
Basic example of scraping pipeline using SpeechSummaryGraph
3+
"""
4+
5+
from scrapegraphai.graphs import SpeechSummaryGraph
6+
OPENAI_API_KEY = "YOUR_API_KEY"
7+
8+
9+
llm_config = {
10+
"api_key": OPENAI_API_KEY
11+
}
12+
13+
# Save the audio to a file
14+
AUDIO_FILE = "website_summary.mp3"
15+
SPEECH_SUMMARY_GRAPH = SpeechSummaryGraph("Make a summary of the webpage to be converted to audio for blind people.",
16+
"https://perinim.github.io/projects/", llm_config,
17+
AUDIO_FILE)
18+
19+
final_state = SPEECH_SUMMARY_GRAPH.run()
20+
print(final_state.get("answer", "No answer found."))

scrapegraphai/graphs/smart_scraper_graph.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
"""
2+
Module for creating the smart scraper
3+
"""
14
from ..models import OpenAI
25
from .base_graph import BaseGraph
36
from ..nodes import (
@@ -6,7 +9,7 @@
69
GetProbableTagsNode,
710
GenerateAnswerNode,
811
ParseHTMLNode
9-
)
12+
)
1013

1114

1215
class SmartScraperGraph:

0 commit comments

Comments
 (0)