File size: 1,714 Bytes
e9652ee
549e55e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9652ee
 
549e55e
e9652ee
 
549e55e
e9652ee
549e55e
e9652ee
 
 
549e55e
 
 
 
 
e9652ee
 
 
 
549e55e
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import gradio as gr
import re

def extract_numbers(text):
    # Extract numbers from input text
    return [float(num) for num in re.findall(r"[-+]?\d*\.\d+|\d+", text)]

def extract_operation(text):
    # Determine the operation based on keywords
    if "add" in text or "plus" in text or "+" in text:
        return "add"
    elif "subtract" in text or "minus" in text or "-" in text:
        return "subtract"
    elif "multiply" in text or "times" in text or "*" in text:
        return "multiply"
    elif "divide" in text or "divided by" in text or "/" in text:
        return "divide"
    else:
        return None

def perform_calculation(numbers, operation):
    if len(numbers) < 2:
        return "Please provide at least two numbers."
    
    a, b = numbers[0], numbers[1]

    if operation == "add":
        return a + b
    elif operation == "subtract":
        return a - b
    elif operation == "multiply":
        return a * b
    elif operation == "divide":
        return a / b if b != 0 else "Cannot divide by zero"
    else:
        return "Unsupported operation."

def calculation_interface(question):
    operation = extract_operation(question.lower())
    numbers = extract_numbers(question)
    if not operation or not numbers:
        return "❌ Invalid input! Please provide a valid mathematical question."
    result = perform_calculation(numbers, operation)
    return f"✅ Result: {result}"

# Gradio interface setup
iface = gr.Interface(
    fn=calculation_interface,
    inputs="text",
    outputs="text",
    title="🧮 Simple Calculation App",
    description="Type something like 'Add 5 and 3' or 'What is 10 divided by 2?'"
)

if __name__ == "__main__":
    iface.launch()