Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,15 +8,16 @@ from ddgs import DDGS
|
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
# -------------------------
|
| 11 |
-
#
|
| 12 |
# -------------------------
|
| 13 |
_last_call = 0
|
| 14 |
|
| 15 |
-
def call_llm(api_key, prompt,
|
|
|
|
| 16 |
global _last_call
|
| 17 |
|
| 18 |
-
if time.time() - _last_call < 2
|
| 19 |
-
time.sleep(2
|
| 20 |
|
| 21 |
_last_call = time.time()
|
| 22 |
|
|
@@ -27,23 +28,21 @@ def call_llm(api_key, prompt, system="", max_tokens=128):
|
|
| 27 |
"Content-Type": "application/json"
|
| 28 |
}
|
| 29 |
|
| 30 |
-
|
| 31 |
"model": "llama-3.3-70b-versatile",
|
| 32 |
-
"messages": [
|
| 33 |
-
{"role": "system", "content": system},
|
| 34 |
-
{"role": "user", "content": prompt}
|
| 35 |
-
],
|
| 36 |
"temperature": 0,
|
| 37 |
"max_tokens": max_tokens
|
| 38 |
}
|
| 39 |
|
| 40 |
-
r = requests.post(url, headers=headers, json=
|
| 41 |
|
| 42 |
if r.status_code != 200:
|
| 43 |
raise Exception(r.text)
|
| 44 |
|
| 45 |
return r.json()["choices"][0]["message"]["content"].strip()
|
| 46 |
|
|
|
|
| 47 |
# -------------------------
|
| 48 |
# CLEAN ANSWER
|
| 49 |
# -------------------------
|
|
@@ -66,33 +65,27 @@ def clean_answer(text):
|
|
| 66 |
|
| 67 |
return text.strip('"').strip("'").strip("*")
|
| 68 |
|
|
|
|
| 69 |
# -------------------------
|
| 70 |
# WEB SEARCH
|
| 71 |
# -------------------------
|
| 72 |
-
def
|
| 73 |
|
| 74 |
results = []
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
return "\n".join(results)
|
| 83 |
|
|
|
|
| 84 |
# -------------------------
|
| 85 |
# AGENT
|
| 86 |
# -------------------------
|
| 87 |
-
SYSTEM = """
|
| 88 |
-
You are solving GAIA benchmark questions.
|
| 89 |
-
|
| 90 |
-
Rules:
|
| 91 |
-
Return ONLY the final answer.
|
| 92 |
-
No explanation.
|
| 93 |
-
Exact match grading.
|
| 94 |
-
"""
|
| 95 |
-
|
| 96 |
class BasicAgent:
|
| 97 |
|
| 98 |
def __init__(self):
|
|
@@ -100,47 +93,44 @@ class BasicAgent:
|
|
| 100 |
self.key = os.getenv("GROQ_API_KEY")
|
| 101 |
|
| 102 |
if not self.key:
|
| 103 |
-
raise RuntimeError("GROQ_API_KEY
|
| 104 |
|
| 105 |
print("Agent ready")
|
| 106 |
|
| 107 |
-
# automatic retry
|
| 108 |
def solve(self, prompt):
|
| 109 |
|
| 110 |
-
for
|
| 111 |
|
| 112 |
try:
|
| 113 |
|
| 114 |
-
answer = call_llm(
|
| 115 |
-
self.key,
|
| 116 |
-
prompt,
|
| 117 |
-
SYSTEM,
|
| 118 |
-
max_tokens=128
|
| 119 |
-
)
|
| 120 |
|
| 121 |
answer = clean_answer(answer)
|
| 122 |
|
| 123 |
-
if
|
| 124 |
return answer
|
| 125 |
|
| 126 |
except Exception as e:
|
|
|
|
| 127 |
print("Retry:", e)
|
| 128 |
|
| 129 |
time.sleep(2)
|
| 130 |
|
| 131 |
return ""
|
| 132 |
|
| 133 |
-
def __call__(self, question, task_id=""):
|
| 134 |
|
| 135 |
print("Question:", question)
|
| 136 |
|
| 137 |
-
search =
|
| 138 |
|
| 139 |
prompt = f"""
|
|
|
|
|
|
|
| 140 |
Question:
|
| 141 |
{question}
|
| 142 |
|
| 143 |
-
|
| 144 |
{search}
|
| 145 |
|
| 146 |
Return ONLY the final answer.
|
|
@@ -152,13 +142,14 @@ Return ONLY the final answer.
|
|
| 152 |
|
| 153 |
return answer
|
| 154 |
|
|
|
|
| 155 |
# -------------------------
|
| 156 |
-
# EVALUATION
|
| 157 |
# -------------------------
|
| 158 |
def run_and_submit_all(profile):
|
| 159 |
|
| 160 |
if not profile:
|
| 161 |
-
return "Please login", None
|
| 162 |
|
| 163 |
username = profile.username
|
| 164 |
|
|
@@ -205,6 +196,7 @@ Correct: {result['correct_count']}
|
|
| 205 |
|
| 206 |
return msg, pd.DataFrame(logs)
|
| 207 |
|
|
|
|
| 208 |
# -------------------------
|
| 209 |
# UI
|
| 210 |
# -------------------------
|
|
@@ -216,9 +208,9 @@ with gr.Blocks() as demo:
|
|
| 216 |
|
| 217 |
run_btn = gr.Button("Run Evaluation")
|
| 218 |
|
| 219 |
-
status = gr.Textbox()
|
| 220 |
|
| 221 |
-
table = gr.DataFrame()
|
| 222 |
|
| 223 |
run_btn.click(
|
| 224 |
run_and_submit_all,
|
|
|
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
# -------------------------
|
| 11 |
+
# LLM CALL
|
| 12 |
# -------------------------
|
| 13 |
_last_call = 0
|
| 14 |
|
| 15 |
+
def call_llm(api_key, prompt, max_tokens=128):
|
| 16 |
+
|
| 17 |
global _last_call
|
| 18 |
|
| 19 |
+
if time.time() - _last_call < 2:
|
| 20 |
+
time.sleep(2)
|
| 21 |
|
| 22 |
_last_call = time.time()
|
| 23 |
|
|
|
|
| 28 |
"Content-Type": "application/json"
|
| 29 |
}
|
| 30 |
|
| 31 |
+
body = {
|
| 32 |
"model": "llama-3.3-70b-versatile",
|
| 33 |
+
"messages": [{"role": "user", "content": prompt}],
|
|
|
|
|
|
|
|
|
|
| 34 |
"temperature": 0,
|
| 35 |
"max_tokens": max_tokens
|
| 36 |
}
|
| 37 |
|
| 38 |
+
r = requests.post(url, headers=headers, json=body, timeout=60)
|
| 39 |
|
| 40 |
if r.status_code != 200:
|
| 41 |
raise Exception(r.text)
|
| 42 |
|
| 43 |
return r.json()["choices"][0]["message"]["content"].strip()
|
| 44 |
|
| 45 |
+
|
| 46 |
# -------------------------
|
| 47 |
# CLEAN ANSWER
|
| 48 |
# -------------------------
|
|
|
|
| 65 |
|
| 66 |
return text.strip('"').strip("'").strip("*")
|
| 67 |
|
| 68 |
+
|
| 69 |
# -------------------------
|
| 70 |
# WEB SEARCH
|
| 71 |
# -------------------------
|
| 72 |
+
def search_web(query):
|
| 73 |
|
| 74 |
results = []
|
| 75 |
|
| 76 |
+
try:
|
| 77 |
+
with DDGS() as ddgs:
|
| 78 |
+
for r in ddgs.text(query, max_results=5):
|
| 79 |
+
results.append(r["body"])
|
| 80 |
+
except:
|
| 81 |
+
pass
|
| 82 |
|
| 83 |
return "\n".join(results)
|
| 84 |
|
| 85 |
+
|
| 86 |
# -------------------------
|
| 87 |
# AGENT
|
| 88 |
# -------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
class BasicAgent:
|
| 90 |
|
| 91 |
def __init__(self):
|
|
|
|
| 93 |
self.key = os.getenv("GROQ_API_KEY")
|
| 94 |
|
| 95 |
if not self.key:
|
| 96 |
+
raise RuntimeError("GROQ_API_KEY not set")
|
| 97 |
|
| 98 |
print("Agent ready")
|
| 99 |
|
|
|
|
| 100 |
def solve(self, prompt):
|
| 101 |
|
| 102 |
+
for _ in range(3):
|
| 103 |
|
| 104 |
try:
|
| 105 |
|
| 106 |
+
answer = call_llm(self.key, prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
answer = clean_answer(answer)
|
| 109 |
|
| 110 |
+
if answer:
|
| 111 |
return answer
|
| 112 |
|
| 113 |
except Exception as e:
|
| 114 |
+
|
| 115 |
print("Retry:", e)
|
| 116 |
|
| 117 |
time.sleep(2)
|
| 118 |
|
| 119 |
return ""
|
| 120 |
|
| 121 |
+
def __call__(self, question: str, task_id: str = "") -> str:
|
| 122 |
|
| 123 |
print("Question:", question)
|
| 124 |
|
| 125 |
+
search = search_web(question)
|
| 126 |
|
| 127 |
prompt = f"""
|
| 128 |
+
Answer the question exactly.
|
| 129 |
+
|
| 130 |
Question:
|
| 131 |
{question}
|
| 132 |
|
| 133 |
+
Context:
|
| 134 |
{search}
|
| 135 |
|
| 136 |
Return ONLY the final answer.
|
|
|
|
| 142 |
|
| 143 |
return answer
|
| 144 |
|
| 145 |
+
|
| 146 |
# -------------------------
|
| 147 |
+
# RUN EVALUATION
|
| 148 |
# -------------------------
|
| 149 |
def run_and_submit_all(profile):
|
| 150 |
|
| 151 |
if not profile:
|
| 152 |
+
return "Please login first", None
|
| 153 |
|
| 154 |
username = profile.username
|
| 155 |
|
|
|
|
| 196 |
|
| 197 |
return msg, pd.DataFrame(logs)
|
| 198 |
|
| 199 |
+
|
| 200 |
# -------------------------
|
| 201 |
# UI
|
| 202 |
# -------------------------
|
|
|
|
| 208 |
|
| 209 |
run_btn = gr.Button("Run Evaluation")
|
| 210 |
|
| 211 |
+
status = gr.Textbox(label="Result")
|
| 212 |
|
| 213 |
+
table = gr.DataFrame(label="Answers")
|
| 214 |
|
| 215 |
run_btn.click(
|
| 216 |
run_and_submit_all,
|