Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
| 4 |
|
|
@@ -6,6 +5,7 @@ model_name = "facebook/blenderbot-400M-distill"
|
|
| 6 |
tokenizer = BlenderbotTokenizer.from_pretrained(model_name)
|
| 7 |
model = BlenderbotForConditionalGeneration.from_pretrained(model_name)
|
| 8 |
|
|
|
|
| 9 |
def translate(text,mode):
|
| 10 |
if mode== "ztoe":
|
| 11 |
from transformers import AutoModelWithLMHead,AutoTokenizer,pipeline
|
|
@@ -21,13 +21,17 @@ def translate(text,mode):
|
|
| 21 |
tokenizer = AutoTokenizer.from_pretrained(mode_name)
|
| 22 |
translation = pipeline("translation_en_to_zh", model=model, tokenizer=tokenizer)
|
| 23 |
translate_result = translation(text, max_length=400)
|
| 24 |
-
|
| 25 |
return translate_result
|
| 26 |
|
|
|
|
|
|
|
| 27 |
chat_history=[]
|
|
|
|
|
|
|
| 28 |
|
|
|
|
|
|
|
| 29 |
def add_emoji(response):
|
| 30 |
-
# Define the keywords and their corresponding emojis
|
| 31 |
keyword_emoji_dict = {
|
| 32 |
"happy": "π",
|
| 33 |
"sad": "π’",
|
|
@@ -44,8 +48,36 @@ def add_emoji(response):
|
|
| 44 |
response = response.replace(keyword, f"{keyword} {emoji}")
|
| 45 |
return response
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
def add_shortform(response):
|
| 48 |
-
# Define the keywords and their corresponding keywords
|
| 49 |
keyword_shortform_dict = {
|
| 50 |
"You only live once": "YOLO",
|
| 51 |
"funny": "LOL",
|
|
@@ -55,43 +87,59 @@ def add_shortform(response):
|
|
| 55 |
"tell me": "LMK",
|
| 56 |
"By the way": "BTW",
|
| 57 |
"don't know":"DK",
|
| 58 |
-
"do not know":"IDK"
|
| 59 |
-
|
|
|
|
| 60 |
for keyword, st in keyword_shortform_dict.items():
|
| 61 |
response = response.replace(keyword, f"{st}")
|
| 62 |
return response
|
| 63 |
|
|
|
|
|
|
|
| 64 |
def chatbot(text,name):
|
| 65 |
-
global chat_history
|
| 66 |
-
global
|
| 67 |
-
global bname
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
if name=='':
|
| 70 |
-
name="
|
|
|
|
| 71 |
if bname != name:
|
| 72 |
chat_history= []
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# Try to detect the language of the input text
|
| 77 |
-
# If the input language is Chinese, convert the text to lowercase and check if it contains any Chinese characters
|
| 78 |
is_chinese = any(0x4e00 <= ord(char) <= 0x9fff for char in text.lower())
|
|
|
|
|
|
|
| 79 |
if is_chinese:
|
| 80 |
text = translate(text,"ztoe")
|
| 81 |
text=f"{text}"
|
| 82 |
text=text[23:(len(text)-3)]
|
|
|
|
| 83 |
|
| 84 |
# Look for keywords in the previous chat history
|
| 85 |
keyword_responses = {
|
| 86 |
"how are you": "I'm doing wellπ, thank you for asking!",
|
| 87 |
"bye": "Goodbye!ππ»",
|
|
|
|
| 88 |
"thank you": "You're welcome!π",
|
|
|
|
| 89 |
"hello": f'I am {bname}. Nice to meet you!π',
|
| 90 |
-
"
|
| 91 |
-
"
|
| 92 |
-
"hi": f'I am {bname}. Nice to meet you!π',
|
| 93 |
}
|
| 94 |
-
|
|
|
|
| 95 |
# Generate a response based on the previous messages
|
| 96 |
if len(chat_history) > 0:
|
| 97 |
# Get the last message from the chat history
|
|
@@ -107,32 +155,39 @@ def chatbot(text,name):
|
|
| 107 |
generated = model.generate(**encoded_input)
|
| 108 |
response = tokenizer.batch_decode(generated, skip_special_tokens=True)[0]
|
| 109 |
response=f"{response}"
|
| 110 |
-
if text in keyword_responses:
|
| 111 |
-
response = keyword_responses[text]
|
| 112 |
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
# If the input language was Chinese, translate the response back to Chinese
|
| 115 |
if is_chinese:
|
| 116 |
-
from hanziconv import HanziConv
|
| 117 |
response = translate(response,"etoz")
|
|
|
|
|
|
|
|
|
|
| 118 |
response = HanziConv.toTraditional(f"{response}")
|
| 119 |
response = f"{response} "
|
| 120 |
response=response[23:(len(response)-4)]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
else:
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
response = add_emoji(response)
|
| 126 |
-
response = add_shortform(response)
|
| 127 |
-
chat_history.append((Itext,response))
|
| 128 |
-
|
| 129 |
-
# Format the chat history as an HTML string for display
|
| 130 |
-
history_str = ""
|
| 131 |
-
for name, msg in chat_history:
|
| 132 |
-
history_str += f"<strong>{name}:</strong> {msg}<br>"
|
| 133 |
-
# Return the response along with the chat history
|
| 134 |
-
return (chat_history)
|
| 135 |
|
|
|
|
| 136 |
iface =gr.Interface(fn=chatbot,
|
| 137 |
inputs=[gr.inputs.Textbox(label="Chatπ", placeholder="Say somehtingπ¬"),
|
| 138 |
gr.inputs.Textbox(label="Name the BotπΊ", placeholder="give me a nameπ")],
|
|
@@ -140,9 +195,9 @@ iface =gr.Interface(fn=chatbot,
|
|
| 140 |
title="πΈEmphatic Chatbotβ€οΈ",
|
| 141 |
allow_flagging=False,
|
| 142 |
layout="vertical",
|
| 143 |
-
theme='gstaff/xkcd' ,
|
| 144 |
-
examples=[["
|
| 145 |
-
)
|
| 146 |
#.launch(share=True)
|
| 147 |
|
| 148 |
iface.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
| 3 |
|
|
|
|
| 5 |
tokenizer = BlenderbotTokenizer.from_pretrained(model_name)
|
| 6 |
model = BlenderbotForConditionalGeneration.from_pretrained(model_name)
|
| 7 |
|
| 8 |
+
# declare translate model
|
| 9 |
def translate(text,mode):
|
| 10 |
if mode== "ztoe":
|
| 11 |
from transformers import AutoModelWithLMHead,AutoTokenizer,pipeline
|
|
|
|
| 21 |
tokenizer = AutoTokenizer.from_pretrained(mode_name)
|
| 22 |
translation = pipeline("translation_en_to_zh", model=model, tokenizer=tokenizer)
|
| 23 |
translate_result = translation(text, max_length=400)
|
|
|
|
| 24 |
return translate_result
|
| 25 |
|
| 26 |
+
|
| 27 |
+
#initial list and variable
|
| 28 |
chat_history=[]
|
| 29 |
+
output=[]
|
| 30 |
+
bname=''
|
| 31 |
|
| 32 |
+
|
| 33 |
+
#declare emoji function
|
| 34 |
def add_emoji(response):
|
|
|
|
| 35 |
keyword_emoji_dict = {
|
| 36 |
"happy": "π",
|
| 37 |
"sad": "π’",
|
|
|
|
| 48 |
response = response.replace(keyword, f"{keyword} {emoji}")
|
| 49 |
return response
|
| 50 |
|
| 51 |
+
|
| 52 |
+
# Define the keywords and their corresponding keywords
|
| 53 |
+
def tran_shortform(response):
|
| 54 |
+
keyword_shortform_dict = {
|
| 55 |
+
"yolo ": "You only live once",
|
| 56 |
+
"lol ":"funny" ,
|
| 57 |
+
"nvm ": "nevermind",
|
| 58 |
+
"lmk ": "tell me",
|
| 59 |
+
"btw ": "by the way",
|
| 60 |
+
"dk ":"don't know",
|
| 61 |
+
"idk ":"I don't know" ,
|
| 62 |
+
"π": "happy",
|
| 63 |
+
"π’": "sad",
|
| 64 |
+
"π": "sorry",
|
| 65 |
+
"β€οΈ": "love",
|
| 66 |
+
"π": "like",
|
| 67 |
+
"π": "dislike",
|
| 68 |
+
"π₯Ί": "Why",
|
| 69 |
+
"π±": "cat",
|
| 70 |
+
"πΆ": "dog",
|
| 71 |
+
}
|
| 72 |
+
response= response.lower()
|
| 73 |
+
for keyword, st in keyword_shortform_dict.items():
|
| 74 |
+
response = response.replace(keyword, f"{st}")
|
| 75 |
+
return response
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# Define the keywords and their corresponding keywords
|
| 80 |
def add_shortform(response):
|
|
|
|
| 81 |
keyword_shortform_dict = {
|
| 82 |
"You only live once": "YOLO",
|
| 83 |
"funny": "LOL",
|
|
|
|
| 87 |
"tell me": "LMK",
|
| 88 |
"By the way": "BTW",
|
| 89 |
"don't know":"DK",
|
| 90 |
+
"do not know":"IDK"
|
| 91 |
+
}
|
| 92 |
+
response= response.lower()
|
| 93 |
for keyword, st in keyword_shortform_dict.items():
|
| 94 |
response = response.replace(keyword, f"{st}")
|
| 95 |
return response
|
| 96 |
|
| 97 |
+
#------------------------------------------------------------------------------------------------------
|
| 98 |
+
#Chatbot start
|
| 99 |
def chatbot(text,name):
|
| 100 |
+
global chat_history #for chatbot to response
|
| 101 |
+
global outputsss # for user to read
|
| 102 |
+
global bname #the previous bot name
|
| 103 |
+
global raw # the text input by user. It will not be changed in the program
|
| 104 |
+
|
| 105 |
+
raw=text #distinguish text for the user to read it(raw) and text for the chatbot (text)
|
| 106 |
+
|
| 107 |
+
# if the user do not name the chatbot.
|
| 108 |
if name=='':
|
| 109 |
+
name="Mr. Chatbot"
|
| 110 |
+
#since the program will keep running in the website. When the name of chatbot change, the history and chatbot will be refreshed
|
| 111 |
if bname != name:
|
| 112 |
chat_history= []
|
| 113 |
+
outputsss=[]
|
| 114 |
+
|
| 115 |
+
bname=name # same the name
|
| 116 |
+
|
| 117 |
+
#change the shortform and emoji to english
|
| 118 |
+
text = tran_shortform(text)
|
| 119 |
|
| 120 |
# Try to detect the language of the input text
|
|
|
|
| 121 |
is_chinese = any(0x4e00 <= ord(char) <= 0x9fff for char in text.lower())
|
| 122 |
+
|
| 123 |
+
# If the input language is Chinese, convert the text to lowercase and check if it contains any Chinese characters
|
| 124 |
if is_chinese:
|
| 125 |
text = translate(text,"ztoe")
|
| 126 |
text=f"{text}"
|
| 127 |
text=text[23:(len(text)-3)]
|
| 128 |
+
|
| 129 |
|
| 130 |
# Look for keywords in the previous chat history
|
| 131 |
keyword_responses = {
|
| 132 |
"how are you": "I'm doing wellπ, thank you for asking!",
|
| 133 |
"bye": "Goodbye!ππ»",
|
| 134 |
+
"bye.": "Goodbye!ππ»",
|
| 135 |
"thank you": "You're welcome!π",
|
| 136 |
+
"thank you.": "You're welcome!π",
|
| 137 |
"hello": f'I am {bname}. Nice to meet you!π',
|
| 138 |
+
"hello.": f'I am {bname}. Nice to meet you!π',
|
| 139 |
+
"hi": f'I am {bname}. Nice to meet you!π',
|
|
|
|
| 140 |
}
|
| 141 |
+
|
| 142 |
+
|
| 143 |
# Generate a response based on the previous messages
|
| 144 |
if len(chat_history) > 0:
|
| 145 |
# Get the last message from the chat history
|
|
|
|
| 155 |
generated = model.generate(**encoded_input)
|
| 156 |
response = tokenizer.batch_decode(generated, skip_special_tokens=True)[0]
|
| 157 |
response=f"{response}"
|
|
|
|
|
|
|
| 158 |
|
| 159 |
|
| 160 |
+
|
| 161 |
+
# Add emojis to the response
|
| 162 |
+
response = add_emoji(response)
|
| 163 |
+
# Add short form to the response
|
| 164 |
+
response = add_shortform(response)
|
| 165 |
+
# update history
|
| 166 |
+
chat_history.append((text,response))
|
| 167 |
+
|
| 168 |
+
# output the standard response
|
| 169 |
+
if text.lower() in keyword_responses:
|
| 170 |
+
response = keyword_responses[text.lower()]
|
| 171 |
+
|
| 172 |
# If the input language was Chinese, translate the response back to Chinese
|
| 173 |
if is_chinese:
|
|
|
|
| 174 |
response = translate(response,"etoz")
|
| 175 |
+
# remove the header of the output
|
| 176 |
+
# translate simplified Chinese to traditional Chinese
|
| 177 |
+
from hanziconv import HanziConv
|
| 178 |
response = HanziConv.toTraditional(f"{response}")
|
| 179 |
response = f"{response} "
|
| 180 |
response=response[23:(len(response)-4)]
|
| 181 |
+
|
| 182 |
+
#if the response is amended. Show the amended text in blanket)
|
| 183 |
+
if raw.lower() == text.lower():
|
| 184 |
+
outputsss.append((raw,response))
|
| 185 |
else:
|
| 186 |
+
outputsss.append((f"{raw} ({text})", response))
|
| 187 |
+
|
| 188 |
+
return (outputsss)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
+
#------------------------------------------------------------------------------------------------------
|
| 191 |
iface =gr.Interface(fn=chatbot,
|
| 192 |
inputs=[gr.inputs.Textbox(label="Chatπ", placeholder="Say somehtingπ¬"),
|
| 193 |
gr.inputs.Textbox(label="Name the BotπΊ", placeholder="give me a nameπ")],
|
|
|
|
| 195 |
title="πΈEmphatic Chatbotβ€οΈ",
|
| 196 |
allow_flagging=False,
|
| 197 |
layout="vertical",
|
| 198 |
+
theme='gstaff/xkcd' , # credit: https://huggingface.co/spaces/gstaff/xkcd/tree/main
|
| 199 |
+
examples=[["δ½ ε₯½"], ["Hello"],["I am sad"],["ζεΎε·εΏ"]],
|
| 200 |
+
article="<H3>Disclaimer</H3><br><oi><li>The chatbot does not have emotions or the ability to empathize in the same way that humans do</li><li>The chatbot can simulate empathy by recognizing and responding to certain emotional cues in language, but responses are generated by algorithms and not based on personal experiences or feelings</li><li>The information and advice provided by the chatbot should not be used as a substitute for professional medical, psychological, or legal adviceUsers should always consult with qualified professionals in these fields for personalized recommendations that take into account their individual circumstances</li><li>The chatbot is not infallible and may encounter technical difficulties or make errors that can impact its performance</li><li>The information presented by the chatbot should always be cross-checked and authenticated before any action is taken based on it</li><li>Interactions with the chatbot may be subject to recording or monitoring for the purposes of quality assurance and training</li><liUser privacy and data protection should always remain a top priority</li><li>Any information collected by the chatbot must be handled in compliance with relevant data protection laws and regulations.</li></oi>")
|
| 201 |
#.launch(share=True)
|
| 202 |
|
| 203 |
iface.launch()
|