CoolFace
Apppublic

smjain/chat

sourceHugging Faceupdated 4y agoView on Hugging Face
0likes
app.py64 linesDownload Raw Back to root
1#from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration2from transformers import AutoModelForCausalLM, AutoTokenizer,BlenderbotForConditionalGeneration3import torch4 5 6chat_tkn = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")7mdl = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")8 9 10#chat_tkn = AutoTokenizer.from_pretrained("facebook/blenderbot-400M-distill")11#mdl = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-400M-distill")12 13def converse(user_input, chat_history=[]):14    15    user_input_ids = chat_tkn(user_input + chat_tkn.eos_token, return_tensors='pt').input_ids16 17    # create a combined tensor with chat history18    bot_input_ids = torch.cat([torch.LongTensor(chat_history), user_input_ids], dim=-1)19 20    # generate a response 21    chat_history = mdl.generate(bot_input_ids, max_length=1000, pad_token_id=chat_tkn.eos_token_id).tolist()22    print (chat_history)23 24    # convert the tokens to text, and then split the responses into lines25    response = chat_tkn.decode(chat_history[0]).split("<|endoftext|>")26    #response.remove("")27    print("starting to print response")28    print(response)29    30    # write some HTML31    html = "<div class='mybot'>"32    for x, mesg in enumerate(response):33        if x%2!=0 :34           mesg="Alicia:"+mesg35           clazz="alicia"36        else :37           clazz="user"38        39        40        print("value of x")41        print(x)42        print("message")43        print (mesg)44        45        html += "<div class='mesg {}'> {}</div>".format(clazz, mesg)46    html += "</div>"47    print(html)48    return html, chat_history49 50import gradio as grad51 52css = """53.mychat {display:flex;flex-direction:column}54.mesg {padding:5px;margin-bottom:5px;border-radius:5px;width:75%}55.mesg.user {background-color:lightblue;color:white}56.mesg.alicia {background-color:orange;color:white,align-self:self-end}57.footer {display:none !important}58"""59text=grad.inputs.Textbox(placeholder="Lets chat together")60grad.Interface(fn=converse,61             theme="default",62             inputs=[text, "state"],63             outputs=["html", "state"],64             css=css).launch()