|
| 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 | + |
| 14 | + |
| 15 | +## required environment variable in .env |
| 16 | +#HUGGINGFACEHUB_API_TOKEN |
| 17 | +load_dotenv() |
| 18 | + |
| 19 | +HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN') |
| 20 | +# ************************************************ |
| 21 | +# Initialize the model instances |
| 22 | +# ************************************************ |
| 23 | + |
| 24 | +repo_id = "mistralai/Mistral-7B-Instruct-v0.2" |
| 25 | + |
| 26 | +llm_model_instance = HuggingFaceEndpoint( |
| 27 | + repo_id=repo_id, max_length=128, temperature=0.5, token=HUGGINGFACEHUB_API_TOKEN |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +embedder_model_instance = HuggingFaceInferenceAPIEmbeddings( |
| 34 | + api_key=HUGGINGFACEHUB_API_TOKEN, model_name="sentence-transformers/all-MiniLM-l6-v2" |
| 35 | +) |
| 36 | + |
| 37 | +# ************************************************ |
| 38 | +# Create the SmartScraperGraph instance and run it |
| 39 | +# ************************************************ |
| 40 | + |
| 41 | +graph_config = { |
| 42 | + "llm": {"model_instance": llm_model_instance}, |
| 43 | + "embeddings": {"model_instance": embedder_model_instance} |
| 44 | +} |
| 45 | + |
| 46 | +smart_scraper_graph = SmartScraperGraph( |
| 47 | + prompt="List me all the events, with the following fields: company_name, event_name, event_start_date, event_start_time, event_end_date, event_end_time, location, event_mode, event_category, third_party_redirect, no_of_days, time_in_hours, hosted_or_attending, refreshments_type, registration_available, registration_link", |
| 48 | + # also accepts a string with the already downloaded HTML code |
| 49 | + source="https://www.hmhco.com/event", |
| 50 | + config=graph_config |
| 51 | +) |
| 52 | + |
| 53 | +result = smart_scraper_graph.run() |
| 54 | +print(result) |
| 55 | + |
| 56 | +# ************************************************ |
| 57 | +# Get graph execution info |
| 58 | +# ************************************************ |
| 59 | + |
| 60 | +graph_exec_info = smart_scraper_graph.get_execution_info() |
| 61 | +print(prettify_exec_info(graph_exec_info)) |
| 62 | + |
| 63 | + |
0 commit comments