Python scraper based on AI
Python ai-crawlerai-scrapingai-searchcrawlerdata-extractionfirecrawl-alternativelarge-language-modelllmmarkdownragscrapingscraping-pythonweb-crawlerweb-crawlersweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
add hugginface integration
VinciGit0053e317d46b64
diff --git a/scrapegraphai/graphs/abstract_graph.py b/scrapegraphai/graphs/abstract_graph.pyindex b2117855..ca4fc65c 100644--- a/scrapegraphai/graphs/abstract_graph.py+++ b/scrapegraphai/graphs/abstract_graph.py@@ -1,9 +1,9 @@-"""+"""Module having abstract class for creating all the graphs"""from abc import ABC, abstractmethodfrom typing import Optional-from ..models import OpenAI, Gemini, Ollama, AzureOpenAI+from ..models import OpenAI, Gemini, Ollama, AzureOpenAI, HuggingFacefrom ..helpers import models_tokens@@ -46,7 +46,7 @@ class AbstractGraph(ABC):# take the model after the last dashllm_params["model"] = llm_params["model"].split("/")[-1]try:- self.model_token = models_tokens["openai"][llm_params["model"]]+ self.model_token = models_tokens["azure"][llm_params["model"]]except KeyError:raise ValueError("Model not supported")return AzureOpenAI(llm_params)@@ -59,14 +59,6 @@ class AbstractGraph(ABC):return Gemini(llm_params)elif "ollama" in llm_params["model"]:- """- Avaiable models:- - llama2- - mistral- - codellama- - dolphin-mixtral- - mistral-openorca- """llm_params["model"] = llm_params["model"].split("/")[-1]# allow user to set model_tokens in config@@ -79,9 +71,15 @@ class AbstractGraph(ABC):raise ValueError("Model not supported")return Ollama(llm_params)-+ elif "hugging_face" in llm_params["model"]:+ try:+ self.model_token = models_tokens["hugging_face"][llm_params["model"]]+ except KeyError:+ raise ValueError("Model not supported")+ return HuggingFace(llm_params)else:- raise ValueError("Model not supported")+ raise ValueError(+ "Model provided by the configuration not supported")@abstractmethoddef _create_graph(self):diff --git a/scrapegraphai/models/__init__.py b/scrapegraphai/models/__init__.pyindex 8d85cd60..b96de107 100644--- a/scrapegraphai/models/__init__.py+++ b/scrapegraphai/models/__init__.py@@ -8,3 +8,4 @@ from .openai_itt import OpenAIImageToTextfrom .openai_tts import OpenAITextToSpeechfrom .gemini import Geminifrom .ollama import Ollama+from .hugging_face import HuggingFacediff --git a/scrapegraphai/models/hugging_face.py b/scrapegraphai/models/hugging_face.pynew file mode 100644index 00000000..d2df52d3--- /dev/null+++ b/scrapegraphai/models/hugging_face.py@@ -0,0 +1,22 @@+"""+Module for implementing the hugginface class+"""+from langchain_community.chat_models.huggingface import ChatHuggingFace+++class HuggingFace(ChatHuggingFace):+ """Provides a convenient wrapper for interacting with Hugging Face language models+ designed for conversational AI applications.++ Args:+ llm_config (dict): A configuration dictionary containing:+ * api_key (str, optional): Your Hugging Face API key.+ * model_name (str): The name of the Hugging Face LLM to load.+ * tokenizer_name (str, optional): Name of the corresponding tokenizer.+ * device (str, optional): Device for running the model ('cpu' by default).++ """++ def __init__(self, llm_config: dict):+ """Initializes the HuggingFace chat model wrapper"""+ super().__init__(**llm_config)diff --git a/scrapegraphai/nodes/rag_node.py b/scrapegraphai/nodes/rag_node.pyindex 37fb4a22..36b36a34 100644--- a/scrapegraphai/nodes/rag_node.py+++ b/scrapegraphai/nodes/rag_node.py@@ -7,9 +7,10 @@ from langchain.docstore.document import Documentfrom langchain.retrievers import ContextualCompressionRetrieverfrom langchain.retrievers.document_compressors import EmbeddingsFilter, DocumentCompressorPipelinefrom langchain_community.document_transformers import EmbeddingsRedundantFilter+from langchain_community.embeddings import HuggingFaceHubEmbeddingsfrom langchain_community.vectorstores import FAISSfrom langchain_openai import OpenAIEmbeddings, AzureOpenAIEmbeddings-from ..models import OpenAI, Ollama, AzureOpenAI+from ..models import OpenAI, Ollama, AzureOpenAI, HuggingFacefrom langchain_community.embeddings import OllamaEmbeddingsfrom .base_node import BaseNode@@ -92,6 +93,8 @@ class RAGNode(BaseNode):embeddings = AzureOpenAIEmbeddings()elif isinstance(embedding_model, Ollama):embeddings = OllamaEmbeddings(model=embedding_model.model)+ elif isinstance(embedding_model, Ollama):+ embeddings = HuggingFaceHubEmbeddings(embedding_model, HuggingFace)else:raise ValueError("Embedding Model missing or not supported")