cdfmlr / muvtuber

Makes your AI vtuber
445 stars 75 forks source link

Suggestion for filtering emoji on ChatGPT responses #48

Closed ilNikk closed 1 year ago

ilNikk commented 1 year ago

ChatGPT responses are annoying when there are emojis. I made changes to filter emojis on answers.

chatgpt_chatbot/chatgpt/chatbot.py: import re, filter_emoji() function

# added for emoji filter
import re

def filter_emoji(text): # emoji filter definition
    emoji_pattern = re.compile("["
        u"\U0001F600-\U0001F64F" 
        u"\U0001F300-\U0001F5FF"  
        u"\U0001F680-\U0001F6FF"  
        u"\U0001F1E0-\U0001F1FF"  
        u"\U00002500-\U00002BEF"  
        u"\U00002702-\U000027B0"
        u"\U00002702-\U000027B0"
        u"\U000024C2-\U0001F251"
        u"\U0001f926-\U0001f937"
        u"\U00010000-\U0010ffff"
        u"\u2640-\u2642"
        u"\u2600-\u2B55"
        u"\u200d"
        u"\u23cf"
        u"\u23e9"
        u"\u231a"
        u"\ufe0f"
        u"\u3030"
        "]+", flags=re.UNICODE)
    filtered_text = emoji_pattern.sub(r'', text)
    return filtered_text 

chatgpt_chatbot/chatgpt/chatbot.py: ask() return updated

    def ask(self, session_id, prompt, **kwargs) -> str:  # raises Exception
        """Ask ChatGPT with prompt, return response text

        - session_id: unused

        Raises:
            ChatGPTError: ChatGPT error
        """
        response: str | None = None

        try:
            with self.lock:
                response = self.chatbot.ask(prompt)
        except Exception as e:
            logging.warning(f"ChatGPT ask error: {e}")
            raise ChatGPTError(str(e))

        if not response:
            raise ChatGPTError("ChatGPT response is None")

        filteredemoji_resp = filter_emoji(response) #filter emoji on response
        return filteredemoji_resp #return filtered message

A nice idea would be to then take the filtered emoji and send them to live2ddriver as instructions for expressions or movements!

Suggestions are welcome 😃

cdfmlr commented 1 year ago

I think you filters out too many characters. It seems deleting all CJK characters 😭:

>>> print(s)
几点了?
何時ですか?
몇 시예요?
What time is it?
What time is it?
Wie spät ist es?
Quelle heure est-il ?
Che ora è?
¿Qué hora es?
كم الساعة؟
ตอนนี้กี่โมงแล้ว?
который сейчас час?
🙋‍♂️🕐❓
>>> print(filter_emoji(s))

?
 ?
What time is it?
What time is it?
Wie spät ist es?
Quelle heure est-il ?
Che ora è?
¿Qué hora es?
كم الساعة؟
ตอนนี้กี่โมงแล้ว?
который сейчас час?
ilNikk commented 1 year ago

Ouch, my fault 😞 Pls check with this. I also added a standard emoticon like:) :( :D etcetc

functions

# added for emoji and emoticon filter
import re

def filter_emoji(text): # emoji filter
    emoji_pattern = re.compile("["
    u"\U0001F600-\U0001F64F"  # emoticons
    u"\U0001F300-\U0001F5FF"  # symbols & pictographs
    u"\U0001F680-\U0001F6FF"  # transport & map symbols
    u"\U0001F1E0-\U0001F1FF"  # flags
    u"\U0001F910-\U0001F95F"  # people & body
    u"\U00002600-\U000027BF"  # dingbats
    u"\U000026A0-\U000026FF"  # misc symbols
    u"\U0000FE0F"             # emoji variation selector
    u"\u2640-\u2642"          # gender symbols
        "]+", flags=re.UNICODE)
    filtered_text = re.sub(emoji_pattern, '', text)
    return filtered_text

def filter_emoticons(text): # emoticon filter :( :D :P :O :o ;) :| :/ :\ :^) :'( :_ :S :@ :x :X :\$ :# :%
    emoticon_pattern = r'(?::|;|=)(?:-)?(?:\)|\(|D|P|O|o|\[|\]|3|>|<|\/|\||\\|\^|\'|_|S|@|x|X|\$|#|%)'
    filtered_text = re.sub(emoticon_pattern, '', text)
    return filtered_text

def filter_response(text): 
    text = str(text)
    filtered_text = filter_emoji(text)
    filtered_text = filter_emoticons(filtered_text)
    return filtered_text

ask()

    def ask(self, session_id, prompt, **kwargs) -> str:  # raises Exception
        """Ask ChatGPT with prompt, return response text

        - session_id: unused

        Raises:
            ChatGPTError: ChatGPT error
        """
        response: str | None = None

        try:
            with self.lock:
                response = self.chatbot.ask(prompt)
        except Exception as e:
            logging.warning(f"ChatGPT ask error: {e}")
            raise ChatGPTError(str(e))

        if not response:
            raise ChatGPTError("ChatGPT response is None")

        filteredemoji_resp = filter_response(response) #filter emoji and emoticon on response
        return filteredemoji_resp #return filtered message
cdfmlr commented 1 year ago

Wow, thanks for your work. 👏 Would you mind to make a Pull Request?

ilNikk commented 1 year ago

committed

cdfmlr commented 1 year ago

Merged. Thanks a lot for your contribution. 🎉