Spaces:
Sleeping
Sleeping
| # calculator_chatbot.py | |
| import streamlit as st | |
| import re | |
| def evaluate_expression(expression): | |
| try: | |
| # Evaluate the mathematical expression | |
| result = eval(expression) | |
| return result | |
| except Exception as e: | |
| return str(e) | |
| def chatbot_response(user_input): | |
| # Regular expression to find mathematical expressions | |
| expression_pattern = r'(\d+\s*[\+\-\*\/]\s*\d+)' | |
| match = re.search(expression_pattern, user_input) | |
| if match: | |
| expression = match.group(0) | |
| result = evaluate_expression(expression) | |
| return f"The result of {expression} is {result}." | |
| else: | |
| return "I'm sorry, I can only help with simple math calculations." | |
| # ---------------- Streamlit UI ---------------- | |
| st.title("🧮 Calculator Chatbot") | |
| st.write("Welcome to the Calculator Chatbot! Type a simple math expression like '2 + 3' or '5 * 4'.") | |
| user_input = st.text_input("You:") | |
| if user_input: | |
| if user_input.lower() == 'exit': | |
| st.write("Chatbot: Goodbye!") | |
| else: | |
| response = chatbot_response(user_input) | |
| st.success(f"Chatbot: {response}") | |
| st.write("\n💡 Example: Try typing '10 / 2' or '6 - 3'") | |