xievic9780 commited on
Commit
d23bc15
·
1 Parent(s): d0f8eba

Update to Gradio 5 ChatInterface

Browse files
Files changed (1) hide show
  1. chatbot.py +13 -46
chatbot.py CHANGED
@@ -54,9 +54,7 @@ def simple_chatbot(message: str, history) -> str:
54
  # Math operations
55
  elif any(op in message_lower for op in ['what is', 'calculate', 'compute']):
56
  try:
57
- # Extract numbers and operators
58
  import re
59
- # Find mathematical expression
60
  math_pattern = r'(\d+\.?\d*)\s*([\+\-\*\/])\s*(\d+\.?\d*)'
61
  match = re.search(math_pattern, message)
62
  if match:
@@ -110,7 +108,7 @@ def simple_chatbot(message: str, history) -> str:
110
  ]
111
  return random.choice(responses)
112
 
113
- # Default response for unknown inputs
114
  else:
115
  responses = [
116
  "That's interesting! Tell me more.",
@@ -121,48 +119,17 @@ def simple_chatbot(message: str, history) -> str:
121
  ]
122
  return random.choice(responses)
123
 
124
- # Create the Gradio interface
125
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
126
- gr.Markdown("# 💬 Simple ChatBot")
127
- gr.Markdown("A friendly chatbot that can chat, do math, tell jokes, and more!")
128
-
129
- chatbot = gr.Chatbot(
130
- height=500,
131
- type='messages'
132
- )
133
-
134
- with gr.Row():
135
- msg = gr.Textbox(
136
- placeholder="Type your message here...",
137
- container=False,
138
- scale=7,
139
- show_label=False
140
- )
141
- submit = gr.Button("Send", scale=1, variant="primary")
142
-
143
- with gr.Row():
144
- clear = gr.Button("Clear Chat")
145
- examples = gr.Button("Show Examples")
146
-
147
- # Handle message submission
148
- def respond(message, chat_history):
149
- bot_message = simple_chatbot(message, chat_history)
150
- chat_history.append({"role": "user", "content": message})
151
- chat_history.append({"role": "assistant", "content": bot_message})
152
- return "", chat_history
153
-
154
- msg.submit(respond, [msg, chatbot], [msg, chatbot])
155
- submit.click(respond, [msg, chatbot], [msg, chatbot])
156
-
157
- clear.click(lambda: [], None, chatbot, queue=False)
158
-
159
- def show_examples():
160
- return [
161
- {"role": "assistant", "content": "Try these examples:\n- Hello!\n- What's your name?\n- What time is it?\n- Tell me a joke\n- What is 25 + 17?\n- How are you?"}
162
- ]
163
-
164
- examples.click(show_examples, None, chatbot)
165
 
166
- # Launch the app
167
  if __name__ == "__main__":
168
- demo.launch(share=False)
 
54
  # Math operations
55
  elif any(op in message_lower for op in ['what is', 'calculate', 'compute']):
56
  try:
 
57
  import re
 
58
  math_pattern = r'(\d+\.?\d*)\s*([\+\-\*\/])\s*(\d+\.?\d*)'
59
  match = re.search(math_pattern, message)
60
  if match:
 
108
  ]
109
  return random.choice(responses)
110
 
111
+ # Default response
112
  else:
113
  responses = [
114
  "That's interesting! Tell me more.",
 
119
  ]
120
  return random.choice(responses)
121
 
122
+ # Create the Gradio ChatInterface (simpler and works better with Gradio 5)
123
+ demo = gr.ChatInterface(
124
+ fn=simple_chatbot,
125
+ title="💬 Simple ChatBot",
126
+ description="A friendly chatbot that can chat, do math, tell jokes, and more!",
127
+ examples=["Hello!", "What time is it?", "Tell me a joke", "What is 25 + 17?", "How are you?"],
128
+ theme=gr.themes.Soft(),
129
+ retry_btn="🔄 Retry",
130
+ undo_btn="↩️ Undo",
131
+ clear_btn="🗑️ Clear"
132
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
 
134
  if __name__ == "__main__":
135
+ demo.launch()