Spaces:
Running
Running
import gradio as gr | |
from transformers import AutoConfig | |
from transformers import GPT2Tokenizer, GPT2LMHeadModel | |
config = AutoConfig.from_pretrained('gorkemgoknar/gpt2chatbotenglish') | |
model = GPT2LMHeadModel.from_pretrained('gorkemgoknar/gpt2chatbotenglish', config=config) | |
tokenizer = GPT2Tokenizer.from_pretrained('gorkemgoknar/gpt2chatbotenglish') | |
tokenizer.model_max_length = 1024 | |
def get_chat_response(name, input_txt = "Hello , what is your name?"): | |
personality = "My name is " + "Gandalf" | |
bot_input_ids = tokenizer.encode(personality + tokenizer.eos_token + input_txt + tokenizer.eos_token , return_tensors='pt') | |
#optimum response and speed | |
chat_history_ids = model.generate( | |
bot_input_ids, max_length=50, | |
pad_token_id=tokenizer.eos_token_id, | |
no_repeat_ngram_size=3, | |
do_sample=True, | |
top_k=60, | |
top_p=0.8, | |
temperature = 1.3 | |
) | |
out_str = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True) | |
return out_str | |
personality_choices = ["Gandalf", "Riddick", "Macleod", "Morpheus", "Neo","Spock","Vader","Indy"] | |
examples= ["Gandalf", "What is your name?"] | |
interface = gr.Interface(fn=get_chat_response, inputs=[gr.inputs.Dropdown(personality_choices) ,"text"], outputs="text") | |
if __name__ == "__main__": | |
interface.launch() |