Behailut commited on
Commit
7cde843
ยท
verified ยท
1 Parent(s): 9b3afc2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import math
3
+ import matplotlib.pyplot as plt
4
+ import numpy as np
5
+
6
+ # Store calculation history
7
+ history = []
8
+
9
+ def calculator(Num1, Num2, operation):
10
+ global history
11
+ try:
12
+ if operation == "Addition":
13
+ result = Num1 + Num2
14
+ elif operation == "Subtraction":
15
+ result = Num1 - Num2
16
+ elif operation == "Multiplication":
17
+ result = Num1 * Num2
18
+ elif operation == "Division":
19
+ result = Num1 / Num2 if Num2 != 0 else "Error: Division by zero"
20
+ elif operation == "Exponentiation":
21
+ result = Num1 ** Num2
22
+ elif operation == "Modulo":
23
+ result = Num1 % Num2 if Num2 != 0 else "Error: Modulo by zero"
24
+ elif operation == "Square Root":
25
+ result = math.sqrt(Num1) if Num1 >= 0 else "Error: Negative number"
26
+ elif operation == "Logarithm":
27
+ result = math.log(Num1, Num2) if Num1 > 0 and Num2 > 0 else "Error: Invalid log inputs"
28
+ elif operation == "Factorial":
29
+ result = math.factorial(int(Num1)) if Num1 >= 0 and Num1 == int(Num1) else "Error: Invalid factorial"
30
+ elif operation == "Absolute Value":
31
+ result = abs(Num1)
32
+ elif operation in ["sin", "cos", "tan"]:
33
+ trig_functions = {"sin": math.sin, "cos": math.cos, "tan": math.tan}
34
+ result = trig_functions[operation](math.radians(Num1))
35
+ else:
36
+ result = "Invalid operation"
37
+
38
+ history.append(f"{operation}: {Num1}, {Num2} = {result}")
39
+ return result
40
+ except Exception as e:
41
+ return f"Error: {str(e)}"
42
+
43
+ def show_history():
44
+ return "\n".join(history[-5:]) if history else "No history yet."
45
+
46
+ def plot_function(expression):
47
+ x = np.linspace(-10, 10, 100)
48
+ try:
49
+ y = eval(expression, {"x": x, "math": math, "np": np})
50
+ plt.figure()
51
+ plt.plot(x, y)
52
+ plt.xlabel("x")
53
+ plt.ylabel("y")
54
+ plt.title(f"Graph of {expression}")
55
+ plt.grid()
56
+ plt.savefig("plot.png")
57
+ return "plot.png"
58
+ except Exception as e:
59
+ return f"Error: {str(e)}"
60
+
61
+ # Define the interface
62
+ with gr.Blocks() as demo:
63
+ gr.Markdown("# ๐Ÿ”ข Scientific Calculator with Graph Plotting")
64
+
65
+ with gr.Row():
66
+ num1 = gr.Number(label="Enter first number")
67
+ num2 = gr.Number(label="Enter second number (if needed)")
68
+
69
+ operation = gr.Radio(
70
+ [
71
+ "Addition", "Subtraction", "Multiplication", "Division",
72
+ "Exponentiation", "Modulo", "Square Root", "Logarithm",
73
+ "Factorial", "Absolute Value", "sin", "cos", "tan"
74
+ ],
75
+ label="Select operation"
76
+ )
77
+
78
+ calc_button = gr.Button("Calculate")
79
+ result = gr.Textbox(label="Result")
80
+
81
+ calc_button.click(calculator, inputs=[num1, num2, operation], outputs=result)
82
+
83
+ # History section
84
+ gr.Markdown("## ๐Ÿ“œ Calculation History (Last 5)")
85
+ history_button = gr.Button("Show History")
86
+ history_output = gr.Textbox(label="History")
87
+ history_button.click(show_history, outputs=history_output)
88
+
89
+ # Graph plotting
90
+ gr.Markdown("## ๐Ÿ“ˆ Graph Plotting")
91
+ expression = gr.Textbox(label="Enter function (e.g., x**2, sin(x))")
92
+ plot_button = gr.Button("Plot Graph")
93
+ plot_output = gr.Image()
94
+ plot_button.click(plot_function, inputs=expression, outputs=plot_output)
95
+
96
+ # Launch the interface with a public share link
97
+ demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
98
+
99
+ # Note: For free permanent hosting, run `gradio deploy` in the working directory
100
+ # to deploy on Hugging Face Spaces (https://huggingface.co/spaces).