Spaces:
Runtime error
Runtime error
import gradio as gr | |
from transformers import AutoModelForCausalLM, AutoTokenizer | |
import torch | |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium") | |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium") | |
max_history = 10 # Maximum number of previous chat turns to include in the conversation history | |
chat_history_ids = None | |
def chatbot(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 max_history tokens | |
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id) | |
# decode and return the generated response | |
response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True) | |
return response | |
styles = { | |
"textarea": "height: 200px; font-size: 18px;", | |
"label": "font-size: 20px; font-weight: bold;", | |
"output": "color: red; font-size: 18px;" | |
} | |
iface = gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Osana Chat Friend", styles=styles) | |
iface.launch() | |