Skip to content

Commit 79ace11

Browse files
authored
Merge pull request #323 from VinciGit00/refactoring-pdf_scraper
Refactoring pdf scraper and json scrape
2 parents 1d217e4 + 743dfe1 commit 79ace11

File tree

50 files changed

+2246
-21
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2246
-21
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
Basic example of scraping pipeline using CSVScraperMultiGraph from CSV documents
3+
"""
4+
5+
import os
6+
from dotenv import load_dotenv
7+
import pandas as pd
8+
from scrapegraphai.graphs import CSVScraperMultiGraph
9+
from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
10+
11+
load_dotenv()
12+
# ************************************************
13+
# Read the CSV file
14+
# ************************************************
15+
16+
FILE_NAME = "inputs/username.csv"
17+
curr_dir = os.path.dirname(os.path.realpath(__file__))
18+
file_path = os.path.join(curr_dir, FILE_NAME)
19+
20+
text = pd.read_csv(file_path)
21+
22+
# ************************************************
23+
# Define the configuration for the graph
24+
# ************************************************
25+
26+
graph_config = {
27+
"llm": {
28+
"api_key": os.getenv("ANTHROPIC_API_KEY"),
29+
"model": "claude-3-haiku-20240307",
30+
"max_tokens": 4000},
31+
}
32+
33+
# ************************************************
34+
# Create the CSVScraperMultiGraph instance and run it
35+
# ************************************************
36+
37+
csv_scraper_graph = CSVScraperMultiGraph(
38+
prompt="List me all the last names",
39+
source=[str(text), str(text)],
40+
config=graph_config
41+
)
42+
43+
result = csv_scraper_graph.run()
44+
print(result)
45+
46+
# ************************************************
47+
# Get graph execution info
48+
# ************************************************
49+
50+
graph_exec_info = csv_scraper_graph.get_execution_info()
51+
print(prettify_exec_info(graph_exec_info))
52+
53+
# Save to json or csv
54+
convert_to_csv(result, "result")
55+
convert_to_json(result, "result")
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Module for showing how JSONScraperMultiGraph multi works
3+
"""
4+
import os
5+
import json
6+
from dotenv import load_dotenv
7+
from scrapegraphai.graphs import JSONScraperMultiGraph
8+
9+
load_dotenv()
10+
11+
graph_config = {
12+
"llm": {
13+
"api_key": os.getenv("ANTHROPIC_API_KEY"),
14+
"model": "claude-3-haiku-20240307",
15+
"max_tokens": 4000
16+
},
17+
}
18+
19+
FILE_NAME = "inputs/example.json"
20+
curr_dir = os.path.dirname(os.path.realpath(__file__))
21+
file_path = os.path.join(curr_dir, FILE_NAME)
22+
23+
with open(file_path, 'r', encoding="utf-8") as file:
24+
text = file.read()
25+
26+
sources = [text, text]
27+
28+
multiple_search_graph = JSONScraperMultiGraph(
29+
prompt= "List me all the authors, title and genres of the books",
30+
source= sources,
31+
schema=None,
32+
config=graph_config
33+
)
34+
35+
result = multiple_search_graph.run()
36+
print(json.dumps(result, indent=4))

examples/anthropic/pdf_scraper_graph_haiku.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
"""
2+
Module for showing how PDFScraper multi works
3+
"""
14
import os, json
25
from dotenv import load_dotenv
36
from scrapegraphai.graphs import PDFScraperGraph
47

58
load_dotenv()
69

7-
810
# ************************************************
911
# Define the configuration for the graph
1012
# ************************************************
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
Module for showing how PDFScraper multi works
3+
"""
4+
import os
5+
import json
6+
from dotenv import load_dotenv
7+
from scrapegraphai.graphs import PdfScraperMultiGraph
8+
9+
load_dotenv()
10+
11+
graph_config = {
12+
"llm": {
13+
"api_key": os.getenv("ANTHROPIC_API_KEY"),
14+
"model": "claude-3-haiku-20240307",
15+
"max_tokens": 4000
16+
},
17+
}
18+
19+
# ***************
20+
# Covert to list
21+
# ***************
22+
23+
sources = [
24+
"This paper provides evidence from a natural experiment on the relationship between positive affect and productivity. We link highly detailed administrative data on the behaviors and performance of all telesales workers at a large telecommunications company with survey reports of employee happiness that we collected on a weekly basis. We use variation in worker mood arising from visual exposure to weather—the interaction between call center architecture and outdoor weather conditions—in order to provide a quasi-experimental test of the effect of happiness on productivity. We find evidence of a positive impact on sales performance, which is driven by changes in labor productivity – largely through workers converting more calls into sales, and to a lesser extent by making more calls per hour and adhering more closely to their schedule. We find no evidence in our setting of effects on measures of high-frequency labor supply such as attendance and break-taking.",
25+
"This paper provides evidence from a natural experiment on the relationship between positive affect and productivity. We link highly detailed administrative data on the behaviors and performance of all telesales workers at a large telecommunications company with survey reports of employee happiness that we collected on a weekly basis. We use variation in worker mood arising from visual exposure to weather—the interaction between call center architecture and outdoor weather conditions—in order to provide a quasi-experimental test of the effect of happiness on productivity. We find evidence of a positive impact on sales performance, which is driven by changes in labor productivity – largely through workers converting more calls into sales, and to a lesser extent by making more calls per hour and adhering more closely to their schedule. We find no evidence in our setting of effects on measures of high-frequency labor supply such as attendance and break-taking.",
26+
"This paper provides evidence from a natural experiment on the relationship between positive affect and productivity. We link highly detailed administrative data on the behaviors and performance of all telesales workers at a large telecommunications company with survey reports of employee happiness that we collected on a weekly basis. We use variation in worker mood arising from visual exposure to weather—the interaction between call center architecture and outdoor weather conditions—in order to provide a quasi-experimental test of the effect of happiness on productivity. We find evidence of a positive impact on sales performance, which is driven by changes in labor productivity – largely through workers converting more calls into sales, and to a lesser extent by making more calls per hour and adhering more closely to their schedule. We find no evidence in our setting of effects on measures of high-frequency labor supply such as attendance and break-taking.",
27+
"This paper provides evidence from a natural experiment on the relationship between positive affect and productivity. We link highly detailed administrative data on the behaviors and performance of all telesales workers at a large telecommunications company with survey reports of employee happiness that we collected on a weekly basis. We use variation in worker mood arising from visual exposure to weather—the interaction between call center architecture and outdoor weather conditions—in order to provide a quasi-experimental test of the effect of happiness on productivity. We find evidence of a positive impact on sales performance, which is driven by changes in labor productivity – largely through workers converting more calls into sales, and to a lesser extent by making more calls per hour and adhering more closely to their schedule. We find no evidence in our setting of effects on measures of high-frequency labor supply such as attendance and break-taking.",
28+
]
29+
30+
prompt = """
31+
You are an expert in reviewing academic manuscripts. Please analyze the abstracts provided from an academic journal article to extract and clearly identify the following elements:
32+
33+
Independent Variable (IV): The variable that is manipulated or considered as the primary cause affecting other variables.
34+
Dependent Variable (DV): The variable that is measured or observed, which is expected to change as a result of variations in the Independent Variable.
35+
Exogenous Shock: Identify any external or unexpected events used in the study that serve as a natural experiment or provide a unique setting for observing the effects on the IV and DV.
36+
Response Format: For each abstract, present your response in the following structured format:
37+
38+
Independent Variable (IV):
39+
Dependent Variable (DV):
40+
Exogenous Shock:
41+
42+
Example Queries and Responses:
43+
44+
Query: This paper provides evidence from a natural experiment on the relationship between positive affect and productivity. We link highly detailed administrative data on the behaviors and performance of all telesales workers at a large telecommunications company with survey reports of employee happiness that we collected on a weekly basis. We use variation in worker mood arising from visual exposure to weather the interaction between call center architecture and outdoor weather conditions in order to provide a quasi-experimental test of the effect of happiness on productivity. We find evidence of a positive impact on sales performance, which is driven by changes in labor productivity largely through workers converting more calls into sales, and to a lesser extent by making more calls per hour and adhering more closely to their schedule. We find no evidence in our setting of effects on measures of high-frequency labor supply such as attendance and break-taking.
45+
46+
Response:
47+
48+
Independent Variable (IV): Employee happiness.
49+
Dependent Variable (DV): Overall firm productivity.
50+
Exogenous Shock: Sudden company-wide increase in bonus payments.
51+
52+
Query: The diffusion of social media coincided with a worsening of mental health conditions among adolescents and young adults in the United States, giving rise to speculation that social media might be detrimental to mental health. In this paper, we provide quasi-experimental estimates of the impact of social media on mental health by leveraging a unique natural experiment: the staggered introduction of Facebook across U.S. colleges. Our analysis couples data on student mental health around the years of Facebook's expansion with a generalized difference-in-differences empirical strategy. We find that the roll-out of Facebook at a college increased symptoms of poor mental health, especially depression. We also find that, among students predicted to be most susceptible to mental illness, the introduction of Facebook led to increased utilization of mental healthcare services. Lastly, we find that, after the introduction of Facebook, students were more likely to report experiencing impairments to academic performance resulting from poor mental health. Additional evidence on mechanisms suggests that the results are due to Facebook fostering unfavorable social comparisons.
53+
54+
Response:
55+
56+
Independent Variable (IV): Exposure to social media.
57+
Dependent Variable (DV): Mental health outcomes.
58+
Exogenous Shock: staggered introduction of Facebook across U.S. colleges.
59+
"""
60+
# *******************************************************
61+
# Create the SmartScraperMultiGraph instance and run it
62+
# *******************************************************
63+
64+
multiple_search_graph = PdfScraperMultiGraph(
65+
prompt=prompt,
66+
source= sources,
67+
schema=None,
68+
config=graph_config
69+
)
70+
71+
result = multiple_search_graph.run()
72+
print(json.dumps(result, indent=4))
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
Basic example of scraping pipeline using XMLScraperMultiGraph from XML documents
3+
"""
4+
5+
import os
6+
from dotenv import load_dotenv
7+
from scrapegraphai.graphs import XMLScraperMultiGraph
8+
from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
9+
load_dotenv()
10+
11+
# ************************************************
12+
# Read the XML file
13+
# ************************************************
14+
15+
FILE_NAME = "inputs/books.xml"
16+
curr_dir = os.path.dirname(os.path.realpath(__file__))
17+
file_path = os.path.join(curr_dir, FILE_NAME)
18+
19+
with open(file_path, 'r', encoding="utf-8") as file:
20+
text = file.read()
21+
22+
# ************************************************
23+
# Define the configuration for the graph
24+
# ************************************************
25+
26+
graph_config = {
27+
"llm": {
28+
"api_key": os.getenv("ANTHROPIC_API_KEY"),
29+
"model": "claude-3-haiku-20240307",
30+
"max_tokens": 4000},
31+
}
32+
33+
# ************************************************
34+
# Create the XMLScraperMultiGraph instance and run it
35+
# ************************************************
36+
37+
xml_scraper_graph = XMLScraperMultiGraph(
38+
prompt="List me all the authors, title and genres of the books",
39+
source=[text, text], # Pass the content of the file, not the file object
40+
config=graph_config
41+
)
42+
43+
result = xml_scraper_graph.run()
44+
print(result)
45+
46+
# ************************************************
47+
# Get graph execution info
48+
# ************************************************
49+
50+
graph_exec_info = xml_scraper_graph.get_execution_info()
51+
print(prettify_exec_info(graph_exec_info))
52+
53+
# Save to json or csv
54+
convert_to_csv(result, "result")
55+
convert_to_json(result, "result")
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
Basic example of scraping pipeline using CSVScraperMultiGraph from CSV documents
3+
"""
4+
5+
import os
6+
from dotenv import load_dotenv
7+
import pandas as pd
8+
from scrapegraphai.graphs import CSVScraperMultiGraph
9+
from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
10+
11+
load_dotenv()
12+
# ************************************************
13+
# Read the CSV file
14+
# ************************************************
15+
16+
FILE_NAME = "inputs/username.csv"
17+
curr_dir = os.path.dirname(os.path.realpath(__file__))
18+
file_path = os.path.join(curr_dir, FILE_NAME)
19+
20+
text = pd.read_csv(file_path)
21+
22+
# ************************************************
23+
# Define the configuration for the graph
24+
# ************************************************
25+
26+
graph_config = {
27+
"llm": {
28+
"client": "client_name",
29+
"model": "bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
30+
"temperature": 0.0
31+
},
32+
"embeddings": {
33+
"model": "bedrock/cohere.embed-multilingual-v3"
34+
}
35+
}
36+
37+
# ************************************************
38+
# Create the CSVScraperMultiGraph instance and run it
39+
# ************************************************
40+
41+
csv_scraper_graph = CSVScraperMultiGraph(
42+
prompt="List me all the last names",
43+
source=[str(text), str(text)],
44+
config=graph_config
45+
)
46+
47+
result = csv_scraper_graph.run()
48+
print(result)
49+
50+
# ************************************************
51+
# Get graph execution info
52+
# ************************************************
53+
54+
graph_exec_info = csv_scraper_graph.get_execution_info()
55+
print(prettify_exec_info(graph_exec_info))
56+
57+
# Save to json or csv
58+
convert_to_csv(result, "result")
59+
convert_to_json(result, "result")
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
Basic example of scraping pipeline using XMLScraperMultiGraph from XML documents
3+
"""
4+
5+
import os
6+
from dotenv import load_dotenv
7+
from scrapegraphai.graphs import XMLScraperMultiGraph
8+
from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
9+
load_dotenv()
10+
11+
# ************************************************
12+
# Read the XML file
13+
# ************************************************
14+
15+
FILE_NAME = "inputs/books.xml"
16+
curr_dir = os.path.dirname(os.path.realpath(__file__))
17+
file_path = os.path.join(curr_dir, FILE_NAME)
18+
19+
with open(file_path, 'r', encoding="utf-8") as file:
20+
text = file.read()
21+
22+
# ************************************************
23+
# Define the configuration for the graph
24+
# ************************************************
25+
26+
graph_config = {
27+
"llm": {
28+
"client": "client_name",
29+
"model": "bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
30+
"temperature": 0.0
31+
},
32+
"embeddings": {
33+
"model": "bedrock/cohere.embed-multilingual-v3"
34+
}
35+
}
36+
37+
# ************************************************
38+
# Create the XMLScraperMultiGraph instance and run it
39+
# ************************************************
40+
41+
xml_scraper_graph = XMLScraperMultiGraph(
42+
prompt="List me all the authors, title and genres of the books",
43+
source=[text, text], # Pass the content of the file, not the file object
44+
config=graph_config
45+
)
46+
47+
result = xml_scraper_graph.run()
48+
print(result)
49+
50+
# ************************************************
51+
# Get graph execution info
52+
# ************************************************
53+
54+
graph_exec_info = xml_scraper_graph.get_execution_info()
55+
print(prettify_exec_info(graph_exec_info))
56+
57+
# Save to json or csv
58+
convert_to_csv(result, "result")
59+
convert_to_json(result, "result")

0 commit comments

Comments
 (0)