diff --git a/CHANGELOG.md b/CHANGELOG.md index e125406..25c7595 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,3 +18,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Leonardo API +## [0.2] - 2023-10-15 + +### Changed +- Bumped submodules to latest versions +- Added test project +- Structure refactoring diff --git a/TODO.md b/TODO.md index 5659fec..c956666 100644 --- a/TODO.md +++ b/TODO.md @@ -1,3 +1,4 @@ -- Add more params and fixes -- Add image handling methods (generalistic) -- Rework as pypi package +- Lint! +- Prettify projects +- Add & finalize testing project +- Refactor utils to Classes diff --git a/examples/__init__.py b/examples/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/examples/image_generation/dalle_test.py b/examples/image_generation/dalle_test.py new file mode 100644 index 0000000..83e789b --- /dev/null +++ b/examples/image_generation/dalle_test.py @@ -0,0 +1,15 @@ +import asyncio + +from openai_api.src.openai_api import DALLE +from creds import oai_token, oai_organization + +from time import sleep + +dalle = DALLE(auth_token=oai_token, organization=oai_organization) +async def main(): + resp = await dalle.create_image_url('robocop (robot policeman, from 80s movie)') + print(resp) + resp = await dalle.create_variation_from_url(resp[0]) + print(resp) + +asyncio.run(main()) \ No newline at end of file diff --git a/examples/speak_and_hear/test_gpt_orig.py b/examples/speak_and_hear/test_gpt_orig.py new file mode 100644 index 0000000..660c0bb --- /dev/null +++ b/examples/speak_and_hear/test_gpt_orig.py @@ -0,0 +1,153 @@ +# -*- coding: utf-8 -*- +""" +Filename: chatgpt.py +Author: Iliya Vereshchagin +Copyright (c) 2023. All rights reserved. + +Created: 25.08.2023 +Last Modified: 25.08.2023 + +Description: +This file contains testing procedures for ChatGPt experiments +""" + +import string +import sys + +import asyncio + +from creds import oai_token, oai_organization +from openai_api.src.openai_api import ChatGPT + +from utils.audio_recorder import AudioRecorder +from utils.transcriptors import CustomTranscriptor +from utils.tts import CustomTTS + +from gpt_functions import gpt_functions, gpt_functions_dict + + +gpt = ChatGPT(auth_token=oai_token, organization=oai_organization, model="gpt-3.5-turbo-0613") +gpt.max_tokens = 200 +gpt.stream = True +gpt.functions = gpt_functions +gpt.function_dict = gpt_functions_dict +gpt.function_call = 'auto' + +tts = CustomTTS(method="google", lang="en") + +# queues +prompt_queue = asyncio.Queue() +tts_queue = asyncio.Queue() + + +async def ask_chat(user_input): + full_response = "" + word = "" + async for response in gpt.str_chat(user_input): + for char in response: + word += char + if char in string.whitespace or char in string.punctuation: + if word: + await prompt_queue.put(word) + word = "" + sys.stdout.write(char) + sys.stdout.flush() + full_response += char + print("\n") + return full_response + + +async def tts_task(): + limit = 5 + empty_counter = 0 + while True: + if prompt_queue.empty(): + empty_counter += 1 + if empty_counter >= 3: + limit = 5 + empty_counter = 0 + words = [] + # Get all available words + limit_counter = 0 + while len(words) < limit: + try: + word = await asyncio.wait_for(prompt_queue.get(), timeout=0.5) + words.extend(word.split()) + if len(words) >= limit: + break + except asyncio.TimeoutError: + limit_counter += 1 + if limit_counter >= 10: + limit = 1 + + # If we have at least limit words or queue was empty 3 times, process them + if len(words) >= limit: + text = " ".join(words) + await tts.process(text) + limit = 1 + + +async def tts_sentence_task(): + punctuation_marks = ".?!,;:" + sentence = "" + while True: + try: + word = await asyncio.wait_for(prompt_queue.get(), timeout=0.5) + sentence += " " + word + # If the last character is a punctuation mark, process the sentence + if sentence[-1] in punctuation_marks: + await tts_queue.put(sentence) + sentence = "" + except Exception as error: + pass + + +async def tts_worker(): + while True: + try: + sentence = await tts_queue.get() + if sentence: + await tts.process(sentence) + tts_queue.task_done() + except Exception as error: + pass + + +async def get_user_input(): + while True: + try: + user_input = input() + if user_input.lower() == "[done]": + break + else: + await ask_chat(user_input) + except KeyboardInterrupt: + break + + +async def main(): + asyncio.create_task(tts_sentence_task()) + asyncio.create_task(tts_worker()) + method = "google" + + while True: + try: + #if "google" not in method: + # file_path = AudioRecorder().listen() + # with open(file_path, "rb") as f: + # transcript = await gpt.transcript(file=f, language="en") + #else: + # transcript = CustomTranscriptor(language="en-US").transcript() + # pass + #if transcript: + # print(f"User: {transcript}") + # #translate = CustomTranslator(source='ru', target='en').translate(transcript) + # #print(translate) + # response = await ask_chat(transcript) + print("John Connor:" "Hello, my name is John Connor!") + response = await ask_chat("Hello, my name is John Connor!") + except KeyboardInterrupt: + break + + asyncio.run(main()) + diff --git a/examples/test_generator/pom/__init__.py b/examples/test_generator/pom/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/examples/test_generator/tests/__init__.py b/examples/test_generator/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/examples/test_generator/tests/conftest.py b/examples/test_generator/tests/conftest.py new file mode 100644 index 0000000..7d23610 --- /dev/null +++ b/examples/test_generator/tests/conftest.py @@ -0,0 +1,21 @@ +import pytest + +from selenium import webdriver +from selenium.webdriver.chrome.options import Options +from webdriver_manager.chrome import ChromeDriverManager + + +@pytest.fixture +def driver(request): + options = Options() + options.add_argument("--headless") + _driver = webdriver.Chrome(ChromeDriverManager().install(), options=options) + + def save_page_source_on_failure(): + if request.node.rep_call.failed or request.node.rep_call.error: + page_html = _driver.page_source + request.node.user_properties.append(("page_html", page_html)) + + request.addfinalizer(save_page_source_on_failure) + + return _driver diff --git a/leonardo_api b/leonardo_api index a1baea0..44c3744 160000 --- a/leonardo_api +++ b/leonardo_api @@ -1 +1 @@ -Subproject commit a1baea0fb4e8350783bfd9360025cd719775d9b7 +Subproject commit 44c3744c86489134b4dea30706c212e7d3790538 diff --git a/openai_api b/openai_api index 51ee463..5804e33 160000 --- a/openai_api +++ b/openai_api @@ -1 +1 @@ -Subproject commit 51ee4630af01dfb6c62971f28c6d02beda718282 +Subproject commit 5804e33fb22f449ef6b2feb1a6b2f113a359e2d9 diff --git a/requirements.txt b/requirements.txt index 1ff6e36..c099a52 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,3 +17,7 @@ numpy==1.26.1 pillow==10.1.0 # Articles readability==0.3.1 +# Testing +webdriver_manager==4.0.1 +selenium==4.14.0 +pytest==7.4.2 \ No newline at end of file