Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions perspective_ranker.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ def filter(self, record: logging.LogRecord) -> bool:
# -- Ranking Logic --
SCORING_TIMEOUT = 1.0 # seconds

# Perspective API limits statement length to 20k
MAX_STATEMENT_LENGTH = 1024 * 20 # 20k


class PerspectiveRanker:
ScoredStatement = namedtuple(
Expand Down Expand Up @@ -197,6 +200,9 @@ async def score(self, attributes, statement, statement_id):
if not statement.strip():
return self.ScoredStatement(statement, [], statement_id, False, 0)

statement = statement.strip()
statement = statement.encode('utf-8')[:MAX_STATEMENT_LENGTH].decode('utf-8', 'ignore') # truncate to n bytes

headers = {"Content-Type": "application/json"}
data = {
"comment": {"text": statement},
Expand Down Expand Up @@ -226,8 +232,14 @@ async def score(self, attributes, statement, statement_id):
response = await self.client.post(
url=PERSPECTIVE_URL, json=data, headers=headers, timeout=self.scoring_timeout
)
try:
# this can no longer be retrieved after we raise, but it may contain useful
# error information
response_json = await response.json()
except:
response_json = None

response.raise_for_status() # should fall through to outer try:
response_json = await response.json()
break
except asyncio.TimeoutError:
scoring_timeouts.inc()
Expand Down Expand Up @@ -264,11 +276,8 @@ async def score(self, attributes, statement, statement_id):

except aiohttp.ClientResponseError as e:
logger.error(f"HTTP error {e.status} {e.message} occurred for statement_id {statement_id}")
try:
# try to get the response text
logger.error(f"HTTP error response text: {await response.text()}")
except Exception:
pass
if response_json and "error" in response_json and "message" in response_json["error"]:
logger.error(f"Perspective API error message: {response_json['error']['message']}")
raise e

except Exception as e:
Expand Down
18 changes: 18 additions & 0 deletions single-request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import asyncio
from perspective_ranker import PerspectiveRanker,perspective_baseline

# a simple script for running a single request against the real API, so it's possible to
# easily examine the result

async def main():
ranker = PerspectiveRanker()

statement = 'foo bar 😅 ' * 5000

result = await ranker.score(perspective_baseline, statement, 'some-id')

breakpoint()
return result


asyncio.run(main())
Loading