File size: 3,617 Bytes
7cde843
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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).