diff --git a/examples/image_generation/dalle_test.py b/examples/image_generation/dalle_test.py index 5435499..fc354f8 100644 --- a/examples/image_generation/dalle_test.py +++ b/examples/image_generation/dalle_test.py @@ -15,6 +15,7 @@ from examples.creds import oai_token, oai_organization + dalle = DALLE(auth_token=oai_token, organization=oai_organization) diff --git a/examples/image_generation/gpt_functions.py b/examples/image_generation/gpt_functions.py index 366d1f7..7e0bcb1 100644 --- a/examples/image_generation/gpt_functions.py +++ b/examples/image_generation/gpt_functions.py @@ -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 @@ -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): @@ -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, diff --git a/examples/image_generation/test_leonardo.py b/examples/image_generation/leonardo_test.py similarity index 74% rename from examples/image_generation/test_leonardo.py rename to examples/image_generation/leonardo_test.py index 2478014..53c2b0f 100644 --- a/examples/image_generation/test_leonardo.py +++ b/examples/image_generation/leonardo_test.py @@ -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", @@ -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()) diff --git a/examples/image_generation/midjourney_test.py b/examples/image_generation/midjourney_test.py new file mode 100644 index 0000000..e6484df --- /dev/null +++ b/examples/image_generation/midjourney_test.py @@ -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()) diff --git a/requirements.txt b/requirements.txt index 650867d..a9b46e6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/utils/discord_interactions.py b/utils/discord_interactions.py new file mode 100644 index 0000000..11f5bac --- /dev/null +++ b/utils/discord_interactions.py @@ -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() diff --git a/utils/discord_watcher.py b/utils/discord_watcher.py new file mode 100644 index 0000000..46b60c1 --- /dev/null +++ b/utils/discord_watcher.py @@ -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...')