Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def calculation_interface(question):
|
| 6 |
-
operation = extract_operation(question)
|
| 7 |
numbers = extract_numbers(question)
|
| 8 |
if not operation or not numbers:
|
| 9 |
-
return "Invalid input! Please provide a valid mathematical question."
|
| 10 |
result = perform_calculation(numbers, operation)
|
| 11 |
-
return f"
|
| 12 |
|
| 13 |
# Gradio interface setup
|
| 14 |
iface = gr.Interface(
|
| 15 |
-
fn=calculation_interface,
|
| 16 |
-
inputs="text",
|
| 17 |
-
outputs="text",
|
| 18 |
-
title="Simple Calculation App",
|
| 19 |
-
description="
|
| 20 |
)
|
| 21 |
|
| 22 |
if __name__ == "__main__":
|
| 23 |
iface.launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import re
|
| 3 |
+
|
| 4 |
+
def extract_numbers(text):
|
| 5 |
+
# Extract numbers from input text
|
| 6 |
+
return [float(num) for num in re.findall(r"[-+]?\d*\.\d+|\d+", text)]
|
| 7 |
+
|
| 8 |
+
def extract_operation(text):
|
| 9 |
+
# Determine the operation based on keywords
|
| 10 |
+
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 None
|
| 20 |
+
|
| 21 |
+
def 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 + b
|
| 29 |
+
elif operation == "subtract":
|
| 30 |
+
return a - b
|
| 31 |
+
elif operation == "multiply":
|
| 32 |
+
return a * b
|
| 33 |
+
elif operation == "divide":
|
| 34 |
+
return a / b if b != 0 else "Cannot divide by zero"
|
| 35 |
+
else:
|
| 36 |
+
return "Unsupported operation."
|
| 37 |
|
| 38 |
def 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 setup
|
| 47 |
iface = 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 |
|
| 55 |
if __name__ == "__main__":
|
| 56 |
iface.launch()
|
| 57 |
+
|