File size: 1,893 Bytes
f7efdfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# app.py
import gradio as gr

def chatbot_response(message, history):
    """Process user input and return chatbot response"""
    user_input = message.lower()
    
    if "hello" in user_input:
        return "Hello there! How can I help you today?"
    elif "bye" in user_input:
        return "Goodbye! 👋"
    else:
        return f"You said: {message}. I'm still learning!"

# Create Gradio interface
with gr.Blocks(title="AI Chatbot", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 🤖 AI Chatbot")
    gr.Markdown("Welcome to my simple AI chatbot! Try saying 'hello' or 'bye'.")
    
    # Chat interface
    chatbot = gr.Chatbot(
        label="Chat",
        height=400,
        show_label=True,
        container=True,
        bubble_full_width=False
    )
    
    # Text input
    msg = gr.Textbox(
        label="Your message",
        placeholder="Type your message here...",
        lines=1,
        max_lines=3,
        show_label=True
    )
    
    # Submit button
    submit_btn = gr.Button("Send", variant="primary")
    
    # Clear button
    clear_btn = gr.Button("Clear", variant="secondary")
    
    # Event handlers
    def user(user_message, history):
        return "", history + [[user_message, None]]
    
    def bot(history):
        user_message = history[-1][0]
        bot_message = chatbot_response(user_message, history)
        history[-1][1] = bot_message
        return history
    
    # Connect the interface
    msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
        bot, chatbot, chatbot
    )
    submit_btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
        bot, chatbot, chatbot
    )
    clear_btn.click(lambda: None, None, chatbot, queue=False)

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)