import gradio as gr from transformers import pipeline # Load a conversational pipeline (chatbot model) chatbot = pipeline("conversational", model="microsoft/DialoGPT-medium") def respond(message, chat_history=[]): from transformers import Conversation conversation = Conversation(message) # Add past messages to conversation history for past_user_msg, past_bot_msg in chat_history: conversation.add_user_input(past_user_msg) conversation.append_response(past_bot_msg) response = chatbot(conversation) bot_reply = response.generated_responses[-1] chat_history.append((message, bot_reply)) return "", chat_history with gr.Blocks() as demo: chatbot_ui = gr.Chatbot() msg = gr.Textbox() msg.submit(respond, [msg, chatbot_ui], [msg, chatbot_ui]) msg.submit(lambda: None, None, msg) # Clear input box after submit demo.launch()