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()