WIP- Under maintenence- Web research working.
This commit is contained in:
76
lib/text_to_image/generate_image_from_prompt.py
Normal file
76
lib/text_to_image/generate_image_from_prompt.py
Normal file
@@ -0,0 +1,76 @@
|
||||
#########################################################
|
||||
#
|
||||
# This module will generate images for the blogs using APIs
|
||||
# from Dall-E and other free resources. Given a prompt, the
|
||||
# images will be stored in local directory.
|
||||
# Required: openai API key.
|
||||
#
|
||||
#########################################################
|
||||
|
||||
# imports
|
||||
import sys
|
||||
import datetime
|
||||
|
||||
import openai # OpenAI Python library to make API calls
|
||||
import os # used to access filepaths
|
||||
from loguru import logger
|
||||
logger.remove()
|
||||
logger.add(sys.stdout,
|
||||
colorize=True,
|
||||
format="<level>{level}</level>|<green>{file}:{line}:{function}</green>| {message}"
|
||||
)
|
||||
|
||||
from .gpt_providers.openai_gpt_provider import generate_dalle2_images, generate_dalle3_images, openai_chatgpt
|
||||
from .stabl_diff_img2html import generate_stable_diffusion_image
|
||||
|
||||
|
||||
def generate_image(user_prompt, image_dir, image_engine="dalle3"):
|
||||
"""
|
||||
The generation API endpoint creates an image based on a text prompt.
|
||||
|
||||
Required inputs:
|
||||
prompt (str): A text description of the desired image(s). The maximum length is 1000 characters.
|
||||
|
||||
Optional inputs:
|
||||
--> image_engine: dalle2, dalle3, stable diffusion are supported.
|
||||
--> num_images (int): The number of images to generate. Must be between 1 and 10. Defaults to 1.
|
||||
--> size (str): The size of the generated images. Must be one of "256x256", "512x512", or "1024x1024".
|
||||
Smaller images are faster. Defaults to "1024x1024".
|
||||
-->response_format (str): The format in which the generated images are returned.
|
||||
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.
|
||||
"""
|
||||
logger.info(f"Generated blog images will be stored at: {image_dir=}")
|
||||
|
||||
img_prompt = generate_img_prompt(user_prompt)
|
||||
# call the OpenAI API to generate image from prompt.
|
||||
logger.info(f"Calling openai.image.generate with prompt: {img_prompt}")
|
||||
|
||||
if 'dalle2' in image_engine:
|
||||
image_stored_at = generate_dalle2_images(img_prompt, image_dir)
|
||||
elif 'dalle3' in image_engine:
|
||||
image_stored_at = generate_dalle3_images(img_prompt, image_dir)
|
||||
elif 'stable_diffusion' in image_engine:
|
||||
image_stored_at = generate_stable_diffusion_image(img_prompt, image_dir)
|
||||
|
||||
return image_stored_at
|
||||
|
||||
|
||||
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 = openai_chatgpt(prompt)
|
||||
return response
|
||||
66
lib/text_to_image/stabl_diff_img2html.py
Normal file
66
lib/text_to_image/stabl_diff_img2html.py
Normal file
@@ -0,0 +1,66 @@
|
||||
import os
|
||||
import io
|
||||
import warnings
|
||||
from PIL import Image
|
||||
from stability_sdk import client
|
||||
import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
|
||||
|
||||
# Set the host URL environment variable. Ensure it doesn't have 'https' or a trailing slash.
|
||||
os.environ['STABILITY_HOST'] = 'grpc.stability.ai:443'
|
||||
|
||||
# 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
|
||||
|
||||
|
||||
def generate_stable_diffusion_image(prompt, image_dir):
|
||||
"""
|
||||
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.
|
||||
"""
|
||||
try:
|
||||
# Initialize the StabilityInference client with the API key and other settings.
|
||||
stability_api = client.StabilityInference(
|
||||
key=os.environ['STABILITY_KEY'], # Reference to the API key.
|
||||
verbose=True, # Enable verbose mode for debug messages.
|
||||
engine="stable-diffusion-xl-1024-v1-0", # Engine used for generation.
|
||||
)
|
||||
|
||||
# Generating the image with specified parameters.
|
||||
answers = stability_api.generate(
|
||||
prompt=prompt,
|
||||
seed=4253978046, # Deterministic seed for reproducible results.
|
||||
steps=50, # Number of inference steps.
|
||||
cfg_scale=7.0, # Strength of prompt matching.
|
||||
width=1024, height=1024, # Image dimensions.
|
||||
samples=1, # Number of images to generate.
|
||||
sampler=generation.SAMPLER_K_DPMPP_2M # Denoising sampler selection.
|
||||
)
|
||||
|
||||
# Process responses and save images.
|
||||
for resp in answers:
|
||||
for artifact in resp.artifacts:
|
||||
if artifact.finish_reason == generation.FILTER:
|
||||
warnings.warn(
|
||||
"Request activated safety filters. Modify the prompt and retry."
|
||||
)
|
||||
if artifact.type == generation.ARTIFACT_IMAGE:
|
||||
img = Image.open(io.BytesIO(artifact.binary))
|
||||
img_name = os.path.join(image_dir, f"{artifact.seed}.png")
|
||||
img.show()
|
||||
img.save(img_name) # Save the image with the seed in the filename.
|
||||
|
||||
except Exception as e:
|
||||
raise Exception(f"Error during image generation or saving: {e}")
|
||||
|
||||
# Example usage:
|
||||
# generate_stable_diffusion_image("A futuristic cityscape", "/path/to/save/images/")
|
||||
|
||||
Reference in New Issue
Block a user