Package: craiyon.py
craiyon.pyAPI Wrapper forcraiyon(formerly DAL-E-MINI) to generate awesome images from text tokens.BadgesProvided By:shields.ioAcknowledgementsIssuesPull RequestsView Project On PyPIAuthors@Rudransh Joshi@mdm9300404InstallationThe easiest way to install craiyon.py is using pippipinstall-Ucraiyon.pyOr just manually clone the repository and build the wheelVersioningThe api wrapper has separate classes revolving around each model, i.e, the Craiyon v1 and v3.
The v2 model has been removed from the api, so the model class around it is also deprecated by commenting it out.
A quick comparison is given below:ModelSpeedQualityAPI_URLImport Namev3Fast (~50s)Besthttps://api.craiyon.com/v3Craiyonv2 (Removed)Fastest (<45s)Goodhttps://api.craiyon.com/drawCraiyonV2v1Slow (~1m)Averagehttps://backend.craiyon.com/generateCraiyonV1Usage / ExamplesGenerate and save the imagesfromcraiyonimportCraiyongenerator=Craiyon()# Instantiates the api wrapperresult=generator.generate("Photorealistic image of shrek eating earth",negative_prompt="spoon",model_type="art")print(result.description)# Description about the generated images # >>> Shrek devouring planet Earth with a sinister grinresult.save_images()# Saves the generated images to 'current working directory/generated', you can also provide a custom pathUse the images in your code without savingfromcraiyonimportCraiyon,craiyon_utilsfromPILimportImage# pip install pillowfromioimportBytesIOimportbase64generator=Craiyon()# Instantiates the api wrapperresult=generator.generate("Professional photo of Obama flashing a flag with his last name")# Generates 9 images by default and you cannot change thatprint(result.description)# >>> Obama holding up a flag with his last name, smiling confidentlyimages=craiyon_utils.encode_base64(result.images)foriinimages:image=Image.open(BytesIO(base64.decodebytes(i)))# To convert the .webp images to .jpg or .png, you can proceed like this# image.convert("RGB").save("image.jpg", "JPEG") # For ".jpg" images# image.convert("RGBA").save("image.png", "PNG") # For ".png" images# Use the PIL's Image object as per your needsJust get the Direct Image URLsfromcraiyonimportCraiyongenerator=Craiyon()# Instantiates the api wrapperresult=generator.generate("Photorealistic image of shrek eating earth")print(result.images)# Prints a list of the Direct Image URLs hosted on https://img.craiyon.com# Loops through the list and prints each image URL one by oneforurlinresult.images:print(url)Async Usage / ExamplesGenerate and save the imagesfromcraiyonimportCraiyonimportasyncioasyncdefmain():generator=Craiyon()# Instantiates the api wrapperresult=awaitgenerator.async_generate("Photorealistic image of shrek eating earth")awaitresult.async_save_images()# Saves the generated images to 'current working directory/generated', you can also provide a custom pathasyncio.run(main())Use with a Discord botfromcraiyonimportCraiyon,craiyon_utilsimportdiscordfromdiscord.extimportcommandsfromioimportBytesIOimportbase64intents=discord.Intents.default()intents.message_content=Truebot=commands.Bot(intents=intents,command_prefix="!")generator=Craiyon()# Initialize Craiyon() class@bot.eventasyncdefon_ready():print(f"Successfully logged in as{bot.user.name}!")# Create command@bot.command()asyncdefgenimage(ctx,*,prompt:str):awaitctx.send(f"Generating prompt\"{prompt}\"...")generated_images=awaitgenerator.async_generate(prompt)# Generate imagesb64_list=awaitcraiyon_utils.async_encode_base64(generated_images.images)# Download images from https://img.craiyon.com and store them as b64 bytestring objectimages1=[]forindex,imageinenumerate(b64_list):# Loop through b64_list, keeping track of the indeximg_bytes=BytesIO(base64.b64decode(image))# Decode the image and store it as a bytes objectimage=discord.File(img_bytes)image.filename=f"result{index}.webp"images1.append(image)# Add the image to the images1 listawaitctx.reply(files=images1)# Reply to the user with all 9 images in 1 messagebot.run("your_token_here")Generation parameters# These parameters are only supported by the v3 modelfromcraiyonimportCraiyongenerator=Craiyon()result=generator.generate("prompt here",negative_prompt="cap",model_type="art")# The negative prompt and model type are optional parameters# Negative prompt helps filtering out certain things from the images that will be generated, defaults to empty string ""# Model type can be "art", "drawing", "photo" and "none", defaults to "none". It does as it's name suggests.# result.description returns a brief description generated by craiyon about the generated images# result.images returns the list of images generated (v3 returns the image links, v1 returns the base64 image data)# result.model returns the model versionSpecify custom tokens/model versionsfromcraiyonimportCraiyon# Importing the v3 model# api_token and model_version are not required, but recommendedgenerator=Craiyon(api_token="your-token-here",model_version="api-model-version")# ...rest is the same stuff as aboveapi_tokenIf you bought a paid subscription to Craiyon.com, you would know that the watermark is removed. If you wish to have the watermark removed from the generated images in your application as well, you can specify a token here.To find your token: Open Google Chrome, go to craiyon.com (make sure you're logged in), Press F12, go to the Network tab, make sure the record button looks like a red circle at the top-left. Put your prompt in the text box and press the orange "Draw" button. Two "draw" items should appear on the left, under "name". One of them will have a "Payload" tab next to "Headers" and "Preview", as well as above "General". Click it, and your token is listed there.model_versionSince Craiyon is still training their V2 model, it is improving every day. We recommend putting your own model version here to get the newest and best model they have at the moment.To get the model version, follow the steps for the api_token listed above, except copy the "version" instead of the "token". Then, just pass it in as an argument while instantiating theCraiyonclass and you're ready!While this is recommended, it is not required. If you do not pass a custom model version, this value will automatically default to "35s5hfwn9n78gb06", which is Craiyon's newest model as of March 10, 2023.Backwards CompatibilityThis library is fully backwards-compatible with older versions.If you were previously using this library before we added support for Craiyon's V3 model and you wish to continue using the old V1 model, simply change the name of the classCraiyontoCraiyonV1! Otherwise, you can update your application to the V3 model by reading the code samples above. Please note that the v2 model used to work earlier, but Craiyon completely removed their v2 model from the api and there are no traces of it left, so we currently removed the CraiyonV2 class. Please use either the v1 or v3 model instead.TodoImplement: Upscale (https://api.craiyon.com/upscale)Payload Format:{"image_id":"date/imagepath.ext","model":"none","negative_prompt":"","prompt":"","token":"null","version":"c4ue22fb7kb6wlac"}Upscaled:"https://pics.craiyon.com/" + resp.json()["images"][0]ContributingContributions are always welcome!Fork this repository.Make the changes in your forked repositry.Make sure to fetch upstream before generating a PR.Generate a pull request.Please adhere to the GitHub'scode of conductfor contributions and contributors.LicenseApache License 2.0