habi01 commited on
Commit
a6f1452
·
verified ·
1 Parent(s): 59046aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -20
app.py CHANGED
@@ -1,28 +1,32 @@
1
  import gradio as gr
2
 
3
- # Define the chat function
4
- def chat_function(message, history):
5
- """
6
- Responds based on predefined logic.
7
- :param message: User's most recent message.
8
- :param history: Chat history with role and content.
9
- """
10
- # Simple logic: Alternates between "Yes" and "No"
11
- bot_responses = [h for h in history if h["role"] == "assistant"]
12
- if len(bot_responses) % 2 == 0:
13
- return f"Yes, I agree with: {message}"
14
- else:
15
- return f"No, I don't think so: {message}"
 
 
 
 
 
 
16
 
17
  # Set up Gradio's ChatInterface
18
  demo = gr.ChatInterface(
19
- fn=chat_function,
20
- type="messages", # Use message-type format
21
- title="Simple Bot",
22
- description="This chatbot alternates between agreeing and disagreeing with your input.",
23
- examples=["Hello!", "Do you like Python?", "What's your opinion on AI?"],
24
- save_history=True, # Enable persistent chat history
25
- theme="default", # Optional: Change to other themes like "compact" or "ocean"
26
  chatbot=gr.Chatbot(height=400, placeholder="Start chatting with the bot!"),
27
  textbox=gr.Textbox(placeholder="Type your message here...", container=False),
28
  )
 
1
  import gradio as gr
2
 
3
+ # Define a response function for the chatbot
4
+ def respond(message, history=[]):
5
+ # Predefined responses (case-insensitive matching)
6
+ responses = {
7
+ "hello": "Hi there! How can I help you?",
8
+ "how are you?": "I'm just a bot, but I'm doing great! How about you?",
9
+ "what's your name?": "I'm your friendly chatbot! What's your name?",
10
+ "bye": "Goodbye! Have a great day!",
11
+ "default": "I'm sorry, I didn't understand that. Can you try rephrasing?",
12
+ }
13
+
14
+ # Check if the user's message matches a predefined response
15
+ message_lower = message.lower()
16
+ response = responses.get(message_lower, responses["default"])
17
+
18
+ # Add the user message and bot response to the chat history
19
+ history.append((message, response))
20
+
21
+ return history
22
 
23
  # Set up Gradio's ChatInterface
24
  demo = gr.ChatInterface(
25
+ fn=respond, # Use the predefined respond function
26
+ type="messages", # Use openai-style 'role' and 'content' keys
27
+ title="Simple Predefined Chatbot",
28
+ description="This chatbot responds to specific predefined messages. Try 'hello' or 'how are you?'.",
29
+ examples=["hello", "how are you?", "what's your name?", "bye"],
 
 
30
  chatbot=gr.Chatbot(height=400, placeholder="Start chatting with the bot!"),
31
  textbox=gr.Textbox(placeholder="Type your message here...", container=False),
32
  )