Giter Site home page Giter Site logo

firehead90544 / craiyon.py Goto Github PK

View Code? Open in Web Editor NEW
70.0 5.0 21.0 83 KB

Unofficial API Wrapper for craiyon.com (DAL-E-MINI). Generate awesome images from text tokens.

License: Apache License 2.0

Python 100.00%
api-wrapper craiyon deeplearning image-generator craiyon-py dal-e-mini dalemini

craiyon.py's Introduction

craiyon.py

API Wrapper for craiyon (formerly DAL-E-MINI) to generate awesome images from text tokens.

Badges

Provided By: shields.io

PyPI Version PyPI Downloads Apache License 2.0 Connect On Discord Code Lines Code Size Pull Requests Issues Contributors

Acknowledgements

Authors

Installation

The easiest way to install craiyon.py is using pip

  pip install -U craiyon.py

Or just manually clone the repository and build the wheel

Versioning

The 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:

Model Speed Quality API_URL Import Name
v3 Fast (~50s) Best https://api.craiyon.com/v3 Craiyon
v2 (Removed) Fastest (<45s) Good https://api.craiyon.com/draw CraiyonV2
v1 Slow (~1m) Average https://backend.craiyon.com/generate CraiyonV1

Usage / Examples

Generate and save the images

from craiyon import Craiyon

generator = Craiyon() # Instantiates the api wrapper
result = 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 grin
result.save_images() # Saves the generated images to 'current working directory/generated', you can also provide a custom path

image

Use the images in your code without saving

from craiyon import Craiyon, craiyon_utils
from PIL import Image # pip install pillow
from io import BytesIO
import base64

generator = Craiyon() # Instantiates the api wrapper
result = generator.generate("Professional photo of Obama flashing a flag with his last name") # Generates 9 images by default and you cannot change that
print(result.description) # >>> Obama holding up a flag with his last name, smiling confidently
images = craiyon_utils.encode_base64(result.images)
for i in images:
    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 needs

image

Just get the Direct Image URLs

from craiyon import Craiyon

generator = Craiyon() # Instantiates the api wrapper
result = 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 one
for url in result.images:
    print(url)

Async Usage / Examples

Generate and save the images

from craiyon import Craiyon
import asyncio


async def main():
    generator = Craiyon() # Instantiates the api wrapper
    result = await generator.async_generate("Photorealistic image of shrek eating earth")
    await result.async_save_images() # Saves the generated images to 'current working directory/generated', you can also provide a custom path
    
asyncio.run(main())

Use with a Discord bot

from craiyon import Craiyon, craiyon_utils
import discord
from discord.ext import commands
from io import BytesIO
import base64

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(intents = intents, command_prefix="!")

generator = Craiyon() # Initialize Craiyon() class

@bot.event
async def on_ready():
    print(f"Successfully logged in as {bot.user.name}!")

# Create command
@bot.command()
async def genimage(ctx, *, prompt: str):
    await ctx.send(f"Generating prompt \"{prompt}\"...")
    
    generated_images = await generator.async_generate(prompt) # Generate images
    b64_list = await craiyon_utils.async_encode_base64(generated_images.images) # Download images from https://img.craiyon.com and store them as b64 bytestring object
    
    images1 = []
    for index, image in enumerate(b64_list): # Loop through b64_list, keeping track of the index
        img_bytes = BytesIO(base64.b64decode(image)) # Decode the image and store it as a bytes object
        image = discord.File(img_bytes)
        image.filename = f"result{index}.webp"
        images1.append(image) # Add the image to the images1 list
        
    await ctx.reply(files=images1) # Reply to the user with all 9 images in 1 message
        

bot.run("your_token_here")

Generation parameters

# These parameters are only supported by the v3 model
from craiyon import Craiyon
generator = 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 version

Specify custom tokens/model versions

from craiyon import Craiyon # Importing the v3 model

# api_token and model_version are not required, but recommended
generator = Craiyon(api_token="your-token-here", model_version="api-model-version")

# ...rest is the same stuff as above

api_token

  • If 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_version

  • Since 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 the Craiyon class 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 Compatibility

This 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 class Craiyon to CraiyonV1! 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.

Todo

Implement: 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]

Contributing

Contributions 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's code of conduct for contributions and contributors.

License

Apache License 2.0

craiyon.py's People

Contributors

firehead90544 avatar mdm9300404 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

craiyon.py's Issues

Why use a class for craiyon.py?

I think it's pretty pointless. It isn't used anywhere else and there's no reason to really use a class for it. Bad use of OOP

Unexpected minetype

Ignoring exception in on_application_command_error
Traceback (most recent call last):
File "D:\Programmierung\Python\Projekte\MiniGamesBot\venv\lib\site-packages\nextcord\client.py", line 490, in _run_event
await coro(*args, **kwargs)
File "D:\Programmierung\Python\Projekte\MiniGamesBot\bot\bot.py", line 193, in on_application_command_error
raise error
File "D:\Programmierung\Python\Projekte\MiniGamesBot\venv\lib\site-packages\nextcord\application_command.py", line 895, in invoke_callback_with_hooks
await self(interaction, *args, **kwargs)
File "D:\Programmierung\Python\Projekte\MiniGamesBot\venv\lib\site-packages\cooldowns\cooldown.py", line 109, in inner
result = await func(*args, **kwargs)
File "D:\Programmierung\Python\Projekte\MiniGamesBot\bot\cogs\Ki stuff.py", line 83, in imagine
result = await generator.async_generate(prompt)
File "D:\Programmierung\Python\Projekte\MiniGamesBot\venv\lib\site-packages\craiyon\craiyon.py", line 76, in async_generate
resp = await resp.json()
File "D:\Programmierung\Python\Projekte\MiniGamesBot\venv\lib\site-packages\aiohttp\client_reqrep.py", line 1104, in json
raise ContentTypeError(
aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: text/html; charset=utf-8', url=URL('https://api.craiyon.com/v3')
ERROR:0, message='Attempt to decode JSON with unexpected mimetype: text/html; charset=utf-8', url=URL('https://api.craiyon.com/v3')

cannot identify image file <_io.BytesIO object

Hi! Thanks for the API!

In your example: "Use the images in your code without saving", when I tried to run I obtained the following error:

UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000002AF3B048AE0>

The error points to the line: 5 image = Image.open(BytesIO(base64.decodebytes(i.encode("utf-8"))))

Do you know how to fix this?
Thanks in advance!

AttributeError: 'Craiyon' object has no attribute 'async_generate'

hello, i have some problems.
i am making discord bot, and i can't call async_generate

here's my code:

async def dall_e_mini(ctx, prompt: str):
    await ctx.response.defer()
    craiyon_gen = Craiyon()

    result = await craiyon_gen.async_generate(prompt)
    await result.async_save_images(path = f".\\temp\\{ctx.author.id}")

    files_to_upload = []

    for file in os.listdir(f'.\\temp\\{ctx.author.id}'):
        files_to_upload.append(discord.File(f'.\\temp\\{ctx.author.id}\\{file}'))
    
    await ctx.send(f'**{prompt}** - {ctx.author.mention}', files=files_to_upload)```

what i do not right?
i'm using Python 3.10.7

API_TOKEN

Craiyon's api_token seems to change whenever I sign in to the service, which breaks already working code. Is there any stable way of getting a persistent api_token instead?

urls feature not working

generator = Craiyon() # Instantiates the api wrapper
result = 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

for url in result.images:
print(url)

when i run the code i get this error ;
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\models.py", line 971, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\json_init_.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "e:\FILES\Documents\Python projects\ELLIOT-BOT\gen 2\advanced_AI.py", line 153, in
result = generator.generate("Photorealistic image of shrek eating earth")
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\craiyon\craiyon.py", line 48, in generate
resp = resp.json()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\models.py", line 975, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

i dont know here is the prob here

v3 image generation throws a 403 error

When trying to create images using the standard Craiyon client, it throws a 403 error from axios. Curious if this is caused by not having an API key with a paid subscription, or if they are down. Has been happening for at least 3 days now.
Screenshot 2023-08-02 121148

Error while used for discord bot

Code:

@app_commands.command(name="texttoimg",description="Give your prompt to generate image")
    async def texttoimg(self,interaction: discord.Interaction,prompt: str):
        # await interaction.response.send_message("Generating: "+prompt+" ....")
        generator = Craiyon()
        generated_images = await generator.async_generate(prompt)
        b64_list = await craiyon_utils.async_encode_base64(generated_images.images) 
        
        images1 = []
        for index, image in enumerate(b64_list):
            img_bytes = BytesIO(base64.b64decode(image)) 
            # image = discord.File(img_bytes)
            image =  discord.File(img_bytes)
            image.filename = f"result{index}.webp"
            images1.append(image)         
        await interaction.response.send_message(files=images1)

Output:

image

If i print in console it works well, but doesn't send img to discord

V3

Pls update it to v3

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Just recently got the time to test it out again for myself. I ran 'pip install -U craiyon.py', so it's updated. Unfortunately, this still seems to be a problem. The error of 'json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)' still persists. I'm unable to get something as simple as the following to work:

from craiyon import Craiyon
generator = Craiyon()
result = generator.generate("Photorealistic image of shrek eating earth", negative_prompt="spoon", model_type="art")
print(result.description)
result.save_images()

This, of course, is just one of the examples given in the Craiyon API documentation on how to use it. Any fix would be appreciated!

Originally posted by @IbyCodes in #18 (comment)

Json Decode Error

Hi guys,
i have a problem.

File "/mount/src/generatore-arte-ia/Craiyon.py", line 13, in Generate
    result = generator.generate(request)
File "/home/adminuser/venv/lib/python3.9/site-packages/craiyon/craiyon.py", line 182, in generate
    return GeneratedImages(resp.json()['images'], model="v1")
File "/home/adminuser/venv/lib/python3.9/site-packages/requests/models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)

I have try v3 and v1 in all version ( async and not) and the result is always this .

How i can resolve ?

Mimetype error with async functions

The switch to CloudScraper worked for the standard generate function. However, the async functions are still throwing Attempt to decode JSON with unexpected mimetype: text/html; charset=utf-8'.

Recommend using a library like aiocfscrape to support CloudScraper's functionality for the async functions.

v2

What do i have to modify in order to use the V2 model?

`from craiyon import Craiyon
from PIL import Image
from io import BytesIO
import streamlit as st
import base64

def Generate(request):
generator = Craiyon() # Instantiates the api wrapper
result = generator.generate(request) # Generates 9 images by default
images = result.images
return images

generator = Craiyon()
request = "a dragon on a motorbike"
image_files = Generate(request)
st.image(Image.open(BytesIO(base64.decodebytes(image_files[0].encode("utf-8")))))`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.