aciang commited on
Commit
5c5cb4d
·
verified ·
1 Parent(s): f09d15a

Update app.py via Colab one-block

Browse files
Files changed (1) hide show
  1. app.py +83 -33
app.py CHANGED
@@ -1,68 +1,118 @@
1
- \
 
2
  import gradio as gr
3
  import sympy as sp
 
 
 
 
4
 
5
  TITLE = "LanguageBridge — Math Fast Agent (SymPy)"
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def solve_math(q: str):
8
  q = (q or "").strip()
9
  if not q:
10
- return "請輸入算式或方程,例如:2x+3=11;或:sin(x)**2 + cos(x)**2;或:factor(x**2-9)"
 
11
  try:
12
- # 支援方程/聯立:可用分號或換行分隔
13
- if "=" in q:
14
- parts = [s.strip() for seg in q.split(";") for s in seg.split("\n")]
 
 
 
15
  eqs, syms = [], set()
16
  for s in parts:
17
- if not s:
18
- continue
19
- if "=" not in s:
20
- continue
21
- left, right = s.split("=", 1)
22
- eq = sp.Eq(sp.sympify(left), sp.sympify(right))
23
- eqs.append(eq)
24
- syms |= eq.free_symbols
25
- if hasattr(eq, "rhs"):
26
- syms |= eq.rhs.free_symbols
27
  if not syms:
28
- x = sp.symbols("x")
29
- syms = {x}
30
  sol = sp.solve(eqs, list(syms), dict=True)
31
  if not sol:
32
  return "無解或需要更多條件。"
33
- lines = []
34
- for i, s in enumerate(sol, 1):
35
- lines.append(f"解 {i}: " + ", ".join([f"{k} = {sp.simplify(v)}" for k, v in s.items()]))
36
- return "\n".join(lines)
37
 
38
- # 非方程:做一輪常見操作
39
- expr = sp.sympify(q)
40
- tips = []
41
  try:
42
- tips.append(f"簡化:{sp.simplify(expr)}")
43
  except Exception:
44
- tips.append(f"簡化:{expr}")
45
  try:
46
  fact = sp.factor(expr)
47
  if fact != expr:
48
- tips.append(f"因式分解:{fact}")
49
  except Exception:
50
  pass
51
  try:
52
- x = list(expr.free_symbols)[0] if expr.free_symbols else sp.symbols("x")
53
- tips.append(f"對 {x} 微分:{sp.diff(expr, x)}")
54
- tips.append(f"對 {x} 積分:{sp.integrate(expr, x)}")
55
  except Exception:
56
  pass
57
- return "\n".join(tips) if tips else f"結果:{expr}"
 
58
  except Exception as e:
59
  return f"解析失敗:{e}"
60
 
 
61
  with gr.Blocks(title=TITLE) as demo:
62
- gr.Markdown(f"## {TITLE}\n貼上算式(可多行 / 用分號 `;` 分隔):")
 
 
 
 
 
63
  q = gr.Textbox(lines=6, label="題目 / 算式(可含聯立方程)")
64
- out = gr.Textbox(lines=10, label="輸出")
65
  gr.Button("送出 🚀").click(fn=solve_math, inputs=q, outputs=out)
66
 
67
  if __name__ == "__main__":
 
68
  demo.launch()
 
1
+
2
+ import re
3
  import gradio as gr
4
  import sympy as sp
5
+ from sympy.parsing.sympy_parser import (
6
+ parse_expr, standard_transformations,
7
+ implicit_multiplication_application, convert_xor
8
+ )
9
 
10
  TITLE = "LanguageBridge — Math Fast Agent (SymPy)"
11
 
12
+ # ---------- 輸入正規化:隱式乘法 / ^→** / 全形→半形 / √→sqrt ----------
13
+ def normalize_ascii(s: str) -> str:
14
+ table = str.maketrans({
15
+ '(':'(', ')':')', ',':',', ';':';', ':':':',
16
+ '=':'=', '-':'-', '+':'+', '*':'*', '/':'/', '。':'.',
17
+ '×':'*', '√':'sqrt'
18
+ })
19
+ return s.translate(table)
20
+
21
+ def auto_insert_stars(s: str) -> str:
22
+ # 3x -> 3*x; x(x+1) -> x*(x+1);(x+1)x -> (x+1)*x; 2sqrt(x) -> 2*sqrt(x)
23
+ s = re.sub(r'(\d)([A-Za-z])', r'\1*\2', s)
24
+ s = re.sub(r'([A-Za-z0-9_])\(', r'\1*(', s)
25
+ s = re.sub(r'\)([A-Za-z0-9_])', r')*\1', s)
26
+ s = re.sub(r'(\d)\s*sqrt', r'\1*sqrt', s)
27
+ return s
28
+
29
+ def preprocess(expr: str) -> str:
30
+ s = (expr or "").strip()
31
+ s = normalize_ascii(s)
32
+ s = s.replace("^", "**") # 2^3 -> 2**3
33
+ s = auto_insert_stars(s) # 隱式乘法 → 顯式
34
+ return s
35
+
36
+ # 支援隱式乘法與 ^ 解析
37
+ TRANS = standard_transformations + (implicit_multiplication_application, convert_xor)
38
+
39
+ def to_sympy_expr(s: str):
40
+ s = preprocess(s)
41
+ return parse_expr(s, transformations=TRANS)
42
+
43
+ def to_sympy_eq(s: str):
44
+ s = preprocess(s)
45
+ if "=" not in s:
46
+ raise ValueError("等式缺少 '='")
47
+ L, R = s.split("=", 1)
48
+ return sp.Eq(parse_expr(L, transformations=TRANS),
49
+ parse_expr(R, transformations=TRANS))
50
+
51
+ # ---------------- 主邏輯(保留原介面與輸出格式) ----------------
52
  def solve_math(q: str):
53
  q = (q or "").strip()
54
  if not q:
55
+ return "請輸入算式或方程,例如:2x+3=11sin(x)^2 + cos(x)^2factor(x^2-9)"
56
+
57
  try:
58
+ # 多題/聯立:分號 ; 或換行 \n 分隔
59
+ parts = [s.strip() for seg in q.split(";") for s in seg.split("\n")]
60
+ parts = [p for p in parts if p]
61
+
62
+ # 若任一行含 '=',啟用「解方程(可聯立)」模式
63
+ if any("=" in p for p in parts):
64
  eqs, syms = [], set()
65
  for s in parts:
66
+ if "=" in s:
67
+ e = to_sympy_eq(s)
68
+ eqs.append(e)
69
+ syms |= e.free_symbols | e.rhs.free_symbols
 
 
 
 
 
 
70
  if not syms:
71
+ syms = {sp.symbols("x")}
 
72
  sol = sp.solve(eqs, list(syms), dict=True)
73
  if not sol:
74
  return "無解或需要更多條件。"
75
+ return "\n".join(
76
+ f"解 {i}: " + ", ".join([f\"{k} = {sp.simplify(v)}\" for k, v in d.items()])
77
+ for i, d in enumerate(sol, 1)
78
+ )
79
 
80
+ # 否則視為單一表達式:簡化 / 因式 / 微分 / 積分
81
+ expr = to_sympy_expr(q)
82
+ out = []
83
  try:
84
+ out.append(f"簡化:{sp.simplify(expr)}")
85
  except Exception:
86
+ out.append(f"簡化:{expr}")
87
  try:
88
  fact = sp.factor(expr)
89
  if fact != expr:
90
+ out.append(f"因式分解:{fact}")
91
  except Exception:
92
  pass
93
  try:
94
+ x = next(iter(expr.free_symbols)) if expr.free_symbols else sp.symbols("x")
95
+ out.append(f"對 {x} 微分:{sp.diff(expr, x)}")
96
+ out.append(f"對 {x} 積分:{sp.integrate(expr, x)}")
97
  except Exception:
98
  pass
99
+ return "\n".join(out) if out else f"結果:{expr}"
100
+
101
  except Exception as e:
102
  return f"解析失敗:{e}"
103
 
104
+ # ---------------- 介面 ----------------
105
  with gr.Blocks(title=TITLE) as demo:
106
+ gr.Markdown(
107
+ "## " + TITLE + "\\n"
108
+ "貼上算式(可多行 / 用分號 `;` 分隔)。\\n\\n"
109
+ "**可直接輸入隱式乘法:** `3x`、`2(x+1)`、`(x)(x+1)`、`2sqrt(x)`;"
110
+ "也可用 `x^2`(自動轉為 `x**2`),`√(x)`(自動轉為 `sqrt(x)`)。"
111
+ )
112
  q = gr.Textbox(lines=6, label="題目 / 算式(可含聯立方程)")
113
+ out = gr.Textbox(lines=12, label="輸出")
114
  gr.Button("送出 🚀").click(fn=solve_math, inputs=q, outputs=out)
115
 
116
  if __name__ == "__main__":
117
+ # Space 環境不需要 share
118
  demo.launch()