File size: 1,031 Bytes
ac8fc31
93d7763
 
 
 
 
 
 
ac8fc31
93d7763
 
 
 
b5f5cd7
 
93d7763
 
 
 
 
 
 
 
ac8fc31
b5f5cd7
93d7763
 
 
 
 
b5f5cd7
 
 
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
import gradio as gr
import re

BOT_NAME = "Phu Quy Ho"

def handle_math(user_input):
    user_input = user_input.lower().strip()

    # Respond to greetings
    greetings = ["hi", "hello", "hey", "yo"]
    if any(greet in user_input for greet in greetings):
        return f"Hello! I am {BOT_NAME}. I can help you with math. Try something like: (3 + 5) * -2"

    # Remove everything except math symbols and numbers
    math_expression = re.sub(r"[^\d\.\+\-\*/\(\)\s]", "", user_input)
    math_expression = math_expression.replace("what is", "").strip()

    try:
        result = eval(math_expression)
        return f"The answer is {result}"
    except:
        return "Sorry, I couldn't understand that. Please enter a valid math expression like (2 + 3) * -1"

# Gradio UI
demo = gr.Interface(
    fn=handle_math,
    inputs="text",
    outputs="text",
    title="Phu Quy Ho – Smart Math Solver",
    description="Say hello or ask me a math question! I support +, -, *, /, negative numbers, and parentheses."
)

demo.launch()