from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
import torch
import gradio as gr
mname = "facebook/blenderbot-400M-distill"
model = BlenderbotForConditionalGeneration.from_pretrained(mname)
tokenizer = BlenderbotTokenizer.from_pretrained(mname)
def take_last_tokens(inputs, note_history, history):
"""Filter the last 128 tokens"""
if inputs['input_ids'].shape[1] > 128:
inputs['input_ids'] = torch.tensor([inputs['input_ids'][0][-128:].tolist()])
inputs['attention_mask'] = torch.tensor([inputs['attention_mask'][0][-128:].tolist()])
note_history = [' '.join(note_history[0].split(' ')[2:])]
history = history[1:]
return inputs, note_history, history
def add_note_to_history(note, note_history):
"""Add a note to the historical information"""
note_history.append(note)
note_history = ' '.join(note_history)
return [note_history]
title = "Mantain a conversation with the bot"
description = """
The bot have been trained to chat with you about whatever you want. Let's talk!
(Image generated from text using DALLĀ·E mini)
""" # https://user-images.githubusercontent.com/105242658/176054244-525c6530-1e78-42c7-8688-91dfedf8db58.png #https://www.craiyon.com/ def chat(message, history): history = history or [] if history: history_useful = ['