From b804671dc4abd9253f1159a4782ec31e52998007 Mon Sep 17 00:00:00 2001 From: prism0x Date: Tue, 4 Oct 2022 13:59:30 +0200 Subject: [PATCH] Started refactoring hemingwai -> textcortex-python --- README.md | 99 +++++++++++---------- setup.py | 67 ++++++++------- textcortex/textcortex.py | 181 ++++++++++++++++++++++++++++----------- 3 files changed, 218 insertions(+), 129 deletions(-) diff --git a/README.md b/README.md index debf3a3..06bc1b4 100644 --- a/README.md +++ b/README.md @@ -1,34 +1,56 @@ -# TextCortex - HemingwAI -![alt text](https://github.com/textcortex/hemingwai/raw/main/textcortex_logo.png?raw=true "TextCortex AI API Hemingway Logo") +# TextCortex Python Library -Generate product descriptions, blogs, ads and more using GPT architecture with a single request to TextCortex API a.k.a -HemingwAI +![alt text](https://github.com/textcortex/textcortex-python/raw/main/textcortex_logo.png?raw=true "TextCortex AI API Hemingway Logo") + +Python Library for high level access to the TextCortex API. Generate product descriptions, blogs, ads and more using GPT architecture with a single request. + +## Installation + +Install the package using pip: + +```sh +pip install textcortex +``` + +## Usage -## How To Generate Content using TextCortex Hemingwai: 1. Signup at https://textcortex.com -2. Sign-in and click on account on top right. -3. Go to API Key section and copy your key. -4. Install textcortex package: - `pip install textcortex` -5. Enter your API Key to hemingwai -6. Generate copy text with a single line of code! +1. Sign-in and click on account on top right. +1. Go to API Key section and copy your key. +1. Set your API Key. -### Here is an example request to Hemingwai for generating Product Descriptions: + +### Example usage + +Make sure you have a TextCortex account. If you don't have one, [click here](https://app.textcortex.com/user/signup) to sign up (it just takes a few seconds). + +Once you are signed in, you can head off to Account > API Key section. [Click here](https://app.textcortex.com/user/dashboard/settings/api-key) to visit that page. + +Copy and paste your API key to the example below. It should return product descriptions based on the given parameters: ```python from textcortex import TextCortex -# Create the hemingwai object and enter your API Key -hemingwai = TextCortex(api_key='YOUR_API_KEY') - -# Generate Product Descriptions using Hemingwai -product_description = hemingwai.generate_product_descriptions( - product_name='Black Leather Backpack Bag', product_category='Shoes & Bags, Women', - brand='Cortexian', product_features='Color: Black, Material: Faux Leather', - source_language='en', word_count=100, temperature=0.7, n_gen=4) +API_KEY = 'YOUR_API_KEY' # <---- Paste your API key here + +# Create the TextCortex API object and enter your API Key +tc = TextCortex(api_key=API_KEY) + +# Generate product descriptions +product_description = tc.generate_product_descriptions( + product_name='Black Leather Backpack Bag', + product_category='Shoes & Bags, Women', + brand='Cortexian', + product_features='Color: Black, Material: Faux Leather', + source_language='en', + token_count=100, + temperature=0.7, + n_gen=4, +) ``` #### Response: + ```json [ { @@ -46,30 +68,12 @@ product_description = hemingwai.generate_product_descriptions( ] ``` -### What kind of texts are possible to generate? - -Currently we support the following methods for generating copy text like the following: -```python -# Generate Blog Articles: -hemingwai.generate_blog - -# Generate Blog Titles: -hemingwai.generate_blog_titles - -# Autocomplete the rest using Hemingwai -hemingwai.generate +## Documentation -# Extend paragraphs with a blog writing tone using Hemingwai -hemingwai.extend +Browse the [documentation on TextCortex](https://textcortex.com/documentation/api). -# Generate Product Descriptions using Hemingwai -hemingwai.generate_product_descriptions + -#### Maintainer/Creator -TextCortex Team (https://textcortex.com) +## Getting help -#### License -MIT +To get help with using TextCortex Python library, join our [Discord](https://discord.textcortex.com/). \ No newline at end of file diff --git a/setup.py b/setup.py index 9faaf8e..9985bdf 100644 --- a/setup.py +++ b/setup.py @@ -1,40 +1,47 @@ from setuptools import setup, find_packages -DESCRIPTION = 'Generate product descriptions, blogs, emails, ads and more using GPT architecture with a single ' \ - 'request to TextCortex API a.k.a Hemingwai' +DESCRIPTION = "Python library for high-level access to the TextCortex API" # read the contents of your README file from os import path + this_directory = path.abspath(path.dirname(__file__)) -with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f: +with open(path.join(this_directory, "README.md"), encoding="utf-8") as f: long_description = f.read() setup( - name='textcortex', - version='1.0.10', - author='TextCortex AI', - author_email='dev@textcortex.com', - packages=find_packages(), - url='https://github.com/textcortex/hemingwai', - license='MIT', - description=DESCRIPTION, - long_description=long_description, - long_description_content_type='text/markdown', - keywords=['TextCortex AI', 'gpt-2', 'gpt-3', 'gptNEO', 'generate text', - 'natural language generation', 'NLP', 'hemingwai', 'transformer', 'generate copy text using AI'], - install_requires=[ - "requests" - ], - classifiers= [ - "Development Status :: 3 - Alpha", - "Intended Audience :: Customer Service", - "Intended Audience :: Information Technology", - "Topic :: Scientific/Engineering :: Artificial Intelligence", - "Natural Language :: English", - "Programming Language :: Python :: 2", - "Programming Language :: Python :: 3", - "Operating System :: MacOS :: MacOS X", - "Operating System :: Microsoft :: Windows", - "Programming Language :: Python :: 3" - ] + name="textcortex", + version="1.0.10", + author="TextCortex AI", + author_email="dev@textcortex.com", + packages=find_packages(), + url="https://github.com/textcortex/textcortex-python", + license="MIT", + description=DESCRIPTION, + long_description=long_description, + long_description_content_type="text/markdown", + keywords=[ + "TextCortex AI", + "gpt-2", + "gpt-3", + "gptNEO", + "generate text", + "natural language generation", + "NLP", + "transformer", + "generate copy text using AI", + ], + install_requires=["requests"], + classifiers=[ + "Development Status :: 3 - Alpha", + "Intended Audience :: Customer Service", + "Intended Audience :: Information Technology", + "Topic :: Scientific/Engineering :: Artificial Intelligence", + "Natural Language :: English", + "Programming Language :: Python :: 2", + "Programming Language :: Python :: 3", + "Operating System :: MacOS :: MacOS X", + "Operating System :: Microsoft :: Windows", + "Programming Language :: Python :: 3", + ], ) diff --git a/textcortex/textcortex.py b/textcortex/textcortex.py index 3982cc7..286606e 100644 --- a/textcortex/textcortex.py +++ b/textcortex/textcortex.py @@ -1,7 +1,7 @@ """ This library allows you to quickly and easily use the TextCortex AI Web API via Python. For more information on this library, see the README on GitHub. - https://github.com/hemingwai/readme + https://github.com/textcortex/textcortex-python/readme For more information on the TextCortex AI API, see the docs: https://textcortex.com/documentation/api """ @@ -14,7 +14,9 @@ import requests -def retry(ExceptionToCheck, tries=4, delay=3, backoff=2, logger=None, default_value=None): +def retry( + ExceptionToCheck, tries=4, delay=3, backoff=2, logger=None, default_value=None +): """Retry calling the decorated function using an exponential backoff. http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/ original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry @@ -56,7 +58,7 @@ def f_retry(*args, **kwargs): class APIError(Exception): - def __init__(self, msg=''): + def __init__(self, msg=""): self.msg = msg logging.error(msg) @@ -65,17 +67,27 @@ def __str__(self): class TextCortex: - def __init__(self, api_key: str): - self.url = 'https://api.textcortex.com/hemingwai/generate_text_v2' + self.url = "https://api.textcortex.com/generate_text_v2" self.api_key = api_key @retry(Exception, tries=2, logger=logging, default_value=None) - def _get_results(self, prompt: dict, word_count: int, source_language: str, temperature: float, - template_name: str, n_gen: int, generation_source: str = None) -> List: + def _get_results( + self, + prompt: dict, + word_count: int, + source_language: str, + temperature: float, + template_name: str, + n_gen: int, + generation_source: str = None, + ) -> List: """Connect to the API and retrieve the generated text""" - headers = {'Content-type': 'application/json', - 'Accept': 'text/plain', 'user-agent': 'Python-hemingwAI'} + headers = { + "Content-type": "application/json", + "Accept": "text/plain", + "user-agent": "Python", + } payload = { "template_name": template_name, "prompt": prompt, @@ -84,27 +96,35 @@ def _get_results(self, prompt: dict, word_count: int, source_language: str, temp "n_gen": n_gen, "source_language": source_language, "api_key": self.api_key, - "generation_source": generation_source + "generation_source": generation_source, } try: req = requests.post(self.url, headers=headers, json=payload) if req.status_code == 403: raise APIError( - 'API Key is invalid. Check out your API key on https://app.textcortex.com/user/account') + "API Key is invalid. Check out your API key on https://app.textcortex.com/user/account" + ) if req.status_code == 402: raise APIError( - 'Reached API Limits, increase limits by contacting us at dev@textcortex.com or upgrade your account') + "Reached API Limits, increase limits by contacting us at dev@textcortex.com or upgrade your account" + ) if req.status_code == 500: - raise APIError('Ops, error {}'.format(str(req.json()))) - return req.json()['generated_text'] + raise APIError("Ops, error {}".format(str(req.json()))) + return req.json()["generated_text"] except APIError: return except Exception: # this will force to retry the connection raise - def generate(self, prompt: str, word_count: int = 100, - source_language: str = "en", temperature: float = 0.65, n_gen=1) -> List: + def generate( + self, + prompt: str, + word_count: int = 100, + source_language: str = "en", + temperature: float = 0.65, + n_gen=1, + ) -> List: """ Generates Text by autocompleting the given prompt using TextCortex Hemingway API @@ -117,11 +137,24 @@ def generate(self, prompt: str, word_count: int = 100, :return: Returns list of generated possible text based on the given prompt """ prompt = {"original_sentence": prompt} - return self._get_results(prompt=prompt, template_name="auto_complete", word_count=word_count, - source_language=source_language, temperature=temperature, n_gen=n_gen) + return self._get_results( + prompt=prompt, + template_name="auto_complete", + word_count=word_count, + source_language=source_language, + temperature=temperature, + n_gen=n_gen, + ) - def generate_blog(self, blog_title: str, blog_keywords: str, word_count: int = 100, - temperature: float = 0.65, source_language: str = "en", n_gen=1) -> List: + def generate_blog( + self, + blog_title: str, + blog_keywords: str, + word_count: int = 100, + temperature: float = 0.65, + source_language: str = "en", + n_gen=1, + ) -> List: """ Generates Blog articles using TextCortex Hemingway API @@ -134,16 +167,26 @@ def generate_blog(self, blog_title: str, blog_keywords: str, word_count: int = 1 :param int n_gen: Defines how many different options will be sent according to the result. :return: Returns list of generated blog articles with focus keyword and character length. """ - prompt = { - "blog_title": blog_title, - "blog_keywords": blog_keywords - } + prompt = {"blog_title": blog_title, "blog_keywords": blog_keywords} - return self._get_results(prompt=prompt, template_name="blog_body", word_count=word_count, - source_language=source_language, temperature=temperature, n_gen=n_gen) + return self._get_results( + prompt=prompt, + template_name="blog_body", + word_count=word_count, + source_language=source_language, + temperature=temperature, + n_gen=n_gen, + ) - def generate_blog_title(self, blog_intro: str, blog_keywords: str, word_count: int = 20, - temperature: float = 0.65, source_language: str = "en", n_gen=1) -> List: + def generate_blog_title( + self, + blog_intro: str, + blog_keywords: str, + word_count: int = 20, + temperature: float = 0.65, + source_language: str = "en", + n_gen=1, + ) -> List: """ Generates Blog titles using TextCortex Hemingway API @@ -156,17 +199,28 @@ def generate_blog_title(self, blog_intro: str, blog_keywords: str, word_count: i :param int n_gen: Defines how many different options will be sent according to the result. :return: Returns list of generated blog articles with focus keyword and character length. """ - prompt = { - "blog_intro": blog_intro, - "blog_keywords": blog_keywords - } + prompt = {"blog_intro": blog_intro, "blog_keywords": blog_keywords} - return self._get_results(prompt=prompt, template_name="blog_title", word_count=word_count, - source_language=source_language, temperature=temperature, n_gen=n_gen) + return self._get_results( + prompt=prompt, + template_name="blog_title", + word_count=word_count, + source_language=source_language, + temperature=temperature, + n_gen=n_gen, + ) - def generate_product_descriptions(self, product_name: str, brand: str, product_category: str, - product_features: str, word_count: int = 100, - temperature: float = 0.65, source_language: str = 'en', n_gen=2) -> List: + def generate_product_descriptions( + self, + product_name: str, + brand: str, + product_category: str, + product_features: str, + word_count: int = 100, + temperature: float = 0.65, + source_language: str = "en", + n_gen=2, + ) -> List: """ Generates Email Subject Line using TextCortex Hemingway API :param str product_name: Input the product title that you want to generate descriptions for. @@ -188,14 +242,27 @@ def generate_product_descriptions(self, product_name: str, brand: str, product_c "product_name": product_name, "brand": brand, "product_category": product_category, - "product_features": product_features + "product_features": product_features, } - return self._get_results(prompt=prompt, template_name="product_description", word_count=word_count, - source_language=source_language, temperature=temperature, n_gen=n_gen) + return self._get_results( + prompt=prompt, + template_name="product_description", + word_count=word_count, + source_language=source_language, + temperature=temperature, + n_gen=n_gen, + ) - def paraphrase(self, prompt: str, tone: str = "", word_count: int = 50, - temperature: float = 1, source_language: str = 'en', n_gen=5) -> List: + def paraphrase( + self, + prompt: str, + tone: str = "", + word_count: int = 50, + temperature: float = 1, + source_language: str = "en", + n_gen=5, + ) -> List: """ Paraphrases given sentence based on the tonality. :param str prompt: Sentence that you would like to paraphrase @@ -209,11 +276,23 @@ def paraphrase(self, prompt: str, tone: str = "", word_count: int = 50, :return: Returns the paraphrased sentences """ prompt = {"original_sentence": prompt, "tone": tone} - return self._get_results(prompt=prompt, template_name="paraphrase", word_count=word_count, - source_language=source_language, temperature=temperature, n_gen=n_gen) + return self._get_results( + prompt=prompt, + template_name="paraphrase", + word_count=word_count, + source_language=source_language, + temperature=temperature, + n_gen=n_gen, + ) - def extend(self, prompt: str, word_count: int = 256, - temperature: float = 0.65, source_language: str = 'en', n_gen=2) -> List: + def extend( + self, + prompt: str, + word_count: int = 256, + temperature: float = 0.65, + source_language: str = "en", + n_gen=2, + ) -> List: """ Extends a given paragraph with a blog like writing tone using TextCortex Hemingway API :param str prompt: Sentence that you would like to paraphrase @@ -226,5 +305,11 @@ def extend(self, prompt: str, word_count: int = 256, :return: Returns the paraphrased sentences """ prompt = {"original_sentence": prompt} - return self._get_results(prompt=prompt, template_name="auto_complete", word_count=word_count, - source_language=source_language, temperature=temperature, n_gen=n_gen) + return self._get_results( + prompt=prompt, + template_name="auto_complete", + word_count=word_count, + source_language=source_language, + temperature=temperature, + n_gen=n_gen, + )