sandy083 commited on
Commit
31e6d9e
·
verified ·
1 Parent(s): 231c4a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -16
app.py CHANGED
@@ -93,7 +93,7 @@ vectorstore = FAISS.load_local(
93
  embeddings=embedding_model,
94
  allow_dangerous_deserialization=True
95
  )
96
- retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
97
  print("✅ RAG 系統初始化完成。")
98
 
99
 
@@ -219,11 +219,16 @@ def math_solver_process(user_question):
219
  return error_msg, "Error", "Error", "Error", "Error"
220
 
221
 
222
- # =========================================================
223
  # 4. Gradio 介面配置 (作為測試介面,確保能跑)
224
  # =========================================================
225
 
226
- # 6. Gradio 介面
 
 
 
 
 
227
  with gr.Blocks(title="實變函數論 AI Agent") as demo:
228
  gr.Markdown("# 📐 實變函數論 AI 助教 (Hugging Face Spaces RAG + CoT)")
229
  gr.Markdown("輸入你的數學問題,AI 將查詢課本、多角度思考、並經由同儕審查機制產出嚴謹證明。")
@@ -232,31 +237,47 @@ with gr.Blocks(title="實變函數論 AI Agent") as demo:
232
  input_box = gr.Textbox(label="請輸入問題", placeholder="例如:請解釋 Lebesgue 積分與 Riemann 積分在極限交換時的差異")
233
  run_btn = gr.Button("開始解題", variant="primary")
234
 
235
- with gr.Accordion("📚 步驟一:檢索資料 (RAG)", open=False):
236
- out_rag = gr.TextArea(label="RAG Context", interactive=False)
 
 
 
 
237
 
238
- with gr.Accordion("🧠 步驟二:思考與選擇 (CoT)", open=False):
239
- out_thoughts = gr.TextArea(label="5 種解題思路", interactive=False)
 
 
 
 
240
 
241
- with gr.Row():
242
- out_draft = gr.TextArea(label="📝 初稿 (Writer)", interactive=False)
243
- out_review = gr.TextArea(label="🧐 審查意見 (Reviewer)", interactive=False)
 
 
 
 
 
 
 
 
 
 
244
 
245
  gr.Markdown("### ✨ 最終解答 (Final Answer)")
246
 
 
247
  out_final = gr.Markdown(
248
  label="最終解答",
249
- latex_delimiters=[
250
- { "left": "$$", "right": "$$", "display": True },
251
- { "left": "$", "right": "$", "display": False }
252
- ]
253
  )
254
 
255
- # 按鈕事件綁定
256
  run_btn.click(
257
  math_solver_process,
258
  inputs=[input_box],
259
- outputs=[out_rag, out_thoughts, out_draft, out_review, out_final]
260
  )
261
 
262
  # 在 Hugging Face 上執行時,只需呼叫 demo.launch()
 
93
  embeddings=embedding_model,
94
  allow_dangerous_deserialization=True
95
  )
96
+ retriever = vectorstore.as_retriever(search_kwargs={"k": 8})
97
  print("✅ RAG 系統初始化完成。")
98
 
99
 
 
219
  return error_msg, "Error", "Error", "Error", "Error"
220
 
221
 
222
+ # =========================================================
223
  # 4. Gradio 介面配置 (作為測試介面,確保能跑)
224
  # =========================================================
225
 
226
+ # 定義 LaTeX 渲染模式(統一所有 Markdown 元件)
227
+ LATEX_DELIMITERS = [
228
+ { "left": "$$", "right": "$$", "display": True },
229
+ { "left": "$", "right": "$", "display": False }
230
+ ]
231
+
232
  with gr.Blocks(title="實變函數論 AI Agent") as demo:
233
  gr.Markdown("# 📐 實變函數論 AI 助教 (Hugging Face Spaces RAG + CoT)")
234
  gr.Markdown("輸入你的數學問題,AI 將查詢課本、多角度思考、並經由同儕審查機制產出嚴謹證明。")
 
237
  input_box = gr.Textbox(label="請輸入問題", placeholder="例如:請解釋 Lebesgue 積分與 Riemann 積分在極限交換時的差異")
238
  run_btn = gr.Button("開始解題", variant="primary")
239
 
240
+ # 步驟一:檢索資料 (RAG) - 使用 Markdown 渲染 LaTeX
241
+ with gr.Accordion("📚 步驟一:檢索資料 (RAG Context)", open=False):
242
+ out_rag = gr.Markdown(
243
+ label="RAG Context",
244
+ latex_delimiters=LATEX_DELIMITERS
245
+ )
246
 
247
+ # 步驟二:思考與選擇 (CoT) - 使用 Markdown 渲染 LaTeX
248
+ with gr.Accordion("🧠 步驟二:思考與選擇 (CoT Planner)", open=False):
249
+ out_thoughts = gr.Markdown(
250
+ label="5 種解題思路",
251
+ latex_delimiters=LATEX_DELIMITERS
252
+ )
253
 
254
+ # 步驟三、四:初稿與審查 - 包裹在一個新的可收合區塊
255
+ with gr.Accordion("📝 步驟三/四:初稿與審查 (Writer / Reviewer)", open=False):
256
+ with gr.Row():
257
+ # 初稿 (Writer) - 使用 Markdown 渲染 LaTeX
258
+ out_draft = gr.Markdown(
259
+ label="初稿 (Writer)",
260
+ latex_delimiters=LATEX_DELIMITERS
261
+ )
262
+ # 審查意見 (Reviewer) - 使用 Markdown 渲染 LaTeX
263
+ out_review = gr.Markdown(
264
+ label="審查意見 (Reviewer)",
265
+ latex_delimiters=LATEX_DELIMITERS
266
+ )
267
 
268
  gr.Markdown("### ✨ 最終解答 (Final Answer)")
269
 
270
+ # 最終解答 (Final Answer) - 維持不變,確保在最顯眼的位置
271
  out_final = gr.Markdown(
272
  label="最終解答",
273
+ latex_delimiters=LATEX_DELIMITERS
 
 
 
274
  )
275
 
276
+ # 按鈕事件綁定 (注意:math_solver_process 必須回傳 5 個輸出)
277
  run_btn.click(
278
  math_solver_process,
279
  inputs=[input_box],
280
+ outputs=[out_rag, out_thoughts, out_draft, out_review, out_final] # 輸出數量與函數回傳匹配
281
  )
282
 
283
  # 在 Hugging Face 上執行時,只需呼叫 demo.launch()