File size: 1,177 Bytes
bf7e32c
 
c3393f9
bf7e32c
 
 
 
 
 
 
 
 
 
 
 
 
 
c3393f9
bf7e32c
 
 
 
 
 
 
c3393f9
 
 
 
 
 
 
 
 
 
bf7e32c
c3393f9
 
 
bf7e32c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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'")