|
| 1 | +""" |
| 2 | +Basic example of scraping pipeline using SmartScraper using Azure OpenAI Key |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +from dotenv import load_dotenv |
| 7 | +from scrapegraphai.graphs import SmartScraperGraph |
| 8 | +from scrapegraphai.utils import prettify_exec_info |
| 9 | +from langchain_community.llms import HuggingFaceEndpoint |
| 10 | +from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings |
| 11 | + |
| 12 | + |
| 13 | +# required environment variables in .env |
| 14 | +# HUGGINGFACEHUB_API_TOKEN |
| 15 | +# ANTHROPIC_API_KEY |
| 16 | +load_dotenv() |
| 17 | + |
| 18 | +HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN') |
| 19 | +# ************************************************ |
| 20 | +# Initialize the model instances |
| 21 | +# ************************************************ |
| 22 | + |
| 23 | + |
| 24 | +embedder_model_instance = HuggingFaceInferenceAPIEmbeddings( |
| 25 | + api_key=HUGGINGFACEHUB_API_TOKEN, model_name="sentence-transformers/all-MiniLM-l6-v2" |
| 26 | +) |
| 27 | + |
| 28 | +# ************************************************ |
| 29 | +# Define the output schema for the graph |
| 30 | +# ************************************************ |
| 31 | + |
| 32 | +schema= """ |
| 33 | + { |
| 34 | + "Projects": [ |
| 35 | + "Project #": |
| 36 | + { |
| 37 | + "title": "...", |
| 38 | + "description": "...", |
| 39 | + }, |
| 40 | + "Project #": |
| 41 | + { |
| 42 | + "title": "...", |
| 43 | + "description": "...", |
| 44 | + } |
| 45 | + ] |
| 46 | + } |
| 47 | +""" |
| 48 | + |
| 49 | +# ************************************************ |
| 50 | +# Create the SmartScraperGraph instance and run it |
| 51 | +# ************************************************ |
| 52 | + |
| 53 | +graph_config = { |
| 54 | + "llm": { |
| 55 | + "api_key": os.getenv("ANTHROPIC_API_KEY"), |
| 56 | + "model": "claude-3-haiku-20240307", |
| 57 | + "max_tokens": 4000}, |
| 58 | + "embeddings": {"model_instance": embedder_model_instance} |
| 59 | +} |
| 60 | + |
| 61 | +smart_scraper_graph = SmartScraperGraph( |
| 62 | + prompt="List me all the projects with their description", |
| 63 | + # also accepts a string with the already downloaded HTML code |
| 64 | + schema=schema, |
| 65 | + source="https://perinim.github.io/projects/", |
| 66 | + config=graph_config |
| 67 | +) |
| 68 | + |
| 69 | +result = smart_scraper_graph.run() |
| 70 | +print(result) |
| 71 | + |
| 72 | +# ************************************************ |
| 73 | +# Get graph execution info |
| 74 | +# ************************************************ |
| 75 | + |
| 76 | +graph_exec_info = smart_scraper_graph.get_execution_info() |
| 77 | +print(prettify_exec_info(graph_exec_info)) |
0 commit comments