From 90381160eab11da569e3a1914a49abee82605d68 Mon Sep 17 00:00:00 2001 From: freddyaboulton Date: Fri, 21 Apr 2023 15:39:58 -0400 Subject: [PATCH 1/5] Add gradio-tools --- requirements.txt | 3 +- src/autogpt_plugins/gradio-tools/README.md | 34 +++ src/autogpt_plugins/gradio-tools/__init__.py | 239 +++++++++++++++++++ src/autogpt_plugins/gradio-tools/tools.py | 141 +++++++++++ 4 files changed, 416 insertions(+), 1 deletion(-) create mode 100644 src/autogpt_plugins/gradio-tools/README.md create mode 100644 src/autogpt_plugins/gradio-tools/__init__.py create mode 100644 src/autogpt_plugins/gradio-tools/tools.py diff --git a/requirements.txt b/requirements.txt index a71fa4cc..ade31929 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,4 +10,5 @@ twine tweepy pandas auto_gpt_plugin_template -python-dotenv \ No newline at end of file +python-dotenv +gradio_tools>=0.0.5 \ No newline at end of file diff --git a/src/autogpt_plugins/gradio-tools/README.md b/src/autogpt_plugins/gradio-tools/README.md new file mode 100644 index 00000000..e152fd59 --- /dev/null +++ b/src/autogpt_plugins/gradio-tools/README.md @@ -0,0 +1,34 @@ +# freddyaboulton/gradio-tools 🤝 + +A plugin giving AutoGPT access to [Gradio](https://github.com/gradio-app/gradio) spaces running on +the [huggingface hub](https://huggingface.co/spaces) and elsewhere! + +Integration powered by [gradio-tools](https://github.com/freddyaboulton/gradio-tools) + +gradio-tools comes with a set of pre-built tools but it is easy to add new tools. + +All contributions to `gradio-tools` and this plugin are welcome! + +## Features(more coming soon!) + +Each tool specified via the env file will add a command that gives AutoGPT +the ability to call that gradio app programmatically and get its prediciton. + +For example, an LLM could use a Gradio tool to transcribe a voice recording it finds online and then summarize it for you. Or it could use a different Gradio tool to apply OCR to a document on your Google Drive and then answer questions about it. + +## Installation + +1. Download this repository, and save it as `autogpt-gradiot-ools.zip` +2. Place the `.zip` file in the plugins directory of your AutoGPT install +3. Add your twitter API information to the `.env` file within AutoGPT: + +``` +################################################################################ +### GRADIO-TOOLS +################################################################################ + +# Consumer Keys are also known as API keys on the dev portal + +AUTOGPT_GRADIO_TOOLS=StableDiffusion,ImageCaptioner +``` + diff --git a/src/autogpt_plugins/gradio-tools/__init__.py b/src/autogpt_plugins/gradio-tools/__init__.py new file mode 100644 index 00000000..6218c571 --- /dev/null +++ b/src/autogpt_plugins/gradio-tools/__init__.py @@ -0,0 +1,239 @@ +"""Twitter API integrations using Tweepy.""" +from typing import Any, Dict, List, Optional, Tuple, TypedDict, TypeVar +from dotenv import load_dotenv +from auto_gpt_plugin_template import AutoGPTPluginTemplate +from pathlib import Path +import os +from .tools import (AutoGPTClipInterrogatorTool, + AutoGPTStableDiffusion, + AutoGPTWhisperTool, + AutoGPTTextToVideoTool, + AutoGPTCaptioner, + AutoGPTPromptGeneratorTool, + AutoGPTImageToMusicTool, + AutoGPTDocumentAnsweringTool, + GradioTool) + +PromptGenerator = TypeVar("PromptGenerator") + + +with open(str(Path(os.getcwd()) / ".env"), 'r') as fp: + load_dotenv(stream=fp) + + +TOOLS = [ + AutoGPTStableDiffusion(hf_token=os.getenv("GRADIO_TOOLS_HF_TOKEN")), + AutoGPTCaptioner(hf_token=os.getenv("GRADIO_TOOLS_HF_TOKEN")), + AutoGPTWhisperTool(hf_token=os.getenv("GRADIO_TOOLS_HF_TOKEN")), + AutoGPTTextToVideoTool(hf_token=os.getenv("GRADIO_TOOLS_HF_TOKEN")), + AutoGPTPromptGeneratorTool(hf_token=os.getenv("GRADIO_TOOLS_HF_TOKEN")), + AutoGPTDocumentAnsweringTool(hf_token=os.getenv("GRADIO_TOOLS_HF_TOKEN")), + AutoGPTImageToMusicTool(hf_token=os.getenv("GRADIO_TOOLS_HF_TOKEN")), + AutoGPTClipInterrogatorTool(hf_token=os.getenv("GRADIO_TOOLS_HF_TOKEN")) +] + +def get_tool(tool: str) -> GradioTool: + return next(t for t in TOOLS if t.name == tool) + + +class Message(TypedDict): + role: str + content: str + + +class AutoGPTGradioTools(AutoGPTPluginTemplate): + """ + Twitter API integrations using Tweepy + """ + + def __init__(self): + super().__init__() + self._name = "autogpt-gradio-tools" + self._version = "0.1.0" + self._description = "Calling Gradio Apps ." + ts = [t for t in os.getenv("AUTOGPT_GRADIO_TOOLS", "").split(",") if t != ""] + self.tools = [get_tool(t) for t in ts] + + def can_handle_on_response(self) -> bool: + """This method is called to check that the plugin can + handle the on_response method. + Returns: + bool: True if the plugin can handle the on_response method.""" + return False + + def on_response(self, response: str, *args, **kwargs) -> str: + """This method is called when a response is received from the model.""" + pass + + def can_handle_post_prompt(self) -> bool: + """This method is called to check that the plugin can + handle the post_prompt method. + Returns: + bool: True if the plugin can handle the post_prompt method.""" + return True + + def can_handle_on_planning(self) -> bool: + """This method is called to check that the plugin can + handle the on_planning method. + Returns: + bool: True if the plugin can handle the on_planning method.""" + return False + + def on_planning( + self, prompt: PromptGenerator, messages: List[str] + ) -> Optional[str]: + """This method is called before the planning chat completeion is done. + Args: + prompt (PromptGenerator): The prompt generator. + messages (List[str]): The list of messages. + """ + pass + + def can_handle_post_planning(self) -> bool: + """This method is called to check that the plugin can + handle the post_planning method. + Returns: + bool: True if the plugin can handle the post_planning method.""" + return False + + def post_planning(self, response: str) -> str: + """This method is called after the planning chat completeion is done. + Args: + response (str): The response. + Returns: + str: The resulting response. + """ + pass + + def can_handle_pre_instruction(self) -> bool: + """This method is called to check that the plugin can + handle the pre_instruction method. + Returns: + bool: True if the plugin can handle the pre_instruction method.""" + return False + + def pre_instruction(self, messages: List[str]) -> List[str]: + """This method is called before the instruction chat is done. + Args: + messages (List[str]): The list of context messages. + Returns: + List[str]: The resulting list of messages. + """ + pass + + def can_handle_on_instruction(self) -> bool: + """This method is called to check that the plugin can + handle the on_instruction method. + Returns: + bool: True if the plugin can handle the on_instruction method.""" + return False + + def on_instruction(self, messages: List[str]) -> Optional[str]: + """This method is called when the instruction chat is done. + Args: + messages (List[str]): The list of context messages. + Returns: + Optional[str]: The resulting message. + """ + pass + + def can_handle_post_instruction(self) -> bool: + """This method is called to check that the plugin can + handle the post_instruction method. + Returns: + bool: True if the plugin can handle the post_instruction method.""" + return False + + def post_instruction(self, response: str) -> str: + """This method is called after the instruction chat is done. + Args: + response (str): The response. + Returns: + str: The resulting response. + """ + pass + + def can_handle_pre_command(self) -> bool: + """This method is called to check that the plugin can + handle the pre_command method. + Returns: + bool: True if the plugin can handle the pre_command method.""" + return False + + def pre_command( + self, command_name: str, arguments: Dict[str, Any] + ) -> Tuple[str, Dict[str, Any]]: + """This method is called before the command is executed. + Args: + command_name (str): The command name. + arguments (Dict[str, Any]): The arguments. + Returns: + Tuple[str, Dict[str, Any]]: The command name and the arguments. + """ + pass + + def can_handle_post_command(self) -> bool: + """This method is called to check that the plugin can + handle the post_command method. + Returns: + bool: True if the plugin can handle the post_command method.""" + return False + + def post_command(self, command_name: str, response: str) -> str: + """This method is called after the command is executed. + Args: + command_name (str): The command name. + response (str): The response. + Returns: + str: The resulting response. + """ + pass + + def can_handle_chat_completion( + self, + messages: list[Dict[Any, Any]], + model: str, + temperature: float, + max_tokens: int, + ) -> bool: + """This method is called to check that the plugin can + handle the chat_completion method. + Args: + messages (Dict[Any, Any]): The messages. + model (str): The model name. + temperature (float): The temperature. + max_tokens (int): The max tokens. + Returns: + bool: True if the plugin can handle the chat_completion method.""" + return False + + def handle_chat_completion( + self, + messages: list[Dict[Any, Any]], + model: str, + temperature: float, + max_tokens: int, + ) -> str: + """This method is called when the chat completion is done. + Args: + messages (Dict[Any, Any]): The messages. + model (str): The model name. + temperature (float): The temperature. + max_tokens (int): The max tokens. + Returns: + str: The resulting response. + """ + return None + + def post_prompt(self, prompt: PromptGenerator) -> PromptGenerator: + """This method is called just after the generate_prompt is called, + but actually before the prompt is generated. + Args: + prompt (PromptGenerator): The prompt generator. + Returns: + PromptGenerator: The prompt generator. + """ + for tool in self.tools: + prompt.add_command(tool.name, tool.description, tool.args, tool.run) + + return prompt diff --git a/src/autogpt_plugins/gradio-tools/tools.py b/src/autogpt_plugins/gradio-tools/tools.py new file mode 100644 index 00000000..ac743c9b --- /dev/null +++ b/src/autogpt_plugins/gradio-tools/tools.py @@ -0,0 +1,141 @@ +from dataclasses import dataclass +from gradio_tools import ( + StableDiffusionTool, + ImageCaptioningTool, + TextToVideoTool, + StableDiffusionPromptGeneratorTool, + WhisperAudioTranscriptionTool, + DocQueryDocumentAnsweringTool, + ClipInterrogatorTool, + ImageToMusicTool, + GradioTool +) +from gradio_tools.tools import Job +from pathlib import Path +import os + + +WORKSPACE_DIR = (Path(os.getcwd()) / "auto_gpt_workspace").resolve() + + +class AutoGPTCaptioner(ImageCaptioningTool): + def __init__( + self, + name="ImageCaptioner", + description="An image captioner. Use this to create a caption for an image. " + "Input will be a path to an image file. " + "The output will be a caption of that image.", + src="gradio-client-demos/comparing-captioning-models", + hf_token=None + ) -> None: + super().__init__(name, description, src, hf_token) + self.args = {"img": ""} + + def create_job(self, query: dict) -> Job: + for v in query.values(): + if Path(v).exists(): + return super().create_job(v) + elif Path(WORKSPACE_DIR / v).exists(): + return super().create_job(v) + raise ValueError(f"Cannot create captioning job for query: {query}") + + +class AutoGPTStableDiffusion(StableDiffusionTool): + def __init__( + self, + name="StableDiffusion", + description="An image generator. Use this to generate images based on " + "text input. Input should be a description of what the image should " + "look like. The output will be a path to an image file.", + src="gradio-client-demos/stable-diffusion", + hf_token=None, + ) -> None: + super().__init__(name, description, src, hf_token) + self.args = {"prompt": "text description of image"} + + +class AutoGPTWhisperTool(WhisperAudioTranscriptionTool): + def __init__( + self, + name="Whisper", + description="A tool for transcribing audio. Use this tool to transcribe an audio file. " + "track from an image. Input will be a path to an audio file. " + "The output will the text transcript of that file.", + src="abidlabs/whisper-large-v2", + hf_token=None, + ) -> None: + super().__init__(name, description, src, hf_token) + self.args = {"audio": "full path of audio file"} + + def create_job(self, query: dict) -> Job: + for v in query.values(): + if Path(v).exists(): + return super().create_job(v) + elif Path(WORKSPACE_DIR / v).exists(): + return super().create_job(v) + raise ValueError(f"Cannot create transcription job for query: {query}") + + +class AutoGPTTextToVideoTool(TextToVideoTool): + def __init__( + self, + name="TextToVideo", + description="A tool for creating videos from text." + "Use this tool to create videos from text prompts. " + "Input will be a text prompt describing a video scene. " + "The output will be a path to a video file.", + src="damo-vilab/modelscope-text-to-video-synthesis", + hf_token=None, + ) -> None: + super().__init__(name, description, src, hf_token) + self.args = {"prompt": "text description of video"} + + +class AutoGPTPromptGeneratorTool(StableDiffusionPromptGeneratorTool): + def __init__( + self, + name="StableDiffusionPromptGenerator", + description="Use this tool to improve a prompt for stable diffusion and other image generators " + "This tool will refine your prompt to include key words and phrases that make " + "stable diffusion perform better. The input is a prompt text string " + "and the output is a prompt text string", + src="microsoft/Promptist", + hf_token=None, + ) -> None: + super().__init__(name, description, src, hf_token) + self.args = {"prompt": "text description of image"} + + +class AutoGPTDocumentAnsweringTool(DocQueryDocumentAnsweringTool): + + def __init__(self, name="DocQuery", description="A tool for answering questions about a document from the from the image of the document. Input will be a two strings separated by a comma: the first will be the path or URL to an image of a document. The second will be your question about the document." "The output will the text answer to your question.", src="abidlabs/docquery", hf_token=None) -> None: + super().__init__(name, description, src, hf_token) + self.args = {"args": "Two strings separated by a comma: the first will be the path or URL to an image of a document. The second will be your question about the document."} + + +class AutoGPTClipInterrogatorTool(ClipInterrogatorTool): + def __init__(self, name="ClipInterrogator", description="A tool for reverse engineering a prompt from a source image. " "Use this tool to create a prompt for StableDiffusion that matches the " "input image. The imput is a path to an image. The output is a text string.", src="pharma/CLIP-Interrogator", hf_token=None) -> None: + super().__init__(name, description, src, hf_token) + self.args = {"image": "The full path to the image file"} + + def create_job(self, query: dict) -> Job: + for v in query.values(): + if Path(v).exists(): + return super().create_job(v) + elif Path(WORKSPACE_DIR / v).exists(): + return super().create_job(v) + raise ValueError(f"Cannot create transcription job for query: {query}") + + +class AutoGPTImageToMusicTool(ImageToMusicTool): + def __init__(self, name="ImagetoMusic", description="A tool for creating music from images. Use this tool to create a musical " "track from an image. Input will be a path to an image file. " "The output will be an audio file generated from that image.", src="fffiloni/img-to-music", hf_token=None) -> None: + super().__init__(name, description, src, hf_token) + self.args = {"image": "The full path to the image file"} + + def create_job(self, query: dict) -> Job: + for v in query.values(): + if Path(v).exists(): + return super().create_job(v) + elif Path(WORKSPACE_DIR / v).exists(): + return super().create_job(v) + raise ValueError(f"Cannot create img-to-music job for query: {query}") \ No newline at end of file From a8833acb6e5431849b391a834d195aaa98c78327 Mon Sep 17 00:00:00 2001 From: freddyaboulton Date: Fri, 21 Apr 2023 16:18:26 -0400 Subject: [PATCH 2/5] Fixing --- src/autogpt_plugins/gradio-tools/README.md | 3 ++- src/autogpt_plugins/gradio-tools/__init__.py | 6 +++--- src/autogpt_plugins/gradio-tools/tools.py | 4 +--- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/autogpt_plugins/gradio-tools/README.md b/src/autogpt_plugins/gradio-tools/README.md index e152fd59..c0a02267 100644 --- a/src/autogpt_plugins/gradio-tools/README.md +++ b/src/autogpt_plugins/gradio-tools/README.md @@ -9,7 +9,7 @@ gradio-tools comes with a set of pre-built tools but it is easy to add new tools All contributions to `gradio-tools` and this plugin are welcome! -## Features(more coming soon!) +## Features Each tool specified via the env file will add a command that gives AutoGPT the ability to call that gradio app programmatically and get its prediciton. @@ -30,5 +30,6 @@ For example, an LLM could use a Gradio tool to transcribe a voice recording it f # Consumer Keys are also known as API keys on the dev portal AUTOGPT_GRADIO_TOOLS=StableDiffusion,ImageCaptioner +GRADIO_TOOLS_HF_TOKEN= ``` diff --git a/src/autogpt_plugins/gradio-tools/__init__.py b/src/autogpt_plugins/gradio-tools/__init__.py index 6218c571..20774b3d 100644 --- a/src/autogpt_plugins/gradio-tools/__init__.py +++ b/src/autogpt_plugins/gradio-tools/__init__.py @@ -11,8 +11,8 @@ AutoGPTCaptioner, AutoGPTPromptGeneratorTool, AutoGPTImageToMusicTool, - AutoGPTDocumentAnsweringTool, - GradioTool) + AutoGPTDocumentAnsweringTool) +from gradio_tools import GradioTool PromptGenerator = TypeVar("PromptGenerator") @@ -234,6 +234,6 @@ def post_prompt(self, prompt: PromptGenerator) -> PromptGenerator: PromptGenerator: The prompt generator. """ for tool in self.tools: - prompt.add_command(tool.name, tool.description, tool.args, tool.run) + prompt.add_command(tool.description, tool.name.lower(), tool.args, tool.run) return prompt diff --git a/src/autogpt_plugins/gradio-tools/tools.py b/src/autogpt_plugins/gradio-tools/tools.py index ac743c9b..4adf5c29 100644 --- a/src/autogpt_plugins/gradio-tools/tools.py +++ b/src/autogpt_plugins/gradio-tools/tools.py @@ -1,4 +1,3 @@ -from dataclasses import dataclass from gradio_tools import ( StableDiffusionTool, ImageCaptioningTool, @@ -8,9 +7,8 @@ DocQueryDocumentAnsweringTool, ClipInterrogatorTool, ImageToMusicTool, - GradioTool ) -from gradio_tools.tools import Job +from gradio_tools.tools.gradio_tool import Job from pathlib import Path import os From 25cc46588bd359211e635e9cc734bae6fc2d444c Mon Sep 17 00:00:00 2001 From: freddyaboulton Date: Fri, 21 Apr 2023 16:48:39 -0400 Subject: [PATCH 3/5] Fix typos --- src/autogpt_plugins/gradio-tools/tools.py | 36 ++--------------------- 1 file changed, 2 insertions(+), 34 deletions(-) diff --git a/src/autogpt_plugins/gradio-tools/tools.py b/src/autogpt_plugins/gradio-tools/tools.py index 4adf5c29..cee0b7ec 100644 --- a/src/autogpt_plugins/gradio-tools/tools.py +++ b/src/autogpt_plugins/gradio-tools/tools.py @@ -23,20 +23,12 @@ def __init__( description="An image captioner. Use this to create a caption for an image. " "Input will be a path to an image file. " "The output will be a caption of that image.", - src="gradio-client-demos/comparing-captioning-models", + src="taesiri/BLIP-2", hf_token=None ) -> None: super().__init__(name, description, src, hf_token) self.args = {"img": ""} - def create_job(self, query: dict) -> Job: - for v in query.values(): - if Path(v).exists(): - return super().create_job(v) - elif Path(WORKSPACE_DIR / v).exists(): - return super().create_job(v) - raise ValueError(f"Cannot create captioning job for query: {query}") - class AutoGPTStableDiffusion(StableDiffusionTool): def __init__( @@ -59,19 +51,11 @@ def __init__( description="A tool for transcribing audio. Use this tool to transcribe an audio file. " "track from an image. Input will be a path to an audio file. " "The output will the text transcript of that file.", - src="abidlabs/whisper-large-v2", + src="abidlabs/whisper", hf_token=None, ) -> None: super().__init__(name, description, src, hf_token) self.args = {"audio": "full path of audio file"} - - def create_job(self, query: dict) -> Job: - for v in query.values(): - if Path(v).exists(): - return super().create_job(v) - elif Path(WORKSPACE_DIR / v).exists(): - return super().create_job(v) - raise ValueError(f"Cannot create transcription job for query: {query}") class AutoGPTTextToVideoTool(TextToVideoTool): @@ -115,25 +99,9 @@ class AutoGPTClipInterrogatorTool(ClipInterrogatorTool): def __init__(self, name="ClipInterrogator", description="A tool for reverse engineering a prompt from a source image. " "Use this tool to create a prompt for StableDiffusion that matches the " "input image. The imput is a path to an image. The output is a text string.", src="pharma/CLIP-Interrogator", hf_token=None) -> None: super().__init__(name, description, src, hf_token) self.args = {"image": "The full path to the image file"} - - def create_job(self, query: dict) -> Job: - for v in query.values(): - if Path(v).exists(): - return super().create_job(v) - elif Path(WORKSPACE_DIR / v).exists(): - return super().create_job(v) - raise ValueError(f"Cannot create transcription job for query: {query}") class AutoGPTImageToMusicTool(ImageToMusicTool): def __init__(self, name="ImagetoMusic", description="A tool for creating music from images. Use this tool to create a musical " "track from an image. Input will be a path to an image file. " "The output will be an audio file generated from that image.", src="fffiloni/img-to-music", hf_token=None) -> None: super().__init__(name, description, src, hf_token) self.args = {"image": "The full path to the image file"} - - def create_job(self, query: dict) -> Job: - for v in query.values(): - if Path(v).exists(): - return super().create_job(v) - elif Path(WORKSPACE_DIR / v).exists(): - return super().create_job(v) - raise ValueError(f"Cannot create img-to-music job for query: {query}") \ No newline at end of file From 1038ab12f37b87e37b0c8d59047d1a0a6245af3b Mon Sep 17 00:00:00 2001 From: freddyaboulton Date: Sun, 23 Apr 2023 19:01:47 -0400 Subject: [PATCH 4/5] Add tests --- .../README.md | 2 +- .../__init__.py | 13 ++-- .../autogpt_gradio_tools/test_gradio_tools.py | 61 +++++++++++++++++++ .../tools.py | 13 +--- 4 files changed, 71 insertions(+), 18 deletions(-) rename src/autogpt_plugins/{gradio-tools => autogpt_gradio_tools}/README.md (97%) rename src/autogpt_plugins/{gradio-tools => autogpt_gradio_tools}/__init__.py (95%) create mode 100644 src/autogpt_plugins/autogpt_gradio_tools/test_gradio_tools.py rename src/autogpt_plugins/{gradio-tools => autogpt_gradio_tools}/tools.py (89%) diff --git a/src/autogpt_plugins/gradio-tools/README.md b/src/autogpt_plugins/autogpt_gradio_tools/README.md similarity index 97% rename from src/autogpt_plugins/gradio-tools/README.md rename to src/autogpt_plugins/autogpt_gradio_tools/README.md index c0a02267..bdab43ab 100644 --- a/src/autogpt_plugins/gradio-tools/README.md +++ b/src/autogpt_plugins/autogpt_gradio_tools/README.md @@ -1,4 +1,4 @@ -# freddyaboulton/gradio-tools 🤝 +# freddyaboulton/autogpt-gradio-tools 🤝 A plugin giving AutoGPT access to [Gradio](https://github.com/gradio-app/gradio) spaces running on the [huggingface hub](https://huggingface.co/spaces) and elsewhere! diff --git a/src/autogpt_plugins/gradio-tools/__init__.py b/src/autogpt_plugins/autogpt_gradio_tools/__init__.py similarity index 95% rename from src/autogpt_plugins/gradio-tools/__init__.py rename to src/autogpt_plugins/autogpt_gradio_tools/__init__.py index 20774b3d..3ee451fb 100644 --- a/src/autogpt_plugins/gradio-tools/__init__.py +++ b/src/autogpt_plugins/autogpt_gradio_tools/__init__.py @@ -1,4 +1,4 @@ -"""Twitter API integrations using Tweepy.""" +"""Integration with Gradio Spaces On HuggingFace via gradio_tools.""" from typing import Any, Dict, List, Optional, Tuple, TypedDict, TypeVar from dotenv import load_dotenv from auto_gpt_plugin_template import AutoGPTPluginTemplate @@ -16,9 +16,9 @@ PromptGenerator = TypeVar("PromptGenerator") - -with open(str(Path(os.getcwd()) / ".env"), 'r') as fp: - load_dotenv(stream=fp) +if (Path(os.getcwd()) / ".env").exists(): + with open(str(Path(os.getcwd()) / ".env"), 'r') as fp: + load_dotenv(stream=fp) TOOLS = [ @@ -233,7 +233,8 @@ def post_prompt(self, prompt: PromptGenerator) -> PromptGenerator: Returns: PromptGenerator: The prompt generator. """ - for tool in self.tools: - prompt.add_command(tool.description, tool.name.lower(), tool.args, tool.run) + if self.tools: + for tool in self.tools: + prompt.add_command(tool.description, tool.name.lower(), tool.args, tool.run) return prompt diff --git a/src/autogpt_plugins/autogpt_gradio_tools/test_gradio_tools.py b/src/autogpt_plugins/autogpt_gradio_tools/test_gradio_tools.py new file mode 100644 index 00000000..e1357f51 --- /dev/null +++ b/src/autogpt_plugins/autogpt_gradio_tools/test_gradio_tools.py @@ -0,0 +1,61 @@ +import os +from unittest.mock import patch, MagicMock +from unittest.mock import mock_open +import unittest +import gradio_tools +from .tools import (AutoGPTClipInterrogatorTool, + AutoGPTStableDiffusion, + AutoGPTWhisperTool, + AutoGPTTextToVideoTool, + AutoGPTCaptioner, + AutoGPTPromptGeneratorTool, + AutoGPTImageToMusicTool, + AutoGPTDocumentAnsweringTool) + + +class TestGradioTools(unittest.TestCase): + @patch.dict( + os.environ, + { + "AUTOGPT_GRADIO_TOOLS": "WhisperAudioTranscription,TextToVideo", + }, + ) + def test_right_tools_loaded(self): + from . import AutoGPTGradioTools + + plugin = AutoGPTGradioTools() + assert plugin.tools[0].name == "WhisperAudioTranscription" + assert plugin.tools[1].name == "TextToVideo" + + @patch.dict( + os.environ, + { + "AUTOGPT_GRADIO_TOOLS": "WhisperAudioTranscription,TextToVideo", + }, + ) + def test_commands_added_to_prompt(self): + from . import AutoGPTGradioTools + + mock_prompt = MagicMock() + plugin = AutoGPTGradioTools() + plugin.post_prompt(mock_prompt) + # Two tools added to prompt + assert mock_prompt.add_command.call_count == 2 + + + def test_tools_configured_correctly(self): + all_tools = [ + (AutoGPTClipInterrogatorTool(), gradio_tools.ClipInterrogatorTool()), + (AutoGPTStableDiffusion(), gradio_tools.StableDiffusionTool()), + (AutoGPTWhisperTool(), gradio_tools.WhisperAudioTranscriptionTool()), + (AutoGPTTextToVideoTool(), gradio_tools.TextToVideoTool()), + (AutoGPTCaptioner(), gradio_tools.ImageCaptioningTool()), + (AutoGPTPromptGeneratorTool(), gradio_tools.StableDiffusionPromptGeneratorTool()), + (AutoGPTImageToMusicTool(), gradio_tools.ImageToMusicTool()), + (AutoGPTDocumentAnsweringTool(), gradio_tools.DocQueryDocumentAnsweringTool()) + ] + for tool_1, tool_2 in all_tools: + assert tool_1.name == tool_2.name + assert tool_1.description == tool_2.description + assert tool_1.src == tool_2.src + assert tool_1.args diff --git a/src/autogpt_plugins/gradio-tools/tools.py b/src/autogpt_plugins/autogpt_gradio_tools/tools.py similarity index 89% rename from src/autogpt_plugins/gradio-tools/tools.py rename to src/autogpt_plugins/autogpt_gradio_tools/tools.py index cee0b7ec..483ffc99 100644 --- a/src/autogpt_plugins/gradio-tools/tools.py +++ b/src/autogpt_plugins/autogpt_gradio_tools/tools.py @@ -47,7 +47,7 @@ def __init__( class AutoGPTWhisperTool(WhisperAudioTranscriptionTool): def __init__( self, - name="Whisper", + name="WhisperAudioTranscription", description="A tool for transcribing audio. Use this tool to transcribe an audio file. " "track from an image. Input will be a path to an audio file. " "The output will the text transcript of that file.", @@ -74,16 +74,7 @@ def __init__( class AutoGPTPromptGeneratorTool(StableDiffusionPromptGeneratorTool): - def __init__( - self, - name="StableDiffusionPromptGenerator", - description="Use this tool to improve a prompt for stable diffusion and other image generators " - "This tool will refine your prompt to include key words and phrases that make " - "stable diffusion perform better. The input is a prompt text string " - "and the output is a prompt text string", - src="microsoft/Promptist", - hf_token=None, - ) -> None: + def __init__(self, name="StableDiffusionPromptGenerator", description="Use this tool to improve a prompt for stable diffusion and other image and video generators. " "This tool will refine your prompt to include key words and phrases that make " "stable diffusion and other art generation algorithms perform better. The input is a prompt text string " "and the output is a prompt text string", src="microsoft/Promptist", hf_token=None) -> None: super().__init__(name, description, src, hf_token) self.args = {"prompt": "text description of image"} From 1fae35e7fb246a81287a96a5af471e77e4ad5467 Mon Sep 17 00:00:00 2001 From: freddyaboulton Date: Sun, 23 Apr 2023 19:05:05 -0400 Subject: [PATCH 5/5] Fix typo --- src/autogpt_plugins/autogpt_gradio_tools/README.md | 2 +- src/autogpt_plugins/autogpt_gradio_tools/test_gradio_tools.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/autogpt_plugins/autogpt_gradio_tools/README.md b/src/autogpt_plugins/autogpt_gradio_tools/README.md index bdab43ab..3a0b5a86 100644 --- a/src/autogpt_plugins/autogpt_gradio_tools/README.md +++ b/src/autogpt_plugins/autogpt_gradio_tools/README.md @@ -18,7 +18,7 @@ For example, an LLM could use a Gradio tool to transcribe a voice recording it f ## Installation -1. Download this repository, and save it as `autogpt-gradiot-ools.zip` +1. Download this repository, and save it as `autogpt-gradio-tools.zip` 2. Place the `.zip` file in the plugins directory of your AutoGPT install 3. Add your twitter API information to the `.env` file within AutoGPT: diff --git a/src/autogpt_plugins/autogpt_gradio_tools/test_gradio_tools.py b/src/autogpt_plugins/autogpt_gradio_tools/test_gradio_tools.py index e1357f51..d5a78bec 100644 --- a/src/autogpt_plugins/autogpt_gradio_tools/test_gradio_tools.py +++ b/src/autogpt_plugins/autogpt_gradio_tools/test_gradio_tools.py @@ -1,6 +1,5 @@ import os from unittest.mock import patch, MagicMock -from unittest.mock import mock_open import unittest import gradio_tools from .tools import (AutoGPTClipInterrogatorTool,