aciang commited on
Commit
2f94119
·
verified ·
1 Parent(s): 094dc0b

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +5 -5
  2. app.py +77 -0
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,12 +1,12 @@
1
  ---
2
- title: LanguageBridge MathFastAgent
3
- emoji: 🌍
4
  colorFrom: yellow
5
- colorTo: purple
6
  sdk: gradio
7
- sdk_version: 5.49.1
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: LanguageBridge — Math Fast Agent (SymPy)
3
+ emoji: 🧮
4
  colorFrom: yellow
5
+ colorTo: blue
6
  sdk: gradio
7
+ sdk_version: "4.44.1"
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
+ SymPy 的數學推理代理(不依賴 LLM),支援一次貼上長式、簡化、解一元/聯立方程。
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # 1) 如果像 "2x+3=11" 或 "x+y=3; x-y=1"
13
+ if "=" in q:
14
+ # 支援多行/分號;同時支援聯立
15
+ parts = [s.strip() for seg in q.split(";") for s in seg.split("\n")]
16
+ eqs = []
17
+ syms = set()
18
+ for s in parts:
19
+ if not 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
+ syms |= eq.rhs.free_symbols if hasattr(eq, "rhs") else set()
26
+ if not syms:
27
+ # 預設一個 x
28
+ x = sp.symbols("x")
29
+ syms = {x}
30
+ sol = sp.solve(eqs, list(syms), dict=True)
31
+ if not sol:
32
+ return "無解或需要更多條件。"
33
+ # 格式化
34
+ lines = []
35
+ for i, s in enumerate(sol, 1):
36
+ lines.append(f"解 {i}: " + ", ".join([f"{k} = {sp.simplify(v)}" for k, v in s.items()]))
37
+ return "\n".join(lines)
38
+
39
+ # 2) 非方程:嘗試自動辨識可做的操作(簡化/因式分解/微分/積分…)
40
+ expr = sp.sympify(q)
41
+ # 嘗試:若像 “diff(sin(x), x)” 就直接回傳求導;否則做一輪常見操作建議
42
+ try:
43
+ simplified = sp.simplify(expr)
44
+ except Exception:
45
+ simplified = expr
46
+
47
+ tips = []
48
+ tips.append(f"簡化:{simplified}")
49
+ try:
50
+ fact = sp.factor(expr)
51
+ if fact != expr:
52
+ tips.append(f"因式分解:{fact}")
53
+ except Exception:
54
+ pass
55
+ try:
56
+ x = list(expr.free_symbols)[0] if expr.free_symbols else sp.symbols("x")
57
+ tips.append(f"對 {x} 微分:{sp.diff(expr, x)}")
58
+ tips.append(f"對 {x} 積分:{sp.integrate(expr, x)}")
59
+ except Exception:
60
+ pass
61
+ return "\n".join(tips) if tips else f"結果:{expr}"
62
+
63
+ except Exception as e:
64
+ return f"解析失敗:{e}"
65
+
66
+ with gr.Blocks(title=TITLE) as demo:
67
+ gr.Markdown(f"## {TITLE}\n貼上算式(可多行 / 用分號 `;` 分隔):")
68
+ with gr.Row():
69
+ q = gr.Textbox(lines=6, label="題目 / 算式(可含聯立方程)")
70
+ with gr.Row():
71
+ out = gr.Textbox(lines=10, label="輸出")
72
+ btn = gr.Button("送出 🚀")
73
+ btn.click(fn=solve_math, inputs=q, outputs=out)
74
+
75
+ if __name__ == "__main__":
76
+ # 在 Colab 本機預覽一定要 share=True 才會印出 public URL
77
+ demo.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio==4.44.1
2
+ sympy>=1.12
3
+ huggingface_hub>=0.24.0