Skip to content

Commit ae92939

Browse files
committed
refactoring for pylint score
1 parent 69fb6ce commit ae92939

12 files changed

+48
-34
lines changed

examples/custom_graph_example.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
)
4141

4242
# execute the graph
43-
inputs = {"user_input": "Give me the news", "url": "https://www.ansa.it/sito/notizie/topnews/index.shtml"}
43+
inputs = {"user_input": "Give me the news",
44+
"url": "https://www.ansa.it/sito/notizie/topnews/index.shtml"}
4445
result = graph.execute(inputs)
4546

4647
# get the answer from the result

examples/graph_builder_example.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
"""
2+
Example of graph builder
3+
"""
14
import os
25
from dotenv import load_dotenv
36
from scrapegraphai.builders import GraphBuilder
@@ -14,8 +17,8 @@
1417
}
1518

1619
# Example usage of GraphBuilder
17-
user_prompt = "Extract the news and generate a text summary with a voiceover."
18-
graph_builder = GraphBuilder(user_prompt, llm_config)
20+
USER_PROMPT = "Extract the news and generate a text summary with a voiceover."
21+
graph_builder = GraphBuilder(USER_PROMPT, llm_config)
1922
graph_json = graph_builder.build_graph()
2023

2124
# Convert the resulting JSON to Graphviz format

examples/smart_scraper_example.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
"model_name": "gpt-3.5-turbo",
1616
}
1717

18-
# Define URL and prompt
19-
url = "https://perinim.github.io/projects/"
20-
prompt = "List me all the titles and project descriptions"
18+
# Define URL and PROMPT
19+
URL = "https://perinim.github.io/projects/"
20+
PROMPT = "List me all the titles and project descriptions"
2121

2222
# Create the SmartScraperGraph instance
23-
smart_scraper_graph = SmartScraperGraph(prompt, url, llm_config)
23+
smart_scraper_graph = SmartScraperGraph(PROMPT, URL, llm_config)
2424

2525
answer = smart_scraper_graph.run()
2626
print(answer)

examples/speech_summary_graph_example.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
curr_dir = os.path.dirname(os.path.realpath(__file__))
1919
output_file_path = os.path.join(curr_dir, "website_summary.mp3")
2020

21-
speech_summary_graph = SpeechSummaryGraph("Make a summary of the webpage to be converted to audio for blind people.",
22-
"https://perinim.github.io/projects/", llm_config,
23-
output_file_path)
21+
speech_summary_graph = SpeechSummaryGraph("""Make a summary of the webpage to be
22+
converted to audio for blind people.""",
23+
"https://perinim.github.io/projects/", llm_config,
24+
output_file_path)
2425

2526
final_state = speech_summary_graph.run()
2627
print(final_state.get("answer", "No answer found."))

scrapegraphai/builders/graph_builder.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def _generate_nodes_description(self):
8181
"""
8282

8383
return "\n".join([
84-
f'- {node}: {data["description"]} (Type: {data["type"]}, Args: {", ".join(data["args"].keys())})'
84+
f"""- {node}: {data["description"]} (Type: {data["type"]},
85+
Args: {", ".join(data["args"].keys())})"""
8586
for node, data in nodes_metadata.items()
8687
])
8788

scrapegraphai/helpers/nodes_metadata.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@
3333
"description": """A node responsible for reducing the amount of text to be processed
3434
by identifying and retrieving the most relevant chunks of text based on the user's query.
3535
Utilizes RecursiveCharacterTextSplitter for chunking, Html2TextTransformer for HTML to text
36-
conversion, and a combination of FAISS and OpenAIEmbeddings for efficient information retrieval.""",
36+
conversion, and a combination of FAISS and OpenAIEmbeddings
37+
for efficient information retrieval.""",
3738
"type": "node",
3839
"args": {
3940
"user_input": "The user's query or question guiding the retrieval.",
4041
"document": "The HTML content to be processed and compressed."
4142
},
42-
"returns": "Updated state with 'relevant_chunks' key containing the most relevant text chunks."
43+
"returns": """Updated state with 'relevant_chunks' key containing
44+
the most relevant text chunks."""
4345
},
4446
"GenerateAnswerNode": {
4547
"description": "Generates an answer based on the user's input and parsed document.",

scrapegraphai/helpers/schemas.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@
4646
"items": {
4747
"type": "string"
4848
},
49-
"description": """An array containing the node_names of the ending nodes of the edge.
50-
If the 'from' node is a conditional node, this array must contain exactly two node_names."""
49+
"description": """An array containing the node_names
50+
of the ending nodes of the edge.
51+
If the 'from' node is a conditional node,
52+
this array must contain exactly two node_names."""
5153
}
5254
},
5355
"required": ["from", "to"]

scrapegraphai/models/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
from .openai import OpenAI
66
from .openai_itt import OpenAIImageToText
7-
from .openai_tts import OpenAITextToSpeech
7+
from .openai_tts import OpenAITextToSpeech

scrapegraphai/nodes/fetch_html_node.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
22
Module for fetching the HTML node
33
"""
4-
5-
from .base_node import BaseNode
64
from langchain_community.document_loaders import AsyncHtmlLoader
5+
from .base_node import BaseNode
6+
77

88
class FetchHTMLNode(BaseNode):
99
"""

scrapegraphai/nodes/generate_answer_node.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ def execute(self, state: dict) -> dict:
8686
Content of {chunk_id}: {context}
8787
Question: {question}
8888
"""
89-
9089
template_merge = """You are a website scraper and you have just scraped the
9190
following content from a website.
9291
You are now asked to answer a question about the content you have scraped.\n {format_instructions} \n
@@ -101,14 +100,15 @@ def execute(self, state: dict) -> dict:
101100
prompt = PromptTemplate(
102101
template=template_chunks,
103102
input_variables=["question"],
104-
partial_variables={"context": chunk.page_content, "chunk_id": i + 1, "format_instructions": format_instructions},
103+
partial_variables={"context": chunk.page_content,
104+
"chunk_id": i + 1, "format_instructions": format_instructions},
105105
)
106106
# Dynamically name the chains based on their index
107107
chain_name = f"chunk{i+1}"
108108
chains_dict[chain_name] = prompt | self.llm | output_parser
109109

110110
# Use dictionary unpacking to pass the dynamically named chains to RunnableParallel
111-
map_chain = RunnableParallel(**chains_dict)
111+
map_chain = RunnableParallel(**chains_dict)
112112
# Chain
113113
answer_map = map_chain.invoke({"question": user_input})
114114

scrapegraphai/nodes/rag_node.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
"""
44

55
from langchain.text_splitter import RecursiveCharacterTextSplitter
6-
from langchain_community.document_transformers import Html2TextTransformer
76
from langchain.docstore.document import Document
7+
from langchain.retrievers import ContextualCompressionRetriever
8+
from langchain.retrievers.document_compressors import EmbeddingsFilter, DocumentCompressorPipeline
9+
from langchain_community.document_transformers import Html2TextTransformer, EmbeddingsRedundantFilter
810
from langchain_community.vectorstores import FAISS
911
from langchain_openai import OpenAIEmbeddings
10-
from langchain.retrievers import ContextualCompressionRetriever
11-
from langchain.retrievers.document_compressors import EmbeddingsFilter
12-
from langchain.retrievers.document_compressors import DocumentCompressorPipeline
13-
from langchain_community.document_transformers import EmbeddingsRedundantFilter
12+
1413

1514
from .base_node import BaseNode
1615

@@ -77,7 +76,8 @@ def execute(self, state):
7776
chunk_overlap=0,
7877
)
7978

80-
docs_transformed = Html2TextTransformer().transform_documents(document)[0]
79+
docs_transformed = Html2TextTransformer(
80+
).transform_documents(document)[0]
8181

8282
chunks = text_splitter.split_text(docs_transformed.page_content)
8383
chunked_docs = []
@@ -90,12 +90,15 @@ def execute(self, state):
9090
},
9191
)
9292
chunked_docs.append(doc)
93-
93+
9494
openai_key = self.llm.openai_api_key
95-
retriever = FAISS.from_documents(chunked_docs, OpenAIEmbeddings(api_key=openai_key)).as_retriever()
96-
embeddings = OpenAIEmbeddings(api_key=openai_key) # could be any embedding of your choice
95+
retriever = FAISS.from_documents(chunked_docs,
96+
OpenAIEmbeddings(api_key=openai_key)).as_retriever()
97+
# could be any embedding of your choice
98+
embeddings = OpenAIEmbeddings(api_key=openai_key)
9799
redundant_filter = EmbeddingsRedundantFilter(embeddings=embeddings)
98-
relevant_filter = EmbeddingsFilter(embeddings=embeddings) # similarity_threshold could be set, now k=20
100+
# similarity_threshold could be set, now k=20
101+
relevant_filter = EmbeddingsFilter(embeddings=embeddings)
99102
pipeline_compressor = DocumentCompressorPipeline(
100103
transformers=[redundant_filter, relevant_filter]
101104
)
@@ -104,7 +107,8 @@ def execute(self, state):
104107
base_compressor=pipeline_compressor, base_retriever=retriever
105108
)
106109

107-
compressed_docs = compression_retriever.get_relevant_documents(user_input)
110+
compressed_docs = compression_retriever.get_relevant_documents(
111+
user_input)
108112
print("Documents compressed and stored in a vector database.")
109113
state.update({"relevant_chunks": compressed_docs})
110114
return state

scrapegraphai/nodes/text_to_speech_node.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from .base_node import BaseNode
77

8+
89
class TextToSpeechNode(BaseNode):
910
"""
1011
A class representing a node that processes text and returns the voice.
@@ -23,7 +24,7 @@ def __init__(self, llm, node_name: str = "TextToSpeechNode"):
2324
super().__init__(node_name, "node")
2425
self.llm = llm
2526

26-
def execute(self, state: dict, text: str | None = None) -> dict:
27+
def execute(self, state: dict) -> dict:
2728
"""
2829
Execute the node's logic and return the updated state.
2930
Args:
@@ -36,7 +37,6 @@ def execute(self, state: dict, text: str | None = None) -> dict:
3637
text2translate = state.get("answer", None)
3738
if not text2translate:
3839
raise ValueError("No text to translate to speech.")
39-
4040
print("---TRANSLATING TEXT TO SPEECH---")
4141
audio = self.llm.run(text2translate["summary"])
4242

0 commit comments

Comments
 (0)