From da06379e2a2180b1abcdca7a6f726edb82b65d3c Mon Sep 17 00:00:00 2001 From: Wyatt Stanke Date: Sun, 18 Feb 2024 17:44:44 -0500 Subject: [PATCH] Flake8 done! --- example.py | 2 +- swarms/__init__.py | 4 ---- swarms/artifacts/text_artifact.py | 9 ++++--- swarms/chunkers/base_chunker.py | 6 ++--- swarms/loaders/pdf_loader.py | 2 +- swarms/memory/dict_internal_memory.py | 6 +++-- swarms/memory/dict_shared_memory.py | 6 +++-- swarms/memory/lanchain_chroma.py | 3 ++- swarms/memory/pinecone.py | 17 +++++++++++-- swarms/models/__init__.py | 10 ++++---- swarms/models/base_multimodal_model.py | 24 +++++++++++++------ swarms/models/biogpt.py | 18 +++++++++++--- swarms/models/cog_agent.py | 2 +- swarms/models/dalle3.py | 6 ++--- swarms/models/distilled_whisperx.py | 4 ++-- swarms/models/eleven_labs.py | 3 ++- swarms/models/gemini.py | 8 +++---- swarms/models/gpt4_sam.py | 3 ++- swarms/models/gpt4_vision_api.py | 5 ++-- swarms/models/idefics.py | 3 ++- swarms/models/kosmos_two.py | 2 +- swarms/models/medical_sam.py | 3 ++- swarms/models/openai_embeddings.py | 8 +++---- swarms/models/openai_models.py | 5 ++-- swarms/models/sam.py | 3 ++- swarms/models/speecht5.py | 29 ++++++++++++++++------- swarms/models/ssd_1b.py | 6 ++--- swarms/models/vip_llava.py | 2 +- swarms/prompts/debate.py | 2 +- swarms/prompts/schema_generator.py | 2 +- swarms/prompts/tools.py | 2 +- swarms/prompts/worker_prompt.py | 2 +- swarms/structs/agent.py | 6 ++--- swarms/structs/async_workflow.py | 3 ++- swarms/structs/base_swarm.py | 5 ---- swarms/structs/base_workflow.py | 15 +++++++++++- swarms/structs/concurrent_workflow.py | 3 ++- swarms/structs/debate.py | 17 +++++++++---- swarms/structs/graph_workflow.py | 3 ++- swarms/structs/majority_voting.py | 3 ++- swarms/structs/model_parallizer.py | 4 ++-- swarms/structs/multi_threaded_workflow.py | 2 +- swarms/tools/format_tools.py | 4 ++-- swarms/tools/logits_processor.py | 2 +- swarms/tools/tool_func_doc_scraper.py | 3 ++- swarms/tools/tool_utils.py | 10 ++++---- swarms/utils/apa.py | 4 ++-- swarms/utils/disable_logging.py | 4 ---- swarms/utils/load_model_torch.py | 3 ++- swarms/utils/main.py | 4 ++-- 50 files changed, 189 insertions(+), 113 deletions(-) diff --git a/example.py b/example.py index d9ba8f1c..5e0c0a5e 100644 --- a/example.py +++ b/example.py @@ -1,6 +1,6 @@ from swarms import Agent, OpenAIChat -## Initialize the workflow +# Initialize the workflow agent = Agent( llm=OpenAIChat(), max_loops="auto", diff --git a/swarms/__init__.py b/swarms/__init__.py index bece68ef..5eb82aef 100644 --- a/swarms/__init__.py +++ b/swarms/__init__.py @@ -14,8 +14,4 @@ from swarms.tokenizers import * # noqa: E402, F403 from swarms.loaders import * # noqa: E402, F403 from swarms.artifacts import * # noqa: E402, F403 from swarms.chunkers import * # noqa: E402, F403 -from swarms.structs import * # noqa: E402, F403 -from swarms.agents import * # noqa: E402, F403 -from swarms.models import * # noqa: E402, F403 from swarms.tools import * # noqa: E402, F403 -from swarms.telemetry import * # noqa: E402, F403 diff --git a/swarms/artifacts/text_artifact.py b/swarms/artifacts/text_artifact.py index e800ad51..82878281 100644 --- a/swarms/artifacts/text_artifact.py +++ b/swarms/artifacts/text_artifact.py @@ -22,9 +22,12 @@ class TextArtifact(BaseArtifact): Methods: __add__(self, other: BaseArtifact) -> TextArtifact: Concatenates the text value of the artifact with another artifact. __bool__(self) -> bool: Checks if the text value of the artifact is non-empty. - generate_embedding(self, driver: BaseEmbeddingModel) -> Optional[list[float]]: Generates the embedding of the text artifact using a given embedding model. - token_count(self, tokenizer: BaseTokenizer) -> int: Counts the number of tokens in the text artifact using a given tokenizer. - to_bytes(self) -> bytes: Converts the text value of the artifact to bytes using the specified encoding and error handler. + generate_embedding(self, driver: BaseEmbeddingModel) -> Optional[list[float]]: Generates the embedding of the text + artifact using a given embedding model. + token_count(self, tokenizer: BaseTokenizer) -> int: Counts the number of tokens in the text artifact + using a given tokenizer. + to_bytes(self) -> bytes: Converts the text value of the artifact to bytes + using the specified encoding and error handler. """ value: str diff --git a/swarms/chunkers/base_chunker.py b/swarms/chunkers/base_chunker.py index a63b3ac7..d12706ba 100644 --- a/swarms/chunkers/base_chunker.py +++ b/swarms/chunkers/base_chunker.py @@ -72,7 +72,7 @@ class BaseChunker(ABC): # If a separator is provided, only use separators after it. if current_separator: separators = self.separators[ - self.separators.index(current_separator) : + self.separators.index(current_separator): ] else: separators = self.separators @@ -120,7 +120,7 @@ class BaseChunker(ABC): second_subchunk = ( separator.value + separator.value.join( - subchunks[balance_index + 1 :] + subchunks[balance_index + 1:] ) ) else: @@ -132,7 +132,7 @@ class BaseChunker(ABC): + separator.value ) second_subchunk = separator.value.join( - subchunks[balance_index + 1 :] + subchunks[balance_index + 1:] ) # Continue recursively chunking the subchunks. diff --git a/swarms/loaders/pdf_loader.py b/swarms/loaders/pdf_loader.py index f3db1448..ff54ece8 100644 --- a/swarms/loaders/pdf_loader.py +++ b/swarms/loaders/pdf_loader.py @@ -43,7 +43,7 @@ class PDFLoader: max_tokens: int def __post_init__(self): - self.chunker = PdfChunker( + self.chunker = PdfChunker( # noqa: F821 tokenizer=self.tokenizer, max_tokens=self.max_tokens ) diff --git a/swarms/memory/dict_internal_memory.py b/swarms/memory/dict_internal_memory.py index daba0b0d..ad57463b 100644 --- a/swarms/memory/dict_internal_memory.py +++ b/swarms/memory/dict_internal_memory.py @@ -7,7 +7,8 @@ class InternalMemoryBase(ABC): """Abstract base class for internal memory of agents in the swarm.""" def __init__(self, n_entries): - """Initialize the internal memory. In the current architecture the memory always consists of a set of soltuions or evaluations. + """Initialize the internal memory. In the current architecture the memory always consists of a set of soltuions or + evaluations. During the operation, the agent should retrivie best solutions from it's internal memory based on the score. Moreover, the project is designed around LLMs for the proof of concepts, so we treat all entry content as a string. @@ -28,7 +29,8 @@ class InternalMemoryBase(ABC): class DictInternalMemory(InternalMemoryBase): def __init__(self, n_entries: int): """ - Initialize the internal memory. In the current architecture the memory always consists of a set of solutions or evaluations. + Initialize the internal memory. In the current architecture the memory always consists of a set of solutions or + evaluations. Simple key-value store for now. Args: diff --git a/swarms/memory/dict_shared_memory.py b/swarms/memory/dict_shared_memory.py index a8b78be7..4f6b56ec 100644 --- a/swarms/memory/dict_shared_memory.py +++ b/swarms/memory/dict_shared_memory.py @@ -16,13 +16,15 @@ class DictSharedMemory: Methods: __init__(self, file_loc: str = None) -> None: Initializes the shared memory. - add_entry(self, score: float, agent_id: str, agent_cycle: int, entry: Any) -> bool: Adds an entry to the internal memory. + add_entry(self, score: float, agent_id: str, agent_cycle: int, entry: Any) -> bool: Adds an entry to the internal + memory. get_top_n(self, n: int) -> None: Gets the top n entries from the internal memory. write_to_file(self, data: Dict[str, Dict[str, Any]]) -> bool: Writes the internal memory to a file. """ def __init__(self, file_loc: str = None) -> None: - """Initialize the shared memory. In the current architecture the memory always consists of a set of soltuions or evaluations. + """Initialize the shared memory. In the current architecture the memory always consists of a set of soltuions + or evaluations. Moreover, the project is designed around LLMs for the proof of concepts, so we treat all entry content as a string. """ if file_loc is not None: diff --git a/swarms/memory/lanchain_chroma.py b/swarms/memory/lanchain_chroma.py index e830ec4d..d213026b 100644 --- a/swarms/memory/lanchain_chroma.py +++ b/swarms/memory/lanchain_chroma.py @@ -152,7 +152,8 @@ class LangchainChromaVectorMemory: query (str): The query to search for. k (int): The number of results to return. type (str): The type of search to perform: "cos" or "mmr". - distance_threshold (float): The similarity threshold to use for the search. Results with distance > similarity_threshold will be dropped. + distance_threshold (float): + The similarity threshold to use for the search. Results with distance > similarity_threshold will be dropped. Returns: list[str]: A list of the top k results. diff --git a/swarms/memory/pinecone.py b/swarms/memory/pinecone.py index b1c0edf3..c10e7217 100644 --- a/swarms/memory/pinecone.py +++ b/swarms/memory/pinecone.py @@ -22,13 +22,26 @@ class PineconeDB(AbstractVectorDatabase): index (pinecone.Index, optional): The Pinecone index to use. Defaults to None. Methods: - upsert_vector(vector: list[float], vector_id: Optional[str] = None, namespace: Optional[str] = None, meta: Optional[dict] = None, **kwargs) -> str: + upsert_vector( + vector: list[float], + vector_id: Optional[str] = None, + namespace: Optional[str] = None, + meta: Optional[dict] = None, + **kwargs + ) -> str: Upserts a vector into the index. load_entry(vector_id: str, namespace: Optional[str] = None) -> Optional[BaseVectorStore.Entry]: Loads a single vector from the index. load_entries(namespace: Optional[str] = None) -> list[BaseVectorStore.Entry]: Loads all vectors from the index. - query(query: str, count: Optional[int] = None, namespace: Optional[str] = None, include_vectors: bool = False, include_metadata=True, **kwargs) -> list[BaseVectorStore.QueryResult]: + query( + query: str, + count: Optional[int] = None, + namespace: Optional[str] = None, + include_vectors: bool = False, + include_metadata: bool = True, + **kwargs + ) -> list[BaseVectorStore.QueryResult]: Queries the index for vectors similar to the given query string. create_index(name: str, **kwargs) -> None: Creates a new index. diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index 00d9d1f2..eeaa6d00 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -1,4 +1,4 @@ -############################################ LLMs +# LLMs from swarms.models.base_llm import AbstractLLM # noqa: E402 from swarms.models.anthropic import Anthropic # noqa: E402 from swarms.models.petals import Petals # noqa: E402 @@ -25,7 +25,7 @@ from swarms.models.mixtral import Mixtral # noqa: E402 # ) # noqa: E402 from swarms.models.together import TogetherLLM # noqa: E402 -################# MultiModal Models +# MultiModal Models from swarms.models.base_multimodal_model import ( BaseMultiModalModel, ) # noqa: E402 @@ -61,10 +61,10 @@ from swarms.models.sam_supervision import SegmentAnythingMarkGenerator # from swarms.models.cog_agent import CogAgent # noqa: E402 -################# Tokenizers +# Tokenizers -############## Types +# Types from swarms.models.types import ( TextModality, ImageModality, @@ -77,7 +77,7 @@ from swarms.models.types import ( from swarms.models.base_embedding_model import BaseEmbeddingModel -##### Utils +# Utils from swarms.models.sampling_params import ( SamplingType, SamplingParams, diff --git a/swarms/models/base_multimodal_model.py b/swarms/models/base_multimodal_model.py index c4a5890a..ff122290 100644 --- a/swarms/models/base_multimodal_model.py +++ b/swarms/models/base_multimodal_model.py @@ -39,15 +39,25 @@ class BaseMultiModalModel: Examples: >>> from swarms.models.base_multimodal_model import BaseMultiModalModel + >>> image = ""https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" >>> model = BaseMultiModalModel() >>> model.run("Generate a summary of this text") - >>> model.run("Generate a summary of this text", "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png") + >>> model.run("Generate a summary of this text", image) >>> model.run_batch(["Generate a summary of this text", "Generate a summary of this text"]) - >>> model.run_batch([("Generate a summary of this text", "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"), ("Generate a summary of this text", "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png")]) + >>> model.run_batch([ + ("Generate a summary of this text", image), + ("Generate a summary of this text", image) + ]) >>> model.run_batch_async(["Generate a summary of this text", "Generate a summary of this text"]) - >>> model.run_batch_async([("Generate a summary of this text", "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"), ("Generate a summary of this text", "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png")]) + >>> model.run_batch_async([ + ("Generate a summary of this text", image), + ("Generate a summary of this text", image) + ]) >>> model.run_batch_async_with_retries(["Generate a summary of this text", "Generate a summary of this text"]) - >>> model.run_batch_async_with_retries([("Generate a summary of this text", "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"), ("Generate a summary of this text", "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png")]) + >>> model.run_batch_async_with_retries([ + ("Generate a summary of this text", image), + ("Generate a summary of this text", image) + ]) >>> model.generate_summary("Generate a summary of this text") >>> model.set_temperature(0.5) >>> model.set_max_tokens(500) @@ -351,9 +361,9 @@ class BaseMultiModalModel: _type_: _description_ """ META_PROMPT = """ - For any labels or markings on an image that you reference in your response, please - enclose them in square brackets ([]) and list them explicitly. Do not use ranges; for - example, instead of '1 - 4', list as '[1], [2], [3], [4]'. These labels could be + For any labels or markings on an image that you reference in your response, please + enclose them in square brackets ([]) and list them explicitly. Do not use ranges; for + example, instead of '1 - 4', list as '[1], [2], [3], [4]'. These labels could be numbers or letters and typically correspond to specific segments or parts of the image. """ return META_PROMPT diff --git a/swarms/models/biogpt.py b/swarms/models/biogpt.py index 9ee5b513..45c93097 100644 --- a/swarms/models/biogpt.py +++ b/swarms/models/biogpt.py @@ -1,8 +1,9 @@ -""" +r""" BioGPT Pre-trained language models have attracted increasing attention in the biomedical domain, inspired by their great success in the general natural language domain. -Among the two main branches of pre-trained language models in the general language domain, i.e. BERT (and its variants) and GPT (and its variants), +Among the two main branches of pre-trained language models in the general language domain, +i.e. BERT (and its variants) and GPT (and its variants), the first one has been extensively studied in the biomedical domain, such as BioBERT and PubMedBERT. While they have achieved great success on a variety of discriminative downstream biomedical tasks, the lack of generation ability constrains their application scope. @@ -24,7 +25,18 @@ advantage of BioGPT on biomedical literature to generate fluent descriptions for number = {6}, year = {2022}, month = {09}, - abstract = "{Pre-trained language models have attracted increasing attention in the biomedical domain, inspired by their great success in the general natural language domain. Among the two main branches of pre-trained language models in the general language domain, i.e. BERT (and its variants) and GPT (and its variants), the first one has been extensively studied in the biomedical domain, such as BioBERT and PubMedBERT. While they have achieved great success on a variety of discriminative downstream biomedical tasks, the lack of generation ability constrains their application scope. In this paper, we propose BioGPT, a domain-specific generative Transformer language model pre-trained on large-scale biomedical literature. We evaluate BioGPT on six biomedical natural language processing tasks and demonstrate that our model outperforms previous models on most tasks. Especially, we get 44.98\%, 38.42\% and 40.76\% F1 score on BC5CDR, KD-DTI and DDI end-to-end relation extraction tasks, respectively, and 78.2\% accuracy on PubMedQA, creating a new record. Our case study on text generation further demonstrates the advantage of BioGPT on biomedical literature to generate fluent descriptions for biomedical terms.}", + abstract = "{Pre-trained language models have attracted increasing attention in the biomedical domain, + inspired by their great success in the general natural language domain. + Among the two main branches of pre-trained language models in the general language domain, + i.e. BERT (and its variants) and GPT (and its variants), the first one has been extensively studied in the + biomedical domain, such as BioBERT and PubMedBERT. While they have achieved great success on a variety of discriminative + downstream biomedical tasks, the lack of generation ability constrains their application scope. In this paper, we propose + BioGPT, a domain-specific generative Transformer language model pre-trained on large-scale biomedical literature. + We evaluate BioGPT on six biomedical natural language processing tasks and demonstrate that our model outperforms previous + models on most tasks. Especially, we get 44.98\%, 38.42\% and 40.76\% F1 score on BC5CDR, KD-DTI and DDI end-to-end + relation extraction tasks, respectively, and 78.2\% accuracy on PubMedQA, creating a new record. Our case study on text + generation further demonstrates the advantage of BioGPT on biomedical literature to generate fluent descriptions for + biomedical terms.}", issn = {1477-4054}, doi = {10.1093/bib/bbac409}, url = {https://doi.org/10.1093/bib/bbac409}, diff --git a/swarms/models/cog_agent.py b/swarms/models/cog_agent.py index 2d0d09e9..d4e74251 100644 --- a/swarms/models/cog_agent.py +++ b/swarms/models/cog_agent.py @@ -123,7 +123,7 @@ class CogAgent(BaseMultiModalModel): with torch.no_grad(): outputs = self.model(**inputs, **kwargs) - outputs = outputs[:, inputs["input_ids"].shape[1] :] + outputs = outputs[:, inputs["input_ids"].shape[1]:] response = self.decode(outputs[0]) response = response.split("")[0] print(response) diff --git a/swarms/models/dalle3.py b/swarms/models/dalle3.py index 6b225b49..381a58f6 100644 --- a/swarms/models/dalle3.py +++ b/swarms/models/dalle3.py @@ -257,7 +257,7 @@ class Dalle3: """Print the Dalle3 dashboard""" print( colored( - f"""Dalle3 Dashboard: + f"""Dalle3 Dashboard: -------------------- Model: {self.model} @@ -271,8 +271,8 @@ class Dalle3: Save Folder: {self.save_folder} Image Format: {self.image_format} -------------------- - - + + """, "green", ) diff --git a/swarms/models/distilled_whisperx.py b/swarms/models/distilled_whisperx.py index 951dcd10..43a34dc3 100644 --- a/swarms/models/distilled_whisperx.py +++ b/swarms/models/distilled_whisperx.py @@ -139,7 +139,7 @@ class DistilWhisperModel: len(audio_input.array) / sample_rate chunks = [ audio_input.array[ - i : i + sample_rate * chunk_duration + i: i + sample_rate * chunk_duration ] for i in range( 0, @@ -175,7 +175,7 @@ class DistilWhisperModel: # Print the chunk's transcription print( colored( - f"Chunk {i+1}/{len(chunks)}: ", "yellow" + f"Chunk {i + 1}/{len(chunks)}: ", "yellow" ) + transcription ) diff --git a/swarms/models/eleven_labs.py b/swarms/models/eleven_labs.py index 2d55e864..7d6cf7c6 100644 --- a/swarms/models/eleven_labs.py +++ b/swarms/models/eleven_labs.py @@ -37,7 +37,8 @@ class ElevenLabsText2SpeechTool(BaseTool): Defaults to ElevenLabsModel.MULTI_LINGUAL. name (str): The name of the tool. Defaults to "eleven_labs_text2speech". description (str): The description of the tool. - Defaults to "A wrapper around Eleven Labs Text2Speech. Useful for when you need to convert text to speech. It supports multiple languages, including English, German, Polish, Spanish, Italian, French, Portuguese, and Hindi." + Defaults to "A wrapper around Eleven Labs Text2Speech. Useful for when you need to convert text to speech. It + supports multiple languages, including English, German, Polish, Spanish, Italian, French, Portuguese, and Hindi." Usage: diff --git a/swarms/models/gemini.py b/swarms/models/gemini.py index d12ea7d9..43d2a03a 100644 --- a/swarms/models/gemini.py +++ b/swarms/models/gemini.py @@ -133,13 +133,13 @@ class Gemini(BaseMultiModalModel): system_prompt (str, optional): _description_. Defaults to None. """ PROMPT = f""" - + {self.system_prompt} - + ###### - + {task} - + """ return PROMPT diff --git a/swarms/models/gpt4_sam.py b/swarms/models/gpt4_sam.py index aef0181f..46eb6624 100644 --- a/swarms/models/gpt4_sam.py +++ b/swarms/models/gpt4_sam.py @@ -10,7 +10,8 @@ from typing import Any class GPT4VSAM(BaseMultiModalModel): """ GPT4VSAM class represents a multi-modal model that combines the capabilities of GPT-4 and SegmentAnythingMarkGenerator. - It takes an instance of BaseMultiModalModel (vlm) and a device as input and provides methods for loading images and making predictions. + It takes an instance of BaseMultiModalModel (vlm) and a device as input and provides + methods for loading images and making predictions. Args: vlm (BaseMultiModalModel): An instance of BaseMultiModalModel representing the visual language model. diff --git a/swarms/models/gpt4_vision_api.py b/swarms/models/gpt4_vision_api.py index 57553bb9..c49a7ecd 100644 --- a/swarms/models/gpt4_vision_api.py +++ b/swarms/models/gpt4_vision_api.py @@ -202,8 +202,9 @@ class GPT4VisionAPI(BaseMultiModalModel): """ PROMPT = f""" - These are frames from a video that I want to upload. Generate a compelling description that I can upload along with the video: - + These are frames from a video that I want to upload. + Generate a compelling description that I can upload along with the video: + {frames} """ return PROMPT diff --git a/swarms/models/idefics.py b/swarms/models/idefics.py index b014fbce..e7460c55 100644 --- a/swarms/models/idefics.py +++ b/swarms/models/idefics.py @@ -61,7 +61,8 @@ class Idefics(BaseMultiModalModel): response = model.chat(user_input) print(response) - user_input = "User: And who is that? https://static.wikia.nocookie.net/asterix/images/2/25/R22b.gif/revision/latest?cb=20110815073052" + user_input = "User: And who is that? \ +https://static.wikia.nocookie.net/asterix/images/2/25/R22b.gif/revision/latest?cb=20110815073052" response = model.chat(user_input) print(response) diff --git a/swarms/models/kosmos_two.py b/swarms/models/kosmos_two.py index 6bc4d810..b5dc41fa 100644 --- a/swarms/models/kosmos_two.py +++ b/swarms/models/kosmos_two.py @@ -323,5 +323,5 @@ class Kosmos(BaseMultiModalModel): def generate_boxees(self, task, image_url): image = self.get_image(image_url) - processed_text, entities = self.process_task(task, image) + _, entities = self.process_task(task, image) self.draw_entity_boxes_on_image(image, entities, show=True) diff --git a/swarms/models/medical_sam.py b/swarms/models/medical_sam.py index 8d096ba5..e22b8bf8 100644 --- a/swarms/models/medical_sam.py +++ b/swarms/models/medical_sam.py @@ -26,7 +26,8 @@ class MedicalSAM: Methods: __post_init__(): Initializes the MedicalSAM object. - download_model_weights(model_path: str): Downloads the model weights from the specified URL and saves them to the given file path. + download_model_weights(model_path: str): Downloads the model weights from the specified URL and + saves them to the given file path. preprocess(img): Preprocesses the input image. run(img, box): Runs the semantic segmentation on the input image within the specified bounding box. diff --git a/swarms/models/openai_embeddings.py b/swarms/models/openai_embeddings.py index 0cbbdbee..747cf751 100644 --- a/swarms/models/openai_embeddings.py +++ b/swarms/models/openai_embeddings.py @@ -386,7 +386,7 @@ class OpenAIEmbeddings(BaseModel, Embeddings): ) for j in range(0, len(token), self.embedding_ctx_length): tokens.append( - token[j : j + self.embedding_ctx_length] + token[j: j + self.embedding_ctx_length] ) indices.append(i) @@ -406,7 +406,7 @@ class OpenAIEmbeddings(BaseModel, Embeddings): for i in _iter: response = embed_with_retry( self, - input=tokens[i : i + _chunk_size], + input=tokens[i: i + _chunk_size], **self._invocation_params, ) batched_embeddings.extend( @@ -486,7 +486,7 @@ class OpenAIEmbeddings(BaseModel, Embeddings): ) for j in range(0, len(token), self.embedding_ctx_length): tokens.append( - token[j : j + self.embedding_ctx_length] + token[j: j + self.embedding_ctx_length] ) indices.append(i) @@ -495,7 +495,7 @@ class OpenAIEmbeddings(BaseModel, Embeddings): for i in range(0, len(tokens), _chunk_size): response = await async_embed_with_retry( self, - input=tokens[i : i + _chunk_size], + input=tokens[i: i + _chunk_size], **self._invocation_params, ) batched_embeddings.extend( diff --git a/swarms/models/openai_models.py b/swarms/models/openai_models.py index b1aa0117..461f3cd4 100644 --- a/swarms/models/openai_models.py +++ b/swarms/models/openai_models.py @@ -45,7 +45,6 @@ from tenacity import ( wait_exponential, ) -logger = logging.getLogger(__name__) from importlib.metadata import version @@ -610,7 +609,7 @@ class BaseOpenAI(BaseLLM): prompts[0] ) sub_prompts = [ - prompts[i : i + self.batch_size] + prompts[i: i + self.batch_size] for i in range(0, len(prompts), self.batch_size) ] return sub_prompts @@ -624,7 +623,7 @@ class BaseOpenAI(BaseLLM): """Create the LLMResult from the choices and prompts.""" generations = [] for i, _ in enumerate(prompts): - sub_choices = choices[i * self.n : (i + 1) * self.n] + sub_choices = choices[i * self.n: (i + 1) * self.n] generations.append( [ Generation( diff --git a/swarms/models/sam.py b/swarms/models/sam.py index 110d80b7..fd667770 100644 --- a/swarms/models/sam.py +++ b/swarms/models/sam.py @@ -26,7 +26,8 @@ class SAM: processor (SamProcessor): The processor for the SAM model. Methods: - run(task=None, img=None, *args, **kwargs): Runs the SAM model on the given image and returns the segmentation scores and masks. + run(task=None, img=None, *args, **kwargs): Runs the SAM model on the given image and returns the + segmentation scores and masks. process_img(img: str = None, *args, **kwargs): Processes the input image and returns the processed image. """ diff --git a/swarms/models/speecht5.py b/swarms/models/speecht5.py index cc6ef931..974fb8c3 100644 --- a/swarms/models/speecht5.py +++ b/swarms/models/speecht5.py @@ -2,18 +2,31 @@ SpeechT5 (TTS task) SpeechT5 model fine-tuned for speech synthesis (text-to-speech) on LibriTTS. -This model was introduced in SpeechT5: Unified-Modal Encoder-Decoder Pre-Training for Spoken Language Processing by Junyi Ao, Rui Wang, Long Zhou, Chengyi Wang, Shuo Ren, Yu Wu, Shujie Liu, Tom Ko, Qing Li, Yu Zhang, Zhihua Wei, Yao Qian, Jinyu Li, Furu Wei. +This model was introduced in SpeechT5: Unified-Modal Encoder-Decoder Pre-Training for Spoken Language Processing by +Junyi Ao, Rui Wang, Long Zhou, Chengyi Wang, Shuo Ren, Yu Wu, Shujie Liu, +Tom Ko, Qing Li, Yu Zhang, Zhihua Wei, Yao Qian, Jinyu Li, Furu Wei. SpeechT5 was first released in this repository, original weights. The license used is MIT. Model Description -Motivated by the success of T5 (Text-To-Text Transfer Transformer) in pre-trained natural language processing models, we propose a unified-modal SpeechT5 framework that explores the encoder-decoder pre-training for self-supervised speech/text representation learning. The SpeechT5 framework consists of a shared encoder-decoder network and six modal-specific (speech/text) pre/post-nets. After preprocessing the input speech/text through the pre-nets, the shared encoder-decoder network models the sequence-to-sequence transformation, and then the post-nets generate the output in the speech/text modality based on the output of the decoder. - -Leveraging large-scale unlabeled speech and text data, we pre-train SpeechT5 to learn a unified-modal representation, hoping to improve the modeling capability for both speech and text. To align the textual and speech information into this unified semantic space, we propose a cross-modal vector quantization approach that randomly mixes up speech/text states with latent units as the interface between encoder and decoder. - -Extensive evaluations show the superiority of the proposed SpeechT5 framework on a wide variety of spoken language processing tasks, including automatic speech recognition, speech synthesis, speech translation, voice conversion, speech enhancement, and speaker identification. - -Developed by: Junyi Ao, Rui Wang, Long Zhou, Chengyi Wang, Shuo Ren, Yu Wu, Shujie Liu, Tom Ko, Qing Li, Yu Zhang, Zhihua Wei, Yao Qian, Jinyu Li, Furu Wei. +Motivated by the success of T5 (Text-To-Text Transfer Transformer) in pre-trained natural language processing models, +we propose a unified-modal SpeechT5 framework that explores the encoder-decoder pre-training for self-supervised speech/text +representation learning. The SpeechT5 framework consists of a shared encoder-decoder network and six modal-specific +(speech/text) pre/post-nets. After preprocessing the input speech/text through the pre-nets, the shared encoder-decoder network +models the sequence-to-sequence transformation, and then the post-nets generate the output in the speech/text modality +based on the output of the decoder. + +Leveraging large-scale unlabeled speech and text data, we pre-train SpeechT5 to learn a unified-modal representation, +hoping to improve the modeling capability for both speech and text. To align the textual and speech information into this +unified semantic space, we propose a cross-modal vector quantization approach that randomly mixes up speech/text states with +latent units as the interface between encoder and decoder. + +Extensive evaluations show the superiority of the proposed SpeechT5 framework on a wide variety of spoken language processing +tasks, including automatic speech recognition, speech synthesis, speech translation, voice conversion, speech enhancement, and +speaker identification. + +Developed by: Junyi Ao, Rui Wang, Long Zhou, Chengyi Wang, Shuo Ren, Yu Wu, Shujie Liu, +Tom Ko, Qing Li, Yu Zhang, Zhihua Wei, Yao Qian, Jinyu Li, Furu Wei. Shared by [optional]: Matthijs Hollemans Model type: text-to-speech Language(s) (NLP): [More Information Needed] diff --git a/swarms/models/ssd_1b.py b/swarms/models/ssd_1b.py index d3b9086b..9edad0d7 100644 --- a/swarms/models/ssd_1b.py +++ b/swarms/models/ssd_1b.py @@ -171,7 +171,7 @@ class SSD1B: """Print the SSD1B dashboard""" print( colored( - f"""SSD1B Dashboard: + f"""SSD1B Dashboard: -------------------- Model: {self.model} @@ -185,8 +185,8 @@ class SSD1B: Save Folder: {self.save_folder} Image Format: {self.image_format} -------------------- - - + + """, "green", ) diff --git a/swarms/models/vip_llava.py b/swarms/models/vip_llava.py index db532913..4d52877e 100644 --- a/swarms/models/vip_llava.py +++ b/swarms/models/vip_llava.py @@ -83,7 +83,7 @@ class VipLlavaMultiModal(BaseMultiModalModel): ) return self.processor.decode( - generate_ids[0][len(inputs["input_ids"][0]) :], + generate_ids[0][len(inputs["input_ids"][0]):], skip_special_tokens=True, ) diff --git a/swarms/prompts/debate.py b/swarms/prompts/debate.py index a11c7af4..197cc618 100644 --- a/swarms/prompts/debate.py +++ b/swarms/prompts/debate.py @@ -31,7 +31,7 @@ def debate_monitor(game_description, word_limit, character_names): Frame the debate topic as a problem to be solved. Be creative and imaginative. Please reply with the specified topic in {word_limit} words or less. - Speak directly to the presidential candidates: {*character_names,}. + Speak directly to the presidential candidates: {*character_names, }. Do not add anything else. """ diff --git a/swarms/prompts/schema_generator.py b/swarms/prompts/schema_generator.py index 4213d0d6..4f20d80d 100644 --- a/swarms/prompts/schema_generator.py +++ b/swarms/prompts/schema_generator.py @@ -123,7 +123,7 @@ class SchemaGenerator: return "\n".join(command_strings + [finish_string]) else: return "\n".join( - f"{i+1}. {item}" for i, item in enumerate(items) + f"{i + 1}. {item}" for i, item in enumerate(items) ) def generate_prompt_string(self) -> str: diff --git a/swarms/prompts/tools.py b/swarms/prompts/tools.py index fe82ba5d..b0b8c5ed 100644 --- a/swarms/prompts/tools.py +++ b/swarms/prompts/tools.py @@ -34,7 +34,7 @@ commands: { """ -########### FEW SHOT EXAMPLES ################ +# FEW SHOT EXAMPLES SCENARIOS = """ commands: { "tools": { diff --git a/swarms/prompts/worker_prompt.py b/swarms/prompts/worker_prompt.py index 165fa058..abd0e3f4 100644 --- a/swarms/prompts/worker_prompt.py +++ b/swarms/prompts/worker_prompt.py @@ -62,6 +62,6 @@ def worker_tools_sop_promp(name: str, memory: str): [{memory}] Human: Determine which next command to use, and respond using the format specified above: - """.format(name=name, memory=memory, time=time) + """.format(name=name, memory=memory, time=time) # noqa: F521 return str(out) diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index 16109dab..de885fd8 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -403,7 +403,7 @@ class Agent: Name: {self.agent_name} Description: {self.agent_description} Standard Operating Procedure: {self.sop} - System Prompt: {self.system_prompt} + System Prompt: {self.system_prompt} Task: {task} Max Loops: {self.max_loops} Stopping Condition: {self.stopping_condition} @@ -706,7 +706,7 @@ class Agent: Follow this standard operating procedure (SOP) to complete tasks: {self.sop} - + ----------------- ################ CHAT HISTORY #################### {history} @@ -716,7 +716,7 @@ class Agent: system_prompt = self.system_prompt agent_history_prompt = f""" System : {system_prompt} - + {history} """ return agent_history_prompt diff --git a/swarms/structs/async_workflow.py b/swarms/structs/async_workflow.py index b46061b2..9f4a32ee 100644 --- a/swarms/structs/async_workflow.py +++ b/swarms/structs/async_workflow.py @@ -95,7 +95,8 @@ class AsyncWorkflow: # if self.dashboard: # self.display() - # Add a stopping condition to stop the workflow, if provided but stopping_condition takes in a parameter s for string + # Add a stopping condition to stop the workflow, + # if provided but stopping_condition takes in a parameter s for string if self.stopping_condition: if self.stopping_condition(self.results): break diff --git a/swarms/structs/base_swarm.py b/swarms/structs/base_swarm.py index a961be58..ba587988 100644 --- a/swarms/structs/base_swarm.py +++ b/swarms/structs/base_swarm.py @@ -91,11 +91,6 @@ class AbstractSwarm(ABC): """Step the swarm""" pass - # @abstractmethod - def add_agent(self, agent: "Agent"): - """Add a agent to the swarm""" - pass - # @abstractmethod def remove_agent(self, agent: "Agent"): """Remove a agent from the swarm""" diff --git a/swarms/structs/base_workflow.py b/swarms/structs/base_workflow.py index 03e503cc..6da0a0fb 100644 --- a/swarms/structs/base_workflow.py +++ b/swarms/structs/base_workflow.py @@ -182,7 +182,20 @@ class BaseWorkflow(BaseStructure): >>> workflow.add("Create a report on these metrics", llm) >>> workflow.delete_task("What's the weather in miami") >>> workflow.tasks - [Task(description='Create a report on these metrics', agent=Agent(llm=OpenAIChat(openai_api_key=''), max_loops=1, dashboard=False), args=[], kwargs={}, result=None, history=[])] + [ + Task( + description='Create a report on these metrics', + agent=Agent( + llm=OpenAIChat(openai_api_key=''), + max_loops=1, + dashboard=False + ), + args=[], + kwargs={}, + result=None, + history=[] + ) + ] """ try: for task in self.tasks: diff --git a/swarms/structs/concurrent_workflow.py b/swarms/structs/concurrent_workflow.py index 8aa5399b..0b34f62c 100644 --- a/swarms/structs/concurrent_workflow.py +++ b/swarms/structs/concurrent_workflow.py @@ -16,7 +16,8 @@ class ConcurrentWorkflow(BaseStructure): Args: max_workers (int): The maximum number of workers to use for the ThreadPoolExecutor. autosave (bool): Whether to save the state of the workflow to a file. Default is False. - saved_state_filepath (str): The filepath to save the state of the workflow to. Default is "runs/concurrent_workflow.json". + saved_state_filepath (str): The filepath to save the state of the workflow to. + Default is "runs/concurrent_workflow.json". print_results (bool): Whether to print the results of each task. Default is False. return_results (bool): Whether to return the results of each task. Default is False. use_processes (bool): Whether to use processes instead of threads. Default is False. diff --git a/swarms/structs/debate.py b/swarms/structs/debate.py index 9db84f06..0d02579a 100644 --- a/swarms/structs/debate.py +++ b/swarms/structs/debate.py @@ -19,7 +19,8 @@ class DebatePlayer(Agent): Args: model_name(str): model name name (str): name of this player - temperature (float): higher values make the output more random, while lower values make it more focused and deterministic + temperature (float): higher values make the output more random, + while lower values make it more focused and deterministic openai_api_key (str): As the parameter name suggests sleep_time (float): sleep because of rate limits """ @@ -33,7 +34,8 @@ class Debate: Args: model_name (str): openai model name - temperature (float): higher values make the output more random, while lower values make it more focused and deterministic + temperature (float): higher values make the output more random, + while lower values make it more focused and deterministic num_players (int): num of players save_file_dir (str): dir path to json file openai_api_key (str): As the parameter name suggests @@ -239,7 +241,7 @@ class Debate: if self.mod_ans["debate_translation"] != "": break else: - print(f"===== Debate Round-{round+2} =====\n") + print(f"===== Debate Round-{round + 2} =====\n") self.affirmative.add_message_to_memory( self.save_file["debate_prompt"].replace( "##oppo_ans##", self.neg_ans @@ -361,6 +363,13 @@ class Debate: # with open(prompts_path, 'w') as file: # json.dump(config, file, ensure_ascii=False, indent=4) -# debate = Debate(save_file_dir=save_file_dir, num_players=3, openai_api_key=openai_api_key, prompts_path=prompts_path, temperature=0, sleep_time=0) + # debate = Debate( + # save_file_dir=save_file_dir, + # num_players=3, + # openai_api_key=openai_api_key, + # prompts_path=prompts_path, + # temperature=0, + # sleep_time=0 + # ) # debate.run() # debate.save_file_to_json(id) diff --git a/swarms/structs/graph_workflow.py b/swarms/structs/graph_workflow.py index c4bcea7e..1bb776bb 100644 --- a/swarms/structs/graph_workflow.py +++ b/swarms/structs/graph_workflow.py @@ -18,7 +18,8 @@ class GraphWorkflow(BaseStructure): connect(from_node, to_node): Connects two nodes in the graph. set_entry_point(node_name): Sets the entry point node for the workflow. add_edge(from_node, to_node): Adds an edge between two nodes in the graph. - add_conditional_edges(from_node, condition, edge_dict): Adds conditional edges from a node to multiple nodes based on a condition. + add_conditional_edges(from_node, condition, edge_dict): Adds conditional edges from a node to multiple nodes + based on a condition. run(): Runs the workflow and returns the graph. Examples: diff --git a/swarms/structs/majority_voting.py b/swarms/structs/majority_voting.py index 05539ecf..cb7825ad 100644 --- a/swarms/structs/majority_voting.py +++ b/swarms/structs/majority_voting.py @@ -115,7 +115,8 @@ class MajorityVoting: multithreaded (bool, optional): Whether to run the agents using multithreading. Defaults to False. multiprocess (bool, optional): Whether to run the agents using multiprocessing. Defaults to False. asynchronous (bool, optional): Whether to run the agents asynchronously. Defaults to False. - output_parser (callable, optional): A callable function to parse the output of the majority voting system. Defaults to None. + output_parser (callable, optional): A callable function to parse the output of the majority voting system. + Defaults to None. Examples: >>> from swarms.structs.agent import Agent diff --git a/swarms/structs/model_parallizer.py b/swarms/structs/model_parallizer.py index 828d4ef4..2a1d75d3 100644 --- a/swarms/structs/model_parallizer.py +++ b/swarms/structs/model_parallizer.py @@ -88,7 +88,7 @@ class ModelParallelizer: """Save responses to file""" with open(filename, "w") as file: table = [ - [f"LLM {i+1}", response] + [f"LLM {i + 1}", response] for i, response in enumerate(self.last_responses) ] file.write(table) @@ -111,7 +111,7 @@ class ModelParallelizer: print(f"{i + 1}. {task}") print("\nLast Responses:") table = [ - [f"LLM {i+1}", response] + [f"LLM {i + 1}", response] for i, response in enumerate(self.last_responses) ] print( diff --git a/swarms/structs/multi_threaded_workflow.py b/swarms/structs/multi_threaded_workflow.py index df17d8ce..7bbc62bf 100644 --- a/swarms/structs/multi_threaded_workflow.py +++ b/swarms/structs/multi_threaded_workflow.py @@ -119,7 +119,7 @@ class MultiThreadedWorkflow(BaseWorkflow): except Exception as e: logging.error( ( - f"Attempt {attempt+1} failed for task" + f"Attempt {attempt + 1} failed for task" f" {task}: {str(e)}" ), exc_info=True, diff --git a/swarms/tools/format_tools.py b/swarms/tools/format_tools.py index 2e620135..caeabb94 100644 --- a/swarms/tools/format_tools.py +++ b/swarms/tools/format_tools.py @@ -109,7 +109,7 @@ class Jsonformer: response[0], skip_special_tokens=True ) - response = response[len(prompt) :] + response = response[len(prompt):] response = response.strip().rstrip(".") self.debug("[generate_number]", response) try: @@ -181,7 +181,7 @@ class Jsonformer: response[0][: len(input_tokens[0])] == input_tokens ).all() ): - response = response[0][len(input_tokens[0]) :] + response = response[0][len(input_tokens[0]):] if response.shape[0] == 1: response = response[0] diff --git a/swarms/tools/logits_processor.py b/swarms/tools/logits_processor.py index ed7fef18..208ed607 100644 --- a/swarms/tools/logits_processor.py +++ b/swarms/tools/logits_processor.py @@ -48,7 +48,7 @@ class NumberStoppingCriteria(StoppingCriteria): scores: torch.FloatTensor, ) -> bool: decoded = self.tokenizer.decode( - input_ids[0][self.prompt_length :], + input_ids[0][self.prompt_length:], skip_special_tokens=True, ) diff --git a/swarms/tools/tool_func_doc_scraper.py b/swarms/tools/tool_func_doc_scraper.py index d233bfae..f3c3b7ae 100644 --- a/swarms/tools/tool_func_doc_scraper.py +++ b/swarms/tools/tool_func_doc_scraper.py @@ -11,7 +11,8 @@ def scrape_tool_func_docs(fn: Callable) -> str: fn (Callable): The function to scrape. Returns: - str: A string containing the function's name, documentation string, and a list of its parameters. Each parameter is represented as a line containing the parameter's name, default value, and annotation. + str: A string containing the function's name, documentation string, and a list of its parameters. Each parameter is + represented as a line containing the parameter's name, default value, and annotation. """ try: # If the function is a tool, get the original function diff --git a/swarms/tools/tool_utils.py b/swarms/tools/tool_utils.py index a5a4e47c..87ebd0cd 100644 --- a/swarms/tools/tool_utils.py +++ b/swarms/tools/tool_utils.py @@ -87,7 +87,7 @@ def tools_prompt_prep(docs: str = None, scenarios: str = SCENARIOS): You will be provided with a list of APIs. These APIs will have a description and a list of parameters and return types for each tool. Your task involves creating varied, complex, and detailed user scenarios - that require to call API calls. You must select what api to call based on + that require to call API calls. You must select what api to call based on the context of the task and the scenario. For instance, given the APIs: SearchHotels, BookHotel, CancelBooking, @@ -118,14 +118,14 @@ def tools_prompt_prep(docs: str = None, scenarios: str = SCENARIOS): different combination of APIs for each scenario. All APIs must be used in at least one scenario. You can only use the APIs provided in the APIs section. - + Note that API calls are not explicitly mentioned and their uses are included in parentheses. This behaviour should be mimicked in your response. - - Output the tool usage in a strict json format with the function name and input to + + Output the tool usage in a strict json format with the function name and input to the function. For example, Deliver your response in this format: - + ‘‘‘ {scenarios} ‘‘‘ diff --git a/swarms/utils/apa.py b/swarms/utils/apa.py index fa73b7b4..fead9486 100644 --- a/swarms/utils/apa.py +++ b/swarms/utils/apa.py @@ -83,7 +83,7 @@ class TestResult: prompt = f""" This function has been executed for {self.visit_times} times. Last execution: 1.Status: {self.runtime_status.name} -2.Input: +2.Input: {self.input_data} 3.Output: @@ -108,7 +108,7 @@ class Action: def to_json(self): try: tool_output = json.loads(self.tool_output) - except: + except json.JSONDecodeError: tool_output = self.tool_output return { "thought": self.thought, diff --git a/swarms/utils/disable_logging.py b/swarms/utils/disable_logging.py index 7f04555f..51f23e46 100644 --- a/swarms/utils/disable_logging.py +++ b/swarms/utils/disable_logging.py @@ -2,10 +2,6 @@ import logging import os import warnings import sys -import logging -import os -import warnings -import sys def disable_logging(): diff --git a/swarms/utils/load_model_torch.py b/swarms/utils/load_model_torch.py index 53649e93..e45e87af 100644 --- a/swarms/utils/load_model_torch.py +++ b/swarms/utils/load_model_torch.py @@ -18,7 +18,8 @@ def load_model_torch( model_path (str): Path to the saved model file. device (torch.device): Device to move the model to. model (nn.Module): The model architecture, if the model file only contains the state dictionary. - strict (bool): Whether to strictly enforce that the keys in the state dictionary match the keys returned by the model's `state_dict()` function. + strict (bool): Whether to strictly enforce that the keys in the state + dictionary match the keys returned by the model's `state_dict()` function. map_location (callable): A function to remap the storage locations of the loaded model. *args: Additional arguments to pass to `torch.load`. **kwargs: Additional keyword arguments to pass to `torch.load`. diff --git a/swarms/utils/main.py b/swarms/utils/main.py index b94fae11..0044d94c 100644 --- a/swarms/utils/main.py +++ b/swarms/utils/main.py @@ -376,7 +376,7 @@ class FileHandler: os.makedirs(os.path.dirname(local_filename), exist_ok=True) with open(local_filename, "wb") as f: size = f.write(data) - print(f"Inputs: {url} ({size//1000}MB) => {local_filename}") + print(f"Inputs: {url} ({size // 1000}MB) => {local_filename}") return local_filename def handle(self, url: str) -> str: @@ -390,7 +390,7 @@ class FileHandler: "SERVER", "http://localhost:8000" ) ) - + 1 : + + 1: ] local_filename = ( Path("file") / local_filepath.split("/")[-1]