Create images for blogs - Stability AI

This commit is contained in:
ajaysi
2024-04-22 19:02:55 +05:30
parent 357cba36e4
commit aec2d6b432
11 changed files with 108 additions and 71 deletions

View File

@@ -1,41 +1,56 @@
from PIL import Image
import requests
# Ensure you sign up for an account to obtain an API key:
# https://platform.stability.ai/
# Your API key can be found here after account creation:
# https://platform.stability.ai/account/keys
import base64
import os
import requests
from PIL import Image
from io import BytesIO
from .save_image import save_generated_image
def generate_stable_diffusion_image(prompt):
"""
Generate images using Stable Diffusion API based on a given prompt.
Args:
prompt (str): The prompt to generate the image.
image_dir (str): The directory where the image will be saved.
Raises:
Warning: If the adult content classifier is triggered.
Exception: For any issues during image generation or saving.
"""
api_key = os.getenv('STABILITY_API_KEY')
engine_id = "stable-diffusion-xl-1024-v1-0"
api_host = os.getenv('API_HOST', 'https://api.stability.ai')
api_key = os.getenv("STABILITY_API_KEY")
if api_key is None:
raise Exception("Missing Stability API key.")
response = requests.post(
f"https://api.stability.ai/v2beta/stable-image/generate/sd3",
f"{api_host}/v1/generation/{engine_id}/text-to-image",
headers={
"authorization": f"Bearer {api_key}",
"accept": "image/*"
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": f"Bearer {api_key}"
},
files={"none": ''},
data={
"prompt": prompt,
"output_format": "webp",
json={
"text_prompts": [
{
"text": prompt
}
],
"cfg_scale": 7,
"height": 1024,
"width": 1024,
"samples": 1,
"steps": 30,
},
)
if response.status_code != 200:
raise Exception("Non-200 response: " + str(response.text))
data = response.json()
save_generated_image(data)
if response.status_code == 200:
with open("./dog-wearing-glasses.jpeg", 'wb') as file:
file.write(response.content)
else:
raise Exception(str(response.json()))
for i, image in enumerate(data["artifacts"]):
# Decode base64 image data
img_data = base64.b64decode(image["base64"])
# Open image using PIL
img = Image.open(BytesIO(img_data))
# Display the image
img.show()

View File

@@ -23,9 +23,10 @@ logger.add(sys.stdout,
#from .gen_dali2_images
from .gen_dali3_images import generate_dalle3_images
from .gen_stabl_diff_img import generate_stable_diffusion_image
from ..text_generation.main_text_generation import llm_text_gen
def generate_image(user_prompt, image_engine="dalle3"):
def generate_image(user_prompt, image_engine):
"""
The generation API endpoint creates an image based on a text prompt.
@@ -41,15 +42,17 @@ def generate_image(user_prompt, image_engine="dalle3"):
Must be one of "url" or "b64_json". Defaults to "url".
--> user (str): A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse.
"""
img_prompt = generate_img_prompt(user_prompt)
# call the OpenAI API to generate image from prompt.
logger.info(f"Calling image.generate with prompt: {img_prompt}")
if 'Dalle3' in image_engine:
image_stored_at = generate_dalle3_images(img_prompt)
elif 'Stable Diffusion' in image_engine:
image_stored_at = generate_stable_diffusion_image(img_prompt)
try:
img_prompt = generate_img_prompt(user_prompt)
if 'Dalle3' in image_engine:
logger.info(f"Calling Dalle3 text-to-image with prompt: {img_prompt}")
image_stored_at = generate_dalle3_images(img_prompt)
elif 'Stability-Stable-Diffusion' in image_engine:
logger.info(f"Calling Stable diffusion text-to-image with prompt: \n{img_prompt}")
print("\n\n")
image_stored_at = generate_stable_diffusion_image(img_prompt)
except Exception as err:
logger.error(f"Failed to generate Image: {err}")
return image_stored_at
@@ -57,17 +60,16 @@ def generate_img_prompt(user_prompt):
"""
Given prompt, this functions generated a prompt for image generation.
"""
# I want you to act as an artist advisor providing advice on various art styles such tips on utilizing
# light & shadow effects effectively in painting, shading techniques while sculpting etc.
# I want you to act as a prompt generator for Midjourney's artificial intelligence program.
# Your job is to provide detailed and creative descriptions that will inspire unique and interesting images from the AI.
# Here is your first prompt: ""
logger.info(f"Generate image prompt for : {user_prompt}")
prompt = f"""As an educationist and expert infographic artist, your tasked to create prompts that will be used for image generation.
Craft prompt for Openai Dall-e image generation program. Clearly describe the given text to represent it as image.
Make sure to avoid common image generation mistakes.
Advice for creating prompt for image from the given text(no more than 150 words).
Reply with only one answer and no descrition. Generate image prompt for the below text.
Text: {user_prompt}"""
response = (prompt)
prompt = f"""
As an expert prompt engineer and artist, I will provide you with 'text' for creating image.
I want you to act as a prompt generator for AI text to image models(no more than 150 words).
\n
Choose from various art styles, utilize light & shadow effects etc.
Make sure to avoid common image generation mistakes.
Reply with only one answer, no descrition and in plaintext.
Make sure your prompt is detailed and creative descriptions that will inspire unique and interesting images from the AI.
\n\ntext:{user_prompt} """
response = llm_text_gen(prompt)
return response

View File

@@ -1,35 +1,28 @@
import base64
import datetime
import os
import requests
from PIL import Image
import logging
def save_generated_image(img_generation_response, image_dir):
def save_generated_image(img_generation_response):
"""
Save generated images for blog, ensuring unique names for SEO.
"""
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
generated_image_name = f"generated_image_{datetime.datetime.now():%Y-%m-%d-%H-%M-%S}.png"
generated_image_filepath = os.path.join(image_dir, generated_image_name)
generated_image_url = img_generation_response.data[0].url
generated_image_name = f"generated_image_{datetime.datetime.now():%Y-%m-%d-%H-%M-%S}.webp"
generated_image_filepath = os.path.join(os.getenv('IMG_SAVE_DIR'), generated_image_name)
logger.info(f"Fetch the image from url: {generated_image_url}")
try:
response = requests.get(generated_image_url, stream=True)
response.raise_for_status()
with open(generated_image_filepath, "wb", encoding="utf-8") as image_file:
image_file.write(response.content)
for i, image in enumerate(img_generation_response["artifacts"]):
with open(generated_image_filepath, "wb") as f:
f.write(base64.b64decode(image["base64"]))
except requests.exceptions.RequestException as e:
logger.error(f"Failed to get generated image content: {e}")
return None
logger.info(f"Saved image at path: {generated_image_filepath}")
if os.environ.get('DISPLAY', ''): # Check if display is supported
img = Image.open(generated_image_filepath)
img.show()
return generated_image_filepath

Binary file not shown.

View File

@@ -8,6 +8,8 @@ from prompt_toolkit import prompt
from prompt_toolkit.completion import WordCompleter
from prompt_toolkit.validation import Validator, ValidationError
from prompt_toolkit.shortcuts import radiolist_dialog
import typer
from rich import print
from lib.ai_web_researcher.gpt_online_researcher import gpt_web_researcher
from lib.ai_web_researcher.metaphor_basic_neural_web_search import metaphor_find_similar
@@ -15,7 +17,7 @@ from lib.ai_writers.keywords_to_blog import write_blog_from_keywords
from lib.ai_writers.speech_to_blog.main_audio_to_blog import generate_audio_blog
from lib.gpt_providers.text_generation.ai_story_writer import ai_story_generator
from lib.gpt_providers.text_generation.ai_essay_writer import ai_essay_generator
from lib.gpt_providers.text_to_image_generation.generate_image_from_prompt import generate_image
from lib.gpt_providers.text_to_image_generation.main_generate_image_from_prompt import generate_image
def blog_from_audio():
@@ -270,7 +272,21 @@ def image_generator():
print("Choose between:: Stable-Diffusion, Dalle2, Dalle3")
img_model = prompt('Choose the image model to use for generation: ', completer=img_models, validator=ModelTypeValidator())
print(f"{img_prompt}----{img_model}")
if 'Stability-Stable-Diffusion' in img_model:
api_key = 'STABILITY_API_KEY'
elif 'Dalle3' in img_model:
api_key = 'OPENAI_API_KEY'
if os.getenv(api_key) is None:
print(f"\n\n[bold green] 🙋 Get {api_key} Here:https://platform.stability.ai/docs/getting-started 🙋 -- \n")
user_input = typer.prompt(f"💩 -**Please Enter(copy/paste) {api_key} Key** - Here🙋:")
os.environ[api_key] = user_input
try:
with open(".env", "a") as env_file:
env_file.write(f"{api_key}={user_input}\n")
print(f"✅ API Key added to .env file.")
except Exception as err:
print(f"Error: {err}")
try:
generate_image(img_prompt, img_model)
except Exception as err:

Binary file not shown.

After

Width:  |  Height:  |  Size: 855 KiB

View File

@@ -0,0 +1,233 @@
2201.11990
2210.02414
2112.11446v2
2203.15556
2201.08239
2204.06745
2305.10403
2307.09288
2208.11857
2302.12095
1905.00537
2209.12356
2301.08745
2302.10198
2009.03300
2208.03299
2212.13138
2201.11903
2211.14275
2001.08361
2001.08361
2307.01952
2206.07682
2302.06476
2206.04615
2211.02011
2212.10403
2303.17564
2204.02329
2205.10625
2205.09712
2206.02336
2206.06315
2207.10342
2209.14610
2210.03057
2209.07686
2210.03493
2210.02441
2210.07128
2210.11610
2212.08635
2212.09597
2212.09561
2212.10001
2212.10071
2301.13379
2302.00923
2302.00093
2302.12246
2303.11381
2305.04118
2305.11255
2305.17812
2301.13848
2311.16452
2303.08774
2304.01373
2302.13971v1
2303.04360
2208.10442
2302.13007
2303.15056
2302.04166
2303.12712
2303.11366
2308.12950
2306.08568
2107.03374
2305.06161
2305.07922
2203.13474
2204.02311
2302.13971
2303.17568
2203.07814
2301.03988
2305.02309
2207.01780
2301.13816
2307.04349
2207.10397
2304.05128
2306.09896
2306.02907
2108.07732
2306.03091
2308.10335
2312.17244
2305.02301
2305.15717
2310.02421
2305.11170
2309.00384
2310.06839
2312.04737
2309.14021
2312.07046
2308.07633
2305.17888
2306.08162
2309.05210
2308.14903
2310.19102
2311.09550
2311.00502
2312.08583
2305.11627
2301.00774
2212.09095
2310.01801
2310.01382
2310.08915
2310.09499
https://github.com/Significant-Gravitas/AutoGPT
https://github.com/gpt-engineer-org/gpt-engineer
https://github.com/reworkd/AgentGPT
https://github.com/geekan/MetaGPT
https://github.com/Josh-XT/AGiXT
https://github.com/litanlitudan/skyagi
https://github.com/joonspk-research/generative_agents
https://github.com/smol-ai/developer
https://github.com/Forethought-Technologies/AutoChain
https://github.com/TransformerOptimus/SuperAGI
https://github.com/homanp/superagent
https://github.com/a16z-infra/ai-town
https://github.com/AI-Engineer-Foundation/agent-protocol
https://github.com/microsoft/autogen
https://github.com/cpacker/MemGPT
https://github.com/shroominic/codeinterpreter-api
https://github.com/aiwaves-cn/agents
https://github.com/dataelement/bisheng
https://github.com/Maplemx/Agently
https://github.com/zilliztech/GPTCache
http://github.com//Significant-Gravitas/AutoGPT
http://github.com//AUTOMATIC1111/stable-diffusion-webui
http://github.com//gpt-engineer-org/gpt-engineer
http://github.com//lencx/ChatGPT
http://github.com//Pythagora-io/gpt-pilot
http://github.com//mouredev/Hello-Python
http://github.com//Bin-Huang/chatbox
http://github.com//getumbrel/llama-gpt
http://github.com//transitive-bullshit/chatgpt-api
http://github.com//python-telegram-bot/python-telegram-bot
http://github.com//skorch-dev/skorch
http://github.com//botpress/botpress
http://github.com//TransformerOptimus/SuperAGI
http://github.com//AMAI-GmbH/AI-Expert-Roadmap
http://github.com//babysor/MockingBird
http://github.com//gventuri/pandas-ai
http://github.com//hpcaitech/ColossalAI
http://github.com//LAION-AI/Open-Assistant
http://github.com//xitu/gold-miner
http://github.com//google-research/google-research
http://github.com//photoprism/photoprism
http://github.com//explosion/spaCy
http://github.com//StanGirard/quivr
http://github.com//microsoft/AI-For-Beginners
http://github.com//GitHubDaily/GitHubDaily
http://github.com//Lightning-AI/pytorch-lightning
http://github.com//lutzroeder/netron
http://github.com//bentoml/OpenLLM
http://github.com//cloneofsimo/lora
http://github.com//eosphoros-ai/DB-GPT
http://github.com//labring/FastGPT
http://github.com//Mintplex-Labs/anything-llm
http://github.com//danswer-ai/danswer
http://github.com//neuml/txtai
http://github.com//run-llama/rags
http://github.com//postgresml/postgresml
http://github.com//JushBJJ/Mr.-Ranedeer-AI-Tutor
http://github.com//s0md3v/roop
http://github.com//microsoft/generative-ai-for-beginners
http://github.com//leon-ai/leon
http://github.com//geekan/MetaGPT
http://github.com//jmorganca/ollama
http://github.com//run-llama/llama_index
http://github.com//milvus-io/milvus
http://github.com//chatchat-space/Langchain-Chatchat
http://github.com//zhayujie/chatgpt-on-wechat
http://github.com//mindsdb/mindsdb
http://github.com//FlowiseAI/Flowise
http://github.com//microsoft/unilm
http://github.com//mlabonne/llm-course
http://github.com//sweepai/sweep
http://github.com//lucidrains/imagen-pytorch
http://github.com//GokuMohandas/Made-With-ML
http://github.com//TabbyML/tabby
http://github.com//chroma-core/chroma
http://github.com//eugeneyan/open-llms
http://github.com//cleanlab/cleanlab
http://github.com//microsoft/semantic-kernel
http://github.com//ymcui/Chinese-LLaMA-Alpaca
http://github.com//mudler/LocalAI
http://github.com//mlc-ai/mlc-llm
http://github.com//THUDM/ChatGLM2-6B
http://github.com//langgenius/dify
http://github.com//vllm-project/vllm
http://github.com//ludwig-ai/ludwig
http://github.com//hiyouga/LLaMA-Factory
http://github.com//h2oai/h2ogpt
http://github.com//css-doodle/css-doodle
http://github.com//williamngan/pts
http://github.com//dair-ai/Prompt-Engineering-Guide
http://github.com//AI4Finance-Foundation/FinGPT
http://github.com//yzfly/awesome-chatgpt-zh
http://github.com//microsoft/promptflow
http://github.com//jina-ai/jina
http://github.com//deepset-ai/haystack
http://github.com//open-mmlab/mmagic
http://github.com//bentoml/BentoML
http://github.com//openvinotoolkit/openvino
http://github.com//reworkd/AgentGPT
http://github.com//logspace-ai/langflow
http://github.com//mayooear/gpt4-pdf-chatbot-langchain
http://github.com//activeloopai/deeplake
http://github.com//danny-avila/LibreChat
http://github.com//liaokongVFX/LangChain-Chinese-Getting-Started-Guide
http://github.com//kyrolabs/awesome-langchain
http://github.com//zilliztech/GPTCache
http://github.com//speechbrain/speechbrain
http://github.com//vercel/ai
http://github.com//baichuan-inc/Baichuan-7B
http://github.com//microsoft/autogen
http://github.com//f/awesome-chatgpt-prompts
http://github.com//xtekky/gpt4free
http://github.com//wechaty/wechaty
http://github.com//RasaHQ/rasa
http://github.com//lobehub/lobe-chat
http://github.com//GaiZhenbiao/ChuanhuChatGPT
http://github.com//gunthercox/ChatterBot
http://github.com//mamoe/mirai
http://github.com//haotian-liu/LLaVA

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB