forked from Significant-Gravitas/AutoGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ImageGen): add stable diffusion support
- Loading branch information
1 parent
57412bc
commit 28cc986
Showing
4 changed files
with
29 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,28 @@ | ||
from kandinsky2 import get_kandinsky2 | ||
import requests | ||
import io | ||
import os.path | ||
from PIL import Image | ||
from config import Config | ||
import uuid | ||
|
||
cfg = Config() | ||
|
||
working_directory = "auto_gpt_workspace" | ||
|
||
API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4" | ||
headers = {"Authorization": "Bearer " + cfg.huggingface_api_token} | ||
|
||
def generate_image(prompt): | ||
|
||
model = get_kandinsky2('cuda', task_type='text2img', model_version='2.1', use_flash_attention=False) | ||
images = model.generate_text2img( | ||
"red cat, 4k photo", # prompt | ||
num_steps=100, | ||
batch_size=1, | ||
guidance_scale=4, | ||
h=768, w=768, | ||
sampler='p_sampler', | ||
prior_cf_scale=4, | ||
prior_steps="5" | ||
) | ||
return images | ||
|
||
# base_url = 'http://export.arxiv.org/api/query?' | ||
# query = f'search_query=all:{search_query}&start=0&max_results={max_results}' | ||
# url = base_url + query | ||
# response = requests.get(url) | ||
|
||
# if response.status_code == 200: | ||
# soup = BeautifulSoup(response.content, 'xml') | ||
# entries = soup.find_all('entry') | ||
|
||
# articles = [] | ||
# for entry in entries: | ||
# title = entry.title.text.strip() | ||
# url = entry.id.text.strip() | ||
# published = entry.published.text.strip() | ||
|
||
# articles.append({ | ||
# 'title': title, | ||
# 'url': url, | ||
# 'published': published | ||
# }) | ||
|
||
# return articles | ||
# else: | ||
# return None | ||
response = requests.post(API_URL, headers=headers, json={ | ||
"inputs": prompt, | ||
}) | ||
image = Image.open(io.BytesIO(response.content)) | ||
print("Image Generated for prompt:" + prompt) | ||
|
||
filename = str(uuid.uuid4()) + ".jpg" | ||
|
||
image.save(os.path.join(working_directory, filename)) | ||
|
||
print("Saved to disk:" + filename) | ||
|
||
return str("Image " + filename + " saved to disk for prompt: " + prompt) |