Spaces:
Sleeping
Sleeping
| # # app.py | |
| # import gradio as gr | |
| # # Define the calculator function | |
| # def calculator(expression): | |
| # try: | |
| # result = eval(expression) | |
| # return result | |
| # except: | |
| # return "Invalid input" | |
| # # Create Gradio interface | |
| # iface = gr.Interface( | |
| # fn=calculator, | |
| # inputs=gr.Textbox(label="Enter a math expression (e.g., 5 + 3 * 2)"), | |
| # outputs=gr.Textbox(label="Result"), | |
| # title="๐งฎ Calculator", | |
| # description="This calculator can do addition (+), subtraction (-), multiplication (*), and division (/)." | |
| # ) | |
| # # Launch the app | |
| # iface.launch() | |
| # app.py | |
| import gradio as gr | |
| import math | |
| # โ Safe Evaluation Function | |
| def safe_eval(expression): | |
| try: | |
| # Allowed functions and constants | |
| allowed_names = {k: v for k, v in math.__dict__.items() if not k.startswith("__")} | |
| allowed_names.update({ | |
| "abs": abs, | |
| "round": round, | |
| "pow": pow, | |
| }) | |
| result = eval(expression, {"__builtins__": None}, allowed_names) | |
| return f"โ Result: {result}" | |
| except ZeroDivisionError: | |
| return "โ Error: Division by zero" | |
| except Exception as e: | |
| return f"โ Error: Invalid expression" | |
| # ๐จ Gradio Interface | |
| iface = gr.Interface( | |
| fn=safe_eval, | |
| inputs=gr.Textbox( | |
| label="๐ Math Expression", | |
| placeholder="e.g. 8 + 2, 10 / 5, 3 * 4, sqrt(16), log(10), 5**2" | |
| ), | |
| outputs=gr.Textbox(label="๐ Result"), | |
| title="๐งฎ AI-Powered Calculator", | |
| description=( | |
| "๐ข This calculator supports:\n" | |
| "- โ Addition (+)\n" | |
| "- โ Subtraction (-)\n" | |
| "- โ๏ธ Multiplication (*)\n" | |
| "- โ Division (/)\n" | |
| "- ๐ง Math functions: sqrt(), log(), sin(), cos(), tan(), abs(), round(), pow()\n\n" | |
| "โ ๏ธ Type only valid math expressions. No natural language." | |
| ) | |
| ) | |
| # ๐ Launch | |
| if __name__ == "__main__": | |
| iface.launch() | |