Answers / app.py
AWS-ANSWER-BOT's picture
Update app.py
3bfbf73 verified
import gradio as gr
from transformers import pipeline
# Load the chatbot model using text-generation pipeline
chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
def respond(message, chat_history):
# Generate a response using the model
response = chatbot(message, max_new_tokens=100)[0]['generated_text']
# Update chat history
chat_history = chat_history or []
chat_history.append(("User", message))
chat_history.append(("Bot", response))
# Format chat history for display
chat_display = ""
for speaker, text in chat_history:
chat_display += f"{speaker}: {text}\n"
return chat_display, chat_history
with gr.Blocks() as demo:
chat_history = gr.State([])
chatbot_output = gr.Textbox(label="Chatbot Output", lines=15)
user_input = gr.Textbox(label="Your Message")
send_button = gr.Button("Send")
send_button.click(
fn=respond,
inputs=[user_input, chat_history],
outputs=[chatbot_output, chat_history]
)
if __name__ == "__main__":
demo.launch()