Team Ai
Apppublic

PrashanthB461/Arithematical_Operations

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py58 linesDownload Raw Back to root
1import gradio as gr2import re3 4def extract_numbers(text):5    # Extract numbers from input text6    return [float(num) for num in re.findall(r"[-+]?\d*\.\d+|\d+", text)]7 8def extract_operation(text):9    # Determine the operation based on keywords10    if "add" in text or "plus" in text or "+" in text:11        return "add"12    elif "subtract" in text or "minus" in text or "-" in text:13        return "subtract"14    elif "multiply" in text or "times" in text or "*" in text:15        return "multiply"16    elif "divide" in text or "divided by" in text or "/" in text:17        return "divide"18    else:19        return None20 21def perform_calculation(numbers, operation):22    if len(numbers) < 2:23        return "Please provide at least two numbers."24    25    a, b = numbers[0], numbers[1]26 27    if operation == "add":28        return a + b29    elif operation == "subtract":30        return a - b31    elif operation == "multiply":32        return a * b33    elif operation == "divide":34        return a / b if b != 0 else "Cannot divide by zero"35    else:36        return "Unsupported operation."37 38def calculation_interface(question):39    operation = extract_operation(question.lower())40    numbers = extract_numbers(question)41    if not operation or not numbers:42        return "❌ Invalid input! Please provide a valid mathematical question."43    result = perform_calculation(numbers, operation)44    return f"✅ Result: {result}"45 46# Gradio interface setup47iface = gr.Interface(48    fn=calculation_interface,49    inputs="text",50    outputs="text",51    title="🧮 Simple Calculation App",52    description="Type something like 'Add 5 and 3' or 'What is 10 divided by 2?'"53)54 55if __name__ == "__main__":56    iface.launch()57 58