Skip to content

Python scraper based on AI

add hugginface integration

VinciGit0053e317d46b64
diff --git a/scrapegraphai/graphs/abstract_graph.py b/scrapegraphai/graphs/abstract_graph.py
index 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, abstractmethod
from typing import Optional
-from ..models import OpenAI, Gemini, Ollama, AzureOpenAI
+from ..models import OpenAI, Gemini, Ollama, AzureOpenAI, HuggingFace
from ..helpers import models_tokens
@@ -46,7 +46,7 @@ class AbstractGraph(ABC):
# take the model after the last dash
llm_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")
@abstractmethod
def _create_graph(self):
diff --git a/scrapegraphai/models/__init__.py b/scrapegraphai/models/__init__.py
index 8d85cd60..b96de107 100644
--- a/scrapegraphai/models/__init__.py
+++ b/scrapegraphai/models/__init__.py
@@ -8,3 +8,4 @@ from .openai_itt import OpenAIImageToText
from .openai_tts import OpenAITextToSpeech
from .gemini import Gemini
from .ollama import Ollama
+from .hugging_face import HuggingFace
diff --git a/scrapegraphai/models/hugging_face.py b/scrapegraphai/models/hugging_face.py
new file mode 100644
index 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.py
index 37fb4a22..36b36a34 100644
--- a/scrapegraphai/nodes/rag_node.py
+++ b/scrapegraphai/nodes/rag_node.py
@@ -7,9 +7,10 @@ from langchain.docstore.document import Document
from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import EmbeddingsFilter, DocumentCompressorPipeline
from langchain_community.document_transformers import EmbeddingsRedundantFilter
+from langchain_community.embeddings import HuggingFaceHubEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings, AzureOpenAIEmbeddings
-from ..models import OpenAI, Ollama, AzureOpenAI
+from ..models import OpenAI, Ollama, AzureOpenAI, HuggingFace
from langchain_community.embeddings import OllamaEmbeddings
from .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")