CoolFace
Apppublic

xnetba/testChat

sourceHugging Faceccupdated 3y agoView on Hugging Face
0likes
app.py32 linesDownload Raw Back to root
1from transformers import AutoModelForCausalLM, AutoTokenizer2import torch3 4#set up the model5tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")6model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")7 8#Defining a predict function9def predict(input, history=[]):10    # tokenize the new input sentence11    new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')12 13    # append the new user input tokens to the chat history14    bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)15 16    # generate a response 17    history = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist()18 19    # convert the tokens to text, and then split the responses into lines20    response = tokenizer.decode(history[0]).split("<|endoftext|>")21    response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)]  # convert to tuples of list22    return response, history23 24#creating a gradio interface25import gradio as gr26 27demo = gr.Interface(fn=predict,28             examples=["How many birds exist on Earth"],29             inputs=["text", "state"],30             outputs=["chatbot", "state"])31 32demo.launch()