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
|
| 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
|
| 12 |
+
mode_name = 'liam168/trans-opus-mt-zh-en'
|
| 13 |
+
model = AutoModelWithLMHead.from_pretrained(mode_name)
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(mode_name)
|
| 15 |
+
translation = pipeline("translation_zh_to_en", model=model, tokenizer=tokenizer)
|
| 16 |
+
translate_result = translation(text, max_length=400)
|
| 17 |
+
if mode == "etoz":
|
| 18 |
+
from transformers import AutoModelWithLMHead,AutoTokenizer,pipeline
|
| 19 |
+
mode_name = 'liam168/trans-opus-mt-en-zh'
|
| 20 |
+
model = AutoModelWithLMHead.from_pretrained(mode_name)
|
| 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": "π’",
|
| 34 |
+
"sorry":"π",
|
| 35 |
+
"love": "β€οΈ",
|
| 36 |
+
"like": "π",
|
| 37 |
+
"dislike": "π",
|
| 38 |
+
"Why": "π₯Ί",
|
| 39 |
+
"cat":"π±",
|
| 40 |
+
"dog":"πΆ",
|
| 41 |
+
"ε¨" : "π"
|
| 42 |
+
}
|
| 43 |
+
for keyword, emoji in keyword_emoji_dict.items():
|
| 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",
|
| 52 |
+
"laugh":"LOL",
|
| 53 |
+
"nevermind": "nvm",
|
| 54 |
+
"sorry": "sorryyyyy",
|
| 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 Itext
|
| 67 |
+
global bname
|
| 68 |
+
if name=='':
|
| 69 |
+
name="your chatbot"
|
| 70 |
+
bname= name
|
| 71 |
+
Itext=text
|
| 72 |
+
|
| 73 |
+
# Try to detect the language of the input text
|
| 74 |
+
# If the input language is Chinese, convert the text to lowercase and check if it contains any Chinese characters
|
| 75 |
+
is_chinese = any(0x4e00 <= ord(char) <= 0x9fff for char in text.lower())
|
| 76 |
+
if is_chinese:
|
| 77 |
+
text = translate(text,"ztoe")
|
| 78 |
+
text=f"{text}"
|
| 79 |
+
text=text[23:(len(text)-3)]
|
| 80 |
+
|
| 81 |
+
# Look for keywords in the previous chat history
|
| 82 |
+
keyword_responses = {
|
| 83 |
+
"how are you": "I'm doing wellπ, thank you for asking!",
|
| 84 |
+
"bye": "Goodbye!ππ»",
|
| 85 |
+
"thank you": "You're welcome!π",
|
| 86 |
+
"hello": f'I am {bname}. Nice to meet you!π',
|
| 87 |
+
"Hello": f'I am {bname}. Nice to meet you!π',
|
| 88 |
+
"Hi": f'I am {bname}. Nice to meet you!π',
|
| 89 |
+
"hi": f'I am {bname}. Nice to meet you!π',
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
# Generate a response based on the previous messages
|
| 93 |
+
if len(chat_history) > 0:
|
| 94 |
+
# Get the last message from the chat history
|
| 95 |
+
last_message = chat_history[-1][1]
|
| 96 |
+
# Generate a response based on the last message
|
| 97 |
+
encoded_input = tokenizer.encode(last_message + tokenizer.eos_token + text, return_tensors='pt')
|
| 98 |
+
generated = model.generate(encoded_input, max_length=1024, do_sample=True)
|
| 99 |
+
response = tokenizer.decode(generated[0], skip_special_tokens=True)
|
| 100 |
+
response=f"{response}"
|
| 101 |
+
else:
|
| 102 |
+
# If there is no previous message, generate a response using the default method
|
| 103 |
+
encoded_input = tokenizer(text, return_tensors='pt')
|
| 104 |
+
generated = model.generate(**encoded_input)
|
| 105 |
+
response = tokenizer.batch_decode(generated, skip_special_tokens=True)[0]
|
| 106 |
+
response=f"{response}"
|
| 107 |
+
if text in keyword_responses:
|
| 108 |
+
response = keyword_responses[text]
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# If the input language was Chinese, translate the response back to Chinese
|
| 112 |
+
if is_chinese:
|
| 113 |
+
from hanziconv import HanziConv
|
| 114 |
+
response = translate(response,"etoz")
|
| 115 |
+
response = HanziConv.toTraditional(f"{response}")
|
| 116 |
+
response = f"{response} "
|
| 117 |
+
response=response[23:(len(response)-4)]
|
| 118 |
+
else:
|
| 119 |
+
response = response
|
| 120 |
+
|
| 121 |
+
# Add emojis to the response
|
| 122 |
+
response = add_emoji(response)
|
| 123 |
+
response = add_shortform(response)
|
| 124 |
+
chat_history.append((Itext,response))
|
| 125 |
+
|
| 126 |
+
# Format the chat history as an HTML string for display
|
| 127 |
+
history_str = ""
|
| 128 |
+
for name, msg in chat_history:
|
| 129 |
+
history_str += f"<strong>{name}:</strong> {msg}<br>"
|
| 130 |
+
# Return the response along with the chat history
|
| 131 |
+
return (chat_history)
|
| 132 |
+
|
| 133 |
+
iface =gr.Interface(fn=chatbot,
|
| 134 |
+
inputs=[gr.inputs.Textbox(label="Chat", placeholder="Say somehting"),
|
| 135 |
+
gr.inputs.Textbox(label="Name the Bot", placeholder="give me a name")],
|
| 136 |
+
outputs=[gr.Chatbot(label="Chat Here")],
|
| 137 |
+
title="Emphatic Chatbot",
|
| 138 |
+
allow_flagging=False,
|
| 139 |
+
layout="vertical",
|
| 140 |
+
theme='gstaff/xkcd' ,
|
| 141 |
+
examples=[["εθ¦"], ["Hello"]]
|
| 142 |
+
)
|
| 143 |
+
#.launch(share=True)
|
| 144 |
+
|
| 145 |
+
iface.launch(share=True)
|