from transformers import AutoModelForCausalLM, AutoTokenizer import torch import gradio as gr """ For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference """ tokenizer = AutoTokenizer.from_pretrained("Dennterry/okt_bot") model = AutoModelForCausalLM.from_pretrained("Dennterry/okt_bot") def dialogpt(text): # encode the new user input, add the eos_token and return a tensor in Pytorch for step in range(50000): new_user_input_ids = tokenizer.encode(text + tokenizer.eos_token, return_tensors='pt') # append the new user input tokens to the chat history bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1) if step > 0 else new_user_input_ids # generated a response while limiting the total chat history to 1000 tokens, chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id) # pretty print last ouput tokens from bot return tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True) inputs = gr.inputs.Textbox(lines=1, label="Input Text") outputs = gr.outputs.Textbox(label="DialoGPT") title = "DialoGPT" description = "demo for Microsoft DialoGPT with Hugging Face transformers. To use it, simply input text or click one of the examples text to load them. Read more at the links below. *This is not a Microsoft product and is developed for Gradio*" article = "
DialoGPT: Large-Scale Generative Pre-training for Conversational Response Generation | Github Repo | Hugging Face DialoGPT-large
" examples = [ ["Hi, how are you?"], ["How far away is the moon?"], ] gr.Interface(dialogpt, inputs, outputs, title=title, description=description, article=article, examples=examples).launch(debug=True)