class_docstr: Perspective API predicts the perceived impact a comment may have on a conversation by evaluating that comment
  across a range of emotional concepts, called attributes. When you send a request to the API, you’ll request the
  specific attributes you want to receive scores for.
  
  The API is hosted on Google Cloud Platform.
  
  Source: https://developers.perspectiveapi.com/s/docs
class_docstr_tok: ["Perspective","API","predicts","the","perceived","impact","a","comment","may","have","on","a","conversation","by","evaluating","that","comment","across","a","range","of","emotional","concepts",",","called","attributes",".","When","you","send","a","request","to","the","API",",","you","\u2019","ll","request","the","specific","attributes","you","want","to","receive","scores","for",".","The","API","is","hosted","on","Google","Cloud","Platform",".","Source",":","https",":","\/\/developers.perspectiveapi.com\/s\/docs"]
docstr: Batch several requests into a single API request and get the toxicity attributes and scores.
  For more information, see https://googleapis.github.io/google-api-python-client/docs/batch.html.
docstr_tok: ["Batch","several","requests","into","a","single","API","request","and","get","the","toxicity","attributes","and","scores",".","For","more","information",",","see","https",":","\/\/googleapis.github.io\/google-api-python-client\/docs\/batch.html","."]
code: def get_toxicity_scores(
      self, request: PerspectiveAPIRequest
  ) -> PerspectiveAPIRequestResult:
      """
      Batch several requests into a single API request and get the toxicity attributes and scores.
      For more information, see https://googleapis.github.io/google-api-python-client/docs/batch.html.
      """
      client = self._get_or_initialize_client()
  
      try:
  
          def do_it():
              text_to_response: Dict[str, Dict] = dict()
  
              def callback(
                  request_id: str, response: Dict, error: HttpError
              ):
                  if error:
                      raise error
                  text_to_response[request_id] = response
  
              # Create a batch request. We will add a request to the batch request for each text string
              batch_request: BatchHttpRequest = (
                  client.new_batch_http_request()
              )
  
              # Add individual request to the batch request. Deduplicate since we use the text as request keys.
              for text in set(request.text_batch):
                  batch_request.add(
                      request=client.comments().analyze(
                          body=PerspectiveAPIClient.create_request_body(
                              text[
                                  : PerspectiveAPIClient.MAX_TEXT_LENGTH
                              ],
                              request.attributes,
                              request.languages,
                          )
                      ),
                      request_id=text,
                      callback=callback,
                  )
  
              with self.request_lock:
                  batch_request.execute()
              return text_to_response
  
          batch_response, cached = self.cache.get(
              asdict(request), do_it
          )
  
      except (BatchError, HttpLib2Error, HttpError) as e:
          return PerspectiveAPIRequestResult(
              success=False,
              cached=False,
              error=f"Error was thrown when making a request to Perspective API: {e}",
          )
  
      return PerspectiveAPIRequestResult(
          success=True,
          cached=cached,
          text_to_toxicity_attributes={
              text: PerspectiveAPIClient.extract_toxicity_attributes(
                  response
              )
              for text, response in batch_response.items()
          },
      )
  
file_path: H2O/h2o_hf/helm/src/helm/proxy/clients/perspective_api_client.py
imports: import threading
  from dataclasses import asdict
  from typing import List, Dict, Optional
  from dacite import from_dict
  from googleapiclient import discovery
  from googleapiclient.errors import BatchError, HttpError
  from googleapiclient.http import BatchHttpRequest
  from httplib2 import HttpLib2Error
  from helm.common.cache import Cache, CacheConfig
  from helm.common.perspective_api_request import ToxicityAttributes, PerspectiveAPIRequest, PerspectiveAPIRequestResult
label_desc: Use image node_id 5 for calling the PerspectiveAPIClient obj's underlying member method code with example usage: obj.get_toxicity_scores(request) and returns: PerspectiveAPIRequestResult, text_to_response, PerspectiveAPIRequestResult