Skip to content

Commit 8581ee2

Browse files
authored
Merge branch 'master' into fix/default-config
2 parents 2db4a5d + ecf2ba1 commit 8581ee2

31 files changed

+426
-3650
lines changed

.env.template

+11-3
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,23 @@ OPENAI_API_KEY=your-openai-api-key
108108

109109
### OPEN AI
110110
## IMAGE_PROVIDER - Image provider (Example: dalle)
111+
## IMAGE_SIZE - Image size (Example: 256)
112+
## DALLE: 256, 512, 1024
111113
# IMAGE_PROVIDER=dalle
114+
# IMAGE_SIZE=256
112115

113116
### HUGGINGFACE
114-
## STABLE DIFFUSION
115-
## (Default URL: https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4)
116-
## Set in image_gen.py)
117+
## HUGGINGFACE_IMAGE_MODEL - Text-to-image model from Huggingface (Default: CompVis/stable-diffusion-v1-4)
117118
## HUGGINGFACE_API_TOKEN - HuggingFace API token (Example: my-huggingface-api-token)
119+
# HUGGINGFACE_IMAGE_MODEL=CompVis/stable-diffusion-v1-4
118120
# HUGGINGFACE_API_TOKEN=your-huggingface-api-token
119121

122+
### STABLE DIFFUSION WEBUI
123+
## SD_WEBUI_AUTH - Stable diffusion webui username:password pair (Example: username:password)
124+
## SD_WEBUI_URL - Stable diffusion webui API URL (Example: http://127.0.0.1:7860)
125+
# SD_WEBUI_AUTH=
126+
# SD_WEBUI_URL=http://127.0.0.1:7860
127+
120128
################################################################################
121129
### AUDIO TO TEXT PROVIDER
122130
################################################################################

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ auto_gpt_workspace/*
1010
*.mpeg
1111
.env
1212
azure.yaml
13-
outputs/*
1413
ai_settings.yaml
1514
last_run_ai_settings.yaml
1615
.vscode

autogpt/agent/agent.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from autogpt.app import execute_command, get_command
44
from autogpt.chat import chat_with_ai, create_chat_message
55
from autogpt.config import Config
6-
from autogpt.json_fixes.master_json_fix_method import fix_json_using_multiple_techniques
7-
from autogpt.json_validation.validate_json import validate_json
6+
from autogpt.json_utils.json_fix_llm import fix_json_using_multiple_techniques
7+
from autogpt.json_utils.utilities import validate_json
88
from autogpt.logs import logger, print_assistant_thoughts
99
from autogpt.speech import say_text
1010
from autogpt.spinner import Spinner

autogpt/app.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from typing import Dict, List, NoReturn, Union
44

55
from autogpt.agent.agent_manager import AgentManager
6+
from autogpt.commands.analyze_code import analyze_code
67
from autogpt.commands.audio_text import read_audio_from_file
7-
from autogpt.commands.evaluate_code import evaluate_code
88
from autogpt.commands.execute_code import (
99
execute_python_file,
1010
execute_shell,
@@ -27,7 +27,7 @@
2727
from autogpt.commands.web_selenium import browse_website
2828
from autogpt.commands.write_tests import write_tests
2929
from autogpt.config import Config
30-
from autogpt.json_fixes.parsing import fix_and_parse_json
30+
from autogpt.json_utils.json_fix_llm import fix_and_parse_json
3131
from autogpt.memory import get_memory
3232
from autogpt.processing.text import summarize_text
3333
from autogpt.speech import say_text
@@ -181,8 +181,8 @@ def execute_command(command_name: str, arguments):
181181
# TODO: Change these to take in a file rather than pasted code, if
182182
# non-file is given, return instructions "Input should be a python
183183
# filepath, write your code to file and try again"
184-
elif command_name == "evaluate_code":
185-
return evaluate_code(arguments["code"])
184+
elif command_name == "analyze_code":
185+
return analyze_code(arguments["code"])
186186
elif command_name == "improve_code":
187187
return improve_code(arguments["suggestions"], arguments["code"])
188188
elif command_name == "write_tests":

autogpt/commands/evaluate_code.py autogpt/commands/analyze_code.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from autogpt.llm_utils import call_ai_function
55

66

7-
def evaluate_code(code: str) -> list[str]:
7+
def analyze_code(code: str) -> list[str]:
88
"""
99
A function that takes in a string and returns a response from create chat
1010
completion api call.

autogpt/commands/image_gen.py

+73-8
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
CFG = Config()
1515

1616

17-
def generate_image(prompt: str) -> str:
17+
def generate_image(prompt: str, size: int = 256) -> str:
1818
"""Generate an image from a prompt.
1919
2020
Args:
2121
prompt (str): The prompt to use
22+
size (int, optional): The size of the image. Defaults to 256. (Not supported by HuggingFace)
2223
2324
Returns:
2425
str: The filename of the image
@@ -27,11 +28,14 @@ def generate_image(prompt: str) -> str:
2728

2829
# DALL-E
2930
if CFG.image_provider == "dalle":
30-
return generate_image_with_dalle(prompt, filename)
31-
elif CFG.image_provider == "sd":
31+
return generate_image_with_dalle(prompt, filename, size)
32+
# HuggingFace
33+
elif CFG.image_provider == "huggingface":
3234
return generate_image_with_hf(prompt, filename)
33-
else:
34-
return "No Image Provider Set"
35+
# SD WebUI
36+
elif CFG.image_provider == "sdwebui":
37+
return generate_image_with_sd_webui(prompt, filename, size)
38+
return "No Image Provider Set"
3539

3640

3741
def generate_image_with_hf(prompt: str, filename: str) -> str:
@@ -45,13 +49,16 @@ def generate_image_with_hf(prompt: str, filename: str) -> str:
4549
str: The filename of the image
4650
"""
4751
API_URL = (
48-
"https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4"
52+
f"https://api-inference.huggingface.co/models/{CFG.huggingface_image_model}"
4953
)
5054
if CFG.huggingface_api_token is None:
5155
raise ValueError(
5256
"You need to set your Hugging Face API token in the config file."
5357
)
54-
headers = {"Authorization": f"Bearer {CFG.huggingface_api_token}"}
58+
headers = {
59+
"Authorization": f"Bearer {CFG.huggingface_api_token}",
60+
"X-Use-Cache": "false",
61+
}
5562

5663
response = requests.post(
5764
API_URL,
@@ -81,10 +88,18 @@ def generate_image_with_dalle(prompt: str, filename: str) -> str:
8188
"""
8289
openai.api_key = CFG.openai_api_key
8390

91+
# Check for supported image sizes
92+
if size not in [256, 512, 1024]:
93+
closest = min([256, 512, 1024], key=lambda x: abs(x - size))
94+
print(
95+
f"DALL-E only supports image sizes of 256x256, 512x512, or 1024x1024. Setting to {closest}, was {size}."
96+
)
97+
size = closest
98+
8499
response = openai.Image.create(
85100
prompt=prompt,
86101
n=1,
87-
size="256x256",
102+
size=f"{size}x{size}",
88103
response_format="b64_json",
89104
)
90105

@@ -96,3 +111,53 @@ def generate_image_with_dalle(prompt: str, filename: str) -> str:
96111
png.write(image_data)
97112

98113
return f"Saved to disk:{filename}"
114+
115+
116+
def generate_image_with_sd_webui(
117+
prompt: str,
118+
filename: str,
119+
size: int = 512,
120+
negative_prompt: str = "",
121+
extra: dict = {},
122+
) -> str:
123+
"""Generate an image with Stable Diffusion webui.
124+
Args:
125+
prompt (str): The prompt to use
126+
filename (str): The filename to save the image to
127+
size (int, optional): The size of the image. Defaults to 256.
128+
negative_prompt (str, optional): The negative prompt to use. Defaults to "".
129+
extra (dict, optional): Extra parameters to pass to the API. Defaults to {}.
130+
Returns:
131+
str: The filename of the image
132+
"""
133+
# Create a session and set the basic auth if needed
134+
s = requests.Session()
135+
if CFG.sd_webui_auth:
136+
username, password = CFG.sd_webui_auth.split(":")
137+
s.auth = (username, password or "")
138+
139+
# Generate the images
140+
response = requests.post(
141+
f"{CFG.sd_webui_url}/sdapi/v1/txt2img",
142+
json={
143+
"prompt": prompt,
144+
"negative_prompt": negative_prompt,
145+
"sampler_index": "DDIM",
146+
"steps": 20,
147+
"cfg_scale": 7.0,
148+
"width": size,
149+
"height": size,
150+
"n_iter": 1,
151+
**extra,
152+
},
153+
)
154+
155+
print(f"Image Generated for prompt:{prompt}")
156+
157+
# Save the image to disk
158+
response = response.json()
159+
b64 = b64decode(response["images"][0].split(",", 1)[0])
160+
image = Image.open(io.BytesIO(b64))
161+
image.save(path_in_workspace(filename))
162+
163+
return f"Saved to disk:{filename}"

autogpt/config/config.py

+6
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,16 @@ def __init__(self) -> None:
8585
self.milvus_collection = os.getenv("MILVUS_COLLECTION", "autogpt")
8686

8787
self.image_provider = os.getenv("IMAGE_PROVIDER")
88+
self.image_size = int(os.getenv("IMAGE_SIZE", 256))
8889
self.huggingface_api_token = os.getenv("HUGGINGFACE_API_TOKEN")
90+
self.huggingface_image_model = os.getenv(
91+
"HUGGINGFACE_IMAGE_MODEL", "CompVis/stable-diffusion-v1-4"
92+
)
8993
self.huggingface_audio_to_text_model = os.getenv(
9094
"HUGGINGFACE_AUDIO_TO_TEXT_MODEL"
9195
)
96+
self.sd_webui_url = os.getenv("SD_WEBUI_URL", "http://localhost:7860")
97+
self.sd_webui_auth = os.getenv("SD_WEBUI_AUTH")
9298

9399
# Selenium browser settings
94100
self.selenium_web_browser = os.getenv("USE_WEB_BROWSER", "chrome")

autogpt/json_fixes/auto_fix.py

-53
This file was deleted.

autogpt/json_fixes/bracket_termination.py

-37
This file was deleted.

autogpt/json_fixes/escaping.py

-33
This file was deleted.

autogpt/json_fixes/master_json_fix_method.py

-33
This file was deleted.

0 commit comments

Comments
 (0)