|
| 1 | +""" |
| 2 | +Basic example of scraping pipeline using CSVScraperGraph from CSV documents |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +import pandas as pd |
| 7 | +from scrapegraphai.graphs import CSVScraperGraph |
| 8 | +from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info |
| 9 | + |
| 10 | +# ************************************************ |
| 11 | +# Read the CSV file |
| 12 | +# ************************************************ |
| 13 | + |
| 14 | +FILE_NAME = "inputs/username.csv" |
| 15 | +curr_dir = os.path.dirname(os.path.realpath(__file__)) |
| 16 | +file_path = os.path.join(curr_dir, FILE_NAME) |
| 17 | + |
| 18 | +text = pd.read_csv(file_path) |
| 19 | + |
| 20 | +# ************************************************ |
| 21 | +# Define the configuration for the graph |
| 22 | +# ************************************************ |
| 23 | + |
| 24 | +graph_config = { |
| 25 | + "llm": { |
| 26 | + "model": "ollama/llama3", |
| 27 | + "temperature": 0, |
| 28 | + "format": "json", # Ollama needs the format to be specified explicitly |
| 29 | + # "model_tokens": 2000, # set context length arbitrarily |
| 30 | + "base_url": "http://localhost:11434", |
| 31 | + }, |
| 32 | + "embeddings": { |
| 33 | + "model": "ollama/nomic-embed-text", |
| 34 | + "temperature": 0, |
| 35 | + "base_url": "http://localhost:11434", |
| 36 | + }, |
| 37 | + "verbose": True, |
| 38 | +} |
| 39 | + |
| 40 | +# ************************************************ |
| 41 | +# Create the CSVScraperGraph instance and run it |
| 42 | +# ************************************************ |
| 43 | + |
| 44 | +csv_scraper_graph = CSVScraperGraph( |
| 45 | + prompt="List me all the last names", |
| 46 | + source=str(text), # Pass the content of the file, not the file object |
| 47 | + config=graph_config |
| 48 | +) |
| 49 | + |
| 50 | +result = csv_scraper_graph.run() |
| 51 | +print(result) |
| 52 | + |
| 53 | +# ************************************************ |
| 54 | +# Get graph execution info |
| 55 | +# ************************************************ |
| 56 | + |
| 57 | +graph_exec_info = csv_scraper_graph.get_execution_info() |
| 58 | +print(prettify_exec_info(graph_exec_info)) |
| 59 | + |
| 60 | +# Save to json or csv |
| 61 | +convert_to_csv(result, "result") |
| 62 | +convert_to_json(result, "result") |
0 commit comments