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,21 +16,20 @@ except ImportError:
|
|
| 16 |
@tool
|
| 17 |
def web_search(query: str) -> str:
|
| 18 |
"""
|
| 19 |
-
|
| 20 |
Args:
|
| 21 |
query: The search query string.
|
| 22 |
"""
|
| 23 |
-
print(f"🕵️ [Debug] Searching
|
| 24 |
try:
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
results = DDGS(timeout=15).text(query, max_results=3)
|
| 28 |
if not results:
|
| 29 |
-
return "No
|
| 30 |
return str(results)
|
| 31 |
except Exception as e:
|
| 32 |
-
print(f"❌
|
| 33 |
-
return
|
| 34 |
|
| 35 |
# -----------------------------------------------------------
|
| 36 |
|
|
@@ -43,10 +42,9 @@ class GroqAgent:
|
|
| 43 |
self.agent = None
|
| 44 |
return
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
# 請改成下面這個:
|
| 48 |
model = OpenAIServerModel(
|
| 49 |
-
model_id="
|
| 50 |
api_base="https://api.groq.com/openai/v1",
|
| 51 |
api_key=self.api_key
|
| 52 |
)
|
|
@@ -54,7 +52,7 @@ class GroqAgent:
|
|
| 54 |
self.agent = CodeAgent(
|
| 55 |
tools=[web_search],
|
| 56 |
model=model,
|
| 57 |
-
max_steps=
|
| 58 |
verbosity_level=1
|
| 59 |
)
|
| 60 |
|
|
@@ -63,12 +61,11 @@ class GroqAgent:
|
|
| 63 |
return "Error: GROQ_API_KEY not configured."
|
| 64 |
|
| 65 |
try:
|
| 66 |
-
# 簡化 Prompt
|
| 67 |
prompt = f"""
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
3. Answer directly.
|
| 72 |
|
| 73 |
Question: {question}
|
| 74 |
"""
|
|
@@ -106,16 +103,16 @@ def run_and_submit_all(profile: Optional[gr.OAuthProfile] = None):
|
|
| 106 |
q = item.get("question")
|
| 107 |
tid = item.get("task_id")
|
| 108 |
|
| 109 |
-
print(f"🚀 [{idx}/{total}]
|
| 110 |
|
| 111 |
-
#
|
| 112 |
ans = agent_wrapper(q)
|
| 113 |
|
| 114 |
answers.append({"task_id": tid, "submitted_answer": ans})
|
| 115 |
-
logs.append({"Task": tid, "
|
| 116 |
|
| 117 |
try:
|
| 118 |
-
print("Submitting
|
| 119 |
res = requests.post(f"{api_url}/submit", json={
|
| 120 |
"username": username,
|
| 121 |
"agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
|
|
@@ -124,20 +121,21 @@ def run_and_submit_all(profile: Optional[gr.OAuthProfile] = None):
|
|
| 124 |
|
| 125 |
data = res.json()
|
| 126 |
score = data.get('score', 0)
|
| 127 |
-
|
|
|
|
|
|
|
| 128 |
return msg, pd.DataFrame(logs)
|
| 129 |
|
| 130 |
except Exception as e:
|
| 131 |
return f"Submit error: {str(e)}", pd.DataFrame(logs)
|
| 132 |
|
| 133 |
-
with gr.Blocks(title="Final Agent (
|
| 134 |
-
gr.Markdown("# 🚀 Final Agent (
|
| 135 |
-
gr.Markdown("Has timeout protection to prevent hanging.")
|
| 136 |
with gr.Row():
|
| 137 |
gr.LoginButton()
|
| 138 |
btn = gr.Button("Run Evaluation", variant="primary")
|
| 139 |
out = gr.Textbox(label="Status")
|
| 140 |
-
tab = gr.DataFrame(label="
|
| 141 |
btn.click(run_and_submit_all, outputs=[out, tab])
|
| 142 |
|
| 143 |
if __name__ == "__main__":
|
|
|
|
| 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 |
+
Search the web for information.
|
| 20 |
Args:
|
| 21 |
query: The search query string.
|
| 22 |
"""
|
| 23 |
+
print(f"🕵️ [Debug] Searching: {query}")
|
| 24 |
try:
|
| 25 |
+
# 設定 10 秒逾時,只抓 2 筆結果,降低負擔
|
| 26 |
+
results = DDGS(timeout=10).text(query, max_results=2)
|
|
|
|
| 27 |
if not results:
|
| 28 |
+
return "No results."
|
| 29 |
return str(results)
|
| 30 |
except Exception as e:
|
| 31 |
+
print(f"❌ Search Error: {e}")
|
| 32 |
+
return "Search failed."
|
| 33 |
|
| 34 |
# -----------------------------------------------------------
|
| 35 |
|
|
|
|
| 42 |
self.agent = None
|
| 43 |
return
|
| 44 |
|
| 45 |
+
# 修正:換回目前 Groq 支援的主力模型
|
|
|
|
| 46 |
model = OpenAIServerModel(
|
| 47 |
+
model_id="llama-3.3-70b-versatile",
|
| 48 |
api_base="https://api.groq.com/openai/v1",
|
| 49 |
api_key=self.api_key
|
| 50 |
)
|
|
|
|
| 52 |
self.agent = CodeAgent(
|
| 53 |
tools=[web_search],
|
| 54 |
model=model,
|
| 55 |
+
max_steps=3, # 減少步數,避免想太久卡住
|
| 56 |
verbosity_level=1
|
| 57 |
)
|
| 58 |
|
|
|
|
| 61 |
return "Error: GROQ_API_KEY not configured."
|
| 62 |
|
| 63 |
try:
|
| 64 |
+
# 極簡化 Prompt,讓模型反應更快
|
| 65 |
prompt = f"""
|
| 66 |
+
Task: Answer the question concisely.
|
| 67 |
+
Tool: Use 'web_search' only for specific facts (names, dates).
|
| 68 |
+
Fallback: If search fails, guess.
|
|
|
|
| 69 |
|
| 70 |
Question: {question}
|
| 71 |
"""
|
|
|
|
| 103 |
q = item.get("question")
|
| 104 |
tid = item.get("task_id")
|
| 105 |
|
| 106 |
+
print(f"🚀 [{idx}/{total}] Task: {tid}")
|
| 107 |
|
| 108 |
+
# 執行 Agent
|
| 109 |
ans = agent_wrapper(q)
|
| 110 |
|
| 111 |
answers.append({"task_id": tid, "submitted_answer": ans})
|
| 112 |
+
logs.append({"Task": tid, "Answer": ans[:50] + "..."})
|
| 113 |
|
| 114 |
try:
|
| 115 |
+
print("Submitting...")
|
| 116 |
res = requests.post(f"{api_url}/submit", json={
|
| 117 |
"username": username,
|
| 118 |
"agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
|
|
|
|
| 121 |
|
| 122 |
data = res.json()
|
| 123 |
score = data.get('score', 0)
|
| 124 |
+
|
| 125 |
+
# 只要有一題對就是成功,通常文字搜尋題會對,圖片題會錯
|
| 126 |
+
msg = f"🎉 Final Score: {score}%"
|
| 127 |
return msg, pd.DataFrame(logs)
|
| 128 |
|
| 129 |
except Exception as e:
|
| 130 |
return f"Submit error: {str(e)}", pd.DataFrame(logs)
|
| 131 |
|
| 132 |
+
with gr.Blocks(title="Final Agent (Fixed Model)") as demo:
|
| 133 |
+
gr.Markdown("# 🚀 Final Agent (Llama 3.3)")
|
|
|
|
| 134 |
with gr.Row():
|
| 135 |
gr.LoginButton()
|
| 136 |
btn = gr.Button("Run Evaluation", variant="primary")
|
| 137 |
out = gr.Textbox(label="Status")
|
| 138 |
+
tab = gr.DataFrame(label="Logs")
|
| 139 |
btn.click(run_and_submit_all, outputs=[out, tab])
|
| 140 |
|
| 141 |
if __name__ == "__main__":
|