Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import pandas as pd
|
|
| 5 |
from typing import Optional
|
| 6 |
from smolagents import CodeAgent, OpenAIServerModel, tool
|
| 7 |
|
| 8 |
-
# --- 搜尋工具
|
| 9 |
try:
|
| 10 |
from duckduckgo_search import DDGS
|
| 11 |
except ImportError:
|
|
@@ -16,33 +16,27 @@ except ImportError:
|
|
| 16 |
@tool
|
| 17 |
def web_search(query: str) -> str:
|
| 18 |
"""
|
| 19 |
-
Performs a web search using DuckDuckGo
|
| 20 |
Args:
|
| 21 |
query: The search query string.
|
| 22 |
"""
|
| 23 |
print(f"🕵️ [Debug] Searching: {query}")
|
| 24 |
try:
|
| 25 |
-
#
|
| 26 |
-
# 限制 max_results=3 以節省讀取時間
|
| 27 |
with DDGS() as ddgs:
|
| 28 |
results = ddgs.text(query, max_results=3, backend="html")
|
| 29 |
|
| 30 |
if not results:
|
| 31 |
-
return "No
|
| 32 |
|
| 33 |
-
# 格式化結果給模型看
|
| 34 |
formatted = []
|
| 35 |
for r in results:
|
| 36 |
-
|
| 37 |
-
body = r.get('body', 'No Description')
|
| 38 |
-
formatted.append(f"Title: {title}\nSnippet: {body}")
|
| 39 |
-
|
| 40 |
return "\n---\n".join(formatted)
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
print(f"❌ Search Error: {e}")
|
| 44 |
-
|
| 45 |
-
return f"Search failed. Please ignore search and answer based on your knowledge."
|
| 46 |
|
| 47 |
# -----------------------------------------------------------
|
| 48 |
|
|
@@ -55,14 +49,13 @@ class GroqAgent:
|
|
| 55 |
self.agent = None
|
| 56 |
return
|
| 57 |
|
| 58 |
-
#
|
| 59 |
model = OpenAIServerModel(
|
| 60 |
-
model_id="
|
| 61 |
api_base="https://api.groq.com/openai/v1",
|
| 62 |
api_key=self.api_key
|
| 63 |
)
|
| 64 |
|
| 65 |
-
# Agent 設定
|
| 66 |
self.agent = CodeAgent(
|
| 67 |
tools=[web_search],
|
| 68 |
model=model,
|
|
@@ -75,18 +68,17 @@ class GroqAgent:
|
|
| 75 |
return "Error: GROQ_API_KEY not configured."
|
| 76 |
|
| 77 |
try:
|
| 78 |
-
# 提示詞
|
| 79 |
prompt = f"""
|
| 80 |
-
|
| 81 |
-
1. Use
|
| 82 |
-
2. If
|
| 83 |
-
3. For questions about images/audio you cannot see, search for the description text provided in the question.
|
| 84 |
|
| 85 |
Question: {question}
|
| 86 |
"""
|
| 87 |
return str(self.agent.run(prompt))
|
| 88 |
except Exception as e:
|
| 89 |
-
return f"Error
|
| 90 |
|
| 91 |
def run_and_submit_all(profile: Optional[gr.OAuthProfile] = None):
|
| 92 |
if profile is None:
|
|
@@ -120,7 +112,6 @@ def run_and_submit_all(profile: Optional[gr.OAuthProfile] = None):
|
|
| 120 |
|
| 121 |
print(f"🚀 [{idx}/{total}] Task: {tid}")
|
| 122 |
|
| 123 |
-
# 執行 Agent
|
| 124 |
ans = agent_wrapper(q)
|
| 125 |
|
| 126 |
answers.append({"task_id": tid, "submitted_answer": ans})
|
|
@@ -136,15 +127,13 @@ def run_and_submit_all(profile: Optional[gr.OAuthProfile] = None):
|
|
| 136 |
|
| 137 |
data = res.json()
|
| 138 |
score = data.get('score', 0)
|
| 139 |
-
|
| 140 |
-
msg = f"🎉 Final Score: {score}%"
|
| 141 |
-
return msg, pd.DataFrame(logs)
|
| 142 |
|
| 143 |
except Exception as e:
|
| 144 |
return f"Submit error: {str(e)}", pd.DataFrame(logs)
|
| 145 |
|
| 146 |
-
with gr.Blocks(title="Final Agent (
|
| 147 |
-
gr.Markdown("# 🚀 Final Agent (
|
| 148 |
with gr.Row():
|
| 149 |
gr.LoginButton()
|
| 150 |
btn = gr.Button("Run Evaluation", variant="primary")
|
|
|
|
| 5 |
from typing import Optional
|
| 6 |
from smolagents import CodeAgent, OpenAIServerModel, tool
|
| 7 |
|
| 8 |
+
# --- 搜尋工具 ---
|
| 9 |
try:
|
| 10 |
from duckduckgo_search import DDGS
|
| 11 |
except ImportError:
|
|
|
|
| 16 |
@tool
|
| 17 |
def web_search(query: str) -> str:
|
| 18 |
"""
|
| 19 |
+
Performs a web search using DuckDuckGo.
|
| 20 |
Args:
|
| 21 |
query: The search query string.
|
| 22 |
"""
|
| 23 |
print(f"🕵️ [Debug] Searching: {query}")
|
| 24 |
try:
|
| 25 |
+
# 使用 backend='html' 比較穩定
|
|
|
|
| 26 |
with DDGS() as ddgs:
|
| 27 |
results = ddgs.text(query, max_results=3, backend="html")
|
| 28 |
|
| 29 |
if not results:
|
| 30 |
+
return "No results."
|
| 31 |
|
|
|
|
| 32 |
formatted = []
|
| 33 |
for r in results:
|
| 34 |
+
formatted.append(f"Title: {r.get('title')}\nSnippet: {r.get('body')}")
|
|
|
|
|
|
|
|
|
|
| 35 |
return "\n---\n".join(formatted)
|
| 36 |
|
| 37 |
except Exception as e:
|
| 38 |
print(f"❌ Search Error: {e}")
|
| 39 |
+
return "Search failed."
|
|
|
|
| 40 |
|
| 41 |
# -----------------------------------------------------------
|
| 42 |
|
|
|
|
| 49 |
self.agent = None
|
| 50 |
return
|
| 51 |
|
| 52 |
+
# 🔥 改用 DeepSeek R1 (70B)
|
| 53 |
model = OpenAIServerModel(
|
| 54 |
+
model_id="deepseek-r1-distill-llama-70b",
|
| 55 |
api_base="https://api.groq.com/openai/v1",
|
| 56 |
api_key=self.api_key
|
| 57 |
)
|
| 58 |
|
|
|
|
| 59 |
self.agent = CodeAgent(
|
| 60 |
tools=[web_search],
|
| 61 |
model=model,
|
|
|
|
| 68 |
return "Error: GROQ_API_KEY not configured."
|
| 69 |
|
| 70 |
try:
|
| 71 |
+
# 針對 R1 模型的提示詞
|
| 72 |
prompt = f"""
|
| 73 |
+
Answer the question concisely.
|
| 74 |
+
1. Use 'web_search' for facts.
|
| 75 |
+
2. If search fails, guess immediately.
|
|
|
|
| 76 |
|
| 77 |
Question: {question}
|
| 78 |
"""
|
| 79 |
return str(self.agent.run(prompt))
|
| 80 |
except Exception as e:
|
| 81 |
+
return f"Error: {str(e)[:150]}"
|
| 82 |
|
| 83 |
def run_and_submit_all(profile: Optional[gr.OAuthProfile] = None):
|
| 84 |
if profile is None:
|
|
|
|
| 112 |
|
| 113 |
print(f"🚀 [{idx}/{total}] Task: {tid}")
|
| 114 |
|
|
|
|
| 115 |
ans = agent_wrapper(q)
|
| 116 |
|
| 117 |
answers.append({"task_id": tid, "submitted_answer": ans})
|
|
|
|
| 127 |
|
| 128 |
data = res.json()
|
| 129 |
score = data.get('score', 0)
|
| 130 |
+
return f"🎉 Score: {score}%", pd.DataFrame(logs)
|
|
|
|
|
|
|
| 131 |
|
| 132 |
except Exception as e:
|
| 133 |
return f"Submit error: {str(e)}", pd.DataFrame(logs)
|
| 134 |
|
| 135 |
+
with gr.Blocks(title="Final Agent (DeepSeek R1)") as demo:
|
| 136 |
+
gr.Markdown("# 🚀 Final Agent (DeepSeek R1 70B)")
|
| 137 |
with gr.Row():
|
| 138 |
gr.LoginButton()
|
| 139 |
btn = gr.Button("Run Evaluation", variant="primary")
|