Prompt-Gen / app.py
5m4ck3r's picture
Update app.py
ce052d7
raw
history blame
No virus
1.43 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")
def chat_with_history(message, chat_history=None):
# Initialize chat history if not provided
if chat_history is None:
chat_history = []
# Encode the new user input, add the eos_token, and return a tensor in PyTorch
new_user_input_ids = tokenizer.encode(message + tokenizer.eos_token, return_tensors='pt')
# Append the new user input tokens to the chat history
bot_input_ids = torch.cat([tokenizer.encode(pair[0] + tokenizer.eos_token, return_tensors='pt') for pair in chat_history] + [new_user_input_ids], dim=-1)
# 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)
# Decode the last output tokens from bot
response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
# Update the chat history with the new user message and bot response
chat_history.append([message, response])
return response
demo = gr.ChatInterface(
fn=chat_with_history,
examples=["hey how are you ?", "hola", "Yo!"],
title="Multi Chat Bot"
)
demo.launch()