File size: 981 Bytes
42f5c81
2018aa6
 
42f5c81
 
2018aa6
42f5c81
 
 
 
 
 
 
 
 
 
 
 
2018aa6
 
 
42f5c81
 
2018aa6
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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()