Upload folder using huggingface_hub
Browse files- README.md +3 -8
- _rebuild_ping.txt +1 -0
- app.py +48 -161
- requirements.txt +1 -8
README.md
CHANGED
|
@@ -1,17 +1,12 @@
|
|
| 1 |
---
|
| 2 |
-
title: LanguageBridge — Math
|
| 3 |
emoji: 🧮
|
| 4 |
colorFrom: yellow
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: "4.44.1"
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
| 13 |
-
- LLM:把**文字題**解析為可計算的數學式或步驟
|
| 14 |
-
- SymPy:遇到**可符號計算**的部分(方程、微積分、因式分解…)直接算出精準解
|
| 15 |
-
- 自動偵測:若輸入就是算式/方程 → 直接 SymPy;否則走 LLM→SymPy 流程
|
| 16 |
-
|
| 17 |
-
> 預設模型:`microsoft/phi-2`(可在 app.py 換成你喜歡的小型模型)
|
|
|
|
| 1 |
---
|
| 2 |
+
title: LanguageBridge — Math Fast Agent (SymPy)
|
| 3 |
emoji: 🧮
|
| 4 |
colorFrom: yellow
|
| 5 |
+
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: "4.44.1"
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
純 SymPy 的數學推理代理(不依賴 LLM),支援一次貼上長式、簡化、解一元/聯立方程。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_rebuild_ping.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
1762069998.8309631
|
app.py
CHANGED
|
@@ -1,181 +1,68 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
import sympy as sp
|
| 4 |
|
| 5 |
-
|
| 6 |
-
AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 7 |
-
)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
MODEL_ID = "microsoft/phi-2"
|
| 11 |
-
MAX_NEW_TOKENS = 512
|
| 12 |
-
|
| 13 |
-
def try_sympy_direct(q: str):
|
| 14 |
-
"""若使用者輸入就是算式/方程,走 SymPy 精準計算。支援多行 / 分號分隔 / 聯立。"""
|
| 15 |
q = (q or "").strip()
|
| 16 |
if not q:
|
| 17 |
-
return
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
try:
|
| 22 |
-
# 支援多式/聯立:以分號或換行切
|
| 23 |
parts = [s.strip() for seg in q.split(";") for s in seg.split("\n")]
|
| 24 |
eqs, syms = [], set()
|
| 25 |
for s in parts:
|
| 26 |
if not s:
|
| 27 |
continue
|
| 28 |
-
if "=" in s:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
else:
|
| 51 |
-
return f"結果:{expr}"
|
| 52 |
-
|
| 53 |
-
if eqs:
|
| 54 |
-
if not syms:
|
| 55 |
-
x = sp.symbols("x")
|
| 56 |
-
syms = {x}
|
| 57 |
-
sol = sp.solve(eqs, list(syms), dict=True)
|
| 58 |
-
if not sol:
|
| 59 |
-
return "SymPy:無解或需要更多條件。"
|
| 60 |
-
lines = []
|
| 61 |
-
for i, sdict in enumerate(sol, 1):
|
| 62 |
-
lines.append("解 {}: ".format(i) + ", ".join([f"{k} = {sp.simplify(v)}" for k, v in sdict.items()]))
|
| 63 |
-
return "\n".join(lines)
|
| 64 |
-
|
| 65 |
-
except Exception as e:
|
| 66 |
-
# 交給 LLM 流程
|
| 67 |
-
return None
|
| 68 |
-
|
| 69 |
-
return None
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
def build_llm():
|
| 73 |
-
"""嘗試以 4-bit 啟動(有 CUDA 時),否則退回 CPU。"""
|
| 74 |
-
import torch
|
| 75 |
-
has_cuda = torch.cuda.is_available()
|
| 76 |
-
load_kwargs = {"device_map":"auto"}
|
| 77 |
-
|
| 78 |
-
if has_cuda:
|
| 79 |
try:
|
| 80 |
-
|
| 81 |
-
load_kwargs.update({"load_in_4bit": True})
|
| 82 |
except Exception:
|
| 83 |
-
|
| 84 |
-
load_kwargs.update({"torch_dtype": torch.float16})
|
| 85 |
-
else:
|
| 86 |
-
# CPU:讓 transformers 自行決定 dtype
|
| 87 |
-
pass
|
| 88 |
-
|
| 89 |
-
tok = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 90 |
-
if tok.pad_token_id is None and tok.eos_token_id is not None:
|
| 91 |
-
tok.pad_token = tok.eos_token
|
| 92 |
-
|
| 93 |
-
mdl = AutoModelForCausalLM.from_pretrained(
|
| 94 |
-
MODEL_ID,
|
| 95 |
-
trust_remote_code=True,
|
| 96 |
-
**load_kwargs
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
-
pipe = pipeline(
|
| 100 |
-
"text-generation",
|
| 101 |
-
model=mdl,
|
| 102 |
-
tokenizer=tok,
|
| 103 |
-
do_sample=False,
|
| 104 |
-
max_new_tokens=MAX_NEW_TOKENS,
|
| 105 |
-
temperature=0.2,
|
| 106 |
-
top_p=0.9
|
| 107 |
-
)
|
| 108 |
-
return pipe
|
| 109 |
-
|
| 110 |
-
LLM = None
|
| 111 |
-
|
| 112 |
-
SYSTEM_PROMPT = (
|
| 113 |
-
"You are a math teacher. When the user asks a word problem,\n"
|
| 114 |
-
"1) parse it into a clean mathematical expression or a system of equations;\n"
|
| 115 |
-
"2) if it is solvable by SymPy, output a single line starting with 'SymPy:' followed by a Python/SymPy expression;\n"
|
| 116 |
-
"3) then give a concise final answer on the next line starting with 'Answer:'."
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
def llm_to_sympy_and_answer(pipe, q: str):
|
| 120 |
-
prompt = (
|
| 121 |
-
f"<s>System:\n{SYSTEM_PROMPT}\n</s>\n"
|
| 122 |
-
f"User: {q}\n"
|
| 123 |
-
f"Assistant:"
|
| 124 |
-
)
|
| 125 |
-
out = pipe(prompt, pad_token_id=pipe.tokenizer.eos_token_id)[0]["generated_text"]
|
| 126 |
-
# 嘗試抓 SymPy: 行
|
| 127 |
-
sym_line = None
|
| 128 |
-
ans_line = None
|
| 129 |
-
for line in out.splitlines():
|
| 130 |
-
if line.strip().startswith("SymPy:"):
|
| 131 |
-
sym_line = line.split("SymPy:",1)[-1].strip()
|
| 132 |
-
if line.strip().startswith("Answer:"):
|
| 133 |
-
ans_line = line.split("Answer:",1)[-1].strip()
|
| 134 |
-
|
| 135 |
-
checked = ""
|
| 136 |
-
if sym_line:
|
| 137 |
try:
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
def solve(q: str):
|
| 153 |
-
global LLM
|
| 154 |
-
q = (q or "").strip()
|
| 155 |
-
if not q:
|
| 156 |
-
return "請輸入題目或算式。"
|
| 157 |
-
|
| 158 |
-
# 1) 先嘗試 SymPy 直接處理
|
| 159 |
-
direct = try_sympy_direct(q)
|
| 160 |
-
if direct:
|
| 161 |
-
return direct
|
| 162 |
-
|
| 163 |
-
# 2) 走 LLM → SymPy
|
| 164 |
-
if LLM is None:
|
| 165 |
-
LLM = build_llm()
|
| 166 |
-
try:
|
| 167 |
-
return llm_to_sympy_and_answer(LLM, q)
|
| 168 |
except Exception as e:
|
| 169 |
-
return f"
|
| 170 |
|
| 171 |
with gr.Blocks(title=TITLE) as demo:
|
| 172 |
-
gr.Markdown(f"## {TITLE}\n
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
out = gr.Textbox(label="輸出", lines=12)
|
| 177 |
-
btn = gr.Button("送出 🚀")
|
| 178 |
-
btn.click(fn=solve, inputs=q, outputs=out)
|
| 179 |
|
| 180 |
if __name__ == "__main__":
|
| 181 |
demo.launch()
|
|
|
|
| 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()
|
requirements.txt
CHANGED
|
@@ -1,10 +1,3 @@
|
|
| 1 |
gradio==4.44.1
|
| 2 |
-
transformers==4.44.2
|
| 3 |
-
accelerate>=0.31.0
|
| 4 |
-
bitsandbytes==0.43.3
|
| 5 |
-
sentencepiece
|
| 6 |
sympy>=1.12
|
| 7 |
-
huggingface_hub
|
| 8 |
-
safetensors
|
| 9 |
-
einops
|
| 10 |
-
numpy<2
|
|
|
|
| 1 |
gradio==4.44.1
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
sympy>=1.12
|
| 3 |
+
huggingface_hub==0.23.4
|
|
|
|
|
|
|
|
|