kyloegraves/android
0
1#2 3 4from transformers import AutoModelForCausalLM, AutoTokenizer5import gradio as gr6import torch7 8 9title = "????AI ChatBot"10description = "A State-of-the-Art Large-scale Pretrained Response generation model (DialoGPT)"11examples = [["How are you?"]]12 13 14tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")15model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")16 17 18def predict(input, history=[]):19 # tokenize the new input sentence20 new_user_input_ids = tokenizer.encode(21 input + tokenizer.eos_token, return_tensors="pt"22 )23 24 # append the new user input tokens to the chat history25 bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)26 27 # generate a response28 history = model.generate(29 bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id30 ).tolist()31 32 # convert the tokens to text, and then split the responses into lines33 response = tokenizer.decode(history[0]).split("<|endoftext|>")34 # print('decoded_response-->>'+str(response))35 response = [36 (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)37 ] # convert to tuples of list38 # print('response-->>'+str(response))39 return response, history40 41 42gr.Interface(43 fn=predict,44 title=title,45 description=description,46 examples=examples,47 inputs=["text", "state"],48 outputs=["chatbot", "state"],49 theme="finlaymacklon/boxy_violet",50).launch()