Added experimental translation services

This commit is contained in:
netkas 2020-12-30 21:41:37 -05:00
parent 7c40272f24
commit 6a41e75852
9 changed files with 199 additions and 3 deletions

View File

@ -28,12 +28,16 @@ clean_langdetect:
clean_spamdetect:
rm -rf services/spam_detection/build services/spam_detection/dist services/spam_detection/coffeehouse_spamdetection.egg-info
clean_translation:
rm -rf services/translation/build services/translation/dist services/translation/coffeehouse_translation.egg-info
clean:
make clean_apt clean_stopwords clean_tokenizer clean_nlpfr
make clean_dltc
make clean_his
make clean_alg
make clean_rf
make clean_translation
make clean_langdetect
make clean_spamdetect
@ -73,12 +77,16 @@ build_langdetect:
build_spamdetect:
cd services/spam_detection; python3 setup.py build; python3 setup.py sdist
build_translation:
cd services/translation; python3 setup.py build; python3 setup.py sdist
build:
make build_nlpfr
make build_his
make build_dltc
make build_alg
make build_rf
make buid_translation
make build_langdetect
make build_spamdetect
@ -119,12 +127,16 @@ install_langdetect:
install_spamdetect:
cd services/spam_detection; python3 setup.py install
install_translation:
cd services/translation; python3 setup.py install
install:
make install_rf
make install_nlpfr
make install_his
make install_dltc
make install_alg
make install_translation
make install_langdetect
make install_spamdetect
@ -148,4 +160,7 @@ start_langdetect:
python3 -m coffeehouse_languagedetection --start-server
start_spamdetect:
python3 -m coffeehouse_spamdetection --start-server
python3 -m coffeehouse_spamdetection --start-server
start_translation:
python3 -m coffeehouse_translation --start-server

View File

@ -31,4 +31,14 @@ Once CoffeeHousePy is installed, you can start it's services indvidually.
```shell
make start_langdetect # Starts the language detection server, runs on port 5606
```
make start_spamdetect # Starts the spam detection server, runs on port 5601
make start_translate # Starts the translation server, runs on port 5603
```
### Services Ports
| Name | Protocol | Port |
|--------------------------------|----------|------|
| CoffeeHouse Language Detection | HTTP | 5606 |
| CoffeeHouse Spam Detection | HTTP | 5601 |
| CoffeeHouse Translate | HTTP | 5603 |

View File

@ -41,7 +41,7 @@ def _help_menu(argv=None):
"CoffeeHouse SpamDetection CLI\n\n"
" --help\n"
" --test\n"
" --start-servver rrr\n"
" --start-server\n"
)
sys.exit()

View File

@ -0,0 +1,6 @@
# Translation
This CoffeeHousePy service provides an endpoint for programs
to translate text into another language. The purpose of this
add-on is to enable more languages than the model can
support, so the resource usage can be cut down to a minimal.

View File

@ -0,0 +1,7 @@
from . import main
from .main import *
from . import server
from .server import *
__all__ = ["main", "ChTranslation", "Server"]

View File

@ -0,0 +1,49 @@
import sys
from coffeehouse_translation import Server
def _real_main(argv=None):
"""
The main command-line processor
:param argv:
:return:
"""
if argv[1] == '--help':
_help_menu(argv)
if argv[1] == '--start-server':
_start_server(argv)
def _start_server(argv=None):
"""
Starts the server
:param argv:
:return:
"""
server = Server()
server.start()
def _help_menu(argv=None):
"""
Displays the help menu and commandline usage
:param argv:
:return:
"""
print(
"CoffeeHouse Translation CLI\n\n"
" --help\n"
" --start-server\n"
)
sys.exit()
if __name__ == '__main__':
try:
_real_main(sys.argv)
except KeyboardInterrupt:
print('\nInterrupted by user')

View File

@ -0,0 +1,24 @@
from googletrans import Translator
__all__ = ['ChTranslation']
class ChTranslation(object):
def __init__(self):
"""
Public Constructor
"""
self.google_translator = Translator()
def google_translate(self, source, output, text_input):
"""
Translates the given input using Google Translate
:rtype: object
"""
results = self.google_translator.translate(text_input, src=source, dest=output)
return {
"text": results.text,
"pronunciation": results.pronunciation
}

View File

@ -0,0 +1,64 @@
from coffeehouse_translation import ChTranslation
from hyper_internal_service import web
__all__ = ['Server']
class Server(object):
def __init__(self, port=5603):
"""
Public Constructor
:param port:
"""
self.port = port
self.web_application = web.Application()
self.web_application.add_routes(
[
web.post('/', self.root_page),
web.post('/google_translate', self.google_translate)
]
)
self.ch_translation = ChTranslation()
async def root_page(self, request):
"""
Handles the "/" page, does nothing special
:return:
"""
return web.json_response({"status": True})
async def google_translate(self, request):
"""
Handles the predict request "/google_translate", usage:
POST:: "source": str Source language
POST:: "output": str Output language
POST:: "input": str The input data
:param request:
:return:
"""
post_data = await request.post()
results = self.ch_translation.google_translate(post_data["source"], post_data["output"], post_data["input"])
response = {
"status": True,
"results": results
}
return web.json_response(response)
def start(self):
"""
Starts the web application
:return:
"""
web.run_app(app=self.web_application, port=self.port)
return True
def stop(self):
"""
Stops the web application
:return:
"""
self.web_application.shutdown()
self.web_application.cleanup()
return True

View File

@ -0,0 +1,21 @@
from setuptools import setup
with open("README.md", "r") as file:
long_description = file.read()
setup(
name='coffeehouse_translation',
version='1.0.0',
description='Translates inputs into different languages',
long_description=long_description,
long_description_content_type="text/markdown",
packages=['coffeehouse_translation'],
package_dir={
'coffeehouse_translation': 'coffeehouse_translation'
},
author='Intellivoid Technologies',
author_email='netkas@intellivoid.net',
install_requires=[
'googletrans'
]
)