Prompt-Gen / app.py
5m4ck3r's picture
Update app.py
e180e59
raw
history blame
1.2 kB
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
import gradio as gr
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
# Initialize chat history
chat_history_ids = None
def chat_cpu(user_input):
global chat_history_ids
# Encode the new user input, add the eos_token, and return a tensor in PyTorch
new_user_input_ids = tokenizer.encode(user_input + 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 chat_history_ids is not None else new_user_input_ids
# Generate 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 output tokens from bot
response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
return "DialoGPT: {}".format(response)
iface = gr.Interface(fn=chat_cpu, inputs="text", outputs="text")
iface.launch(share=True)