Python scraper based on AI
Python ai-crawlerai-scrapingai-searchcrawlerdata-extractionfirecrawl-alternativelarge-language-modelllmmarkdownragscrapingscraping-pythonweb-crawlerweb-crawlersweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
removed unused files
VinciGit004e509e1f6646
diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.ymlindex 1ae95910..d1e30d26 100644--- a/.github/workflows/pylint.yml+++ b/.github/workflows/pylint.yml@@ -20,4 +20,8 @@ jobs:pip install pylintpip install -r requirements.txt- name: Analysing the code with pylint- run: pylint --disable=C0114,C0115,C0116 --exit-zero scrapegraphai/**/*.py scrapegraphai/*.py examples/*.py tests/*.py | python .github/workflows/evaluate_pylint_score.py+ run: |+ pylint --disable=C0114,C0115,C0116 --exit-zero scrapegraphai/**/*.py scrapegraphai/*.py examples/*.py tests/*.py+ - name: Process Pylint output+ run: |+ python .github/workflows/evaluate_pylint_score.pydiff --git a/scrapegraphai/__init__.py b/scrapegraphai/__init__.pyindex b6019e29..448d6511 100644--- a/scrapegraphai/__init__.py+++ b/scrapegraphai/__init__.py@@ -1,8 +1,3 @@"""__init__.py file for scrapegraphai folder"""-from .class_creator import create_class-from .class_generator import Generator-from .getter import _get_function-from .request import send_request-from .utils.convert_to_csv import convert_to_csvdiff --git a/scrapegraphai/class_creator.py b/scrapegraphai/class_creator.pydeleted file mode 100644index 7cc546b0..00000000--- a/scrapegraphai/class_creator.py+++ /dev/null@@ -1,32 +0,0 @@-"""-Module for creating a class-"""---def create_class(data_dict: dict):- """- This function creates a class at runtime using the values from the list.-- Args:- data_dict (dict): A dictionary containing the description of the prompt.- It should have the following keys:- - "title" (str): The title of the field.- - "type" (str): The type of the field.- - "description" (str): The description of the field.-- Returns:- None- """-- base_script = """-from langchain_core.pydantic_v1 import BaseModel, Field--class _Response(BaseModel):- """-- for elem in data_dict:- base_script = base_script + \- f" {elem['title']}: {elem['type']} = Field(description='{elem['description']}')\n"-- with open("./scrapegraphai/pydantic_class.py", "w", encoding="utf-8") as f:- f.write(base_script)diff --git a/scrapegraphai/class_generator.py b/scrapegraphai/class_generator.pydeleted file mode 100644index 99e787a1..00000000--- a/scrapegraphai/class_generator.py+++ /dev/null@@ -1,61 +0,0 @@-"""-Module for generating responses using language model-"""-from langchain_openai import ChatOpenAI-from langchain.prompts import PromptTemplate-from langchain.output_parsers import PydanticOutputParser-from .pydantic_class import _Response---class Generator:- """- Class to generate responses using language model- """-- def __init__(- self,- api_key: str,- temperature_param: float = 0.0,- model_name: str = "gpt-3.5-turbo"- ) -> None:- """- Initializes the Generator object.-- Args:- api_key (str): The API key for accessing the language model.- temperature_param (float): A parameter controlling the randomness of the language- model's output.- model_name (str): The name of the language model to be used (default: "gpt-3.5-turbo").- All the possible models are available at the- following link: https://platform.openai.com/docs/models-- Returns:- result_dict (dict): The result of the language model invocation, converted- to a dictionary.- """-- self.parser = PydanticOutputParser(pydantic_object=_Response)-- self.prompt = PromptTemplate(- template="Answer the user query.\n{format_instructions}\n{query}\n",- input_variables=["query"],- partial_variables={- "format_instructions": self.parser.get_format_instructions()},- )-- self.model = ChatOpenAI(openai_api_key=api_key, temperature=temperature_param,- model=model_name)-- self.chain = self.prompt | self.model | self.parser-- def invocation(self, query_info):- """- Invokes the language model to generate a response- """- try:- result = self.chain.invoke({"query": query_info})- result_dict = result.dict()- return result_dict- except Exception as e:- print(f"Error: {e}")- return Nonediff --git a/scrapegraphai/json_getter.py b/scrapegraphai/json_getter.pydeleted file mode 100644index eb3a3374..00000000--- a/scrapegraphai/json_getter.py+++ /dev/null@@ -1,75 +0,0 @@-"""-Module for creating the json schema-"""-from tqdm import tqdm-from langchain_openai import ChatOpenAI-from langchain.prompts import PromptTemplate-from langchain_core.output_parsers import JsonOutputParser-from .scrapegraphai.getter import _get_function-from .info.dictionaries import schema_example-from .token_calculator import truncate_text_tokens--EMBEDDING_ENCODING = 'cl100k_base'---def get_json(key: str, link: str, model_name: str,- encoding_name_chunk: str = EMBEDDING_ENCODING) -> str:- """- Function that creates a JSON schema given a link- Args:- key (str): openai key- link (str): link to analyze- model_name (str): The name of the openai language model to be used.- encoding_name_chunk (str): The name of the encoding- to be used (default: EMBEDDING_ENCODING).- Returns:- str: the HTML schema of the website- """- model = ChatOpenAI(temperature=0, openai_api_key=key)- parser = JsonOutputParser()-- html = _get_function(link)-- chunks = truncate_text_tokens(- html, model=model_name, encoding_name=encoding_name_chunk)-- progress_bar = tqdm(total=len(chunks), desc="Sending chunks")-- result = []-- for chunk in chunks:- prompt = PromptTemplate(- template=("You are a website scraper and you want to extract information "- "in a schema like the example provided. Write a dictionary where "- "the key is the section and the value is the type"- ".\n{format_instructions}\n{query}\n. Example: {example}"),- input_variables=["query"],- partial_variables={- "format_instructions": parser.get_format_instructions(),- "example": str(schema_example),- },- )-- chain = prompt | model | parser-- result.append(chain.invoke({"query": chunk}))-- progress_bar.update(1)-- progress_bar.close()-- if len(result) > 1:- prompt = PromptTemplate(- template=("You are a website scraper and you have to merge the given "- "schemas without repetitions.\n{format_instructions}}\n"- ". Example: {to_merge}"),- input_variables=["to_merge"],- partial_variables={"format_instructions": parser.get_format_instructions()- },- )-- chain = prompt | model | parser-- result = chain.invoke({"query": str(result)})-- return resultdiff --git a/scrapegraphai/pydantic_class.py b/scrapegraphai/pydantic_class.pydeleted file mode 100644index 795786ff..00000000--- a/scrapegraphai/pydantic_class.py+++ /dev/null@@ -1,8 +0,0 @@-"""-Pydantic module to send-"""-from langchain_core.pydantic_v1 import BaseModel, Field---class _Response(BaseModel):- title_news: str = Field(description='Give me the name of the news')diff --git a/scrapegraphai/request.py b/scrapegraphai/request.pydeleted file mode 100644index 246d2bf4..00000000--- a/scrapegraphai/request.py+++ /dev/null@@ -1,61 +0,0 @@-"""-Module for sending the request-"""-import time-from typing import List-from tqdm import tqdm-from .remover import remover-from .class_generator import Generator-from .class_creator import create_class-from .token_calculator import truncate_text_tokens--EMBEDDING_ENCODING = 'cl100k_base'--LAST_REQUEST_TIME = 0-REQUEST_INTERVAL = 20---def send_request(key: str, text: str, values: List[dict],- model: str, temperature: float = 0.0,- encoding_name: str = EMBEDDING_ENCODING) -> List[dict]:- """- Send a request to openai.- Args:- key (str): The API key for accessing the language model.- text (str): The input text to be processed.- values (List[dict]): Settings of the request.- Each element of the list should have the following keys:- - "title" (str): The title of the field.- - "type" (str): The type of the field.- - "description" (str): The description of the field.- model (str): The name of the openai language model to be used.- temperature (float): A parameter controlling the randomness of the language- model's output (default: 0).- encoding_name (str): The name of the encoding to be used (default: EMBEDDING_ENCODING).- Returns:- List[dict]: The result of the request to openai.- """-- global LAST_REQUEST_TIME-- res = []- create_class(values)- time.sleep(2) # TODO: implement asynchronous waiting-- messages = truncate_text_tokens(remover(text), model, encoding_name)- processed_messages = 0-- with tqdm(total=len(messages)) as pbar:- for message in messages:- current_time = time.time()- time_since_last_request = current_time - LAST_REQUEST_TIME- if time_since_last_request < REQUEST_INTERVAL:- time.sleep(REQUEST_INTERVAL - time_since_last_request)- generator_instance = Generator(key, temperature, model)-- res.append(generator_instance.invocation(message))- processed_messages += 1- pbar.update(1)-- LAST_REQUEST_TIME = time.time()- return res