Yinxing commited on
Commit
7391a59
·
verified ·
1 Parent(s): 5785839

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -96
app.py CHANGED
@@ -1,97 +1,101 @@
1
- import gradio as gr
2
- import numpy as np
3
-
4
- try:
5
- # demo close
6
- demo.close()
7
- except:
8
- pass
9
-
10
-
11
- # --- LaTeX形式で行列を表現 ---
12
- def matrix_to_latex(M, name="A"):
13
- rows = [" & ".join(map(lambda x: f"{x:.1f}", row)) for row in M]
14
- body = " \\\\ ".join(rows)
15
- return f"{name} = \\begin{{bmatrix}} {body} \\end{{bmatrix}}"
16
-
17
- # --- ランダム行列生成 ---
18
- def random_matrix(rows, cols, min_val, max_val, step):
19
- values = np.arange(min_val, max_val + step / 2, step)
20
- return np.random.choice(values, size=(rows, cols))
21
-
22
- # --- 問題生成 ---
23
- def generate_problem(rows_A, cols_A, cols_B, min_val, max_val, step):
24
- rows_B = cols_A # 行列積が可能なように調整
25
- A = random_matrix(int(rows_A), int(cols_A), min_val, max_val, step)
26
- B = random_matrix(int(rows_B), int(cols_B), min_val, max_val, step)
27
-
28
- md = (
29
- "### 以下の行列の積を求めよ:\n\n"
30
- + "$$ " + matrix_to_latex(A, "A") + " $$\n\n"
31
- + "$$ " + matrix_to_latex(B, "B") + " $$\n\n"
32
- + "求めるもの:$$ A \\times B $$"
33
- )
34
- return (A.tolist(), B.tolist(), md)
35
-
36
- # --- 解答生成 ---
37
- def show_answer(A, B):
38
- A = np.array(A); B = np.array(B)
39
- C = A @ B
40
-
41
- steps = []
42
- for i in range(C.shape[0]):
43
- for j in range(C.shape[1]):
44
- terms = [f"{A[i,k]:.1f}\\times{B[k,j]:.1f}" for k in range(A.shape[1])]
45
- steps.append(f"$c_{{{i+1}{j+1}}} = " + " + ".join(terms) + f" = {C[i,j]:.1f}$")
46
-
47
- md = (
48
- "### 【解答】\n\n"
49
- + "$$ " + matrix_to_latex(C, "C") + " $$\n\n"
50
- + "### 【計算手順】\n\n"
51
- + "\n\n".join(steps)
52
- )
53
- return md
54
-
55
- # --- Gradio UI設定 ---
56
- latex_delims = [
57
- {"left": "$$", "right": "$$", "display": True},
58
- {"left": "$", "right": "$", "display": False},
59
- ]
60
-
61
- with gr.Blocks(title="行列積ランダム出題") as demo:
62
- gr.Markdown("## 🎲 ランダム行列積クイズ\n"
63
- "行列サイズや数値範囲を調整して、練習用の問題を自動生成できます。")
64
-
65
- with gr.Accordion("⚙️ パラメータ設定", open=False):
66
- with gr.Row():
67
- rows_A = gr.Slider(2, 4, value=3, step=1, label="Aの行数")
68
- cols_A = gr.Slider(2, 4, value=3, step=1, label="Aの列数(=Bの行数)")
69
- cols_B = gr.Slider(2, 4, value=3, step=1, label="Bの列数")
70
- with gr.Row():
71
- min_val = gr.Number(value=-5.0, label="最小値(例:-5)")
72
- max_val = gr.Number(value=5.0, label="最大値(例:5)")
73
- step_val = gr.Number(value=0.5, label="刻み幅(例:0.5)")
74
-
75
- A_state = gr.State()
76
- B_state = gr.State()
77
-
78
- problem_md = gr.Markdown("", latex_delimiters=latex_delims, label="問題")
79
- answer_md = gr.Markdown("", latex_delimiters=latex_delims, label="解答")
80
-
81
- with gr.Row():
82
- gen_btn = gr.Button("🔄 問題を再作成", variant="primary")
83
- ans_btn = gr.Button("💡 解答提示", variant="secondary")
84
-
85
- gen_btn.click(
86
- fn=generate_problem,
87
- inputs=[rows_A, cols_A, cols_B, min_val, max_val, step_val],
88
- outputs=[A_state, B_state, problem_md]
89
- )
90
-
91
- ans_btn.click(
92
- fn=show_answer,
93
- inputs=[A_state, B_state],
94
- outputs=answer_md
95
- )
96
-
 
 
 
 
97
  demo.launch()
 
1
+ import gradio as gr
2
+ import numpy as np
3
+
4
+ try:
5
+ # demo close
6
+ demo.close()
7
+ except:
8
+ pass
9
+
10
+ import gradio as gr
11
+ import numpy as np
12
+
13
+ # --- LaTeX形式で行列を表現 ---
14
+ def matrix_to_latex(M, name="A"):
15
+ rows = [" & ".join(map(lambda x: f"{x:.1f}", row)) for row in M]
16
+ body = " \\\\ ".join(rows)
17
+ return f"{name} = \\begin{{bmatrix}} {body} \\end{{bmatrix}}"
18
+
19
+ # --- ランダム行列生成 ---
20
+ def random_matrix(rows, cols, min_val, max_val, step):
21
+ values = np.arange(min_val, max_val + step / 2, step)
22
+ return np.random.choice(values, size=(rows, cols))
23
+
24
+ # --- 問題生成 ---
25
+ def generate_problem(rows_A, cols_A, cols_B, min_val, max_val, step):
26
+ rows_B = cols_A # 行列積が可能なように調整
27
+ A = random_matrix(int(rows_A), int(cols_A), min_val, max_val, step)
28
+ B = random_matrix(int(rows_B), int(cols_B), min_val, max_val, step)
29
+
30
+ md = (
31
+ "### 以下の行列の積を求めよ:\n\n"
32
+ + "$$ " + matrix_to_latex(A, "A") + " $$\n\n"
33
+ + "$$ " + matrix_to_latex(B, "B") + " $$\n\n"
34
+ + "求めるもの:$$ A \\times B $$"
35
+ )
36
+ # 新しい問題と一緒に解答欄を空にリセット
37
+ return (A.tolist(), B.tolist(), md, "")
38
+
39
+ # --- 解答生成 ---
40
+ def show_answer(A, B):
41
+ A = np.array(A); B = np.array(B)
42
+ C = A @ B
43
+
44
+ steps = []
45
+ for i in range(C.shape[0]):
46
+ for j in range(C.shape[1]):
47
+ terms = [f"{A[i,k]:.1f}\\times{B[k,j]:.1f}" for k in range(A.shape[1])]
48
+ steps.append(f"$c_{{{i+1}{j+1}}} = " + " + ".join(terms) + f" = {C[i,j]:.1f}$")
49
+
50
+ md = (
51
+ "### 【解答】\n\n"
52
+ + "$$ " + matrix_to_latex(C, "C") + " $$\n\n"
53
+ + "### 【計算手順】\n\n"
54
+ + "\n\n".join(steps)
55
+ )
56
+ return md
57
+
58
+ # --- Gradio UI設定 ---
59
+ latex_delims = [
60
+ {"left": "$$", "right": "$$", "display": True},
61
+ {"left": "$", "right": "$", "display": False},
62
+ ]
63
+
64
+ with gr.Blocks(title="行列積ランダム出題") as demo:
65
+ gr.Markdown("## 🎲 ランダム行列積クイズ\n"
66
+ "行列サイズや数値範囲を調整して、練習用の問題を自動生成できます。")
67
+
68
+ with gr.Accordion("⚙️ パラメータ設定", open=False):
69
+ with gr.Row():
70
+ rows_A = gr.Slider(2, 4, value=3, step=1, label="Aの行数")
71
+ cols_A = gr.Slider(2, 4, value=3, step=1, label="Aの列数(=Bの行数)")
72
+ cols_B = gr.Slider(2, 4, value=3, step=1, label="Bの列数")
73
+ with gr.Row():
74
+ min_val = gr.Number(value=-5.0, label="最小値(例:-5)")
75
+ max_val = gr.Number(value=5.0, label="最大値(例:5)")
76
+ step_val = gr.Number(value=0.5, label="刻み幅(例:0.5)")
77
+
78
+ A_state = gr.State()
79
+ B_state = gr.State()
80
+
81
+ problem_md = gr.Markdown("", latex_delimiters=latex_delims, label="問題")
82
+ answer_md = gr.Markdown("", latex_delimiters=latex_delims, label="解答")
83
+
84
+ with gr.Row():
85
+ gen_btn = gr.Button("🔄 問題を再作成", variant="primary")
86
+ ans_btn = gr.Button("💡 解答提示", variant="secondary")
87
+
88
+ # 新しい問題作成時に、解答欄もクリア
89
+ gen_btn.click(
90
+ fn=generate_problem,
91
+ inputs=[rows_A, cols_A, cols_B, min_val, max_val, step_val],
92
+ outputs=[A_state, B_state, problem_md, answer_md]
93
+ )
94
+
95
+ ans_btn.click(
96
+ fn=show_answer,
97
+ inputs=[A_state, B_state],
98
+ outputs=answer_md
99
+ )
100
+
101
  demo.launch()