mirror of
https://github.com/brycedrennan/imaginAIry
synced 2024-10-31 03:20:40 +00:00
9b95e8b0b6
- do not provide automatically imported api functions and objects in `imaginairy` root module - horrible hack to overcome horrible design choices by easy_install/setuptools The hack modifies the installed script to remove the __import__ pkg_resources line If we don't do this then the scripts will be slow to start up because of pkg_resources.require() which is called by setuptools to ensure the "correct" version of the package is installed. before modification example: ``` __requires__ = 'imaginAIry==14.0.0b5' __import__('pkg_resources').require('imaginAIry==14.0.0b5') __file__ = '/home/user/projects/imaginairy/imaginairy/bin/aimg' with open(__file__) as f: exec(compile(f.read(), __file__, 'exec')) ```
39 lines
931 B
Python
39 lines
931 B
Python
import base64
|
|
from io import BytesIO
|
|
|
|
from imaginairy.api import imagine
|
|
|
|
|
|
def generate_image(prompt):
|
|
"""ImaginePrompt to generated image."""
|
|
result = next(imagine([prompt]))
|
|
img = result.images["generated"]
|
|
img_io = BytesIO()
|
|
img.save(img_io, "JPEG")
|
|
img_io.seek(0)
|
|
return img_io
|
|
|
|
|
|
def generate_image_b64(prompt):
|
|
"""ImaginePrompt to generated base64 encoded image."""
|
|
img_io = generate_image(prompt)
|
|
img_base64 = base64.b64encode(img_io.getvalue())
|
|
return img_base64
|
|
|
|
|
|
class Base64Bytes(bytes):
|
|
@classmethod
|
|
def __get_validators__(cls):
|
|
yield cls.validate
|
|
|
|
@classmethod
|
|
def validate(cls, v, info):
|
|
if isinstance(v, bytes):
|
|
return v
|
|
if isinstance(v, str):
|
|
return base64.b64decode(v)
|
|
raise ValueError("Byte value must be either str or bytes")
|
|
|
|
def __str__(self):
|
|
return base64.b64encode(self).decode()
|