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
1 change: 1 addition & 0 deletions applications/Chat/inference/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ uvicorn
git+https://github.com/huggingface/transformers
accelerate
bitsandbytes
jieba
11 changes: 5 additions & 6 deletions applications/Chat/inference/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from threading import Lock
from typing import Any, Callable, Generator, List, Optional
import json
import jieba

import torch
import torch.distributed as dist
Expand Down Expand Up @@ -130,10 +131,7 @@ def __init__(self, tokenizer, context: str, max_len: int = 2048, censored_words:
self.tokenizer = tokenizer
self.context = context
self.max_len = max_len
if len(censored_words) > 0:
self.censored_pat = re.compile(f'({"|".join(map(re.escape, censored_words))})', flags=re.I)
else:
self.censored_pat = None
self.censored_words = set([word.lower() for word in censored_words])
# These will be initialized after the first call of preprocess_prompt()
self.context_len: Optional[int] = None
self.dialogue_placeholder_len: Optional[int] = None
Expand Down Expand Up @@ -179,9 +177,10 @@ def postprocess_output(self, output: str) -> str:
return output.strip()

def has_censored_words(self, text: str) -> bool:
if self.censored_pat is None:
if len(self.censored_words) == 0:
return False
return self.censored_pat.search(text) is not None
intersection = set(jieba.cut(text.lower())) & self.censored_words
return len(intersection) > 0

class LockedIterator:

Expand Down