ccg_nlpy.remote_pipeline module
import json
import requests
import sys
import os
import logging
from backports.configparser import RawConfigParser
from .pipeline_base import *
from google.protobuf import json_format
from .protobuf import TextAnnotation_pb2
from .core.text_annotation import *
from .download import get_model_path
from . import pipeline_config
from . import utils
WEB_SERVER_SUFFIX = '/annotate'
WEB_SERVER_SUFFIX_ADDVIEWS = '/addviews'
logger = logging.getLogger(__name__)
class RemotePipeline(PipelineBase):
def __init__(self, server_api=None, file_name=None):
"""
Constructor to set up remote pipeline
@param: server_api, the address and port of the server (ex. http://fancyUrlName.com:8080)
file_name, the file name of custom config file
"""
super(RemotePipeline,self).__init__(file_name)
# reroute to new API if user provides one
pipeline_config.change_temporary_config(self.config, self.models_downloaded, True, server_api)
self.url = self.config['remote_pipeline_setting']['api']
logger.info("pipeline has been set up")
def call_server(self, text, views):
"""
Funtion to get preprocess text annotation from server
@param: text, the text to generate text annotation on
views, the views to generate
@return: raw text of the response from server
"""
response = None
try:
data = {'text': text, 'views': views}
response = requests.post(self.url+WEB_SERVER_SUFFIX, data)
except:
logger.error("Fail to connect to server.")
raise
try:
if response.status_code == 200:
return response.text
elif response.status_code == 429:
logger.error("You reached maximum query limit with default remote server (100 queries/day)")
raise Exception("You reached maximum query limit with default remote server (100 queries/day)")
else:
logger.warn("Unexpected status code {}, please open an issue on GitHub for further investigation.".format(response.status_code))
return None
except:
raise
def call_server_pretokenized(self, pretokenized_text, views):
"""
Text Annotation for pre-tokenized text is not implemented for
remote pipeline. Use local pipeline instead.
"""
raise NotImplemented
def add_additional_views_to_TA(self, textannotation, views):
response = None
try:
jsonStrTA = json.dumps(textannotation.as_json)
data = {'jsonstr': jsonStrTA, 'views': views}
response = requests.post(self.url+WEB_SERVER_SUFFIX_ADDVIEWS, data)
except:
logger.error("Fail to connect to server.")
raise
try:
if response.status_code == 200:
return response.text
elif response.status_code == 429:
logger.error("You reached maximum query limit with default remote server (100 queries/day)")
raise Exception("You reached maximum query limit with default remote server (100 queries/day)")
else:
logger.warn("Unexpected status code {}, please open an issue on GitHub for further investigation.".format(response.status_code))
return None
except:
raise
Module variables
var WEB_SERVER_SUFFIX
var WEB_SERVER_SUFFIX_ADDVIEWS
var logger
Classes
class RemotePipeline
class RemotePipeline(PipelineBase):
def __init__(self, server_api=None, file_name=None):
"""
Constructor to set up remote pipeline
@param: server_api, the address and port of the server (ex. http://fancyUrlName.com:8080)
file_name, the file name of custom config file
"""
super(RemotePipeline,self).__init__(file_name)
# reroute to new API if user provides one
pipeline_config.change_temporary_config(self.config, self.models_downloaded, True, server_api)
self.url = self.config['remote_pipeline_setting']['api']
logger.info("pipeline has been set up")
def call_server(self, text, views):
"""
Funtion to get preprocess text annotation from server
@param: text, the text to generate text annotation on
views, the views to generate
@return: raw text of the response from server
"""
response = None
try:
data = {'text': text, 'views': views}
response = requests.post(self.url+WEB_SERVER_SUFFIX, data)
except:
logger.error("Fail to connect to server.")
raise
try:
if response.status_code == 200:
return response.text
elif response.status_code == 429:
logger.error("You reached maximum query limit with default remote server (100 queries/day)")
raise Exception("You reached maximum query limit with default remote server (100 queries/day)")
else:
logger.warn("Unexpected status code {}, please open an issue on GitHub for further investigation.".format(response.status_code))
return None
except:
raise
def call_server_pretokenized(self, pretokenized_text, views):
"""
Text Annotation for pre-tokenized text is not implemented for
remote pipeline. Use local pipeline instead.
"""
raise NotImplemented
def add_additional_views_to_TA(self, textannotation, views):
response = None
try:
jsonStrTA = json.dumps(textannotation.as_json)
data = {'jsonstr': jsonStrTA, 'views': views}
response = requests.post(self.url+WEB_SERVER_SUFFIX_ADDVIEWS, data)
except:
logger.error("Fail to connect to server.")
raise
try:
if response.status_code == 200:
return response.text
elif response.status_code == 429:
logger.error("You reached maximum query limit with default remote server (100 queries/day)")
raise Exception("You reached maximum query limit with default remote server (100 queries/day)")
else:
logger.warn("Unexpected status code {}, please open an issue on GitHub for further investigation.".format(response.status_code))
return None
except:
raise
Ancestors (in MRO)
- RemotePipeline
- ccg_nlpy.pipeline_base.PipelineBase
- __builtin__.object
Instance variables
var url
Methods
def __init__(
self, server_api=None, file_name=None)
Constructor to set up remote pipeline
@param: server_api, the address and port of the server (ex. http://fancyUrlName.com:8080) file_name, the file name of custom config file
def __init__(self, server_api=None, file_name=None):
"""
Constructor to set up remote pipeline
@param: server_api, the address and port of the server (ex. http://fancyUrlName.com:8080)
file_name, the file name of custom config file
"""
super(RemotePipeline,self).__init__(file_name)
# reroute to new API if user provides one
pipeline_config.change_temporary_config(self.config, self.models_downloaded, True, server_api)
self.url = self.config['remote_pipeline_setting']['api']
logger.info("pipeline has been set up")
def add_additional_views_to_TA(
self, textannotation, views)
def add_additional_views_to_TA(self, textannotation, views):
response = None
try:
jsonStrTA = json.dumps(textannotation.as_json)
data = {'jsonstr': jsonStrTA, 'views': views}
response = requests.post(self.url+WEB_SERVER_SUFFIX_ADDVIEWS, data)
except:
logger.error("Fail to connect to server.")
raise
try:
if response.status_code == 200:
return response.text
elif response.status_code == 429:
logger.error("You reached maximum query limit with default remote server (100 queries/day)")
raise Exception("You reached maximum query limit with default remote server (100 queries/day)")
else:
logger.warn("Unexpected status code {}, please open an issue on GitHub for further investigation.".format(response.status_code))
return None
except:
raise
def call_server(
self, text, views)
Funtion to get preprocess text annotation from server
@param: text, the text to generate text annotation on views, the views to generate @return: raw text of the response from server
def call_server(self, text, views):
"""
Funtion to get preprocess text annotation from server
@param: text, the text to generate text annotation on
views, the views to generate
@return: raw text of the response from server
"""
response = None
try:
data = {'text': text, 'views': views}
response = requests.post(self.url+WEB_SERVER_SUFFIX, data)
except:
logger.error("Fail to connect to server.")
raise
try:
if response.status_code == 200:
return response.text
elif response.status_code == 429:
logger.error("You reached maximum query limit with default remote server (100 queries/day)")
raise Exception("You reached maximum query limit with default remote server (100 queries/day)")
else:
logger.warn("Unexpected status code {}, please open an issue on GitHub for further investigation.".format(response.status_code))
return None
except:
raise
def call_server_pretokenized(
self, pretokenized_text, views)
Text Annotation for pre-tokenized text is not implemented for remote pipeline. Use local pipeline instead.
def call_server_pretokenized(self, pretokenized_text, views):
"""
Text Annotation for pre-tokenized text is not implemented for
remote pipeline. Use local pipeline instead.
"""
raise NotImplemented
def doc(
self, text='Hello World', pretokenized=False)
Initialize text annotation of given text
@param: text, the text to be processed @return: TextAnnotation instance of the text, None if text is empty
def doc(self, text="Hello World", pretokenized=False):
"""
Initialize text annotation of given text
@param: text, the text to be processed
@return: TextAnnotation instance of the text, None if text is empty
"""
if not pretokenized:
response = self.call_server(text, "TOKENS")
else:
response = self.call_server_pretokenized(text, "TOKENS")
if response is not None:
return TextAnnotation(response, self)
else:
return None