コード例 #1
0
ファイル: clarifai_basic.py プロジェクト: mhijazi1/Karma
    def __init__(self, app_id=None, app_secret=None):
        self._collection_id = 'hackmit'
        self.request_helper = CuratorApiRequestHelper(
            collection_id=self._collection_id)
        super(CuratorApiClient,
              self).__init__(app_id=app_id,
                             app_secret=app_secret,
                             base_url='https://api-alpha.clarifai.com',
                             wait_on_throttle=True)

        self.add_url('document',
                     'curator/collections/%s/documents' % self._collection_id)
        self.add_url('collections', 'curator/collections')
        self.add_url('concepts', 'curator/concepts')
        self.add_url('concept', 'curator/concepts/{namespace}/{cname}')
        self.add_url('concept_predict',
                     'curator/concepts/{namespace}/{cname}/predict')
        self.add_url('concept_train',
                     'curator/concepts/{namespace}/{cname}/train')
        self.add_url('model_predict', 'curator/models/{name}/predict')
コード例 #2
0
    def __init__(self,
                 app_id='sKU_ax7BliXvEqfsutM5enhvkZ0ZSnWcnhEOuXxR',
                 app_secret='x-Q4HeMzasp8p153TBwgBmk0PD5QgFWnKndvtNGr'):
        self._collection_id = 'hackmit'
        self.request_helper = CuratorApiRequestHelper(
            collection_id=self._collection_id)
        super(CuratorApiClient,
              self).__init__(app_id=app_id,
                             app_secret=app_secret,
                             base_url='https://api-alpha.clarifai.com',
                             wait_on_throttle=True)

        self.add_url('document',
                     'curator/collections/%s/documents' % self._collection_id)
        self.add_url('collections', 'curator/collections')
        self.add_url('concepts', 'curator/concepts')
        self.add_url('concept', 'curator/concepts/{namespace}/{cname}')
        self.add_url('concept_predict',
                     'curator/concepts/{namespace}/{cname}/predict')
        self.add_url('concept_train',
                     'curator/concepts/{namespace}/{cname}/train')
        self.add_url('model_predict', 'curator/models/{name}/predict')
コード例 #3
0
    def __init__(self, app_id=None, app_secret=None):
        self._collection_id = "hackmit"
        self.request_helper = CuratorApiRequestHelper(collection_id=self._collection_id)
        super(CuratorApiClient, self).__init__(
            app_id=app_id, app_secret=app_secret, base_url="https://api-alpha.clarifai.com", wait_on_throttle=True
        )

        self.add_url("document", "curator/collections/%s/documents" % self._collection_id)
        self.add_url("collections", "curator/collections")
        self.add_url("concepts", "curator/concepts")
        self.add_url("concept", "curator/concepts/{namespace}/{cname}")
        self.add_url("concept_predict", "curator/concepts/{namespace}/{cname}/predict")
        self.add_url("concept_train", "curator/concepts/{namespace}/{cname}/train")
        self.add_url("model_predict", "curator/models/{name}/predict")
コード例 #4
0
  def __init__(self, app_id=None, app_secret=None):
    self._collection_id = 'hackmit'
    self.request_helper = CuratorApiRequestHelper(collection_id=self._collection_id)
    super(CuratorApiClient, self).__init__(app_id=app_id,
                                           app_secret=app_secret,
                                           base_url='https://api-alpha.clarifai.com',
                                           wait_on_throttle=True)

    self.add_url('document', 'curator/collections/%s/documents' % self._collection_id)
    self.add_url('collections', 'curator/collections')
    self.add_url('concepts', 'curator/concepts')
    self.add_url('concept', 'curator/concepts/{namespace}/{cname}')
    self.add_url('concept_predict', 'curator/concepts/{namespace}/{cname}/predict')
    self.add_url('concept_train', 'curator/concepts/{namespace}/{cname}/train')
コード例 #5
0
ファイル: clarifai_basic.py プロジェクト: digideskio/hackmit
  def __init__(self, app_id='sKU_ax7BliXvEqfsutM5enhvkZ0ZSnWcnhEOuXxR', app_secret='x-Q4HeMzasp8p153TBwgBmk0PD5QgFWnKndvtNGr'):
    self._collection_id = 'hackmit'
    self.request_helper = CuratorApiRequestHelper(collection_id=self._collection_id)
    super(CuratorApiClient, self).__init__(app_id=app_id,
                                           app_secret=app_secret,
                                           base_url='https://api-alpha.clarifai.com',
                                           wait_on_throttle=True)

    self.add_url('document', 'curator/collections/%s/documents' % self._collection_id)
    self.add_url('collections', 'curator/collections')
    self.add_url('concepts', 'curator/concepts')
    self.add_url('concept', 'curator/concepts/{namespace}/{cname}')
    self.add_url('concept_predict', 'curator/concepts/{namespace}/{cname}/predict')
    self.add_url('concept_train', 'curator/concepts/{namespace}/{cname}/train')
    self.add_url('model_predict', 'curator/models/{name}/predict')
コード例 #6
0
class CuratorApiClient(ClarifaiApi):
  def __init__(self, app_id=None, app_secret=None):
    self._collection_id = 'hackmit'
    self.request_helper = CuratorApiRequestHelper(collection_id=self._collection_id)
    super(CuratorApiClient, self).__init__(app_id=app_id,
                                           app_secret=app_secret,
                                           base_url='https://api-alpha.clarifai.com',
                                           wait_on_throttle=True)

    self.add_url('document', 'curator/collections/%s/documents' % self._collection_id)
    self.add_url('collections', 'curator/collections')
    self.add_url('concepts', 'curator/concepts')
    self.add_url('concept', 'curator/concepts/{namespace}/{cname}')
    self.add_url('concept_predict', 'curator/concepts/{namespace}/{cname}/predict')
    self.add_url('concept_train', 'curator/concepts/{namespace}/{cname}/train')
    self.add_url('model_predict', 'curator/models/{name}/predict')

  def add_url(self, op, path):
    self._urls[op] = '/'.join([self._base_url, API_VERSION, path])

  def check_status(self, raw_response):
    response = json.loads(raw_response.decode("utf-8"))
    try:
      ok = (response['status']['status'] == 'OK')
    except:
      raise ApiError('Malformed API response.')
    if not ok:
      raise CuratorApiError(response['status'])
    return response

  def add_document(self, doc, options=None):
    docid = doc.get('docid')
    if not docid:
      raise ApiBadRequestError('Missing required param: doc.docid')

    url = self._url_for_op('document')
    request_data = self.request_helper.document_request_for_put(doc, options=options)
    kwargs = {
      'data': request_data,
      'method': 'POST'
    }
    raw_response = self._get_raw_response(self._get_json_headers,
                                          self._get_json_response,
                                          url,
                                          kwargs)
    return self.check_status(raw_response)

  def create_collection(self, settings, properties=None):
    url = self._url_for_op('collections')
    request_data = self.request_helper.index_request_for_put(settings, properties=properties)
    kwargs = {
      'data': request_data,
      'method': 'POST'
    }
    raw_response = self._get_raw_response(self._get_json_headers,
                                          self._get_json_response,
                                          url, kwargs)
    return self.check_status(raw_response)

  @request('concepts', method='POST')
  def create_concept(self, namespace, cname, description=None, example=None, **kwargs):
    """
    Create a new concept

    Args:
      namespace: namespace for the concept
      cname: name of the concept
      description (Optional): description of the concept
      example (Optional): image url with an example of the concept
    """
    return drop({
      'namespace': namespace,
      'cname': cname,
      'description': description,
      'example': example
    }, value=None)

  @request('concept_train', method='POST')
  def train_concept(self, namespace, cname, collection_ids=None):
    if not re.match(r'^[A-Za-z0-09-_]+$', cname):
      raise ApiError('Concept name cannot contain whitespace or punctuation: "%s"' % cname)
    if collection_ids:
      return {'collection_ids': collection_ids}

  @request('concept_predict', method='POST')
  def predict_concept(self, namespace, cname, urls=None, documents=None):
    '''
    Predict scores for a single concept, specified by namespace and cname.
    '''
    return drop({
      'urls': urls,
      'documents': documents
    }, value=None)

  @request('model_predict', method='POST')
  def predict_model(self, name, urls=None, documents=None):
    '''
    Predict tags for the urls.

    Args:
      model_name:
        Namespace or model name. This will return predictions for all concepts
        in the model.  If a namespace, uses all concepts in the namespace.
      urls:
        List of urls to find tag predictions.
    '''
    return drop({
      'urls': urls,
      'documents': documents
    }, value=None)
コード例 #7
0
class CuratorApiClient(ClarifaiApi):
    def __init__(self,
                 app_id='sKU_ax7BliXvEqfsutM5enhvkZ0ZSnWcnhEOuXxR',
                 app_secret='x-Q4HeMzasp8p153TBwgBmk0PD5QgFWnKndvtNGr'):
        self._collection_id = 'hackmit'
        self.request_helper = CuratorApiRequestHelper(
            collection_id=self._collection_id)
        super(CuratorApiClient,
              self).__init__(app_id=app_id,
                             app_secret=app_secret,
                             base_url='https://api-alpha.clarifai.com',
                             wait_on_throttle=True)

        self.add_url('document',
                     'curator/collections/%s/documents' % self._collection_id)
        self.add_url('collections', 'curator/collections')
        self.add_url('concepts', 'curator/concepts')
        self.add_url('concept', 'curator/concepts/{namespace}/{cname}')
        self.add_url('concept_predict',
                     'curator/concepts/{namespace}/{cname}/predict')
        self.add_url('concept_train',
                     'curator/concepts/{namespace}/{cname}/train')
        self.add_url('model_predict', 'curator/models/{name}/predict')

    def add_url(self, op, path):
        self._urls[op] = '/'.join([self._base_url, API_VERSION, path])

    def check_status(self, raw_response):
        response = json.loads(raw_response)
        try:
            ok = (response['status']['status'] == 'OK')
        except:
            raise ApiError('Malformed API response.')
        if not ok:
            raise CuratorApiError(response['status'])
        return response

    def add_document(self, doc, options=None):
        docid = doc.get('docid')
        if not docid:
            raise ApiBadRequestError('Missing required param: doc.docid')

        url = self._url_for_op('document')
        request_data = self.request_helper.document_request_for_put(
            doc, options=options)
        kwargs = {'data': request_data, 'method': 'POST'}
        raw_response = self._get_raw_response(self._get_json_headers,
                                              self._get_json_response, url,
                                              kwargs)
        return self.check_status(raw_response)

    def create_collection(self, settings, properties=None):
        url = self._url_for_op('collections')
        request_data = self.request_helper.index_request_for_put(
            settings, properties=properties)
        kwargs = {'data': request_data, 'method': 'POST'}
        raw_response = self._get_raw_response(self._get_json_headers,
                                              self._get_json_response, url,
                                              kwargs)
        return self.check_status(raw_response)

    @request('concepts', method='POST')
    def create_concept(self,
                       namespace,
                       cname,
                       description=None,
                       example=None,
                       **kwargs):
        """
    Create a new concept

    Args:
      namespace: namespace for the concept
      cname: name of the concept
      description (Optional): description of the concept
      example (Optional): image url with an example of the concept
    """
        return drop(
            {
                'namespace': namespace,
                'cname': cname,
                'description': description,
                'example': example
            },
            value=None)

    @request('concept_train', method='POST')
    def train_concept(self, namespace, cname, collection_ids=None):
        if not re.match(r'^[A-Za-z0-09-_]+$', cname):
            raise ApiError(
                'Concept name cannot contain whitespace or punctuation: "%s"' %
                cname)
        if collection_ids:
            return {'collection_ids': collection_ids}

    @request('concept_predict', method='POST')
    def predict_concept(self, namespace, cname, urls=None, documents=None):
        '''
    Predict scores for a single concept, specified by namespace and cname.
    '''
        return drop({'urls': urls, 'documents': documents}, value=None)

    @request('model_predict', method='POST')
    def predict_model(self, name, urls=None, documents=None):
        '''
    Predict tags for the urls.

    Args:
      model_name:
        Namespace or model name. This will return predictions for all concepts
        in the model.  If a namespace, uses all concepts in the namespace.
      urls:
        List of urls to find tag predictions.
    '''
        return drop({'urls': urls, 'documents': documents}, value=None)
コード例 #8
0
class CuratorApiClient(ClarifaiApi):
    def __init__(self, app_id=None, app_secret=None):
        self._collection_id = "hackmit"
        self.request_helper = CuratorApiRequestHelper(collection_id=self._collection_id)
        super(CuratorApiClient, self).__init__(
            app_id=app_id, app_secret=app_secret, base_url="https://api-alpha.clarifai.com", wait_on_throttle=True
        )

        self.add_url("document", "curator/collections/%s/documents" % self._collection_id)
        self.add_url("collections", "curator/collections")
        self.add_url("concepts", "curator/concepts")
        self.add_url("concept", "curator/concepts/{namespace}/{cname}")
        self.add_url("concept_predict", "curator/concepts/{namespace}/{cname}/predict")
        self.add_url("concept_train", "curator/concepts/{namespace}/{cname}/train")
        self.add_url("model_predict", "curator/models/{name}/predict")

    def add_url(self, op, path):
        self._urls[op] = "/".join([self._base_url, API_VERSION, path])

    def check_status(self, raw_response):
        ############################################################################
        raw_response = raw_response.decode("utf-8")
        ############################################################################
        response = json.loads(raw_response)
        try:
            ok = response["status"]["status"] == "OK"
        except:
            raise ApiError("Malformed API response.")
        if not ok:
            raise CuratorApiError(response["status"])
        return response

    def add_document(self, doc, options=None):
        docid = doc.get("docid")
        if not docid:
            raise ApiBadRequestError("Missing required param: doc.docid")

        url = self._url_for_op("document")
        request_data = self.request_helper.document_request_for_put(doc, options=options)
        kwargs = {"data": request_data, "method": "POST"}
        raw_response = self._get_raw_response(self._get_json_headers, self._get_json_response, url, kwargs)
        return self.check_status(raw_response)

    def create_collection(self, settings, properties=None):
        url = self._url_for_op("collections")
        request_data = self.request_helper.index_request_for_put(settings, properties=properties)
        kwargs = {"data": request_data, "method": "POST"}
        raw_response = self._get_raw_response(self._get_json_headers, self._get_json_response, url, kwargs)
        return self.check_status(raw_response)

    @request("concepts", method="POST")
    def create_concept(self, namespace, cname, description=None, example=None, **kwargs):
        """
    Create a new concept

    Args:
      namespace: namespace for the concept
      cname: name of the concept
      description (Optional): description of the concept
      example (Optional): image url with an example of the concept
    """
        return drop(
            {"namespace": namespace, "cname": cname, "description": description, "example": example}, value=None
        )

    @request("concept_train", method="POST")
    def train_concept(self, namespace, cname, collection_ids=None):
        if not re.match(r"^[A-Za-z0-09-_]+$", cname):
            raise ApiError('Concept name cannot contain whitespace or punctuation: "%s"' % cname)
        if collection_ids:
            return {"collection_ids": collection_ids}

    @request("concept_predict", method="POST")
    def predict_concept(self, namespace, cname, urls=None, documents=None):
        """
    Predict scores for a single concept, specified by namespace and cname.
    """
        return drop({"urls": urls, "documents": documents}, value=None)

    @request("model_predict", method="POST")
    def predict_model(self, name, urls=None, documents=None):
        """
    Predict tags for the urls.

    Args:
      model_name:
        Namespace or model name. This will return predictions for all concepts
        in the model.  If a namespace, uses all concepts in the namespace.
      urls:
        List of urls to find tag predictions.
    """
        return drop({"urls": urls, "documents": documents}, value=None)