Skip to content

Commit

Permalink
v0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
wwakabobik committed Nov 20, 2023
1 parent 12a3ba0 commit 3204866
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 8 deletions.
1 change: 1 addition & 0 deletions examples/image_generation/dalle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from examples.creds import oai_token, oai_organization


dalle = DALLE(auth_token=oai_token, organization=oai_organization)


Expand Down
6 changes: 3 additions & 3 deletions examples/image_generation/gpt_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Copyright (c) 2023. All rights reserved.
Created: 15.10.2023
Last Modified: 17.10.2023
Last Modified: 20.11.2023
Description:
This file contains testing functions for ChatGPT function calling using DALLE and Leonardo experiments
Expand All @@ -18,7 +18,7 @@
from leonardo_api import Leonardo
from openai_python_api import DALLE

from examples.creds import oai_token, oai_organization, openweathermap_appid
from examples.creds import oai_token, oai_organization, openweathermap_appid, leonardo_token


def get_weather(city, units):
Expand Down Expand Up @@ -78,7 +78,7 @@ def draw_image(prompt):
:param prompt: (str) Prompt, the description, what should be drawn and how
:return: (dict) dict with url of image
"""
leonardo = Leonardo(auth_token="a0178171-c67f-4922-afb3-458f24ecef1a")
leonardo = Leonardo(auth_token=leonardo_token)
leonardo.get_user_info()
response = leonardo.post_generations(
prompt=prompt,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
# -*- coding: utf-8 -*-
"""
Filename: test_leonardo.py
Filename: leonardo_test.py
Author: Iliya Vereshchagin
Copyright (c) 2023. All rights reserved.
Created: 15.10.2023
Last Modified: 17.10.2023
Last Modified: 20.11.2023
Description:
This file contains testing procedures for Leonardo experiments
"""

import json

import asyncio
from leonardo_api.leonardo_async import Leonardo

from examples.creds import leonardo_token


async def main():
"""Main function"""
leonardo = Leonardo(auth_token="a0178171-c67f-4922-afb3-458f24ecef1a")
leonardo = Leonardo(auth_token=leonardo_token)
response = await leonardo.get_user_info()
print(response)
prompt = "a beautiful necromancer witch resurrects skeletons against the backdrop of a burning ruined castle"
response = await leonardo.post_generations(
prompt="a beautiful necromancer witch resurrects skeletons against " "the backdrop of a burning ruined castle",
prompt=prompt,
num_images=1,
negative_prompt="bright colors, good characters, positive",
model_id="e316348f-7773-490e-adcd-46757c738eb7",
Expand All @@ -34,5 +38,4 @@ async def main():
response = await leonardo.wait_for_image_generation(generation_id=response["sdGenerationJob"]["generationId"])
print(json.dumps(response[0]["url"]))


asyncio.run(main())
39 changes: 39 additions & 0 deletions examples/image_generation/midjourney_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
"""
Filename: leonardo_test.py
Author: Iliya Vereshchagin
Copyright (c) 2023. All rights reserved.
Created: 15.10.2023
Last Modified: 20.11.2023
Description:
This file contains testing procedures for Midjourney experiments
"""

import asyncio

from utils.discord_watcher import DiscordWatcher
from utils.discord_interactions import DiscordInteractions

from examples.creds import discord_watcher_token, discord_midjourney_payload


# Usage
async def main():
"""Main function"""
prompt = "a beautiful necromancer witch resurrects skeletons against the backdrop of a burning ruined castle"
discord = DiscordInteractions(token=discord_midjourney_payload['auth_token'],
application_id=discord_midjourney_payload['application_id'],
guild_id=discord_midjourney_payload['guild_id'],
channel_id=discord_midjourney_payload['channel_id'],
session_id=discord_midjourney_payload['session_id'],
version=discord_midjourney_payload['version'],
interaction_id=discord_midjourney_payload['interaction_id'])
response = await discord.post_interaction(my_text_prompt=prompt)
print(response)
bot = DiscordWatcher(watch_user_id=int(discord_midjourney_payload['application_id']))
bot.run(discord_watcher_token)


asyncio.run(main())
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ llamaapi==0.1.36
leonardo-api==0.0.7
openai-python-api==0.0.5
ablt-python-api==0.0.2
# Discord
py-cord==2.4.1
68 changes: 68 additions & 0 deletions utils/discord_interactions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# -*- coding: utf-8 -*-
"""
Filename: discord_interaction.py
Author: Iliya Vereshchagin
Copyright (c) 2023. All rights reserved.
Created: 20.11.2023
Last Modified: 20.11.2023
Description:
This file contains discord interactions to python API.
"""

import aiohttp


class DiscordInteractions:
""""""
def __init__(self, token, **kwargs):
"""
Initialize DiscordInteractions class.
:param token: The token to use for authorization.
:param kwargs: The default parameters for the interaction.
"""
self.token = token
self.headers = {"authorization": self.token}
self.url = "https://discord.com/api/v9/interactions"
self.default_params = kwargs

async def post_interaction(self, my_text_prompt, **kwargs):
"""
Post any discord interaction.
:param my_text_prompt: The text prompt to post.
:type my_text_prompt: str
:param kwargs: The parameters for the interaction.
:return: The response from the interaction.
:rtype: dict
"""
params = {**self.default_params, **kwargs}

payload_data = {
"type": 2,
"application_id": params.get('application_id'),
"guild_id": params.get('guild_id'),
"channel_id": params.get('channel_id'),
"session_id": params.get('session_id'),
"data": {
"version": params.get('version'),
"id": params.get('interaction_id'),
"name": "imagine",
"type": 1,
"options": [
{
"type": 3,
"name": "prompt",
"value": my_text_prompt
}
]
}
}

async with aiohttp.ClientSession() as session:
async with session.post(self.url, json=payload_data, headers=self.headers) as resp:
if resp.status != 200:
raise ValueError(f"Request failed with status code {resp.status}")
return await resp.json()
61 changes: 61 additions & 0 deletions utils/discord_watcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
"""
Filename: discord_watcher.py
Author: Iliya Vereshchagin
Copyright (c) 2023. All rights reserved.
Created: 20.11.2023
Last Modified: 20.11.2023
Description:
This file contains discord interactions to python API.
"""
from abc import ABC

from discord import Intents
from discord.ext import commands

from utils.logger_config import setup_logger


class DiscordWatcher(commands.Bot, ABC):
def __init__(self, watch_user_id=None, **options):
"""
Initialize DiscordWatcher class.
:param command_prefix: The prefix for the bot.
:param watch_user_id: The user ID to watch.
:param options: The options for the bot.
"""
super().__init__(command_prefix='/', intents=Intents.all(), **options)
self.target_user_id = watch_user_id
self.___logger = setup_logger("discord_watcher", "discord_watcher.log")
self.___logger.info('DiscordWatcher initialized')

async def on_ready(self):
"""This function is called when the bot is ready."""
self.___logger.debug('We have logged in as %s', self.user)

async def on_message(self, message):
"""
This function is called when a message is created and sent.
:param message: The message that was sent.
:type message: discord.Message
:return: The message content.
:rtype: str
"""
self.___logger.debug('Got a message from %s : %s : %s', message.author, message.author.id, message.content)
if message.author.id == self.target_user_id:
if 'Waiting to start' not in message.content:
self.___logger.debug('Found a message from the target user: %s', message.content)
if message.attachments:
for attachment in message.attachments:
self.___logger.debug('Found an attachment: %s', attachment.url)
return attachment.url
if message.embeds:
for embed in message.embeds:
self.___logger.debug('Found an embed: %s', embed.to_dict())
return embed.to_dict()
else:
self.___logger.debug('Found a message from the target user, but content is not ready yet...')

0 comments on commit 3204866

Please sign in to comment.