Spaces:
Running
Running
Update index.html
Browse files- index.html +87 -45
index.html
CHANGED
|
@@ -1,57 +1,99 @@
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
</style>
|
| 19 |
-
</head>
|
| 20 |
-
<body>
|
| 21 |
-
<gradio-lite>
|
| 22 |
-
<gradio-file name="app.py" entrypoint>
|
| 23 |
import gradio as gr
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
process,
|
| 33 |
-
"image",
|
| 34 |
-
"image",
|
| 35 |
-
examples=["lion.jpg", "logo.png"],
|
| 36 |
-
)
|
| 37 |
|
| 38 |
-
demo.launch()
|
| 39 |
-
</gradio-file>
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
def as_gray(image):
|
| 45 |
-
return rgb2gray(image)
|
| 46 |
-
</gradio-file>
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
</html>
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="utf-8" />
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
| 6 |
+
<title>Calculator & Function Plotter</title>
|
| 7 |
+
<script type="module" src="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.js"></script>
|
| 8 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.css" />
|
| 9 |
+
</head>
|
| 10 |
+
<body>
|
| 11 |
+
<gradio-lite>
|
| 12 |
+
<gradio-requirements>
|
| 13 |
+
numpy
|
| 14 |
+
matplotlib
|
| 15 |
+
</gradio-requirements>
|
| 16 |
+
|
| 17 |
+
<gradio-file name="app.py" entrypoint>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
import gradio as gr
|
| 19 |
+
import numpy as np
|
| 20 |
+
import matplotlib.pyplot as plt
|
| 21 |
|
| 22 |
+
# ---------- Basic Calculator ----------
|
| 23 |
+
def calculate(num1, operation, num2):
|
| 24 |
+
try:
|
| 25 |
+
num1 = float(num1)
|
| 26 |
+
num2 = float(num2)
|
| 27 |
+
except (TypeError, ValueError):
|
| 28 |
+
return "Enter valid numbers"
|
| 29 |
|
| 30 |
+
if operation == "+":
|
| 31 |
+
result = num1 + num2
|
| 32 |
+
elif operation == "-":
|
| 33 |
+
result = num1 - num2
|
| 34 |
+
elif operation == "×":
|
| 35 |
+
result = num1 * num2
|
| 36 |
+
elif operation == "÷":
|
| 37 |
+
if num2 == 0:
|
| 38 |
+
return "Error: Division by zero"
|
| 39 |
+
result = num1 / num2
|
| 40 |
+
elif operation == "^":
|
| 41 |
+
result = num1 ** num2
|
| 42 |
+
else:
|
| 43 |
+
return "Unknown operation"
|
| 44 |
|
| 45 |
+
return f"Result: {result}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# ---------- Function Plotter ----------
|
| 49 |
+
def plot_function(expression, x_min, x_max):
|
| 50 |
+
try:
|
| 51 |
+
x = np.linspace(float(x_min), float(x_max), 400)
|
| 52 |
+
|
| 53 |
+
allowed_names = {
|
| 54 |
+
"x": x, "np": np, "sin": np.sin, "cos": np.cos, "tan": np.tan,
|
| 55 |
+
"exp": np.exp, "log": np.log, "sqrt": np.sqrt, "pi": np.pi,
|
| 56 |
+
"abs": np.abs
|
| 57 |
+
}
|
| 58 |
+
y = eval(expression, {"__builtins__": {}}, allowed_names)
|
| 59 |
+
|
| 60 |
+
fig, ax = plt.subplots(figsize=(6, 4))
|
| 61 |
+
ax.plot(x, y, color="#2563eb", linewidth=2)
|
| 62 |
+
ax.axhline(0, color="black", linewidth=0.5)
|
| 63 |
+
ax.axvline(0, color="black", linewidth=0.5)
|
| 64 |
+
ax.set_title(f"y = {expression}")
|
| 65 |
+
ax.grid(True, alpha=0.3)
|
| 66 |
+
return fig
|
| 67 |
+
except Exception as e:
|
| 68 |
+
fig, ax = plt.subplots(figsize=(6, 4))
|
| 69 |
+
ax.text(0.5, 0.5, f"Error: {e}", ha="center", va="center", color="red")
|
| 70 |
+
ax.axis("off")
|
| 71 |
+
return fig
|
| 72 |
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
with gr.Blocks(title="Calculator + Graph Plotter") as demo:
|
| 75 |
+
gr.Markdown("# 🧮 Calculator & Function Plotter")
|
| 76 |
|
| 77 |
+
with gr.Tab("Basic Calculator"):
|
| 78 |
+
with gr.Row():
|
| 79 |
+
num1 = gr.Number(label="First Number")
|
| 80 |
+
operation = gr.Dropdown(["+", "-", "×", "÷", "^"], label="Operation", value="+")
|
| 81 |
+
num2 = gr.Number(label="Second Number")
|
| 82 |
+
calc_btn = gr.Button("Calculate", variant="primary")
|
| 83 |
+
calc_output = gr.Textbox(label="Output")
|
| 84 |
+
calc_btn.click(calculate, inputs=[num1, operation, num2], outputs=calc_output)
|
| 85 |
+
|
| 86 |
+
with gr.Tab("Function Plotter"):
|
| 87 |
+
expr = gr.Textbox(label="Function of x (e.g. sin(x), x**2 + 3*x - 1)", value="sin(x)")
|
| 88 |
+
with gr.Row():
|
| 89 |
+
x_min = gr.Number(label="x min", value=-10)
|
| 90 |
+
x_max = gr.Number(label="x max", value=10)
|
| 91 |
+
plot_btn = gr.Button("Plot", variant="primary")
|
| 92 |
+
plot_output = gr.Plot(label="Graph")
|
| 93 |
+
plot_btn.click(plot_function, inputs=[expr, x_min, x_max], outputs=plot_output)
|
| 94 |
+
|
| 95 |
+
demo.launch()
|
| 96 |
+
</gradio-file>
|
| 97 |
+
</gradio-lite>
|
| 98 |
+
</body>
|
| 99 |
</html>
|