Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

Turn safety and LIGHT-ness MTurk job (#137) #313

Draft
wants to merge 792 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
792 commits
Select commit Hold shift + click to select a range
bf1df82
Merge pull request #59 from facebookresearch/dev/limit-custom-worlds
Lucaskabela Jul 28, 2020
94019e5
Markers for slow test ,pytest and coverage running
Jul 28, 2020
0303e54
Merge branch 'master' into refactor/testing/update-tests
Jul 28, 2020
0486125
Merge pull request #62 from facebookresearch/refactor/testing/update-…
Lucaskabela Jul 28, 2020
715b3a0
merge master
Jul 28, 2020
3ebc031
Merge pull request #63 from facebookresearch/refactor/formatting/pret…
Lucaskabela Jul 28, 2020
072bb6d
Merge master
Jul 28, 2020
0d027ef
Merge branch 'master' into refactor/formatting/black
Lucaskabela Jul 28, 2020
e0e9725
Merge pull request #64 from facebookresearch/refactor/formatting/black
Lucaskabela Jul 28, 2020
b50054d
Merge master
Jul 28, 2020
d400dc1
Merge pull request #66 from facebookresearch/model-soul-shared
JackUrb Jul 28, 2020
b356156
How's your partner soul, parner?
JackUrb Jul 28, 2020
7131636
Lucas comments
JackUrb Jul 28, 2020
f5b84e9
merge
Jul 28, 2020
cb51ef3
Merge pull request #65 from facebookresearch/refactor/formatting/pre-…
Lucaskabela Jul 28, 2020
fc0bccb
Update Autosave - 2 minutes not 20
Lucaskabela Jul 28, 2020
15c1c41
Address kurt's comments, add some TODOs
JackUrb Jul 28, 2020
a285c42
Freeze requirements
Jul 28, 2020
a20883f
Merge conflicts
Jul 28, 2020
22ca442
precommit hook
Jul 28, 2020
f020e14
Merge branch 'master' into heuristic-model-soul
JackUrb Jul 28, 2020
24c6eec
Merge pull request #67 from facebookresearch/heuristic-model-soul
JackUrb Jul 28, 2020
8f7c4e9
Remove .coverage
Jul 28, 2020
331ac56
Create python-test.yml
Lucaskabela Jul 28, 2020
e4eb289
Merge branch 'refactor/testing/integration' into Lucaskabela-patch-1-1
Jul 28, 2020
acd3b0b
Fix requirements
Jul 28, 2020
895274a
Try and get things set up (need loop running earlier)
Jul 28, 2020
f7c972a
Confirm that added to history
JackUrb Jul 29, 2020
efc13f9
Adding heuristic hitting
JackUrb Jul 29, 2020
0e4aebf
Adding heuristic hitting
JackUrb Jul 29, 2020
b956115
Oh
Jul 29, 2020
ca3776b
pay down technical debt fixing flake8 errors
Jul 29, 2020
d682ad4
Try and fix workflow
Jul 29, 2020
fe18e01
Do not require yourself, that is silly
Jul 29, 2020
94b05fc
Another fix
Jul 29, 2020
c1f3b6c
Way less requirements
Jul 29, 2020
026d021
Put requirements back in
Jul 29, 2020
5ef57c3
try my best
Jul 29, 2020
a2d9784
Try and fix some linting, test
Jul 29, 2020
2cfd5ed
Merge branch 'master' into refactor/testing/integration
Jul 29, 2020
6f6ca88
Fix failing test (dead prop)
Jul 29, 2020
95b3a67
Merge branch 'refactor/testing/integration' into Lucaskabela-patch-1-1
Jul 29, 2020
fbd44f3
Merge pull request #68 from facebookresearch/refactor/testing/integra…
Lucaskabela Jul 29, 2020
cb906f1
Refactor common resources
Jul 29, 2020
7e4ffd7
Merge pull request #70 from facebookresearch/dev/run-web
Lucaskabela Jul 29, 2020
304696a
fix whitespace'
Jul 29, 2020
c16415f
Update README.md
Lucaskabela Jul 29, 2020
d737fa4
[Souls] Proper await syntax for observation
JackUrb Jul 29, 2020
47bcf94
Merge pull request #72 from facebookresearch/correct-observe-cancel
JackUrb Jul 29, 2020
fa2c207
Merge pull request #71 from facebookresearch/pertner-heuristic-fixes
JackUrb Jul 29, 2020
57d29c3
Merge pull request #69 from facebookresearch/Lucaskabela-patch-1-1
Lucaskabela Jul 30, 2020
b032db4
Fix test
Jul 30, 2020
e6430b7
Fix directions
Jul 30, 2020
5616370
Merge branch 'master' into dev/bugfix/user-dires
Jul 30, 2020
7a06232
Fix test
Jul 30, 2020
226035c
Try and fix action
Jul 30, 2020
7fd24f9
Fix lints, logger bugs
Jul 30, 2020
c755e56
Fill the soul after removing the player
Jul 30, 2020
8024828
Frontend fixes (centered and todo)
Jul 30, 2020
affc6c0
Missed a letter
Jul 30, 2020
fbd85ad
Merge branch 'dev/bugfix/dir-exists' into dev/bugfix/frontend-fixes
Jul 30, 2020
61bdcce
Merge pull request #73 from facebookresearch/dev/bugfix/user-dires
Lucaskabela Jul 31, 2020
12ca0ee
Merge pull request #74 from facebookresearch/dev/bugfix/dir-exists
Lucaskabela Jul 31, 2020
918ba01
Frontend fixes
Jul 31, 2020
c4caa47
More frontend fixing
Jul 31, 2020
d12f930
Update requirements.txt to fix flaky
Lucaskabela Jul 31, 2020
e8a93a3
Merge pull request #75 from facebookresearch/dev/bugfix/frontend-fixes
Lucaskabela Aug 3, 2020
1b58676
Fixed bug with death event ending player logger
Aug 3, 2020
5ac9ad2
Lay out some initial thoughts and design
Aug 3, 2020
4f4e72d
Break into more files
Aug 3, 2020
7e0f1ba
Work on fleshing out the design (method stubs and classes)
Aug 3, 2020
33e5f8d
Some clean up
Aug 3, 2020
08888b6
Fill in more
Aug 4, 2020
5548b65
Provide some structure, almost works
Aug 4, 2020
74b6ca2
Try and plan more for what extract episodes should do
Aug 4, 2020
8d2aca6
Get the present agents even in SayEvent
Aug 4, 2020
4bbc94f
Got a version working (is it semantically correct?)
Aug 4, 2020
61d01f5
Got a print statement showing the text logs - looks like it works
Aug 4, 2020
23e4833
Skeleton for writing the episodes
Aug 4, 2020
bfcec31
Writing some stuff
Aug 4, 2020
5b078ef
Change some class level comments, reorganize
Aug 4, 2020
b1ec84f
Some small QOL fixes
Aug 4, 2020
af9639b
Rebase
Aug 4, 2020
75598a0
Solidify format more
Aug 4, 2020
8585a39
Small semantic changes
Aug 5, 2020
25ac100
Merge branch 'dev/dataset-pipeline/extraction' into dev/dataset-pipel…
Aug 5, 2020
e895bc2
Update format ever so slightly
Aug 5, 2020
9d32625
Provide option to write an entire directory
Aug 5, 2020
5688556
Fix some slight linting related errors
Aug 5, 2020
cbf328d
Small update to format as building out task
Aug 5, 2020
632b8c8
Generalize episode to have potentially multiple settings
Aug 6, 2020
8b58970
Put idea for seperating stop/start to task in comments
Aug 6, 2020
fd485bf
Merge
Aug 6, 2020
b8db45e
Try and generalize format
Aug 6, 2020
2b8f36d
Merge on changes
Aug 6, 2020
26066d7
Need __ stuff for the current view
Aug 6, 2020
cf50e4c
Just need to change error event
Aug 6, 2020
490c44e
Try and fix all kings of things
Aug 6, 2020
efd3df8
Get room pov working too
Aug 6, 2020
1fb9767
Merge to branch with rooms
Aug 6, 2020
291b384
Working change of format
Aug 6, 2020
859e879
Fix the no exit bug
Aug 6, 2020
1143f2e
Remove prints
Aug 6, 2020
329a667
Fix export
Aug 7, 2020
4d33a6d
Merge pull request #77 from facebookresearch/dev/dataset-pipeline/design
Lucaskabela Aug 7, 2020
38b9e6c
Change naming convention
Aug 7, 2020
0407a54
Fix enter bug
Aug 7, 2020
f762a91
Merge pull request #78 from facebookresearch/dev/dataset-pipeline/ext…
Lucaskabela Aug 7, 2020
637c092
Merge pull request #79 from facebookresearch/dev/dataset-pipeline/con…
Lucaskabela Aug 7, 2020
cbe78bc
Clean up export a bit
Aug 7, 2020
8354d93
Merge pull request #80 from facebookresearch/bugfix/world-builder
Lucaskabela Aug 7, 2020
db7fd96
Make exception to log go event so we have the direction
Aug 10, 2020
a97f55f
Create script to convert old files into new dataset
Aug 11, 2020
e0525eb
Hug event fix
Aug 11, 2020
d72af80
Merge branch 'dev/go-event-fix' into dev/conversion-scripts
Aug 11, 2020
ccdb848
Fix drop event
Aug 11, 2020
62471e6
Ensuring line breaks in spoken messages, fix room swap partner clear
JackUrb Aug 11, 2020
6dad045
Merge pull request #81 from facebookresearch/line-break-debug
JackUrb Aug 11, 2020
3fdc224
Some cleanup on triggered logic, graph events
Aug 11, 2020
12883e0
Merge branch 'dev/go-event-fix' into dev/conversion-scripts
Aug 11, 2020
1919ffa
Test fixes
Aug 11, 2020
6779395
Merge branch 'dev/go-event-fix' into dev/conversion-scripts
Aug 11, 2020
50842c1
Add execute on the world and get possible actions
Aug 12, 2020
2463bd1
Merge branch 'dev/go-event-fix' into dev/conversion-scripts
Aug 12, 2020
c6d2d61
Flip order (action first, then text)
Aug 13, 2020
0124c06
Propogating Hobbot fixes downstream
JackUrb Aug 13, 2020
d46973c
load map json files
jaseweston Aug 13, 2020
24c01c8
lint
jaseweston Aug 13, 2020
bcf575e
moar
jaseweston Aug 13, 2020
c036932
rename map json, fix drop view
jaseweston Aug 13, 2020
f92218a
lint
jaseweston Aug 13, 2020
e4ba9d4
black
jaseweston Aug 13, 2020
a8767a2
Merge pull request #85 from facebookresearch/loadjsonfile
jaseweston Aug 13, 2020
2217dd4
simple world json for testing
jaseweston Aug 13, 2020
f9500ae
Merge pull request #86 from facebookresearch/safety
jaseweston Aug 13, 2020
0f1b2a5
safe dialogue
jaseweston Aug 13, 2020
5aaea9b
safety classifier opt path
jaseweston Aug 14, 2020
7684577
lint
jaseweston Aug 14, 2020
188e12d
Merge pull request #82 from facebookresearch/dev/go-event-fix
Lucaskabela Aug 14, 2020
bb61988
Provide example usage on scripts
Aug 14, 2020
c7717a4
Merge branch 'master' into dev/conversion-scripts
Lucaskabela Aug 14, 2020
3305167
Merge pull request #83 from facebookresearch/dev/conversion-scripts
Lucaskabela Aug 14, 2020
53310e3
Merge pull request #87 from facebookresearch/safety2
jaseweston Aug 14, 2020
6230abc
documentation last minute
Aug 14, 2020
b21f725
Touch up
Aug 14, 2020
0301a14
Merge pull request #88 from facebookresearch/documentation/doc-changes
Lucaskabela Aug 14, 2020
3ab5d8a
Update README.md
Lucaskabela Aug 14, 2020
a8ad544
on_eents
jaseweston Aug 14, 2020
d965846
Fixing db builder for test
JackUrb Aug 19, 2020
010f624
Merge pull request #84 from facebookresearch/hobbot-fix-syn
JackUrb Aug 19, 2020
298f659
added other events
jaseweston Aug 21, 2020
83eb8f1
on_Events in data
jaseweston Aug 21, 2020
fa5ac9e
added flags to play_map
jaseweston Aug 21, 2020
6e57f4e
lint
jaseweston Aug 21, 2020
f710a13
Merge pull request #89 from facebookresearch/block1
jaseweston Aug 21, 2020
99ef5c7
block: WIP
jaseweston Aug 23, 2020
84e1bfe
block
jaseweston Aug 23, 2020
5dcf3c5
yup
jaseweston Aug 23, 2020
a67afa3
lint
jaseweston Aug 23, 2020
80f9ebf
use works!
jaseweston Aug 28, 2020
f65f262
fix
jaseweston Aug 28, 2020
389b49d
moar
jaseweston Aug 28, 2020
cbb9043
moar
jaseweston Aug 28, 2020
312df05
tests
jaseweston Aug 28, 2020
e587f26
Merge pull request #90 from facebookresearch/block2
jaseweston Aug 28, 2020
1bed294
longcontext task
jaseweston Aug 28, 2020
083b7aa
toytoy
jaseweston Aug 30, 2020
026c137
Searching by string match first
JackUrb Aug 31, 2020
d5c6af9
moar
jaseweston Sep 1, 2020
28d2527
logger
jaseweston Sep 1, 2020
0f3f1fc
Merge pull request #91 from facebookresearch/toytoy1
jaseweston Sep 1, 2020
7166088
load and save maps
jaseweston Sep 1, 2020
6b8d425
bigger world saved
jaseweston Sep 1, 2020
987043d
Merge pull request #92 from facebookresearch/toytoy2
jaseweston Sep 1, 2020
f8f505f
npc action fixes
jaseweston Sep 1, 2020
a1b2b18
lint
jaseweston Sep 1, 2020
0a8a560
Merge pull request #93 from facebookresearch/toytoy3
jaseweston Sep 1, 2020
1ea21ce
String match and model suggestions
JackUrb Sep 1, 2020
49ba54b
lint
JackUrb Sep 1, 2020
8061deb
Merge branch 'master' of https://github.com/facebookresearch/LIGHT in…
JackUrb Sep 2, 2020
7fed9ee
Fixing test
JackUrb Sep 2, 2020
e1e9cb1
Merge pull request #94 from facebookresearch/string-match-first
JackUrb Sep 2, 2020
0285fc6
message directing to builder when game is full
JackUrb Sep 2, 2020
3e67d84
Testing message, fixing
JackUrb Sep 2, 2020
dafe170
Creating local cache of DB for json loading
JackUrb Sep 2, 2020
7883892
Merge pull request #95 from facebookresearch/direct-to-builder
JackUrb Sep 2, 2020
0094fee
longcontext task progres
jaseweston Sep 2, 2020
a61310e
moar
jaseweston Sep 2, 2020
a3d0988
moar
jaseweston Sep 2, 2020
fb60a4e
Merge pull request #97 from facebookresearch/toytoy5
jaseweston Sep 2, 2020
4f25716
Merge pull request #96 from facebookresearch/cache-cache
JackUrb Sep 2, 2020
8e0e577
switch to fixed db
jaseweston Sep 2, 2020
2906f35
Merge pull request #98 from facebookresearch/toytoy6
jaseweston Sep 2, 2020
17fa8d5
Updating cache keys
JackUrb Sep 2, 2020
7036d51
Merge pull request #99 from facebookresearch/fix-cache
JackUrb Sep 2, 2020
2c96d88
No strings loading from json
JackUrb Sep 3, 2020
fd90587
Merge pull request #100 from facebookresearch/fix-cache-more
JackUrb Sep 3, 2020
c7e1fed
longcontext kinda works
jaseweston Sep 4, 2020
bedfdd2
Merge pull request #101 from facebookresearch/toytoy8
jaseweston Sep 4, 2020
c07849c
Fixing lowercase text and other text bugs
JackUrb Sep 17, 2020
64bc363
Fixing tests
JackUrb Sep 17, 2020
116bd08
Merge pull request #102 from facebookresearch/fixing-text
JackUrb Sep 18, 2020
2c25798
blocking function
jaseweston Oct 25, 2020
6432341
lint
jaseweston Oct 25, 2020
8a184c9
Merge pull request #103 from facebookresearch/block
jaseweston Oct 25, 2020
db67735
fix get bug
jaseweston Oct 26, 2020
e86736d
Merge pull request #104 from facebookresearch/block3
jaseweston Oct 26, 2020
07334d5
wear and wield max limits
jaseweston Oct 26, 2020
cbd27b2
lint
jaseweston Oct 26, 2020
da14839
tests passing
jaseweston Oct 26, 2020
c3dfbd0
oops
jaseweston Oct 26, 2020
9849b73
Merge pull request #105 from facebookresearch/block4
jaseweston Oct 26, 2020
bfeb6e0
moving makes you tired
jaseweston Oct 26, 2020
d00a6a4
lint
jaseweston Oct 26, 2020
8f34d3c
test
jaseweston Oct 26, 2020
6416725
test
jaseweston Oct 26, 2020
c34d6d4
Merge pull request #106 from facebookresearch/block6
jaseweston Oct 26, 2020
1dc0f0a
more attacking logic
jaseweston Oct 26, 2020
bf5db35
test
jaseweston Oct 26, 2020
40d1749
Merge pull request #107 from facebookresearch/moar3
jaseweston Oct 27, 2020
1ed0aed
pacifist
jaseweston Oct 27, 2020
fe352d9
pacifism
jaseweston Oct 27, 2020
4f90522
lint
jaseweston Oct 27, 2020
02722ae
Merge pull request #108 from facebookresearch/pacificist
jaseweston Oct 27, 2020
5e3b92f
steal and block dexterity
jaseweston Oct 27, 2020
a031fb9
tests
jaseweston Oct 27, 2020
9b7f31b
Merge pull request #109 from facebookresearch/fight2
jaseweston Oct 27, 2020
ff580ac
Adding exception handling to async wrappers
JackUrb Oct 27, 2020
b1d535e
indentation
JackUrb Oct 27, 2020
85f1c8d
Merge pull request #110 from facebookresearch/asyncio-errors
JackUrb Oct 27, 2020
812c5e7
a
jaseweston Oct 27, 2020
fd3e548
moar
jaseweston Oct 27, 2020
1d31ea1
Merge pull request #111 from facebookresearch/fight3
jaseweston Oct 27, 2020
9f4306d
npcs
jaseweston Oct 27, 2020
0428ade
Merge pull request #112 from facebookresearch/fight4
jaseweston Oct 27, 2020
2c73968
health stats
jaseweston Oct 27, 2020
64e5b1d
Merge pull request #113 from facebookresearch/fight5
jaseweston Oct 27, 2020
667bc78
moar
jaseweston Oct 28, 2020
fce64a4
lint
jaseweston Oct 28, 2020
466f577
Merge pull request #114 from facebookresearch/fight7
jaseweston Oct 28, 2020
0259ad4
spells
jaseweston Oct 28, 2020
92b67af
lint
jaseweston Oct 28, 2020
cdf02c6
Merge pull request #115 from facebookresearch/magic1
jaseweston Oct 28, 2020
899ce19
Give On_events
jaseweston Oct 28, 2020
ca4b7df
Merge pull request #116 from facebookresearch/magic2
jaseweston Oct 28, 2020
ad397f3
Safety classify everything
JackUrb Nov 2, 2020
3a6b981
Merge branch 'main' into safety-scripts
JackUrb Aug 31, 2022
c2154ce
Deleting extra merge files
JackUrb Aug 31, 2022
5e1442e
Removing more post-merge
JackUrb Aug 31, 2022
cbf154a
last round of deletes
JackUrb Aug 31, 2022
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
80 changes: 80 additions & 0 deletions crowdsourcing/quests/examine_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from mephisto.core.local_database import LocalMephistoDB
from mephisto.core.data_browser import DataBrowser as MephistoDataBrowser
from mephisto.data_model.worker import Worker

db = LocalMephistoDB()
mephisto_data_browser = MephistoDataBrowser(db=db)

units = mephisto_data_browser.get_units_for_task_name(input("Input task name: "))

tasks_to_show = input("Tasks to see? (a)ll/(u)nreviewed: ")
if tasks_to_show in ["all", "a"]:
pass
else:
units = [u for u in units if u.get_status() == "completed"]


def format_action(action_obj):
action_type = action_obj["action_type"]
action_vals = action_obj["values"]
if action_type in [
"hit",
"hug",
"drop",
"wield",
"wear",
"eat",
"drink",
"follow",
"go",
]:
return f"{action_type} {action_vals[0]}"
if action_type == "give":
return f"give {action_vals[0]} to {action_vals[1]}"
if action_type == "steal":
return f"steal {action_vals[0]} from {action_vals[1]}"
if action_type == "use":
return f"use {action_vals[0]} with {action_vals[1]}"
if action_type == "get":
if action_vals[2] == "0":
return f"get {action_vals[0]}"
return f"get {action_vals[0]} from {action_vals[1]}"
if action_type == "put":
split_word = "on" if action_vals[2] == "0" else "in"
return f"put {action_vals[0]} {split_word} {action_vals[1]}"


def get_timeline_string(times, values):
timeline_string = ""
for idx, t in enumerate(times):
timeline_string += f" -- {t}\n" f" -- {format_action(values[idx])}\n"
return timeline_string


def format_for_printing_data(data):
worker_name = Worker(db, data["worker_id"]).worker_name
contents = data["data"]
duration = contents["times"]["task_end"] - contents["times"]["task_start"]
metadata_string = f"Worker: {worker_name}\nDuration: {int(duration)}\n"

inputs = contents["inputs"]
setting_split = " ".join(inputs["description"].split("\n"))
inputs_string = (
f"Character: {inputs['character']}\nPersona: {inputs['persona']}\n"
f"Setting: {setting_split}\nGoal text: {inputs['goal']}\n"
)

outputs = contents["outputs"]["final_data"]
output_string = (
f" Short term goal: {outputs['text_values'][0]}\n"
f" Mid term goal: {outputs['text_values'][1]}\n"
f" Long term goal: {outputs['text_values'][2]}\n"
f" Timeline:\n"
f"{get_timeline_string(inputs['time'], outputs['actions'])}\n"
)
return f"-------------------\n{metadata_string}{inputs_string}{output_string}"


for unit in units:
print(format_for_printing_data(mephisto_data_browser.get_data_from_unit(unit)))
# TODO can add review stuff if desired?
132 changes: 132 additions & 0 deletions scripts/safety/run_safety.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/usr/bin/env python3

# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from parlai.agents.transformer.transformer import TransformerClassifierAgent
from parlai.core.agents import create_agent
from parlai.core.params import ParlaiParser
from parlai_internal.agents.safety_wrapper.multiturn_safety import (
MultiturnOffensiveLanguageClassifier,
)

from tqdm import tqdm
import os
from light.data_model.light_database import LIGHTDatabase

OUT_DIR = os.path.expanduser("~/LIGHT/scripts/safety/out/")
db_path = os.path.expanduser("~/ParlAI/data/LIGHT/merged.db")
CONT = '________'

class AdversarialOffensiveLanguageClassifier(MultiturnOffensiveLanguageClassifier):
"""
Load model trained to detect offensive language in the context of multi- turn
dialogue utterances.
This model was trained to be robust to adversarial examples created by humans. See
<http://parl.ai/projects/dialogue_safety/> for more information.
"""

def _create_safety_model(self):
parser = ParlaiParser(False, False)
TransformerClassifierAgent.add_cmdline_args(parser)
parser.set_params(
model_file='zoo:bot_adversarial_dialogue/multi_turn/model',
print_scores=True,
split_lines=True,
model_parallel=False,
bs=1,
)
safety_opt = parser.parse_args([])
return create_agent(safety_opt, requireModelExists=True)

safety = AdversarialOffensiveLanguageClassifier()
db = LIGHTDatabase(db_path)

def process_single_turns():
with db as ldb:
utterances = ldb.get_utterance()
utt_dict = {utt['id'] : utt['dialogue'] for utt in utterances}
utt_count = len(utt_dict)
safe_count = 0
out_fn = os.path.join(OUT_DIR, 'turn_ratings.txt')
print(f"Classifying {utt_count} utterances")
with open(out_fn, 'w+') as classifieds:
for utt_id, utt in tqdm(utt_dict.items()):
is_safe, thresh = safety.contains_offensive_language(utt)
classifieds.write(f"{utt_id}, {is_safe}, {thresh}, {utt}\n")
if is_safe:
safe_count += 1
print(f"Utterances classified, {safe_count}/{utt_count} or {safe_count/utt_count} are safe.")


def process_locations():
with db as ldb:
rooms = ldb.get_room()
room_dict = {r['id']: dict(r) for r in rooms}
print(list(room_dict.values())[0])

room_count = len(room_dict)
safe_count = 0
out_fn = os.path.join(OUT_DIR, 'room_ratings.txt')
print(f"Classifying {room_count} locations")
with open(out_fn, 'w+') as classifieds:
for room_id, room in tqdm(utt_dict.items()):
desc_is_safe, thresh = safety.contains_offensive_language(room['description'])
classifieds.write(f"{room_id}, {is_safe}, {thresh}, {room['description']}\n")
backstory_is_safe, thresh = safety.contains_offensive_language(room['backstory'])
classifieds.write(f"{CONT}, {is_safe}, {thresh}, {room['backstory']}\n")
if is_safe and backstory_is_safe:
safe_count += 1
print(f"Rooms classified, {safe_count}/{room_count} or {safe_count/room_count} are safe.")


def process_objects():
with db as ldb:
objects = ldb.get_room()
obj_dict = {obj['id']: dict(obj) for obj in objects}
print(list(obj_dict.values())[0])

obj_count = len(obj_dict)
safe_count = 0
out_fn = os.path.join(OUT_DIR, 'obj_ratings.txt')
print(f"Classifying {obj_count} locations")
with open(out_fn, 'w+') as classifieds:
for obj_id, obj in tqdm(obj_dict.items()):
desc = f"{obj['name']}. {obj['description']}"
is_safe, thresh = safety.contains_offensive_language(desc)
classifieds.write(f"{obj_id}, {is_safe}, {thresh}, {desc}\n")
if is_safe:
safe_count += 1
print(f"Objects classified, {safe_count}/{obj_count} or {safe_count/obj_count} are safe.")


def process_characters():
with db as ldb:
chars = ldb.get_room()
char_dict = {char['id']: dict(char) for char in chars}
print(list(char_dict.values())[0])

char_count = len(char_dict)
safe_count = 0
out_fn = os.path.join(OUT_DIR, 'char_ratings.txt')
print(f"Classifying {char_count} locations")
with open(out_fn, 'w+') as classifieds:
for char_id, char in tqdm(char_dict.items()):
base = f"{char['name']}. {char['persona']}"
base_is_safe, thresh = safety.contains_offensive_language(base)
classifieds.write(f"{char_id}, {is_safe}, {thresh}, {base}\n")
desc = char['description']
desc_is_safe, thresh = safety.contains_offensive_language(desc)
classifieds.write(f"{CONT}, {is_safe}, {thresh}, {desc}\n")

if base_is_safe and desc_is_safe:
safe_count += 1
print(f"Objects classified, {safe_count}/{char_count} or {safe_count/char_count} are safe.")


if __name__ == '__main__':
process_single_turns()
process_locations()
process_objects()
process_characters()