Added CoffeeHouse ALG

This commit is contained in:
netkas 2020-12-25 15:03:32 -05:00
parent 83a3f682af
commit f7a5d5cda7
10 changed files with 248 additions and 1 deletions

View File

@ -13,9 +13,13 @@ clean_dltc:
clean_nlpfr:
rm -rf nlpfr/build nlpfr/dist nlpfr/nltk.egg-info
clean_alg:
rm -rf alg/build alg/dist alg/alg.egg-info
clean:
make clean_apt clean_stopwords clean_tokenizer clean_nlpfr
make clean_dltc
make clean_alg
# ======================================================================================================================
@ -38,9 +42,13 @@ build_nlpfr:
build_dltc:
cd dltc; python3 setup.py build; python3 setup.py sdist
build_alg:
cd alg; python3 setup.py build; python3 setup.py dist
build:
make build_nlpfr
make build_dltc
make build_alg
# ======================================================================================================================
@ -63,5 +71,10 @@ install_nlpfr:
install_dltc:
cd dltc; python3 setup.py install
install_alg:
cd alg; python3 setup.py install
install:
make install_nlpfr install_dltc
make install_nlpfr
make install_dltc
make install_alg

24
alg/README.md Normal file
View File

@ -0,0 +1,24 @@
# CoffeeHouse ALG
CoffeeHouse ALG is a algorithmia API wrapper
## Image Tagger
Predicts the content and appropriate tags for an image
```python
from coffeehouse_alg.utilities import Utilities
from coffeehouse_alg.image_tagger import ImageTagger
url = "https://cdn4.telesco.pe/file/ETCYVng0VOEczJuIjUbsgzC4a7EOi8wMfwfFBAss_OlKZd822FNiF5Xy5V7MpY0nUvHcBCzDt1MexuBrawuit0WL5HGa0-O5bZXYsBwA5P4QAFKM6VyH0k1G9VbnCn-QiFJ5ipClcP1azPykgcRqDz4d-g_3IHWegBY3wHBD8BHm1T589XhkXbfCl_HN6TJSZ1LIYnoASnN1FF6YDV92UOhMc0-c2FTQkoxx-gGt4A7Vwy821x4-TAd6AlNI_q5NO4NKe047oRlJfm_MCtEsojYGP5tyJKB-xZYm_zRtzriwC0GSJh2zoY_RCv4L-Uudq3GGQ0_8A2OrHCmCNj0AGA.jpg"
# Process the image
image_tagger = ImageTagger()
image_content = Utilities.process_img_from_url(url)
image_tagger.predict_tags(image_content)
# {'result': {'character': [], 'copyright': [], 'general': [{'photo': 0.995330810546875}, {'cosplay': 0.28029680252075195}, {'solo': 0.2679383456707001}], 'rating': [{'safe': 0.7592381834983826}, {'questionable': 0.21827033162117004}, {'explicit': 0.024775557219982147}]}, 'metadata': {'content_type': 'json', 'duration': 0.298899313}}
image_tagger.predict_inception(image_content)
# {'result': {'tags': [{'class': 'Persian cat', 'confidence': 0.6589894890785217}, {'class': 'Egyptian cat', 'confidence': 0.009713547304272652}, {'class': 'handkerchief, hankie, hanky, hankey', 'confidence': 0.00886216014623642}, {'class': 'Angora, Angora rabbit', 'confidence': 0.008085943758487701}, {'class': 'teddy, teddy bear', 'confidence': 0.00662141153588891}]}, 'metadata': {'content_type': 'json', 'duration': 0.594030507}}
```

View File

@ -0,0 +1,14 @@
from . import utilities
from .utilities import *
from . import image_tagger
from image_tagger import *
from . import exceptions
from exceptions import *
__all__ = ['utilities',
'image_tagger',
'exceptions',
'Utilities',
'CoffeeHouseAlgException']

View File

@ -0,0 +1,14 @@
__all__ = ["CoffeeHouseAlgException"]
class CoffeeHouseAlgException(Exception):
"""
Exception raised by API errors.
The exception message is set to the server's response.
"""
def __init__(self, type, message, content):
self.type = type
self.content = content
self.message = "{0} ERROR: {1}".format(type, message)
super().__init__(self.message or content)

View File

@ -0,0 +1,4 @@
from . import image_tagger
from .image_tagger import *
__all__ = ['image_tagger', 'ImageTagger']

View File

@ -0,0 +1,85 @@
import json
import requests
from coffeehouse_alg.exceptions import CoffeeHouseAlgException
from coffeehouse_alg.utilities import Utilities
class ImageTagger:
def __init__(self, access_key=None,
illustration_endpoint="https://api.algorithmia.com/v1/web/algo/demo/IllustrationTaggerDemo/0.1.0",
inception_endpoint="https://api.algorithmia.com/v1/web/algo/demo/InceptionNetDemo/0.1.0",
js_source="https://demos.algorithmia.com/image-tagger/public/js/main.js"):
"""
Public constructor
:param endpoint: The main endpoint for Illustration Tagger
:param js_source: The javascript source code containing the API Key
"""
self.access_key = access_key
self.illustration_endpoint = illustration_endpoint
self.inception_endpoint = inception_endpoint
self.js_source = js_source
if access_key is None:
self.access_key = Utilities.get_access_key(self.js_source)
def predict_tags(self, image_content, results=12):
"""
Processes the bare-bone request and returns the JSON results
:param image_content:
:param results:
:return:
"""
headers = {
"Accept": "application/json, text/javascript",
"Authorization": self.access_key,
"Content-Type": "application/json",
"Origin": "https://demos.algorithmia.com",
"Referer": "https://demos.algorithmia.com/image-tagger",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-site",
}
payload = {
"image": image_content,
"numResults": results
}
headers = Utilities.add_client_headers(headers)
response = requests.post(self.illustration_endpoint, data=json.dumps(payload), headers=headers)
j_response = json.loads(response.text)
if 'error' in j_response:
raise CoffeeHouseAlgException('SERVER', j_response['error']['message'], response)
return j_response
def predict_inception(self, image_content):
"""
Processes the bare-bone request and returns the JSON results
:param image_content:
:return:
"""
headers = {
"Accept": "application/json, text/javascript",
"Authorization": self.access_key,
"Content-Type": "application/json",
"Origin": "https://demos.algorithmia.com",
"Referer": "https://demos.algorithmia.com/image-tagger",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-site",
}
headers = Utilities.add_client_headers(headers)
response = requests.post(self.inception_endpoint, data=json.dumps(image_content), headers=headers)
j_response = json.loads(response.text)
if 'error' in j_response:
raise CoffeeHouseAlgException('SERVER', j_response['error']['message'], response)
return j_response

View File

@ -0,0 +1,58 @@
import requests
import mimetypes
import base64
import re
class Utilities:
@staticmethod
def get_cookies(http_endpoint):
"""
Makes the initial request to get all the required cookies
:return: dict value of all the returned cookies
"""
session = requests.Session()
session.get(http_endpoint)
return session.cookies.get_dict()
@staticmethod
def get_access_key(http_endpoint):
"""
Sends a request to a Javascript content file and parses the contents
to extract the access key (Simple)
:param http_endpoint:
:return:
"""
response = requests.get(http_endpoint)
access_key = re.findall('\'([^"]*)\'', response.text.splitlines()[1])[0]
return "Simple {0}".format(access_key)
@staticmethod
def process_img_from_url(url):
"""
Downloads the image from the URl and processes it into a proper
base64 encoded image with the mime type information included
:param url:
:return:
"""
mimetype = mimetypes.guess_type(url)[0]
image_content = requests.get(url).content
image_b64content = base64.b64encode(image_content).decode('utf8')
return "data:%s;base64,%s" % (mimetype, image_b64content)
@staticmethod
def add_client_headers(headers):
"""
Adds the required client headers
:param headers:
:return:
"""
headers['Accept-Encoding'] = 'gzip, deflate, br'
headers['Accept-Language'] = 'en'
headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ' \
'Chrome/80.0.3987.122 Safari/537.36 '
return headers

1
alg/requirements.txt Normal file
View File

@ -0,0 +1 @@
requests

23
alg/setup.py Normal file
View File

@ -0,0 +1,23 @@
from setuptools import setup, find_packages
setup(
name='coffeehouse_alg',
version='1.0.0',
description='CoffeeHouse algorithmia wrapper',
url='https://github.com/Intellivoid/CoffeeHouse-ALG',
author='Zi Xing Narrakas',
author_email='netkas@intellivoid.info',
classifiers=[
# 3 - Alpha
# 4 - Beta
# 5 - Production/Stable
'Development Status :: 4 - Beta',
'Topic :: API Wrapper',
'Programming Language :: Python :: 3',
],
keywords='multi-label classification nlp neural networks deep learning api wrapper',
packages=find_packages(exclude=['tests']),
install_requires=[
'requests'
]
)

11
alg/tests/example.py Normal file
View File

@ -0,0 +1,11 @@
from coffeehouse_alg.utilities import Utilities
from coffeehouse_alg.image_tagger import ImageTagger
url = "https://cdn4.telesco.pe/file/ETCYVng0VOEczJuIjUbsgzC4a7EOi8wMfwfFBAss_OlKZd822FNiF5Xy5V7MpY0nUvHcBCzDt1MexuBrawuit0WL5HGa0-O5bZXYsBwA5P4QAFKM6VyH0k1G9VbnCn-QiFJ5ipClcP1azPykgcRqDz4d-g_3IHWegBY3wHBD8BHm1T589XhkXbfCl_HN6TJSZ1LIYnoASnN1FF6YDV92UOhMc0-c2FTQkoxx-gGt4A7Vwy821x4-TAd6AlNI_q5NO4NKe047oRlJfm_MCtEsojYGP5tyJKB-xZYm_zRtzriwC0GSJh2zoY_RCv4L-Uudq3GGQ0_8A2OrHCmCNj0AGA.jpg"
image_tagger = ImageTagger()
image_content = Utilities.process_img_from_url(url)
print(image_tagger.predict_tags(image_content))
print(image_tagger.predict_inception(image_content))