Skip to content

Commit

Permalink
v0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
wwakabobik committed Nov 24, 2023
1 parent a95cde9 commit a005f11
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[mypy]
ignore_missing_imports = True

[mypy-creds,*ablt_models]
ignore_errors = True
3 changes: 3 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ max-line-length=120

[MESSAGES CONTROL]
disable=E1101,R0913

[MASTER]
ignore=creds,ablt_models
3 changes: 2 additions & 1 deletion examples/image_generation/dalle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import asyncio
from openai_python_api import DALLE

from examples.creds import oai_token, oai_organization
# pylint: disable=import-error
from examples.creds import oai_token, oai_organization # type: ignore


dalle = DALLE(auth_token=oai_token, organization=oai_organization)
Expand Down
3 changes: 2 additions & 1 deletion examples/image_generation/gpt_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
from leonardo_api import Leonardo
from openai_python_api import DALLE

from examples.creds import oai_token, oai_organization, openweathermap_appid, leonardo_token
# pylint: disable=import-error
from examples.creds import oai_token, oai_organization, openweathermap_appid, leonardo_token # type: ignore


def get_weather(city, units):
Expand Down
123 changes: 114 additions & 9 deletions examples/image_generation/image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import ssl
import time
from pprint import pprint
from urllib.parse import unquote

import aiofiles
import aiohttp
Expand All @@ -24,9 +25,11 @@
from leonardo_api import LeonardoAsync as Leonardo
from openai_python_api.dalle import DALLE

from examples.creds import oai_token, oai_organization, leonardo_token, ablt_token, discord_midjourney_payload
# pylint: disable=import-error
from examples.creds import oai_token, oai_organization, leonardo_token, ablt_token, discord_midjourney_payload # type: ignore
from utils.discord_interactions import DiscordInteractions


# Initialize the APIs
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
Expand All @@ -41,6 +44,9 @@ async def midjourney_wrapper(prompt):
Wrapper for midjourney testing.
:param prompt: The prompt to use for the function.
:type prompt: str
:return: The attachment found in the log file.
:rtype: str
"""
discord = DiscordInteractions(
token=discord_midjourney_payload["auth_token"],
Expand All @@ -56,6 +62,15 @@ async def midjourney_wrapper(prompt):


async def leonardo_wrapper(prompt):
"""
Wrapper for leonardo testing.
:param prompt: The prompt to use for the function.
:type prompt: str
:return: The image url.
:rtype: str
"""
response = await leonardo.post_generations(
prompt=prompt,
num_images=1,
Expand Down Expand Up @@ -98,8 +113,9 @@ async def save_image_from_url(url, file_path):
:param file_path: The file path to use for the function.
:type file_path: str
"""
print(url)
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
async with session.get(unquote(url).strip('"').strip("'")) as response:
if response.status == 200:
f = await aiofiles.open(file_path, mode="wb")
await f.write(await response.read())
Expand All @@ -111,6 +127,12 @@ async def save_image_from_url(url, file_path):


async def generate_image():
"""
Generate image.
:return: The image list with dict (contains url and filepathes).
:rtype: list
"""
prompts = (
"beautiful and scary necromancer girl riding white unicorn",
"draw a character that is a toast-mascot in cartoon style",
Expand All @@ -121,14 +143,14 @@ async def generate_image():
midjourney_prompt = await ablt.chat(
bot_slug="maina",
prompt=f"Please write a midjourney prompt with aspect ratio 1:1, realistic style: '{prompt}'. "
f"Give me the prompt only, without any comments and descriptions. "
f"Just prompt output for midjourney.",
f"Give me the prompt only, without any comments and descriptions. "
f"Just prompt output for midjourney.",
stream=False,
).__anext__()
dalle_prompt = await ablt.chat(
bot_slug="maina",
prompt=f"Please write a dalle3 prompt: '{prompt}'. "
f"Give me the prompt only, without any comments and descriptions. Just prompt output.",
f"Give me the prompt only, without any comments and descriptions. Just prompt output.",
stream=False,
).__anext__()
midjourney_prompt = midjourney_prompt.replace("`", "").replace("n", "")
Expand All @@ -147,7 +169,10 @@ async def generate_image():
image_list.append(
{
"images": {"leonardo": leonardo_image, "dalle3": dalle3_image, "midjourney": midjourney_image},
"url": {"leonardo": leonardo_image_url, "dalle3": dalle3_image_url, "midjourney": midjourney_image_url},
"url": {"leonardo": leonardo_image_url.strip("'").strip('"'),
"dalle3": dalle3_image_url.strip("'").strip('"'),
"midjourney": midjourney_image_url.strip("'").strip('"')},
"prompts": {"leonardo": dalle_prompt, "dalle3": dalle_prompt, "midjourney": midjourney_prompt},
}
)
return image_list
Expand All @@ -170,16 +195,96 @@ async def get_dalle_variations(image_list):
with open(file_path, "rb") as file:
url = await dalle.create_variation_from_file_and_get_url(file)
image = await save_image_from_url(url, f"dalle3_variation_{index}.png")
variations.append({"url": url, "image": image})
variations.append({"url": url.strip("'").strip('"'), "image": image})
return variations


async def get_midjourney_variations(image_list):
"""
Get variations from midjourney images.
:param image_list: The image list to use for the function.
:type image_list: list
:return: The variations from midjourney images.
:rtype: list
"""
variations = []
for index, images in enumerate(image_list):
midjourney_url = await midjourney_wrapper(f"{images["url"]["midjourney"]} {images["prompts"]["midjourney"]}")
midjourney_file = await save_image_from_url(midjourney_url, f"midjourney_variation_{index}.png")
variations.append({"url": midjourney_url.strip("'").strip('"'), "image": midjourney_file})
return variations


async def get_leonardo_variations(image_list):
"""
Get variations from leonardo images.
:param image_list: The image list to use for the function.
:type image_list: list
:return: The variations from leonardo images.
:rtype: list
"""
variations = []
for index, images in enumerate(image_list):
image_file = images["images"]["leonardo"]
leonardo_generation = await leonardo.upload_init_image(image_file)
response = await leonardo.post_generations(
prompt=images["prompts"]["leonardo"],
num_images=1,
model_id="1e60896f-3c26-4296-8ecc-53e2afecc132",
width=1024,
height=1024,
prompt_magic=True,
init_image_id=leonardo_generation,

)
response = await leonardo.wait_for_image_generation(generation_id=response["sdGenerationJob"]["generationId"])
leonardo_url = json.dumps(response["url"])
leonardo_file = await save_image_from_url(leonardo_url, f"leonardo_variation_{index}.png")
variations.append({"url": leonardo_url, "image": leonardo_file})
return variations


async def generate_variations(image_list):
"""
Generate variations.
:return: The variations list.
:rtype: list
"""

dalle_variations_coro = get_dalle_variations(image_list)
midjourney_variations_coro = get_midjourney_variations(image_list)
leonardo_variations_coro = get_leonardo_variations(image_list)
dalle_variations, midjourney_variations, leonardo_variations = await asyncio.gather(dalle_variations_coro,
midjourney_variations_coro,
leonardo_variations_coro)
variations = []
for leonardo_item, dalle_item, midjourney_item, image_item in zip(leonardo_variations, dalle_variations,
midjourney_variations, image_list):
variations.append({
"images": {
"leonardo": leonardo_item['image'],
"dalle3": dalle_item['image'],
"midjourney": midjourney_item['image']
},
"url": {
"leonardo": leonardo_item['url'],
"dalle3": dalle_item['url'],
"midjourney": midjourney_item['url']
},
"prompts": image_item['prompts']
})
return variations


async def main():
"""Main function."""
image_list = await generate_image()
pprint(image_list)
dalle_variations = await get_dalle_variations(image_list)
pprint(dalle_variations)
variation_list = await generate_variations(image_list)
pprint(variation_list)


asyncio.run(main())
3 changes: 2 additions & 1 deletion examples/image_generation/leonardo_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import asyncio
from leonardo_api.leonardo_async import Leonardo

from examples.creds import leonardo_token
# pylint: disable=import-error
from examples.creds import leonardo_token # type: ignore


async def main():
Expand Down
3 changes: 2 additions & 1 deletion examples/image_generation/midjourney_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
from utils.discord_watcher import DiscordWatcher
from utils.discord_interactions import DiscordInteractions

from examples.creds import discord_watcher_token, discord_midjourney_payload
# pylint: disable=import-error
from examples.creds import discord_watcher_token, discord_midjourney_payload # type: ignore


# Usage
Expand Down
3 changes: 2 additions & 1 deletion examples/llm_api_comparison/original_llm_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from llamaapi import LlamaAPI
from openai_python_api import ChatGPT

from examples.creds import oai_token, oai_organization, cohere_token, llama_token
# pylint: disable=import-error
from examples.creds import oai_token, oai_organization, cohere_token, llama_token # type: ignore
from examples.llm_api_comparison.csv_saver import save_to_csv
from examples.llm_api_comparison.llm_questions import llm_questions
from utils.llm_timer_wrapper import TimeMetricsWrapperAsync, TimeMetricsWrapperSync
Expand Down
5 changes: 3 additions & 2 deletions examples/llm_api_comparison/wrapped_llm_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

from ablt_python_api import ABLTApi

from examples.creds import ablt_token
from examples.llm_api_comparison.ablt_models import unique_models
# pylint: disable=import-error
from examples.creds import ablt_token # type: ignore
from examples.llm_api_comparison.ablt_models import unique_models # type: ignore
from examples.llm_api_comparison.csv_saver import save_to_csv
from examples.llm_api_comparison.llm_questions import llm_questions
from utils.llm_timer_wrapper import TimeMetricsWrapperSync
Expand Down
3 changes: 2 additions & 1 deletion examples/speak_and_hear/test_gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import asyncio
from openai_python_api import ChatGPT

from examples.creds import oai_token, oai_organization
# pylint: disable=import-error
from examples.creds import oai_token, oai_organization # type: ignore
from utils.audio_recorder import AudioRecorder
from utils.transcriptors import CustomTranscriptor
from utils.tts import CustomTTS
Expand Down
3 changes: 2 additions & 1 deletion examples/test_generator/generator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import asyncio
from openai_python_api import ChatGPT

from examples.creds import oai_token, oai_organization
# pylint: disable=import-error
from examples.creds import oai_token, oai_organization # type: ignore
from examples.test_generator.gpt_functions import gpt_functions, gpt_functions_dict
from examples.test_generator.pom_case_generator import PomTestCaseGenerator
from utils.logger_config import setup_logger
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ pytest-xdist==3.5.0
cohere==4.36
llamaapi==0.1.36
# My AI APIs
leonardo-api==0.0.8
leonardo-api==0.0.10
openai-python-api==0.0.6
ablt-python-api==0.0.2
# Discord
py-cord==2.4.1
openai==1.3.4
openai==1.3.5

0 comments on commit a005f11

Please sign in to comment.