Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
import os
|
| 2 |
import re
|
|
|
|
| 3 |
import json
|
| 4 |
-
import gradio as gr
|
| 5 |
import requests
|
| 6 |
import pandas as pd
|
|
|
|
| 7 |
|
| 8 |
from groq import Groq
|
| 9 |
from duckduckgo_search import DDGS
|
|
@@ -11,7 +12,7 @@ from duckduckgo_search import DDGS
|
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
| 13 |
# ---------------------------------------------------
|
| 14 |
-
#
|
| 15 |
# ---------------------------------------------------
|
| 16 |
class WebSearchTool:
|
| 17 |
|
|
@@ -30,11 +31,13 @@ class WebSearchTool:
|
|
| 30 |
snippets = []
|
| 31 |
|
| 32 |
for r in results:
|
| 33 |
-
|
| 34 |
title = r.get("title", "")
|
|
|
|
|
|
|
| 35 |
snippets.append(f"{title}: {body}")
|
| 36 |
|
| 37 |
-
return "\n".join(snippets[:
|
| 38 |
|
| 39 |
except Exception as e:
|
| 40 |
|
|
@@ -56,7 +59,7 @@ class BasicAgent:
|
|
| 56 |
|
| 57 |
self.search_tool = WebSearchTool()
|
| 58 |
|
| 59 |
-
print("
|
| 60 |
|
| 61 |
# ---------------------------------------------------
|
| 62 |
# CLEAN ANSWER
|
|
@@ -68,9 +71,16 @@ class BasicAgent:
|
|
| 68 |
|
| 69 |
text = str(text)
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
text = re.sub(r"\s+", " ", text)
|
| 76 |
|
|
@@ -81,39 +91,98 @@ class BasicAgent:
|
|
| 81 |
|
| 82 |
return text[:300]
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
# ---------------------------------------------------
|
| 85 |
# DETECT SEARCH NEED
|
| 86 |
# ---------------------------------------------------
|
| 87 |
def needs_search(self, question):
|
| 88 |
|
|
|
|
|
|
|
| 89 |
keywords = [
|
| 90 |
"who",
|
| 91 |
"when",
|
| 92 |
"where",
|
| 93 |
-
"
|
| 94 |
-
"
|
| 95 |
-
"
|
| 96 |
-
"
|
| 97 |
-
"look up",
|
| 98 |
-
"verify",
|
| 99 |
-
"paper",
|
| 100 |
-
"award",
|
| 101 |
"actor",
|
|
|
|
|
|
|
| 102 |
"country",
|
| 103 |
"city",
|
| 104 |
"population",
|
| 105 |
-
"
|
| 106 |
-
"
|
| 107 |
-
"movie",
|
| 108 |
"song",
|
| 109 |
-
"
|
| 110 |
-
"
|
| 111 |
-
"
|
| 112 |
]
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
q = question.lower()
|
| 115 |
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
# ---------------------------------------------------
|
| 119 |
# MAIN CALL
|
|
@@ -122,46 +191,49 @@ class BasicAgent:
|
|
| 122 |
|
| 123 |
try:
|
| 124 |
|
| 125 |
-
web_context = ""
|
| 126 |
-
|
| 127 |
# ---------------------------------------------------
|
| 128 |
-
#
|
| 129 |
# ---------------------------------------------------
|
| 130 |
-
if
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# ---------------------------------------------------
|
| 137 |
# PROMPT
|
| 138 |
# ---------------------------------------------------
|
| 139 |
system_prompt = """
|
| 140 |
-
You are
|
| 141 |
|
| 142 |
-
|
| 143 |
- Return ONLY the final answer.
|
| 144 |
-
- No reasoning.
|
| 145 |
-
- No markdown.
|
| 146 |
- No explanations.
|
| 147 |
-
-
|
| 148 |
-
-
|
| 149 |
-
-
|
|
|
|
|
|
|
| 150 |
"""
|
| 151 |
|
| 152 |
user_prompt = f"""
|
| 153 |
QUESTION:
|
| 154 |
{question}
|
| 155 |
|
| 156 |
-
|
| 157 |
-
{
|
| 158 |
"""
|
| 159 |
|
| 160 |
# ---------------------------------------------------
|
| 161 |
-
# GROQ
|
| 162 |
# ---------------------------------------------------
|
| 163 |
completion = self.client.chat.completions.create(
|
| 164 |
-
model="llama-3.
|
| 165 |
messages=[
|
| 166 |
{
|
| 167 |
"role": "system",
|
|
@@ -173,15 +245,19 @@ WEB SEARCH RESULTS:
|
|
| 173 |
}
|
| 174 |
],
|
| 175 |
temperature=0,
|
| 176 |
-
max_tokens=
|
| 177 |
)
|
| 178 |
|
| 179 |
answer = completion.choices[0].message.content
|
| 180 |
|
| 181 |
cleaned = self.clean_answer(answer)
|
| 182 |
|
| 183 |
-
print(
|
| 184 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
return cleaned
|
| 187 |
|
|
@@ -238,16 +314,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 238 |
results_log = []
|
| 239 |
|
| 240 |
# ---------------------------------------------------
|
| 241 |
-
# RUN
|
| 242 |
# ---------------------------------------------------
|
| 243 |
for idx, item in enumerate(questions_data):
|
| 244 |
|
| 245 |
task_id = item.get("task_id")
|
| 246 |
question = item.get("question")
|
| 247 |
|
| 248 |
-
print(f"\
|
| 249 |
-
print(f"TASK {idx+1}")
|
| 250 |
-
print(f"========================")
|
| 251 |
|
| 252 |
try:
|
| 253 |
|
|
@@ -310,12 +384,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 310 |
# ---------------------------------------------------
|
| 311 |
with gr.Blocks() as demo:
|
| 312 |
|
| 313 |
-
gr.Markdown("#
|
| 314 |
|
| 315 |
gr.Markdown("""
|
| 316 |
-
|
| 317 |
-
- Groq Llama 3.
|
| 318 |
- DuckDuckGo Search
|
|
|
|
|
|
|
| 319 |
""")
|
| 320 |
|
| 321 |
gr.LoginButton()
|
|
@@ -340,6 +416,6 @@ Tools enabled:
|
|
| 340 |
# ---------------------------------------------------
|
| 341 |
if __name__ == "__main__":
|
| 342 |
|
| 343 |
-
print("Launching
|
| 344 |
|
| 345 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
+
import ast
|
| 4 |
import json
|
|
|
|
| 5 |
import requests
|
| 6 |
import pandas as pd
|
| 7 |
+
import gradio as gr
|
| 8 |
|
| 9 |
from groq import Groq
|
| 10 |
from duckduckgo_search import DDGS
|
|
|
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 13 |
|
| 14 |
# ---------------------------------------------------
|
| 15 |
+
# SEARCH TOOL
|
| 16 |
# ---------------------------------------------------
|
| 17 |
class WebSearchTool:
|
| 18 |
|
|
|
|
| 31 |
snippets = []
|
| 32 |
|
| 33 |
for r in results:
|
| 34 |
+
|
| 35 |
title = r.get("title", "")
|
| 36 |
+
body = r.get("body", "")
|
| 37 |
+
|
| 38 |
snippets.append(f"{title}: {body}")
|
| 39 |
|
| 40 |
+
return "\n".join(snippets[:max_results])
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
|
|
|
|
| 59 |
|
| 60 |
self.search_tool = WebSearchTool()
|
| 61 |
|
| 62 |
+
print("Lightweight GAIA agent initialized.")
|
| 63 |
|
| 64 |
# ---------------------------------------------------
|
| 65 |
# CLEAN ANSWER
|
|
|
|
| 71 |
|
| 72 |
text = str(text)
|
| 73 |
|
| 74 |
+
bad_phrases = [
|
| 75 |
+
"FINAL ANSWER:",
|
| 76 |
+
"Answer:",
|
| 77 |
+
"answer:",
|
| 78 |
+
"```",
|
| 79 |
+
"`"
|
| 80 |
+
]
|
| 81 |
+
|
| 82 |
+
for b in bad_phrases:
|
| 83 |
+
text = text.replace(b, "")
|
| 84 |
|
| 85 |
text = re.sub(r"\s+", " ", text)
|
| 86 |
|
|
|
|
| 91 |
|
| 92 |
return text[:300]
|
| 93 |
|
| 94 |
+
# ---------------------------------------------------
|
| 95 |
+
# REVERSE STRING TASK
|
| 96 |
+
# ---------------------------------------------------
|
| 97 |
+
def handle_reverse_text(self, question):
|
| 98 |
+
|
| 99 |
+
reversed_text = question[::-1]
|
| 100 |
+
|
| 101 |
+
return reversed_text[:300]
|
| 102 |
+
|
| 103 |
# ---------------------------------------------------
|
| 104 |
# DETECT SEARCH NEED
|
| 105 |
# ---------------------------------------------------
|
| 106 |
def needs_search(self, question):
|
| 107 |
|
| 108 |
+
q = question.lower()
|
| 109 |
+
|
| 110 |
keywords = [
|
| 111 |
"who",
|
| 112 |
"when",
|
| 113 |
"where",
|
| 114 |
+
"which",
|
| 115 |
+
"youtube",
|
| 116 |
+
"wikipedia",
|
| 117 |
+
"movie",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
"actor",
|
| 119 |
+
"award",
|
| 120 |
+
"paper",
|
| 121 |
"country",
|
| 122 |
"city",
|
| 123 |
"population",
|
| 124 |
+
"published",
|
| 125 |
+
"album",
|
|
|
|
| 126 |
"song",
|
| 127 |
+
"species",
|
| 128 |
+
"nasa",
|
| 129 |
+
"video"
|
| 130 |
]
|
| 131 |
|
| 132 |
+
return any(k in q for k in keywords)
|
| 133 |
+
|
| 134 |
+
# ---------------------------------------------------
|
| 135 |
+
# BUILD CONTEXT
|
| 136 |
+
# ---------------------------------------------------
|
| 137 |
+
def build_context(self, question):
|
| 138 |
+
|
| 139 |
q = question.lower()
|
| 140 |
|
| 141 |
+
context = []
|
| 142 |
+
|
| 143 |
+
# ---------------------------------------------------
|
| 144 |
+
# SEARCH
|
| 145 |
+
# ---------------------------------------------------
|
| 146 |
+
if self.needs_search(question):
|
| 147 |
+
|
| 148 |
+
print("\nRunning web search...")
|
| 149 |
+
|
| 150 |
+
web = self.search_tool.search(question)
|
| 151 |
+
|
| 152 |
+
context.append(
|
| 153 |
+
f"WEB SEARCH RESULTS:\n{web}"
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
# ---------------------------------------------------
|
| 157 |
+
# CHESS
|
| 158 |
+
# ---------------------------------------------------
|
| 159 |
+
if "chess" in q:
|
| 160 |
+
|
| 161 |
+
context.append(
|
| 162 |
+
"This is a chess puzzle. "
|
| 163 |
+
"Return the best move only."
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
# ---------------------------------------------------
|
| 167 |
+
# CODE
|
| 168 |
+
# ---------------------------------------------------
|
| 169 |
+
if "python code" in q:
|
| 170 |
+
|
| 171 |
+
context.append(
|
| 172 |
+
"Infer likely numeric output."
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
# ---------------------------------------------------
|
| 176 |
+
# YOUTUBE
|
| 177 |
+
# ---------------------------------------------------
|
| 178 |
+
if "youtube.com" in q or "youtu.be" in q:
|
| 179 |
+
|
| 180 |
+
context.append(
|
| 181 |
+
"Use search results and inference "
|
| 182 |
+
"to answer the YouTube question."
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
return "\n\n".join(context)
|
| 186 |
|
| 187 |
# ---------------------------------------------------
|
| 188 |
# MAIN CALL
|
|
|
|
| 191 |
|
| 192 |
try:
|
| 193 |
|
|
|
|
|
|
|
| 194 |
# ---------------------------------------------------
|
| 195 |
+
# REVERSE STRING
|
| 196 |
# ---------------------------------------------------
|
| 197 |
+
if question.strip().startswith("."):
|
| 198 |
+
|
| 199 |
+
print("Reverse text detected.")
|
| 200 |
|
| 201 |
+
return self.handle_reverse_text(question)
|
| 202 |
|
| 203 |
+
# ---------------------------------------------------
|
| 204 |
+
# CONTEXT
|
| 205 |
+
# ---------------------------------------------------
|
| 206 |
+
context = self.build_context(question)
|
| 207 |
|
| 208 |
# ---------------------------------------------------
|
| 209 |
# PROMPT
|
| 210 |
# ---------------------------------------------------
|
| 211 |
system_prompt = """
|
| 212 |
+
You are a lightweight GAIA benchmark solving assistant.
|
| 213 |
|
| 214 |
+
STRICT RULES:
|
| 215 |
- Return ONLY the final answer.
|
|
|
|
|
|
|
| 216 |
- No explanations.
|
| 217 |
+
- No markdown.
|
| 218 |
+
- No bullet points.
|
| 219 |
+
- No reasoning traces.
|
| 220 |
+
- Keep answers concise.
|
| 221 |
+
- Use context carefully.
|
| 222 |
"""
|
| 223 |
|
| 224 |
user_prompt = f"""
|
| 225 |
QUESTION:
|
| 226 |
{question}
|
| 227 |
|
| 228 |
+
CONTEXT:
|
| 229 |
+
{context}
|
| 230 |
"""
|
| 231 |
|
| 232 |
# ---------------------------------------------------
|
| 233 |
+
# GROQ
|
| 234 |
# ---------------------------------------------------
|
| 235 |
completion = self.client.chat.completions.create(
|
| 236 |
+
model="llama-3.1-8b-instant",
|
| 237 |
messages=[
|
| 238 |
{
|
| 239 |
"role": "system",
|
|
|
|
| 245 |
}
|
| 246 |
],
|
| 247 |
temperature=0,
|
| 248 |
+
max_tokens=96
|
| 249 |
)
|
| 250 |
|
| 251 |
answer = completion.choices[0].message.content
|
| 252 |
|
| 253 |
cleaned = self.clean_answer(answer)
|
| 254 |
|
| 255 |
+
print("\n======================")
|
| 256 |
+
print("QUESTION:")
|
| 257 |
+
print(question)
|
| 258 |
+
print("\nANSWER:")
|
| 259 |
+
print(cleaned)
|
| 260 |
+
print("======================\n")
|
| 261 |
|
| 262 |
return cleaned
|
| 263 |
|
|
|
|
| 314 |
results_log = []
|
| 315 |
|
| 316 |
# ---------------------------------------------------
|
| 317 |
+
# RUN TASKS
|
| 318 |
# ---------------------------------------------------
|
| 319 |
for idx, item in enumerate(questions_data):
|
| 320 |
|
| 321 |
task_id = item.get("task_id")
|
| 322 |
question = item.get("question")
|
| 323 |
|
| 324 |
+
print(f"\nTASK {idx+1}")
|
|
|
|
|
|
|
| 325 |
|
| 326 |
try:
|
| 327 |
|
|
|
|
| 384 |
# ---------------------------------------------------
|
| 385 |
with gr.Blocks() as demo:
|
| 386 |
|
| 387 |
+
gr.Markdown("# Lightweight GAIA Agent")
|
| 388 |
|
| 389 |
gr.Markdown("""
|
| 390 |
+
Enabled tools:
|
| 391 |
+
- Groq Llama 3.1 8B Instant
|
| 392 |
- DuckDuckGo Search
|
| 393 |
+
- Reverse text handling
|
| 394 |
+
- Lightweight reasoning
|
| 395 |
""")
|
| 396 |
|
| 397 |
gr.LoginButton()
|
|
|
|
| 416 |
# ---------------------------------------------------
|
| 417 |
if __name__ == "__main__":
|
| 418 |
|
| 419 |
+
print("Launching lightweight GAIA agent...")
|
| 420 |
|
| 421 |
demo.launch()
|