Download app.py from Kung-Hsun/test: direct link, hf CLI and curl.
- Browser
- Download file 1.7 kB
-
https://huggingface.co/spaces/Kung-Hsun/test/resolve/main/app.py
- Command line
-
hf download hf://spaces/Kung-Hsun/test/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Kung-Hsun/test/resolve/main/app.py
1.7 kB
| import gradio as gr | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| def polynomial_plot(coeffs, x_power, y_power): | |
| try: | |
| coeffs = [float(c.strip()) for c in coeffs.split(",")] | |
| except: | |
| return "❌ 請輸入正確的係數 (逗號分隔)", None | |
| x = np.linspace(-10, 10, 400) | |
| y = np.zeros_like(x) | |
| # 建立 f(x) | |
| for i, coef in enumerate(coeffs): | |
| y += coef * (x**i) | |
| # 應用 x 的最高次方限制 | |
| if x_power < len(coeffs): | |
| y = sum(coeffs[i] * (x**i) for i in range(x_power+1)) | |
| # y 的次方 (例如 y^2 = f(x)) | |
| if y_power != 1: | |
| # 避免負數開根 | |
| y = np.sign(y) * (np.abs(y))**(1.0/y_power) | |
| fig, ax = plt.subplots() | |
| ax.plot(x, y, label=f"Polynomial (deg={x_power}), y^{y_power}") | |
| ax.axhline(0, color="black", linewidth=0.8) | |
| ax.axvline(0, color="black", linewidth=0.8) | |
| ax.set_xlabel("x") | |
| ax.set_ylabel("y") | |
| ax.legend() | |
| ax.grid(True) | |
| return "✅ 繪圖完成", fig | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## 📈 多項式繪圖器 (Polynomial Plotter)") | |
| gr.Markdown("輸入係數,指定 x 與 y 的次方") | |
| coeffs = gr.Textbox(label="係數列表 (由常數到高次,例如: 1, -3, 2 代表 1 - 3x + 2x²)", value="1,-3,2") | |
| x_power = gr.Slider(1, 10, value=2, step=1, label="x 的最高次方 (degree)") | |
| y_power = gr.Slider(1, 5, value=1, step=1, label="y 的次方 (例如 2 表示 y² = f(x))") | |
| plot_btn = gr.Button("繪圖") | |
| status = gr.Textbox(label="狀態", interactive=False) | |
| plot = gr.Plot(label="圖形") | |
| plot_btn.click(fn=polynomial_plot, inputs=[coeffs, x_power, y_power], outputs=[status, plot]) | |
| demo.launch() |