Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,11 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
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,27 +17,31 @@ except ImportError:
|
|
| 16 |
@tool
|
| 17 |
def web_search(query: str) -> str:
|
| 18 |
"""
|
| 19 |
-
Performs a web search using
|
| 20 |
Args:
|
| 21 |
query: The search query string.
|
| 22 |
"""
|
| 23 |
-
print(f"🕵️ [Debug] Searching: {query}")
|
| 24 |
try:
|
| 25 |
-
# 使用 backend='
|
|
|
|
| 26 |
with DDGS() as ddgs:
|
| 27 |
-
results = ddgs.text(query, max_results=
|
| 28 |
|
| 29 |
if not results:
|
| 30 |
-
return "No results found.
|
| 31 |
|
| 32 |
formatted = []
|
| 33 |
for r in results:
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 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,10 +54,9 @@ class GroqAgent:
|
|
| 49 |
self.agent = None
|
| 50 |
return
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
# 它的推理能力比 Llama 3.3 強,非常適合解決這種邏輯題
|
| 54 |
model = OpenAIServerModel(
|
| 55 |
-
model_id="
|
| 56 |
api_base="https://api.groq.com/openai/v1",
|
| 57 |
api_key=self.api_key
|
| 58 |
)
|
|
@@ -69,12 +73,12 @@ class GroqAgent:
|
|
| 69 |
return "Error: GROQ_API_KEY not configured."
|
| 70 |
|
| 71 |
try:
|
| 72 |
-
#
|
| 73 |
prompt = f"""
|
| 74 |
-
Answer the question concisely.
|
| 75 |
1. Use 'web_search' to find facts.
|
| 76 |
-
2. If search
|
| 77 |
-
3.
|
| 78 |
|
| 79 |
Question: {question}
|
| 80 |
"""
|
|
@@ -134,8 +138,8 @@ def run_and_submit_all(profile: Optional[gr.OAuthProfile] = None):
|
|
| 134 |
except Exception as e:
|
| 135 |
return f"Submit error: {str(e)}", pd.DataFrame(logs)
|
| 136 |
|
| 137 |
-
with gr.Blocks(title="Final Agent (
|
| 138 |
-
gr.Markdown("# 🚀 Final Agent (
|
| 139 |
with gr.Row():
|
| 140 |
gr.LoginButton()
|
| 141 |
btn = gr.Button("Run Evaluation", variant="primary")
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
+
import time
|
| 6 |
from typing import Optional
|
| 7 |
from smolagents import CodeAgent, OpenAIServerModel, tool
|
| 8 |
|
| 9 |
+
# --- 搜尋工具 (Lite 後門版) ---
|
| 10 |
try:
|
| 11 |
from duckduckgo_search import DDGS
|
| 12 |
except ImportError:
|
|
|
|
| 17 |
@tool
|
| 18 |
def web_search(query: str) -> str:
|
| 19 |
"""
|
| 20 |
+
Performs a web search using the 'lite' backend to bypass rate limits.
|
| 21 |
Args:
|
| 22 |
query: The search query string.
|
| 23 |
"""
|
| 24 |
+
print(f"🕵️ [Debug] Searching (Lite): {query}")
|
| 25 |
try:
|
| 26 |
+
# 關鍵修改:使用 backend='lite'
|
| 27 |
+
# 這是最原始的 HTML 介面,極少被封鎖
|
| 28 |
with DDGS() as ddgs:
|
| 29 |
+
results = ddgs.text(query, max_results=3, backend="lite")
|
| 30 |
|
| 31 |
if not results:
|
| 32 |
+
return "No results found."
|
| 33 |
|
| 34 |
formatted = []
|
| 35 |
for r in results:
|
| 36 |
+
title = r.get('title', 'No Title')
|
| 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 |
+
return "Search failed. Ignore this and guess based on your knowledge."
|
| 45 |
|
| 46 |
# -----------------------------------------------------------
|
| 47 |
|
|
|
|
| 54 |
self.agent = None
|
| 55 |
return
|
| 56 |
|
| 57 |
+
# 堅持使用 Llama 3.3 (目前唯一活著且聰明的模型)
|
|
|
|
| 58 |
model = OpenAIServerModel(
|
| 59 |
+
model_id="llama-3.3-70b-versatile",
|
| 60 |
api_base="https://api.groq.com/openai/v1",
|
| 61 |
api_key=self.api_key
|
| 62 |
)
|
|
|
|
| 73 |
return "Error: GROQ_API_KEY not configured."
|
| 74 |
|
| 75 |
try:
|
| 76 |
+
# 提示詞:強調「如果 Lite 搜尋失敗就直接回答」
|
| 77 |
prompt = f"""
|
| 78 |
+
You are a helpful assistant. Answer the question concisely.
|
| 79 |
1. Use 'web_search' to find facts.
|
| 80 |
+
2. If search returns "No results" or fails, DO NOT RETRY. Make your BEST GUESS from your training data immediately.
|
| 81 |
+
3. Answer directly.
|
| 82 |
|
| 83 |
Question: {question}
|
| 84 |
"""
|
|
|
|
| 138 |
except Exception as e:
|
| 139 |
return f"Submit error: {str(e)}", pd.DataFrame(logs)
|
| 140 |
|
| 141 |
+
with gr.Blocks(title="Final Agent (Lite Backend)") as demo:
|
| 142 |
+
gr.Markdown("# 🚀 Final Agent (Lite Mode)")
|
| 143 |
with gr.Row():
|
| 144 |
gr.LoginButton()
|
| 145 |
btn = gr.Button("Run Evaluation", variant="primary")
|