akhaliq HF Staff commited on
Commit
3641859
·
verified ·
1 Parent(s): a1dfdab

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+
4
+ # Simple chatbot responses
5
+ responses = {
6
+ "hello": ["Hi there!", "Hello!", "Hey! How can I help you?"],
7
+ "how are you": ["I'm doing well, thank you!", "Great! Thanks for asking.", "I'm a chatbot, so I'm always ready to help!"],
8
+ "bye": ["Goodbye!", "See you later!", "Bye! Have a great day!"],
9
+ "default": ["I'm not sure I understand.", "Can you tell me more?", "That's interesting!", "Let's talk about something else."]
10
+ }
11
+
12
+ def chatbot(message, history):
13
+ """
14
+ Simple chatbot function that responds to user messages.
15
+
16
+ Args:
17
+ message (str): The user's input message
18
+ history (list): List of previous conversations
19
+
20
+ Returns:
21
+ str: The chatbot's response
22
+ """
23
+ message = message.lower()
24
+
25
+ # Check for specific keywords and respond accordingly
26
+ if "hello" in message or "hi" in message:
27
+ response = random.choice(responses["hello"])
28
+ elif "how are you" in message:
29
+ response = random.choice(responses["how are you"])
30
+ elif "bye" in message:
31
+ response = random.choice(responses["bye"])
32
+ else:
33
+ response = random.choice(responses["default"])
34
+
35
+ return response
36
+
37
+ # Create the chatbot interface
38
+ with gr.Blocks(title="Simple Chatbot") as demo:
39
+ gr.Markdown("# 🤖 Simple Chatbot")
40
+ gr.Markdown("Talk to this basic chatbot. Type 'hello', 'how are you', or 'bye' for specific responses!")
41
+
42
+ # Chatbot component
43
+ chatbot_interface = gr.Chatbot(
44
+ label="Chatbot Conversation",
45
+ bubble_full_width=False
46
+ )
47
+
48
+ # User input
49
+ user_input = gr.Textbox(
50
+ label="Your Message",
51
+ placeholder="Type your message here...",
52
+ lines=2
53
+ )
54
+
55
+ # Submit button
56
+ submit_btn = gr.Button("Send Message")
57
+
58
+ # Clear button
59
+ clear_btn = gr.Button("Clear Conversation")
60
+
61
+ # Connect components
62
+ user_input.submit(
63
+ fn=chatbot,
64
+ inputs=[user_input, chatbot_interface],
65
+ outputs=[chatbot_interface],
66
+ queue=False
67
+ ).then(
68
+ fn=lambda: "",
69
+ inputs=None,
70
+ outputs=user_input,
71
+ queue=False
72
+ )
73
+
74
+ submit_btn.click(
75
+ fn=chatbot,
76
+ inputs=[user_input, chatbot_interface],
77
+ outputs=[chatbot_interface],
78
+ queue=False
79
+ ).then(
80
+ fn=lambda: "",
81
+ inputs=None,
82
+ outputs=user_input,
83
+ queue=False
84
+ )
85
+
86
+ clear_btn.click(
87
+ fn=lambda: None,
88
+ inputs=None,
89
+ outputs=chatbot_interface,
90
+ queue=False
91
+ )
92
+
93
+ # Launch the app
94
+ if __name__ == "__main__":
95
+ demo.launch()