QUANWEI commited on
Commit
31ca372
·
verified ·
1 Parent(s): 81917a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -5
app.py CHANGED
@@ -3,6 +3,8 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
 
 
6
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
@@ -12,12 +14,29 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
  class BasicAgent:
14
  def __init__(self):
15
- print("BasicAgent initialized.")
 
 
 
 
 
 
 
 
 
 
16
  def __call__(self, question: str) -> str:
17
- print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
 
 
 
 
 
 
 
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ from smolagents import CodeAgent, OpenAIServerModel
7
+ import re
8
 
9
  # (Keep Constants as is)
10
  # --- Constants ---
 
14
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
15
  class BasicAgent:
16
  def __init__(self):
17
+ # 這裡從你之前設置的 Secret 讀取能源
18
+ self.api_key = os.getenv("DEEPSEEK")
19
+ self.model = OpenAIServerModel(
20
+ model_id="deepseek-chat",
21
+ api_base="https://api.deepseek.com/v1",
22
+ api_key=self.api_key
23
+ )
24
+ # 初始化大腦,max_steps 設為 10 避免超時
25
+ self.agent = CodeAgent(model=self.model, tools=[], add_base_tools=True, max_steps=10)
26
+ print("✅ DeepSeek 採煤機已就緒")
27
+
28
  def __call__(self, question: str) -> str:
29
+ try:
30
+ # 告訴 Agent 只准輸出結果,不准廢話
31
+ prompt = f"Identify the answer to this task. Provide ONLY the final result (number or word) without any explanation. Question: {question}"
32
+ result = self.agent.run(prompt)
33
+
34
+ # 格式清理:去掉「答案是:」之類的廢話
35
+ clean_result = str(result).strip()
36
+ clean_result = re.sub(r"(?i)(final answer|answer|答案)\s*[::]", "", clean_result)
37
+ return clean_result.split('\n')[0].strip()
38
+ except Exception as e:
39
+ return f"Error: {str(e)}"
40
 
41
  def run_and_submit_all( profile: gr.OAuthProfile | None):
42
  """