Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app.py (
|
| 2 |
import os
|
| 3 |
import re
|
| 4 |
import io
|
|
@@ -26,20 +26,21 @@ except Exception:
|
|
| 26 |
Image = None
|
| 27 |
pytesseract = None
|
| 28 |
|
| 29 |
-
#
|
| 30 |
# Constants
|
| 31 |
-
#
|
| 32 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 33 |
WIKIPEDIA_API = "https://en.wikipedia.org/w/api.php"
|
| 34 |
USER_AGENT = {"User-Agent": "HF-GAIA-Agent/1.0 (contact: you@example.com)"}
|
| 35 |
|
| 36 |
-
#
|
| 37 |
# Utility functions
|
| 38 |
-
#
|
| 39 |
def extract_numbers(text):
|
| 40 |
"""Return list of numeric strings found in text (integers or floats)."""
|
| 41 |
if not text:
|
| 42 |
return []
|
|
|
|
| 43 |
nums = re.findall(r"\d{1,4}(?:,\d{3})*(?:\.\d+)?|\d+\.\d+|\d+", text.replace("\xa0", " "))
|
| 44 |
# normalize commas
|
| 45 |
clean = [n.replace(",", "") for n in nums]
|
|
@@ -95,11 +96,13 @@ def wiki_try_find_number(question):
|
|
| 95 |
Returns a candidate numeric string or None.
|
| 96 |
"""
|
| 97 |
q = question
|
| 98 |
-
# remove leading
|
| 99 |
search_hint = q
|
| 100 |
-
search_hint = re.sub(r"(?i)how many|between
|
|
|
|
|
|
|
| 101 |
# fallback use whole q
|
| 102 |
-
title = wikipedia_search_first_page(search_hint)
|
| 103 |
if not title:
|
| 104 |
# try full question
|
| 105 |
title = wikipedia_search_first_page(q)
|
|
@@ -135,7 +138,7 @@ def wiki_try_find_number(question):
|
|
| 135 |
|
| 136 |
def fetch_file_text(api_url, task_id):
|
| 137 |
"""Call GET /files/{task_id} to fetch file content if present.
|
| 138 |
-
|
| 139 |
"""
|
| 140 |
try:
|
| 141 |
files_url = f"{api_url}/files/{task_id}"
|
|
@@ -216,16 +219,17 @@ def youtube_oembed_title_desc(url):
|
|
| 216 |
pass
|
| 217 |
return ""
|
| 218 |
|
| 219 |
-
#
|
| 220 |
# Agent
|
| 221 |
-
#
|
| 222 |
class BasicAgent:
|
| 223 |
def __init__(self):
|
| 224 |
print("Level-2 Rule Agent initialized (wiki + file tools).")
|
| 225 |
self.api_url = DEFAULT_API_URL
|
| 226 |
|
| 227 |
def solve_math(self, text):
|
| 228 |
-
|
|
|
|
| 229 |
# if pattern like "What is 12 + 5?" or "12 + 5 = ?"
|
| 230 |
if len(expr) >= 3:
|
| 231 |
try:
|
|
@@ -336,9 +340,9 @@ class BasicAgent:
|
|
| 336 |
# fallback
|
| 337 |
return "unknown"
|
| 338 |
|
| 339 |
-
#
|
| 340 |
# Submission runner
|
| 341 |
-
#
|
| 342 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 343 |
"""
|
| 344 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
@@ -387,48 +391,4 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 387 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 388 |
time.sleep(0.2) # polite pause to avoid hammering external services
|
| 389 |
except Exception as e:
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
if not answers_payload:
|
| 393 |
-
return "Agent did not produce any answers.", pd.DataFrame(results_log)
|
| 394 |
-
|
| 395 |
-
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 396 |
-
|
| 397 |
-
try:
|
| 398 |
-
resp = requests.post(submit_url, json=submission_data, headers=USER_AGENT, timeout=60)
|
| 399 |
-
resp.raise_for_status()
|
| 400 |
-
result = resp.json()
|
| 401 |
-
final_status = (
|
| 402 |
-
f"Submission Successful!\nUser: {result.get('username')}\n"
|
| 403 |
-
f"Overall Score: {result.get('score', 'N/A')}% "
|
| 404 |
-
f"({result.get('correct_count', '?')}/{result.get('total_attempted', '?')} correct)\n"
|
| 405 |
-
f"Message: {result.get('message', '')}"
|
| 406 |
-
)
|
| 407 |
-
return final_status, pd.DataFrame(results_log)
|
| 408 |
-
except requests.exceptions.HTTPError as e:
|
| 409 |
-
try:
|
| 410 |
-
body = e.response.json()
|
| 411 |
-
detail = body.get("detail") or json.dumps(body)[:400]
|
| 412 |
-
except Exception:
|
| 413 |
-
detail = e.response.text[:400]
|
| 414 |
-
return f"Submission Failed: HTTP {e.response.status_code} - {detail}", pd.DataFrame(results_log)
|
| 415 |
-
except Exception as e:
|
| 416 |
-
return f"Submission Failed: {e}", pd.DataFrame(results_log)
|
| 417 |
-
|
| 418 |
-
# ------------------------------
|
| 419 |
-
# Gradio UI
|
| 420 |
-
# ------------------------------
|
| 421 |
-
with gr.Blocks() as demo:
|
| 422 |
-
gr.Markdown("# Level-2 Agent (Rule-based + Wiki/File Tools)")
|
| 423 |
-
gr.Markdown(
|
| 424 |
-
"Duplicate this space, make it public, then login and press **Run Evaluation & Submit All Answers**."
|
| 425 |
-
)
|
| 426 |
-
gr.LoginButton()
|
| 427 |
-
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 428 |
-
status_output = gr.Textbox(label="Run Status / Submission Result", lines=6, interactive=False)
|
| 429 |
-
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 430 |
-
|
| 431 |
-
run_button.click(fn=run_and_submit_all, inputs=[], outputs=[status_output, results_table])
|
| 432 |
-
|
| 433 |
-
if __name__ == "__main__":
|
| 434 |
-
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|
|
|
|
| 1 |
+
# app.py (FIXED - Rule-based Level 2 Agent using Wikipedia + file reading + heuristics)
|
| 2 |
import os
|
| 3 |
import re
|
| 4 |
import io
|
|
|
|
| 26 |
Image = None
|
| 27 |
pytesseract = None
|
| 28 |
|
| 29 |
+
# ---
|
| 30 |
# Constants
|
| 31 |
+
# ---
|
| 32 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 33 |
WIKIPEDIA_API = "https://en.wikipedia.org/w/api.php"
|
| 34 |
USER_AGENT = {"User-Agent": "HF-GAIA-Agent/1.0 (contact: you@example.com)"}
|
| 35 |
|
| 36 |
+
# ---
|
| 37 |
# Utility functions
|
| 38 |
+
# ---
|
| 39 |
def extract_numbers(text):
|
| 40 |
"""Return list of numeric strings found in text (integers or floats)."""
|
| 41 |
if not text:
|
| 42 |
return []
|
| 43 |
+
# Fixed regex pattern with proper OR operator
|
| 44 |
nums = re.findall(r"\d{1,4}(?:,\d{3})*(?:\.\d+)?|\d+\.\d+|\d+", text.replace("\xa0", " "))
|
| 45 |
# normalize commas
|
| 46 |
clean = [n.replace(",", "") for n in nums]
|
|
|
|
| 96 |
Returns a candidate numeric string or None.
|
| 97 |
"""
|
| 98 |
q = question
|
| 99 |
+
# remove leading patterns to get search hint - FIXED with proper OR operator
|
| 100 |
search_hint = q
|
| 101 |
+
search_hint = re.sub(r"(?i)how many|between.*from.*to.*|included|in the video", "", search_hint)
|
| 102 |
+
search_hint = search_hint.strip()
|
| 103 |
+
|
| 104 |
# fallback use whole q
|
| 105 |
+
title = wikipedia_search_first_page(search_hint if search_hint else q)
|
| 106 |
if not title:
|
| 107 |
# try full question
|
| 108 |
title = wikipedia_search_first_page(q)
|
|
|
|
| 138 |
|
| 139 |
def fetch_file_text(api_url, task_id):
|
| 140 |
"""Call GET /files/{task_id} to fetch file content if present.
|
| 141 |
+
Returns text or None.
|
| 142 |
"""
|
| 143 |
try:
|
| 144 |
files_url = f"{api_url}/files/{task_id}"
|
|
|
|
| 219 |
pass
|
| 220 |
return ""
|
| 221 |
|
| 222 |
+
# ---
|
| 223 |
# Agent
|
| 224 |
+
# ---
|
| 225 |
class BasicAgent:
|
| 226 |
def __init__(self):
|
| 227 |
print("Level-2 Rule Agent initialized (wiki + file tools).")
|
| 228 |
self.api_url = DEFAULT_API_URL
|
| 229 |
|
| 230 |
def solve_math(self, text):
|
| 231 |
+
# Fixed regex pattern
|
| 232 |
+
expr = re.findall(r"[-+]?\d+\.?\d*|[\+\-\*\/]", text)
|
| 233 |
# if pattern like "What is 12 + 5?" or "12 + 5 = ?"
|
| 234 |
if len(expr) >= 3:
|
| 235 |
try:
|
|
|
|
| 340 |
# fallback
|
| 341 |
return "unknown"
|
| 342 |
|
| 343 |
+
# ---
|
| 344 |
# Submission runner
|
| 345 |
+
# ---
|
| 346 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 347 |
"""
|
| 348 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 391 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 392 |
time.sleep(0.2) # polite pause to avoid hammering external services
|
| 393 |
except Exception as e:
|
| 394 |
+
results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|