habi01 commited on
Commit
ecc8436
·
verified ·
1 Parent(s): a12f2c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -1,36 +1,41 @@
1
  import gradio as gr
2
 
3
  """
4
- This is a simple offline chatbot using predefined responses.
5
  """
6
 
7
- # Define a response function for the chatbot
8
- def respond(message, history=[]):
9
- # Predefined responses (case-insensitive matching)
 
 
 
 
 
 
10
  responses = {
11
  "hello": "Hi there! How can I help you?",
12
  "how are you?": "I'm just a bot, but I'm doing great! How about you?",
13
  "what's your name?": "I'm your friendly chatbot! What's your name?",
14
  "bye": "Goodbye! Have a great day!",
15
- "default": "I'm sorry, I didn't understand that. Can you try rephrasing?",
16
  }
17
 
18
- # Check if the user's message matches a predefined response
19
- message_lower = message.lower()
20
- response = responses.get(message_lower, responses["default"])
21
 
22
- # Add the user message and bot response to the chat history
23
  history.append({"role": "user", "content": message})
24
  history.append({"role": "assistant", "content": response})
25
 
26
  return history
27
 
28
- # Create a simple Gradio chat interface
29
  demo = gr.ChatInterface(
30
- respond,
31
- title="Offline Chatbot",
32
- description="Type a message to chat with this friendly offline bot!",
33
- type="messages",
 
34
  )
35
 
36
  # Launch the app
 
1
  import gradio as gr
2
 
3
  """
4
+ This is a simple chatbot inspired by the Gradio guide, with predefined responses.
5
  """
6
 
7
+ # Define the chatbot response function
8
+ def respond(message, history):
9
+ """
10
+ Respond to user input based on predefined logic.
11
+ :param message: User's most recent message.
12
+ :param history: List of previous messages in the format [{"role": ..., "content": ...}].
13
+ :return: Updated history with the bot's response.
14
+ """
15
+ # Predefined responses
16
  responses = {
17
  "hello": "Hi there! How can I help you?",
18
  "how are you?": "I'm just a bot, but I'm doing great! How about you?",
19
  "what's your name?": "I'm your friendly chatbot! What's your name?",
20
  "bye": "Goodbye! Have a great day!",
 
21
  }
22
 
23
+ # Default response if no match is found
24
+ response = responses.get(message.lower(), "I'm sorry, I didn't understand that. Can you try rephrasing?")
 
25
 
26
+ # Update the chat history
27
  history.append({"role": "user", "content": message})
28
  history.append({"role": "assistant", "content": response})
29
 
30
  return history
31
 
32
+ # Set up Gradio's ChatInterface
33
  demo = gr.ChatInterface(
34
+ fn=respond, # The chatbot response function
35
+ type="messages", # Ensure OpenAI-style 'role' and 'content' keys are used
36
+ title="Simple Chatbot",
37
+ description="Type a message to chat with this friendly bot!",
38
+ examples=["hello", "how are you?", "what's your name?", "bye"], # Predefined examples for testing
39
  )
40
 
41
  # Launch the app