Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,6 +24,7 @@ def web_search(query: str) -> str:
|
|
| 24 |
except Exception as e:
|
| 25 |
return f"Search error: {e}"
|
| 26 |
|
|
|
|
| 27 |
def wikipedia_search(query: str) -> str:
|
| 28 |
try:
|
| 29 |
search_url = "https://en.wikipedia.org/w/api.php"
|
|
@@ -48,6 +49,7 @@ def wikipedia_search(query: str) -> str:
|
|
| 48 |
except Exception as e:
|
| 49 |
return f"Wikipedia error: {e}"
|
| 50 |
|
|
|
|
| 51 |
def run_python(code: str) -> str:
|
| 52 |
import sys
|
| 53 |
from io import StringIO
|
|
@@ -63,37 +65,47 @@ def run_python(code: str) -> str:
|
|
| 63 |
finally:
|
| 64 |
sys.stdout = old_stdout
|
| 65 |
|
|
|
|
| 66 |
class SmartAgent:
|
| 67 |
def __init__(self):
|
| 68 |
-
self.api_key = os.getenv("
|
| 69 |
if not self.api_key:
|
| 70 |
-
raise ValueError("
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
| 74 |
|
| 75 |
def call_llm(self, prompt: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
payload = {
|
| 77 |
-
"
|
| 78 |
-
"
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
}
|
| 82 |
}
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
try:
|
| 86 |
-
response = requests.post(self.api_url, json=payload, timeout=60)
|
| 87 |
response.raise_for_status()
|
| 88 |
data = response.json()
|
| 89 |
-
return data["
|
| 90 |
except requests.exceptions.HTTPError as e:
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
| 94 |
else:
|
| 95 |
raise e
|
| 96 |
-
|
|
|
|
| 97 |
|
| 98 |
def __call__(self, question: str) -> str:
|
| 99 |
print(f"\nQuestion: {question[:100]}...")
|
|
@@ -118,8 +130,8 @@ Rules for the answer:
|
|
| 118 |
conversation = f"{system}\n\nQuestion: {question}"
|
| 119 |
|
| 120 |
for iteration in range(6):
|
| 121 |
-
#
|
| 122 |
-
time.sleep(
|
| 123 |
|
| 124 |
response = self.call_llm(conversation)
|
| 125 |
print(f" LLM [{iteration}]: {response[:200]}")
|
|
@@ -139,7 +151,6 @@ Rules for the answer:
|
|
| 139 |
if not python_match:
|
| 140 |
python_match = re.search(r'PYTHON:\s*(.+?)(?:\nSEARCH|\nWIKIPEDIA|\nANSWER|$)', response, re.DOTALL)
|
| 141 |
|
| 142 |
-
# Execution logic changed to 'elif' to avoid overlapping tool executions
|
| 143 |
if search_match:
|
| 144 |
query = search_match.group(1).strip()
|
| 145 |
print(f" Tool: web_search({query})")
|
|
@@ -212,8 +223,8 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 212 |
print(f"Error on task {task_id}: {e}")
|
| 213 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
|
| 214 |
|
| 215 |
-
# Pausing between questions to ensure
|
| 216 |
-
time.sleep(
|
| 217 |
|
| 218 |
if not answers_payload:
|
| 219 |
return "Agent did not produce any answers.", pd.DataFrame(results_log)
|
|
@@ -247,10 +258,10 @@ with gr.Blocks() as demo:
|
|
| 247 |
gr.Markdown("# 🤖 Smart Agent — GAIA Benchmark Runner")
|
| 248 |
gr.Markdown(
|
| 249 |
"""
|
| 250 |
-
**Powered by
|
| 251 |
|
| 252 |
**Instructions:**
|
| 253 |
-
1. Make sure `
|
| 254 |
2. Log in with your Hugging Face account below
|
| 255 |
3. Click **Run Evaluation & Submit All Answers**
|
| 256 |
"""
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
return f"Search error: {e}"
|
| 26 |
|
| 27 |
+
|
| 28 |
def wikipedia_search(query: str) -> str:
|
| 29 |
try:
|
| 30 |
search_url = "https://en.wikipedia.org/w/api.php"
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
return f"Wikipedia error: {e}"
|
| 51 |
|
| 52 |
+
|
| 53 |
def run_python(code: str) -> str:
|
| 54 |
import sys
|
| 55 |
from io import StringIO
|
|
|
|
| 65 |
finally:
|
| 66 |
sys.stdout = old_stdout
|
| 67 |
|
| 68 |
+
|
| 69 |
class SmartAgent:
|
| 70 |
def __init__(self):
|
| 71 |
+
self.api_key = os.getenv("GROQ_API_KEY")
|
| 72 |
if not self.api_key:
|
| 73 |
+
raise ValueError("GROQ_API_KEY environment variable not set! Please add it to your Space secrets.")
|
| 74 |
+
|
| 75 |
+
# Groq's OpenAI-compatible endpoint
|
| 76 |
+
self.api_url = "https://api.groq.com/openai/v1/chat/completions"
|
| 77 |
+
self.model = "llama-3.3-70b-versatile"
|
| 78 |
+
print(f"SmartAgent initialized with Groq ({self.model})")
|
| 79 |
|
| 80 |
def call_llm(self, prompt: str) -> str:
|
| 81 |
+
headers = {
|
| 82 |
+
"Authorization": f"Bearer {self.api_key}",
|
| 83 |
+
"Content-Type": "application/json"
|
| 84 |
+
}
|
| 85 |
payload = {
|
| 86 |
+
"model": self.model,
|
| 87 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 88 |
+
"temperature": 0.1,
|
| 89 |
+
"max_tokens": 1024
|
|
|
|
| 90 |
}
|
| 91 |
+
|
| 92 |
+
# Exponential backoff retry mechanism
|
| 93 |
+
wait_times = [15, 30, 60]
|
| 94 |
+
for attempt, wait_time in enumerate(wait_times):
|
| 95 |
try:
|
| 96 |
+
response = requests.post(self.api_url, headers=headers, json=payload, timeout=60)
|
| 97 |
response.raise_for_status()
|
| 98 |
data = response.json()
|
| 99 |
+
return data["choices"][0]["message"]["content"].strip()
|
| 100 |
except requests.exceptions.HTTPError as e:
|
| 101 |
+
status = response.status_code
|
| 102 |
+
if status == 429 or status == 503:
|
| 103 |
+
print(f"Groq API Error ({status})! Waiting {wait_time} seconds... (Attempt {attempt+1}/3)")
|
| 104 |
+
time.sleep(wait_time)
|
| 105 |
else:
|
| 106 |
raise e
|
| 107 |
+
|
| 108 |
+
raise Exception("Failed to call LLM after 3 attempts due to API limits or server errors.")
|
| 109 |
|
| 110 |
def __call__(self, question: str) -> str:
|
| 111 |
print(f"\nQuestion: {question[:100]}...")
|
|
|
|
| 130 |
conversation = f"{system}\n\nQuestion: {question}"
|
| 131 |
|
| 132 |
for iteration in range(6):
|
| 133 |
+
# Groq is fast, but we pause slightly to respect free tier limits
|
| 134 |
+
time.sleep(2.5)
|
| 135 |
|
| 136 |
response = self.call_llm(conversation)
|
| 137 |
print(f" LLM [{iteration}]: {response[:200]}")
|
|
|
|
| 151 |
if not python_match:
|
| 152 |
python_match = re.search(r'PYTHON:\s*(.+?)(?:\nSEARCH|\nWIKIPEDIA|\nANSWER|$)', response, re.DOTALL)
|
| 153 |
|
|
|
|
| 154 |
if search_match:
|
| 155 |
query = search_match.group(1).strip()
|
| 156 |
print(f" Tool: web_search({query})")
|
|
|
|
| 223 |
print(f"Error on task {task_id}: {e}")
|
| 224 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
|
| 225 |
|
| 226 |
+
# Pausing between questions to ensure safety against rate limits
|
| 227 |
+
time.sleep(3)
|
| 228 |
|
| 229 |
if not answers_payload:
|
| 230 |
return "Agent did not produce any answers.", pd.DataFrame(results_log)
|
|
|
|
| 258 |
gr.Markdown("# 🤖 Smart Agent — GAIA Benchmark Runner")
|
| 259 |
gr.Markdown(
|
| 260 |
"""
|
| 261 |
+
**Powered by Groq (Llama 3.3 70B)**
|
| 262 |
|
| 263 |
**Instructions:**
|
| 264 |
+
1. Make sure `GROQ_API_KEY` is set in your Space secrets
|
| 265 |
2. Log in with your Hugging Face account below
|
| 266 |
3. Click **Run Evaluation & Submit All Answers**
|
| 267 |
"""
|