File size: 900 Bytes
77a444f
 
 
 
 
 
d5f9984
 
77a444f
 
 
 
 
 
 
 
d5f9984
77a444f
 
d5f9984
77a444f
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import transformers
import gradio as gr
import torch

from transformers import GPT2LMHeadModel, GPT2Tokenizer

tokenizer = GPT2Tokenizer.from_pretrained('dennis-fast/DialoGPT-ElonMusk')
model = GPT2LMHeadModel.from_pretrained('dennis-fast/DialoGPT-ElonMusk')

def chat(message, token_response):
    token_message = tokenizer.encode(message + tokenizer.eos_token, return_tensors='pt')
    token_response = model.generate(token_message, max_length=1000, pad_token_id=tokenizer.eos_token_id)
    response = tokenizer.decode(token_response[:, token_message.shape[-1]:][0], skip_special_tokens=True)
    return response, token_response

input = gr.inputs.Textbox(lines=2, label='User:')
output = gr.outputs.Textbox(label='Elon:')

gr.Interface(fn=chat,
    title="Chat with Elon",
    inputs=[input, "state"],
    outputs=[output, "state"],
    allow_screenshot=False,
    allow_flagging='never').launch()