import gradio as gr from transformers import GPT2LMHeadModel, GPT2Tokenizer model_name = "anezatra/gpt2_openassistant_guanaco" model = GPT2LMHeadModel.from_pretrained(model_name) tokenizer = GPT2Tokenizer.from_pretrained(model_name) def respond(message, history): prompt = "\n".join(history + [message]) inputs = tokenizer.encode(prompt, return_tensors="pt") outputs = model.generate( inputs, temperature=0.8, max_new_tokens=200, top_k=1, num_return_sequences=1, no_repeat_ngram_size=2, do_sample=True, ) response = tokenizer.decode(outputs[0], skip_special_tokens=True) response = response.split("### Assistant:")[-1].strip() if "### Human:" in response: response = response.split("### Human:")[1].strip() return response banner = gr.HTML("""
GPT-2 MEDIUM CHATBOT
""") with gr.Blocks(theme=gr.Theme.from_hub('gradio/monochrome')) as demo: banner.render() chatbot = gr.Chatbot() msg = gr.Textbox(label="Write your message") with gr.Row(): clear = gr.Button("Clear chat") submit = gr.Button("Send message") def user_input(user_message, history): response = respond(user_message, history) return "", history + [[user_message, response]] msg.submit(user_input, [msg, chatbot], [msg, chatbot], queue=False) clear.click(lambda: None, None, chatbot, queue=False) submit.click(lambda: msg.submit(), None, chatbot, queue=False) if __name__ == "__main__": demo.launch(share=True)