Download app.py from Behailut/mathai: direct link, hf CLI and curl.
- Browser
- Download file 3.62 kB
-
https://huggingface.co/spaces/Behailut/mathai/resolve/main/app.py
- Command line
-
hf download hf://spaces/Behailut/mathai/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Behailut/mathai/resolve/main/app.py
3.62 kB
| import gradio as gr | |
| import math | |
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| # Store calculation history | |
| history = [] | |
| def calculator(Num1, Num2, operation): | |
| global history | |
| try: | |
| if operation == "Addition": | |
| result = Num1 + Num2 | |
| elif operation == "Subtraction": | |
| result = Num1 - Num2 | |
| elif operation == "Multiplication": | |
| result = Num1 * Num2 | |
| elif operation == "Division": | |
| result = Num1 / Num2 if Num2 != 0 else "Error: Division by zero" | |
| elif operation == "Exponentiation": | |
| result = Num1 ** Num2 | |
| elif operation == "Modulo": | |
| result = Num1 % Num2 if Num2 != 0 else "Error: Modulo by zero" | |
| elif operation == "Square Root": | |
| result = math.sqrt(Num1) if Num1 >= 0 else "Error: Negative number" | |
| elif operation == "Logarithm": | |
| result = math.log(Num1, Num2) if Num1 > 0 and Num2 > 0 else "Error: Invalid log inputs" | |
| elif operation == "Factorial": | |
| result = math.factorial(int(Num1)) if Num1 >= 0 and Num1 == int(Num1) else "Error: Invalid factorial" | |
| elif operation == "Absolute Value": | |
| result = abs(Num1) | |
| elif operation in ["sin", "cos", "tan"]: | |
| trig_functions = {"sin": math.sin, "cos": math.cos, "tan": math.tan} | |
| result = trig_functions[operation](math.radians(Num1)) | |
| else: | |
| result = "Invalid operation" | |
| history.append(f"{operation}: {Num1}, {Num2} = {result}") | |
| return result | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| def show_history(): | |
| return "\n".join(history[-5:]) if history else "No history yet." | |
| def plot_function(expression): | |
| x = np.linspace(-10, 10, 100) | |
| try: | |
| y = eval(expression, {"x": x, "math": math, "np": np}) | |
| plt.figure() | |
| plt.plot(x, y) | |
| plt.xlabel("x") | |
| plt.ylabel("y") | |
| plt.title(f"Graph of {expression}") | |
| plt.grid() | |
| plt.savefig("plot.png") | |
| return "plot.png" | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Define the interface | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# π’ Scientific Calculator with Graph Plotting") | |
| with gr.Row(): | |
| num1 = gr.Number(label="Enter first number") | |
| num2 = gr.Number(label="Enter second number (if needed)") | |
| operation = gr.Radio( | |
| [ | |
| "Addition", "Subtraction", "Multiplication", "Division", | |
| "Exponentiation", "Modulo", "Square Root", "Logarithm", | |
| "Factorial", "Absolute Value", "sin", "cos", "tan" | |
| ], | |
| label="Select operation" | |
| ) | |
| calc_button = gr.Button("Calculate") | |
| result = gr.Textbox(label="Result") | |
| calc_button.click(calculator, inputs=[num1, num2, operation], outputs=result) | |
| # History section | |
| gr.Markdown("## π Calculation History (Last 5)") | |
| history_button = gr.Button("Show History") | |
| history_output = gr.Textbox(label="History") | |
| history_button.click(show_history, outputs=history_output) | |
| # Graph plotting | |
| gr.Markdown("## π Graph Plotting") | |
| expression = gr.Textbox(label="Enter function (e.g., x**2, sin(x))") | |
| plot_button = gr.Button("Plot Graph") | |
| plot_output = gr.Image() | |
| plot_button.click(plot_function, inputs=expression, outputs=plot_output) | |
| # Launch the interface with a public share link | |
| demo.launch(server_name="0.0.0.0", server_port=7860, share=True) | |
| # Note: For free permanent hosting, run `gradio deploy` in the working directory | |
| # to deploy on Hugging Face Spaces (https://huggingface.co/spaces). | |