Skip to content

Commit d7a152a

Browse files
committed
Save only flag
1 parent 37f3a0c commit d7a152a

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

fastchat/serve/gradio_block_arena_vision.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,9 @@ def add_text(state, model_selector, chat_input, context: Context, request: gr.Re
241241
)
242242

243243
text_flagged, nsfw_flag, csam_flag = (
244-
moderation_type_to_response_map["text_moderation"]["flagged"],
245-
moderation_type_to_response_map["nsfw_moderation"]["flagged"],
246-
moderation_type_to_response_map["csam_moderation"]["flagged"],
244+
state.content_moderator.text_flagged,
245+
state.content_moderator.nsfw_flagged,
246+
state.content_moderator.csam_flagged,
247247
)
248248

249249
if csam_flag:

fastchat/serve/gradio_block_arena_vision_anony.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,13 @@ def add_text(
333333

334334
# Use the first state to get the moderation response because this is based on user input so it is independent of the model
335335
moderation_image_input = images[0] if len(images) > 0 else None
336-
moderation_type_to_response_map = states[
337-
0
338-
].content_moderator.image_and_text_moderation_filter(
336+
moderation_type_to_response_map = states[0].content_moderator.image_and_text_moderation_filter(
339337
moderation_image_input, text, model_list, do_moderation=True
340338
)
341339
text_flagged, nsfw_flag, csam_flag = (
342-
moderation_type_to_response_map["text_moderation"]["flagged"],
343-
moderation_type_to_response_map["nsfw_moderation"]["flagged"],
344-
moderation_type_to_response_map["csam_moderation"]["flagged"],
340+
states[0].content_moderator.text_flagged,
341+
states[0].content_moderator.nsfw_flagged,
342+
states[0].content_moderator.csam_flagged,
345343
)
346344

347345
if csam_flag:

fastchat/serve/gradio_block_arena_vision_named.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,13 @@ def add_text(
257257

258258
# Use the first state to get the moderation response because this is based on user input so it is independent of the model
259259
moderation_image_input = images[0] if len(images) > 0 else None
260-
moderation_type_to_response_map = states[
261-
0
262-
].content_moderator.image_and_text_moderation_filter(
260+
moderation_type_to_response_map = states[0].content_moderator.image_and_text_moderation_filter(
263261
moderation_image_input, text, model_list, do_moderation=False
264262
)
265-
266263
text_flagged, nsfw_flag, csam_flag = (
267-
moderation_type_to_response_map["text_moderation"]["flagged"],
268-
moderation_type_to_response_map["nsfw_moderation"]["flagged"],
269-
moderation_type_to_response_map["csam_moderation"]["flagged"],
264+
states[0].content_moderator.text_flagged,
265+
states[0].content_moderator.nsfw_flagged,
266+
states[0].content_moderator.csam_flagged,
270267
)
271268

272269
if csam_flag:

fastchat/serve/moderation/moderator.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,11 @@ def image_moderation_filter(self, image: Image):
136136
self.nsfw_flagged = nsfw_flagged_map["flagged"]
137137
self.csam_flagged = csam_flagged_map["flagged"]
138138

139+
# We save only the boolean value instead of the entire response dictionary
140+
# to save space. nsfw_flagged_map and csam_flagged_map will contain the whole dictionary
139141
return {
140-
"nsfw_moderation": nsfw_flagged_map,
141-
"csam_moderation": csam_flagged_map,
142+
"nsfw_moderation": {"flagged": self.nsfw_flagged},
143+
"csam_moderation": {"flagged": self.csam_flagged},
142144
}
143145

144146
def _openai_moderation_filter(
@@ -205,12 +207,17 @@ def text_moderation_filter(
205207

206208
moderation_response_map = {"flagged": False}
207209
if do_moderation:
210+
# We save the entire response dictionary here
208211
moderation_response_map = self._openai_moderation_filter(
209212
text, custom_thresholds
210213
)
211214
self.text_flagged = moderation_response_map["flagged"]
215+
else:
216+
self.text_flagged = False
212217

213-
return {"text_moderation": moderation_response_map}
218+
# We only save whether the text was flagged or not instead of the entire response dictionary
219+
# to save space. moderation_response_map will contain the whole dictionary
220+
return {"text_moderation": {"flagged": self.text_flagged}}
214221

215222
def image_and_text_moderation_filter(
216223
self, image: Image, text: str, model_list: List[str], do_moderation=True

0 commit comments

Comments
 (0)