Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,48 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
from groq import Groq
|
|
|
|
| 7 |
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# ---------------------------------------------------
|
| 11 |
# AGENT
|
| 12 |
# ---------------------------------------------------
|
|
@@ -18,8 +54,13 @@ class BasicAgent:
|
|
| 18 |
api_key=os.getenv("GROQ_API_KEY")
|
| 19 |
)
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
def clean_answer(self, text):
|
| 24 |
|
| 25 |
if text is None:
|
|
@@ -31,55 +72,128 @@ class BasicAgent:
|
|
| 31 |
text = text.replace("FINAL ANSWER:", "")
|
| 32 |
text = text.replace("Answer:", "")
|
| 33 |
|
|
|
|
|
|
|
| 34 |
text = text.strip()
|
| 35 |
|
|
|
|
| 36 |
text = text.split("\n")[0]
|
| 37 |
|
| 38 |
-
return text[:
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
def __call__(self, question: str) -> str:
|
| 41 |
|
| 42 |
try:
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
completion = self.client.chat.completions.create(
|
| 45 |
-
model="llama-3.
|
| 46 |
messages=[
|
| 47 |
{
|
| 48 |
"role": "system",
|
| 49 |
-
"content":
|
| 50 |
-
"You are solving benchmark questions. "
|
| 51 |
-
"Return ONLY the final answer. "
|
| 52 |
-
"No explanations. "
|
| 53 |
-
"Be concise."
|
| 54 |
-
)
|
| 55 |
},
|
| 56 |
{
|
| 57 |
"role": "user",
|
| 58 |
-
"content":
|
| 59 |
}
|
| 60 |
],
|
| 61 |
temperature=0,
|
| 62 |
-
max_tokens=
|
| 63 |
)
|
| 64 |
|
| 65 |
answer = completion.choices[0].message.content
|
| 66 |
|
| 67 |
cleaned = self.clean_answer(answer)
|
| 68 |
|
| 69 |
-
print(f"\
|
| 70 |
-
print(f"
|
| 71 |
|
| 72 |
return cleaned
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
|
| 76 |
-
print(f"
|
| 77 |
|
| 78 |
return ""
|
| 79 |
|
| 80 |
|
| 81 |
# ---------------------------------------------------
|
| 82 |
-
# MAIN
|
| 83 |
# ---------------------------------------------------
|
| 84 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 85 |
|
|
@@ -91,7 +205,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 91 |
questions_url = f"{DEFAULT_API_URL}/questions"
|
| 92 |
submit_url = f"{DEFAULT_API_URL}/submit"
|
| 93 |
|
| 94 |
-
#
|
|
|
|
|
|
|
| 95 |
try:
|
| 96 |
|
| 97 |
agent = BasicAgent()
|
|
@@ -100,7 +216,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 100 |
|
| 101 |
return f"Agent init error: {e}", None
|
| 102 |
|
| 103 |
-
#
|
|
|
|
|
|
|
| 104 |
try:
|
| 105 |
|
| 106 |
response = requests.get(
|
|
@@ -108,6 +226,8 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 108 |
timeout=30
|
| 109 |
)
|
| 110 |
|
|
|
|
|
|
|
| 111 |
questions_data = response.json()
|
| 112 |
|
| 113 |
except Exception as e:
|
|
@@ -117,12 +237,18 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 117 |
answers_payload = []
|
| 118 |
results_log = []
|
| 119 |
|
| 120 |
-
#
|
| 121 |
-
|
|
|
|
|
|
|
| 122 |
|
| 123 |
task_id = item.get("task_id")
|
| 124 |
question = item.get("question")
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
try:
|
| 127 |
|
| 128 |
answer = agent(question)
|
|
@@ -146,7 +272,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 146 |
"Answer": f"ERROR: {e}"
|
| 147 |
})
|
| 148 |
|
| 149 |
-
#
|
|
|
|
|
|
|
| 150 |
try:
|
| 151 |
|
| 152 |
submission = {
|
|
@@ -165,7 +293,8 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 165 |
|
| 166 |
status = (
|
| 167 |
f"Score: {result.get('score')}%\n"
|
| 168 |
-
f"Correct:
|
|
|
|
| 169 |
f"{result.get('total_attempted')}"
|
| 170 |
)
|
| 171 |
|
|
@@ -183,6 +312,12 @@ with gr.Blocks() as demo:
|
|
| 183 |
|
| 184 |
gr.Markdown("# HF Agents Course Assignment")
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
gr.LoginButton()
|
| 187 |
|
| 188 |
btn = gr.Button("Run Evaluation")
|
|
@@ -205,4 +340,6 @@ with gr.Blocks() as demo:
|
|
| 205 |
# ---------------------------------------------------
|
| 206 |
if __name__ == "__main__":
|
| 207 |
|
|
|
|
|
|
|
| 208 |
demo.launch()
|
|
|
|
| 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
|
| 10 |
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
| 13 |
+
# ---------------------------------------------------
|
| 14 |
+
# WEB SEARCH TOOL
|
| 15 |
+
# ---------------------------------------------------
|
| 16 |
+
class WebSearchTool:
|
| 17 |
+
|
| 18 |
+
def __init__(self):
|
| 19 |
+
self.ddgs = DDGS()
|
| 20 |
+
|
| 21 |
+
def search(self, query, max_results=5):
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
|
| 25 |
+
results = self.ddgs.text(
|
| 26 |
+
query,
|
| 27 |
+
max_results=max_results
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
snippets = []
|
| 31 |
+
|
| 32 |
+
for r in results:
|
| 33 |
+
body = r.get("body", "")
|
| 34 |
+
title = r.get("title", "")
|
| 35 |
+
snippets.append(f"{title}: {body}")
|
| 36 |
+
|
| 37 |
+
return "\n".join(snippets[:5])
|
| 38 |
+
|
| 39 |
+
except Exception as e:
|
| 40 |
+
|
| 41 |
+
print(f"Search error: {e}")
|
| 42 |
+
|
| 43 |
+
return ""
|
| 44 |
+
|
| 45 |
+
|
| 46 |
# ---------------------------------------------------
|
| 47 |
# AGENT
|
| 48 |
# ---------------------------------------------------
|
|
|
|
| 54 |
api_key=os.getenv("GROQ_API_KEY")
|
| 55 |
)
|
| 56 |
|
| 57 |
+
self.search_tool = WebSearchTool()
|
| 58 |
+
|
| 59 |
+
print("Agent initialized.")
|
| 60 |
|
| 61 |
+
# ---------------------------------------------------
|
| 62 |
+
# CLEAN ANSWER
|
| 63 |
+
# ---------------------------------------------------
|
| 64 |
def clean_answer(self, text):
|
| 65 |
|
| 66 |
if text is None:
|
|
|
|
| 72 |
text = text.replace("FINAL ANSWER:", "")
|
| 73 |
text = text.replace("Answer:", "")
|
| 74 |
|
| 75 |
+
text = re.sub(r"\s+", " ", text)
|
| 76 |
+
|
| 77 |
text = text.strip()
|
| 78 |
|
| 79 |
+
# first line only
|
| 80 |
text = text.split("\n")[0]
|
| 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 |
+
"latest",
|
| 94 |
+
"current",
|
| 95 |
+
"search",
|
| 96 |
+
"find",
|
| 97 |
+
"look up",
|
| 98 |
+
"verify",
|
| 99 |
+
"paper",
|
| 100 |
+
"award",
|
| 101 |
+
"actor",
|
| 102 |
+
"country",
|
| 103 |
+
"city",
|
| 104 |
+
"population",
|
| 105 |
+
"website",
|
| 106 |
+
"nasa",
|
| 107 |
+
"movie",
|
| 108 |
+
"song",
|
| 109 |
+
"audio",
|
| 110 |
+
"youtube",
|
| 111 |
+
"wikipedia"
|
| 112 |
+
]
|
| 113 |
+
|
| 114 |
+
q = question.lower()
|
| 115 |
+
|
| 116 |
+
return any(k in q for k in keywords)
|
| 117 |
+
|
| 118 |
+
# ---------------------------------------------------
|
| 119 |
+
# MAIN CALL
|
| 120 |
+
# ---------------------------------------------------
|
| 121 |
def __call__(self, question: str) -> str:
|
| 122 |
|
| 123 |
try:
|
| 124 |
|
| 125 |
+
web_context = ""
|
| 126 |
+
|
| 127 |
+
# ---------------------------------------------------
|
| 128 |
+
# WEB SEARCH
|
| 129 |
+
# ---------------------------------------------------
|
| 130 |
+
if self.needs_search(question):
|
| 131 |
+
|
| 132 |
+
print(f"\nSearching web for: {question}")
|
| 133 |
+
|
| 134 |
+
web_context = self.search_tool.search(question)
|
| 135 |
+
|
| 136 |
+
# ---------------------------------------------------
|
| 137 |
+
# PROMPT
|
| 138 |
+
# ---------------------------------------------------
|
| 139 |
+
system_prompt = """
|
| 140 |
+
You are an advanced GAIA benchmark solving assistant.
|
| 141 |
+
|
| 142 |
+
Rules:
|
| 143 |
+
- Return ONLY the final answer.
|
| 144 |
+
- No reasoning.
|
| 145 |
+
- No markdown.
|
| 146 |
+
- No explanations.
|
| 147 |
+
- Be concise.
|
| 148 |
+
- If the answer is a list, format correctly.
|
| 149 |
+
- If unsure, still provide your best answer.
|
| 150 |
+
"""
|
| 151 |
+
|
| 152 |
+
user_prompt = f"""
|
| 153 |
+
QUESTION:
|
| 154 |
+
{question}
|
| 155 |
+
|
| 156 |
+
WEB SEARCH RESULTS:
|
| 157 |
+
{web_context}
|
| 158 |
+
"""
|
| 159 |
+
|
| 160 |
+
# ---------------------------------------------------
|
| 161 |
+
# GROQ CALL
|
| 162 |
+
# ---------------------------------------------------
|
| 163 |
completion = self.client.chat.completions.create(
|
| 164 |
+
model="llama-3.3-70b-versatile",
|
| 165 |
messages=[
|
| 166 |
{
|
| 167 |
"role": "system",
|
| 168 |
+
"content": system_prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
},
|
| 170 |
{
|
| 171 |
"role": "user",
|
| 172 |
+
"content": user_prompt
|
| 173 |
}
|
| 174 |
],
|
| 175 |
temperature=0,
|
| 176 |
+
max_tokens=128
|
| 177 |
)
|
| 178 |
|
| 179 |
answer = completion.choices[0].message.content
|
| 180 |
|
| 181 |
cleaned = self.clean_answer(answer)
|
| 182 |
|
| 183 |
+
print(f"\nQUESTION:\n{question}")
|
| 184 |
+
print(f"\nANSWER:\n{cleaned}")
|
| 185 |
|
| 186 |
return cleaned
|
| 187 |
|
| 188 |
except Exception as e:
|
| 189 |
|
| 190 |
+
print(f"Agent error: {e}")
|
| 191 |
|
| 192 |
return ""
|
| 193 |
|
| 194 |
|
| 195 |
# ---------------------------------------------------
|
| 196 |
+
# MAIN EVALUATION
|
| 197 |
# ---------------------------------------------------
|
| 198 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 199 |
|
|
|
|
| 205 |
questions_url = f"{DEFAULT_API_URL}/questions"
|
| 206 |
submit_url = f"{DEFAULT_API_URL}/submit"
|
| 207 |
|
| 208 |
+
# ---------------------------------------------------
|
| 209 |
+
# INIT AGENT
|
| 210 |
+
# ---------------------------------------------------
|
| 211 |
try:
|
| 212 |
|
| 213 |
agent = BasicAgent()
|
|
|
|
| 216 |
|
| 217 |
return f"Agent init error: {e}", None
|
| 218 |
|
| 219 |
+
# ---------------------------------------------------
|
| 220 |
+
# FETCH QUESTIONS
|
| 221 |
+
# ---------------------------------------------------
|
| 222 |
try:
|
| 223 |
|
| 224 |
response = requests.get(
|
|
|
|
| 226 |
timeout=30
|
| 227 |
)
|
| 228 |
|
| 229 |
+
response.raise_for_status()
|
| 230 |
+
|
| 231 |
questions_data = response.json()
|
| 232 |
|
| 233 |
except Exception as e:
|
|
|
|
| 237 |
answers_payload = []
|
| 238 |
results_log = []
|
| 239 |
|
| 240 |
+
# ---------------------------------------------------
|
| 241 |
+
# RUN QUESTIONS
|
| 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"\n========================")
|
| 249 |
+
print(f"TASK {idx+1}")
|
| 250 |
+
print(f"========================")
|
| 251 |
+
|
| 252 |
try:
|
| 253 |
|
| 254 |
answer = agent(question)
|
|
|
|
| 272 |
"Answer": f"ERROR: {e}"
|
| 273 |
})
|
| 274 |
|
| 275 |
+
# ---------------------------------------------------
|
| 276 |
+
# SUBMIT
|
| 277 |
+
# ---------------------------------------------------
|
| 278 |
try:
|
| 279 |
|
| 280 |
submission = {
|
|
|
|
| 293 |
|
| 294 |
status = (
|
| 295 |
f"Score: {result.get('score')}%\n"
|
| 296 |
+
f"Correct: "
|
| 297 |
+
f"{result.get('correct_count')}/"
|
| 298 |
f"{result.get('total_attempted')}"
|
| 299 |
)
|
| 300 |
|
|
|
|
| 312 |
|
| 313 |
gr.Markdown("# HF Agents Course Assignment")
|
| 314 |
|
| 315 |
+
gr.Markdown("""
|
| 316 |
+
Tools enabled:
|
| 317 |
+
- Groq Llama 3.3 70B
|
| 318 |
+
- DuckDuckGo Search
|
| 319 |
+
""")
|
| 320 |
+
|
| 321 |
gr.LoginButton()
|
| 322 |
|
| 323 |
btn = gr.Button("Run Evaluation")
|
|
|
|
| 340 |
# ---------------------------------------------------
|
| 341 |
if __name__ == "__main__":
|
| 342 |
|
| 343 |
+
print("Launching app...")
|
| 344 |
+
|
| 345 |
demo.launch()
|