import gradio as gr
import matplotlib
import numpy as np
import matplotlib.pyplot as plt

matplotlib.use('Agg')   

def quadratic_plot(a, b, c) -> plt.Figure:  
  x = np.linspace(-10, 10, 400)
  y = a * x**2 + b * x + c

  fig, ax = plt.subplots()
  ax.plot(x, y, label=f'y = {a} x^2 + {b}x + {c}')
  ax.axhline(0, color='black', linewidth=0.5)
  ax.axvline(0, color='black', linewidth=0.5)
  ax.set_xlabel("x")
  ax.set_ylabel("y")
  ax.grid(True)
  ax.legend()
  return fig

# 初期状態のグラフを作成
initial_plot = quadratic_plot(1, 0, 0) 

demo = gr.Interface(     
  fn=quadratic_plot,
  inputs=[             
    gr.Slider(minimum=-10, maximum=10, step=0.1, value=1, label="係数 a"),
    gr.Slider(minimum=-10, maximum=10, step=0.1, value=0, label="係数 b"),
    gr.Slider(minimum=-10, maximum=10, step=0.1, value=0, label="係数 c")
  ],
  outputs=gr.Plot(value=initial_plot),
  live=True,
  title="2次関数のグラフ描画",
)

demo.launch()
