my-ai-chatbot / app.py
Percy3822's picture
Update app.py
42f5c81 verified
raw
history blame contribute delete
981 Bytes
from transformers import pipeline, Conversation
import gradio as gr
# Load DialoGPT model (small version)
chatbot = pipeline("conversational", model="microsoft/DialoGPT-small")
def chat_with_ai(user_input, chat_history=[]):
# Create a conversation object with previous messages
conversation = Conversation(user_input)
for message in chat_history:
conversation.add_user_input(message)
# Get model response
response = chatbot(conversation)
bot_reply = response.generated_responses[-1]
# Update history
chat_history.append(user_input)
chat_history.append(bot_reply)
return bot_reply, chat_history
# Build Gradio interface
with gr.Blocks() as demo:
gr.Markdown("## 🤖 AI Chatbot (DialoGPT)")
chat_history = gr.State([])
user_input = gr.Textbox(label="Type your message:")
output = gr.Textbox(label="Bot reply:")
user_input.submit(chat_with_ai, [user_input, chat_history], [output, chat_history])
demo.launch()