MCPcopy Create free account
hub / github.com/GXimingLu/Quark / request_bulk

Method request_bulk

utils/perspective_api.py:85–123  ·  view source on GitHub ↗
(self,
                     corpus: Union[Iterable[str], Iterable[Tuple[str, str]]],
                     output_file: Union[str, Path],
                     pbar: tqdm = None)

Source from the content-addressed store, hash-verified

83 return list(responses.values())
84
85 def request_bulk(self,
86 corpus: Union[Iterable[str], Iterable[Tuple[str, str]]],
87 output_file: Union[str, Path],
88 pbar: tqdm = None):
89 # Check for output file
90 output_file = Path(output_file)
91 assert not output_file.exists()
92
93 # Set up progress bar
94 if not pbar:
95 total = len(corpus) if isinstance(corpus, collections.abc.Sequence) else None
96 pbar = tqdm(total=total, dynamic_ncols=True)
97 pbar.set_description(f'Perspective API')
98
99 i = 0
100 num_failures = 0
101 with output_file.open('a') as f:
102 for batch in batchify(corpus, self.rate_limit):
103 request_ids = None
104 if isinstance(batch[0], tuple):
105 request_ids, batch = zip(*batch)
106
107 for j, (response, exception) in enumerate(self.request(batch)):
108 response_dict = {
109 'request_id': request_ids[j] if request_ids else i,
110 'response': response,
111 'error': str(exception) if exception else None
112 }
113
114 # Save response
115 json.dump(response_dict, f)
116 f.write('\n')
117
118 if exception:
119 num_failures += 1
120
121 i += len(batch)
122 pbar.update(len(batch))
123 pbar.set_postfix(failures=num_failures, rate_limt=self.rate_limit)
124
125 @staticmethod
126 def _make_service(api_key: str):

Callers 1

perspective_workerMethod · 0.95

Calls 3

requestMethod · 0.95
batchifyFunction · 0.90
updateMethod · 0.45

Tested by

no test coverage detected