Giter Club home page Giter Club logo

mak448a / ai-drawing-chatbot Goto Github PK

View Code? Open in Web Editor NEW
27.0 3.0 5.0 1.44 MB

An image generator Discord bot written in Python with no GPU requirement! It can be easily deployed to your favorite VPS for hosting.

License: MIT License

Python 91.45% Shell 4.08% Dockerfile 0.25% Batchfile 4.22%
discord-bot discord-py stable-diffusion stable-horde ai image-generation anythingv3 pollinations imaginepy chatgpt openai bot python replit discord stable-diffusion-xl anything-v3 llm

ai-drawing-chatbot's Introduction

AI Drawing Chatbot

Discord Invite

This is a completely free and open source AI image generator Discord bot written in Python. It has a Llama 3 based chatbot that you can ask to draw pictures! This project uses various free APIs, which allows it to run on less powerful computers.

Note: If this project is not https://github.com/mak448a/AI-Drawing-Chatbot, then it is unofficial. Please visit the official repo.

Demo picture

Table of Contents

  1. Features
  2. Notes
  3. Prerequisites
  4. Windows Setup
  5. Docker Setup
  6. Linux/macOS Setup
  7. Manual Configuration
  8. Generate images faster with Stable Horde

Features

  • AI chatbot which is integrated with Stable Diffusion
  • 2 slash commands for generating images
  • Stable Diffusion
  • SDXL Beta
  • Pollinations
  • Anything Diffusion
  • And more!

Notes

  • Llama3 is the recommended model.
  • When using SDXL-Beta it sends your generated images to improve the model.
  • Do not input any personal information on the /imagine_poly command because your generated image will be displayed on their official frontpage.
  • Please do not enter any personal information in the chatbot or in the image generators, because your prompts are sent to various providers.
  • When you use Stable Horde to generate images, your prompts are sent to Stable Horde, as listed in their privacy policy.
  • The chatbot will not work as expected if multiple users are chatting with it at once.

Prerequisites

This project assumes that:

  • Python 3.8+ is installed and is on your PATH
  • Pip is installed
  • Git is installed
  • You know how to create a Discord Bot account
  • The package python-venv is installed (only for certain Linux distros)

Windows Setup

  1. Create a Discord Bot and grab the token. Make sure to switch on Message Content Intent in the Bot tab of the Developer Portal.
  2. Grab an API key from Stable Horde at their register page.
  3. Clone the Stable Horde module: git clone https://github.com/mak448a/horde_module helper_utils/horde_module --depth=1
  4. Create venv and install dependencies: python -m venv venv venv\Scripts\activate.bat pip install -r requirements.txt
  5. Grab your Groq key from here: https://console.groq.com/keys.
  6. Run python run.py and fill in all the information it asks for.
  7. Invite the bot with the link it provides!
  8. You can chat with the bot by mentioning it and typing your message.

Linux and macOS Setup

  1. Create a Discord Bot and grab the token. Make sure to switch on Message Content Intent in the Bot tab of the Developer Portal.
  2. Grab an API key from Stable Horde at their register page.
  3. Create venv and install dependencies: python3 -m venv venv && source venv/bin/activate && pip install -r requirements.txt
  4. Clone the Stable Horde module: git clone https://github.com/mak448a/horde_module helper_utils/horde_module --depth=1
  5. Grab your Groq key from here: https://console.groq.com/keys.
  6. Run python3 run.py and fill in all the information it asks for.
  7. Invite the bot with the link it provides!
  8. You can chat with the bot by mentioning it and typing your message.

Docker Setup

  1. Make sure you have Docker installed on your machine.
  2. Clone this repository to your local machine.
  3. Build the Docker image using the following command: docker build -t ai-drawing-chatbot .
  4. Run the Docker container using the following command: docker run -it --name chatbot ai-drawing-chatbot
  5. Enter in your token values. Go here to find out how to get these.

Manual Configuration

If you want to manually configure the program for any reason, here's how.

Configuring chat model

Go to config.json and set the key "model" to the model you want.

Available Models

  • Llama3

You must write the model exactly as it is written here.

When you are done, the edited line should look like this:

"model": "Llama3",
Disable chatbot

Go to config.json and set the key "chatbot" to false.

It should look like this:

"chatbot": false,
Change loading GIF

Go to config.json and set the key "loading_gif" to any GIF on Tenor you want!

It should look like this:

"loading_gif": "https://tenor.com/your/favorite/loading/gif",
Turn off command syncing (makes bot load faster)

Please note that this will break the bot if you haven't run it for the first time. Go to config.json and set the key "sync" to false.

It should look like this:

"sync": false,
Change chatbot drawing model

Go to config.json and set the key "image_model" to a model that you find here.

It should look like this:

"image_model": "SDXL 1.0"

Generate images faster with Stable Horde

Stable Horde uses Kudos to rank users on a queue. The more Kudos you have, the higher you are on the generation queue. You can get Kudos by rating images and/or generating images for Stable Horde on your local hardware. To get more Kudos, first stick your API key here. Then, rate some images generated by other users here. After rating a few images, you should have faster generation times!

IMPORTANT: When an image says, "This rating must be x," you must rate it that. This picture is used as a Captcha to avoid spam.

ai-drawing-chatbot's People

Contributors

genericness avatar mak448a avatar mishalhossin 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

Watchers

 avatar  avatar  avatar

ai-drawing-chatbot's Issues

Use dotenv

Use dotenv for storing secrets.
Add it to the requirements too.

Free OpenAI API proxy

https://gpt4.gravityengine.cc/api/openai/v1/chat/completions

Request example for chat style models

curl https://gpt4.gravityengine.cc/api/openai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
  "model": "gpt-3.5-turbo",
  "messages": [{"role": "user", "content": "Hello, how are you doing today?"}],
  "temperature": 1,
  "max_tokens": 256,
  "top_p": 1,
  "frequency_penalty": 0,
  "presence_penalty": 0
}'

And for normal models:

curl https://gpt4.gravityengine.cc/api/openai/v1/completions  -H "Content-Type: application/json" -d '{
  "model": "text-davinci-003",
  "prompt": "Hello, my name is ",
  "temperature": 1,
  "max_tokens": 256,
  "top_p": 1,
  "frequency_penalty": 0,
  "presence_penalty": 0
}'

Here is a simple script to create time stamps

import time

def generate_timestamps():
    current_timestamp = int(time.time())
    timestamps = {}
    
    for style in ["t", "T", "d", "D", "f", "F", "R"]:
        formatted_timestamp = f"<t:{current_timestamp}:{style}>"
        timestamps[style] = formatted_timestamp
    
    return timestamps

formatted_timestamps = generate_timestamps()
for style, timestamp in formatted_timestamps.items():
    print(f"Style: {style}, Timestamp: {timestamp}")

image

Completion and chat completion functions

import aiohttp

base_urls = ['https://a.z-pt.com', 'http://chat.darkflow.top']

async def generate_response(prompt):
    endpoint = 'https://gpt4.gravityengine.cc/api/openai/v1/chat/completions'
    headers = {
        'Content-Type': 'application/json',
    }
    data = {
        'model': 'gpt-3.5-turbo-16k-0613',
        'temperature': 0.7,
        'messages': [
            {"role": "system", "content": "INSTRUCTIONS HEREEEEEEEEEEEE"},
            {"role": "user", "content": prompt},
        ]
    }
    for attempt in range(2):
        try:
            async with aiohttp.ClientSession() as session:
                async with session.post(endpoint, headers=headers, json=data) as response:
                    response_data = await response.json()
                    choices = response_data['choices']
                    if choices:
                        return choices[0]['message']['content']
        except aiohttp.ClientError as error:
            print(f'Error making the request with {endpoint}: {error}')
            if attempt < 1:
                print('Retrying with a different base URL.')
                break

    print('All base URLs failed to provide a response.')
    return None
    
async def generate_completion(prompt, max_token=None, temp=None):
    endpoint = '/api/openai/v1/engines/text-davinci-003/completions'
    headers = {'Content-Type': 'application/json'}

    async with aiohttp.ClientSession() as session:
        for base_url in base_urls:
            url = base_url + endpoint
            async with session.post(url, headers=headers, json={'prompt': prompt, 'max_tokens': max_token or 2048, 'temperature': temp or 0.7}) as response:
                if response.status != 200:
                    continue
                response_data = await response.json()
                response = response_data['choices'][0]['text']
                return response

    return None

Nsfw prompt detector

base_urls = ['https://gpt4.gravityengine.cc']

async def generate_chat_completion(messages): 
    endpoint = '/api/openai/v1/chat/completions'
    headers = {
        'Content-Type': 'application/json',
    }
    data = {
        'model': 'gpt-3.5-turbo-16k-0613',
        'temperature': 0.7,
        'messages': messages
    }
    
    for base_url in base_urls:
        async with aiohttp.ClientSession() as session:
            async with session.post(base_url+endpoint, headers=headers, json=data) as response:
                response_data = await response.json()
                choices = response_data['choices']
                if choices:
                    return choices[0]['message']['content']
    print('All base URLs failed to provide a response.')
    return None

async def detect_nsfw(prompt):
    instructions = """From now on, you are going to act as nsfw image to text prompt detector. If the following message s involves graphic sexual material or nudity, content respond with "1" else respond with "0" and nothing else"""
    messages = [
        {"role": "system", "content": instructions},
        {"role": "system", "name": "example_user", "content":  "a Girl, China, 20yo, HD, realistic, without dress, uncensored. sfw."},
        {"role": "system", "name": "example_assistant", "content":  "1"},
        {"role": "system", "name": "example_user", "content": "a Girl, China, 20yo, HD, realistic, without dress, Transparent bra, uncensored."},
        {"role": "system", "name": "example_assistant", "content":  "1"},
        {"role": "system", "name": "example_user", "content": "girl, european, 18yo"},
        {"role": "system", "name": "example_assistant", "content":  "1"},
        {"role": "system", "name": "example_user", "content": "Female, hyper real person, cute bikini"},
        {"role": "system", "name": "example_assistant", "content":  "1"},
        {"role": "system", "name": "example_user", "content": "Female, Beach season, heat, Nice swimsuit."},
        {"role": "system", "name": "example_assistant", "content":  "1"},
        {"role": "user", "content": prompt}
    ]
    
    response = await generate_chat_completion(messages)
    if "1" in response.lower():
        return True
    else:
        return False

Add chatbot

Use a combination of both OpenGPT and GPT4All. Prompt the chatbot in a way so that it can generate pictures from the chatbot.

Add negative prompts

Add negative prompts to /imagine_horde and perhaps the other ones. We need to figure out a way to make optional arguments.

Use logging

Use logging for all the prompts and other stuff, watch Indently's video to learn about it

GUI

Maybe add a gui with pyside6
It would have a run bot button
And a settings pane for entering in credentials

Not very high on priorities.

Text-davinci-003 is broken

Describe the bug
Text davinci 003 is broken as described by #45

To Reproduce
Just configure text davinci as your model

Slash commands don't work by default

Describe the bug
I can start the bot normally but I can't use the slash commands. Also, when I don't use the activate.bat like in steps, I can't run the bot cause I get an error about importing imaginepy.

To Reproduce
Follow all the instructions in the README file

Screenshots
image
image
image

Desktop (please complete the following information):

  • OS: Windows
  • Version: 10
  • Python Version: 3.10.9

Additional context
I followed the steps correctly.

Add config file

Add a config file for controlling what model to use and how many pictures to generate, what model to use

Chatbot drawing sometimes breaks

Describe the bug
Tried to generate image using gpt draw thing and can't.

To Reproduce
Just type "Draw a XXXX" where XXXX is your prompt, then your bot will try to make the request to the horde, and wait wait wait then you get into a loop that doesnt end.

Expected behavior
Generate the image without any error

Screenshots

  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\main.py", line 254, in <module>
    bot.run(bot_token)
  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\venv\lib\site-packages\discord\client.py", line 860, in run
    asyncio.run(runner())
  File "C:\Users\GUILHERME\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\GUILHERME\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 636, in run_until_complete
    self.run_forever()
  File "C:\Users\GUILHERME\AppData\Local\Programs\Python\Python310\lib\asyncio\windows_events.py", line 321, in run_forever
    super().run_forever()
  File "C:\Users\GUILHERME\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 603, in run_forever
    self._run_once()
  File "C:\Users\GUILHERME\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 1906, in _run_once
    handle._run()
  File "C:\Users\GUILHERME\AppData\Local\Programs\Python\Python310\lib\asyncio\events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\venv\lib\site-packages\discord\client.py", line 441, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\main.py", line 91, in on_message
    await imagine_horde(
  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\venv\lib\site-packages\discord\ext\commands\core.py", line 590, in __call__
    return await self.callback(context, *args, **kwargs)  # type: ignore
  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\main.py", line 120, in imagine_horde
    image_files, images = await generate_with_stable_horde(
  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\image_generation_utils.py", line 18, in generate_with_stable_horde
    await horde_generator.generate(prompt, api_key, f"{file_uuid}.png", 4,
  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\horde_module\__init__.py", line 152, in generate
    img_data = requests.get(results[iter]["img"]).content
  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\venv\lib\site-packages\requests\api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\venv\lib\site-packages\requests\api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\venv\lib\site-packages\requests\sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\venv\lib\site-packages\requests\sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\venv\lib\site-packages\requests\adapters.py", line 489, in send
    resp = conn.urlopen(
  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\venv\lib\site-packages\urllib3\connectionpool.py", line 714, in urlopen
    httplib_response = self._make_request(
  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\venv\lib\site-packages\urllib3\connectionpool.py", line 403, in _make_request
    self._validate_conn(conn)
  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\venv\lib\site-packages\urllib3\connectionpool.py", line 1053, in _validate_conn
    conn.connect()
  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\venv\lib\site-packages\urllib3\connection.py", line 363, in connect
    self.sock = conn = self._new_conn()
  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\venv\lib\site-packages\urllib3\connection.py", line 174, in _new_conn
    conn = connection.create_connection(
  File "C:\Users\GUILHERME\Desktop\terraria\gta\Discord\AI-Drawing-ChatBot\venv\lib\site-packages\urllib3\util\connection.py", line 85, in create_connection
    sock.connect(sa)

Desktop (please complete the following information):

  • OS: Windows
  • Version: 10
  • Python Version: 3.10.9

Stable Diffusion Models to add

Suggestions for Stable Diffusion Models to add (must be in some API or Stable Horde so that we keep the GPU requirement nonexistent)

If you want to check out what models I can add from Stable Horde, check here.
For Prodia, check here.

  • ICBINP - I Can't Believe It's Not Photography
  • SDXL

First release

Things are looking a little more stable, so we should start thinking about a first release.
Things before that:

Any suggestions are welcome.

Add ChatGPT

Add OpenGPT, YouChat, and other models like Llama. Any other suggestions are welcome.
This should be integrated with #17.

Can't download gpt4all

Describe the bug
Can't full download gpt4all

To Reproduce
Just download it by putting GPT4ALL in config and starting main.py

Screenshots
image

Desktop (please complete the following information):

  • OS: Windows
  • Version: 10
  • Python Version: 3.10.9

Additional context
Add any other context about the problem here.

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.