|
|
|
import gradio as gr |
|
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration |
|
|
|
model_name = "facebook/blenderbot-400M-distill" |
|
tokenizer = BlenderbotTokenizer.from_pretrained(model_name) |
|
model = BlenderbotForConditionalGeneration.from_pretrained(model_name) |
|
|
|
def translate(text,mode): |
|
if mode== "ztoe": |
|
from transformers import AutoModelWithLMHead,AutoTokenizer,pipeline |
|
mode_name = 'liam168/trans-opus-mt-zh-en' |
|
model = AutoModelWithLMHead.from_pretrained(mode_name) |
|
tokenizer = AutoTokenizer.from_pretrained(mode_name) |
|
translation = pipeline("translation_zh_to_en", model=model, tokenizer=tokenizer) |
|
translate_result = translation(text, max_length=400) |
|
if mode == "etoz": |
|
from transformers import AutoModelWithLMHead,AutoTokenizer,pipeline |
|
mode_name = 'liam168/trans-opus-mt-en-zh' |
|
model = AutoModelWithLMHead.from_pretrained(mode_name) |
|
tokenizer = AutoTokenizer.from_pretrained(mode_name) |
|
translation = pipeline("translation_en_to_zh", model=model, tokenizer=tokenizer) |
|
translate_result = translation(text, max_length=400) |
|
|
|
return translate_result |
|
|
|
chat_history=[] |
|
|
|
def add_emoji(response): |
|
|
|
keyword_emoji_dict = { |
|
"happy": "π", |
|
"sad": "π’", |
|
"sorry":"π", |
|
"love": "β€οΈ", |
|
"like": "π", |
|
"dislike": "π", |
|
"Why": "π₯Ί", |
|
"cat":"π±", |
|
"dog":"πΆ", |
|
"ε¨" : "π" |
|
} |
|
for keyword, emoji in keyword_emoji_dict.items(): |
|
response = response.replace(keyword, f"{keyword} {emoji}") |
|
return response |
|
|
|
def add_shortform(response): |
|
|
|
keyword_shortform_dict = { |
|
"You only live once": "YOLO", |
|
"funny": "LOL", |
|
"laugh":"LOL", |
|
"nevermind": "nvm", |
|
"sorry": "sorryyyyy", |
|
"tell me": "LMK", |
|
"By the way": "BTW", |
|
"don't know":"DK", |
|
"do not know":"IDK" |
|
} |
|
for keyword, st in keyword_shortform_dict.items(): |
|
response = response.replace(keyword, f"{st}") |
|
return response |
|
|
|
def chatbot(text,name): |
|
global chat_history |
|
global Itext |
|
global bname |
|
if name=='': |
|
name="your chatbot" |
|
bname= name |
|
Itext=text |
|
|
|
|
|
|
|
is_chinese = any(0x4e00 <= ord(char) <= 0x9fff for char in text.lower()) |
|
if is_chinese: |
|
text = translate(text,"ztoe") |
|
text=f"{text}" |
|
text=text[23:(len(text)-3)] |
|
|
|
|
|
keyword_responses = { |
|
"how are you": "I'm doing wellπ, thank you for asking!", |
|
"bye": "Goodbye!ππ»", |
|
"thank you": "You're welcome!π", |
|
"hello": f'I am {bname}. Nice to meet you!π', |
|
"Hello": f'I am {bname}. Nice to meet you!π', |
|
"Hi": f'I am {bname}. Nice to meet you!π', |
|
"hi": f'I am {bname}. Nice to meet you!π', |
|
} |
|
|
|
|
|
if len(chat_history) > 0: |
|
|
|
last_message = chat_history[-1][1] |
|
|
|
encoded_input = tokenizer.encode(last_message + tokenizer.eos_token + text, return_tensors='pt') |
|
generated = model.generate(encoded_input, max_length=1024, do_sample=True) |
|
response = tokenizer.decode(generated[0], skip_special_tokens=True) |
|
response=f"{response}" |
|
else: |
|
|
|
encoded_input = tokenizer(text, return_tensors='pt') |
|
generated = model.generate(**encoded_input) |
|
response = tokenizer.batch_decode(generated, skip_special_tokens=True)[0] |
|
response=f"{response}" |
|
if text in keyword_responses: |
|
response = keyword_responses[text] |
|
|
|
|
|
|
|
if is_chinese: |
|
from hanziconv import HanziConv |
|
response = translate(response,"etoz") |
|
response = HanziConv.toTraditional(f"{response}") |
|
response = f"{response} " |
|
response=response[23:(len(response)-4)] |
|
else: |
|
response = response |
|
|
|
|
|
response = add_emoji(response) |
|
response = add_shortform(response) |
|
chat_history.append((Itext,response)) |
|
|
|
|
|
history_str = "" |
|
for name, msg in chat_history: |
|
history_str += f"<strong>{name}:</strong> {msg}<br>" |
|
|
|
return (chat_history) |
|
|
|
iface =gr.Interface(fn=chatbot, |
|
inputs=[gr.inputs.Textbox(label="Chat", placeholder="Say somehting"), |
|
gr.inputs.Textbox(label="Name the Bot", placeholder="give me a name")], |
|
outputs=[gr.Chatbot(label="Chat Here")], |
|
title="Emphatic Chatbot", |
|
allow_flagging=False, |
|
layout="vertical", |
|
theme='gstaff/xkcd' , |
|
examples=[["εθ¦"], ["Hello"]] |
|
) |
|
|
|
|
|
iface.launch(share=True) |