Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,56 +3,43 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
-
"""
|
| 12 |
-
class SafeWikipediaSearch(WikipediaSearchTool):
|
| 13 |
-
def __call__(self, query: str) -> str:
|
| 14 |
-
result = super().__call__(query)
|
| 15 |
-
return str(result) # force string
|
| 16 |
-
"""
|
| 17 |
-
|
| 18 |
# --- Basic Agent Definition ---
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
print("BasicAgent initialized.")
|
| 22 |
-
|
| 23 |
-
# Load the HF-supported model using InferenceClientModel
|
| 24 |
-
self.model = InferenceClientModel(
|
| 25 |
-
model_id="meta-llama/Llama-3.1-8B-Instruct" #meta-llama/Llama-3.1-8B-Instruct meta-llama/Meta-Llama-3-8B-Instruct
|
| 26 |
-
#meta-llama/Llama-3.1-8B-Instruct HuggingFaceH4/zephyr-7b-beta
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
# Tools
|
| 30 |
-
duckduckgo = DuckDuckGoSearchTool()
|
| 31 |
-
wiki = WikipediaSearchTool()
|
| 32 |
-
|
| 33 |
-
# Compose the agent
|
| 34 |
-
# wiki = SafeWikipediaSearch()
|
| 35 |
-
|
| 36 |
-
self.agent = CodeAgent(
|
| 37 |
-
tools=[duckduckgo, wiki], #wiki
|
| 38 |
-
model=self.model
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
def __call__(self, question: str) -> str:
|
| 43 |
-
print(f"Agent received question: {question[:50]}…")
|
| 44 |
-
try:
|
| 45 |
-
answer = self.agent.run(question)
|
| 46 |
-
print(f"Type of agent output: {type(answer)}")
|
| 47 |
-
print(f"Agent returning answer: {str(answer)[:80]}…")
|
| 48 |
-
return str(answer)
|
| 49 |
-
except Exception as e:
|
| 50 |
-
print(f"AGENT ERROR: {e}")
|
| 51 |
-
return f"AGENT ERROR: {e}"
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 57 |
"""
|
| 58 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
@@ -60,7 +47,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 60 |
"""
|
| 61 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 62 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 63 |
-
|
| 64 |
if profile:
|
| 65 |
username= f"{profile.username}"
|
| 66 |
print(f"User logged in: {username}")
|
|
@@ -72,17 +59,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 72 |
questions_url = f"{api_url}/questions"
|
| 73 |
submit_url = f"{api_url}/submit"
|
| 74 |
|
| 75 |
-
# 1.
|
| 76 |
-
try:
|
| 77 |
-
agent = BasicAgent()
|
| 78 |
-
except Exception as e:
|
| 79 |
-
print(f"Error instantiating agent: {e}")
|
| 80 |
-
return f"Error initializing agent: {e}", None
|
| 81 |
-
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
| 82 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 83 |
-
print(agent_code)
|
| 84 |
-
|
| 85 |
-
# 2. Fetch Questions
|
| 86 |
print(f"Fetching questions from: {questions_url}")
|
| 87 |
try:
|
| 88 |
response = requests.get(questions_url, timeout=15)
|
|
@@ -106,6 +83,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 106 |
# 3. Run your Agent
|
| 107 |
results_log = []
|
| 108 |
answers_payload = []
|
|
|
|
| 109 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 110 |
for item in questions_data:
|
| 111 |
task_id = item.get("task_id")
|
|
@@ -114,7 +92,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 114 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 115 |
continue
|
| 116 |
try:
|
| 117 |
-
submitted_answer =
|
| 118 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 119 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 120 |
except Exception as e:
|
|
@@ -187,7 +165,6 @@ with gr.Blocks() as demo:
|
|
| 187 |
**Disclaimers:**
|
| 188 |
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
| 189 |
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
| 190 |
-
Please note that this version requires an OpenAI Key to run.
|
| 191 |
"""
|
| 192 |
)
|
| 193 |
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
|
| 9 |
# (Keep Constants as is)
|
| 10 |
# --- Constants ---
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# --- Basic Agent Definition ---
|
| 14 |
+
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 15 |
+
# Configure logging at the entry point
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
+
def read_jsonl_file(file_path:str) -> dict:
|
| 19 |
+
"""
|
| 20 |
+
Read a JSONL file line by line and yield each parsed JSON object.
|
| 21 |
+
|
| 22 |
+
Args:
|
| 23 |
+
file_path (str): Path to the JSONL file
|
| 24 |
+
|
| 25 |
+
Yields:
|
| 26 |
+
dict: Parsed JSON object from each line
|
| 27 |
+
"""
|
| 28 |
+
with open(file_path, "r") as f:
|
| 29 |
+
for line in f:
|
| 30 |
+
line = line.strip() # Remove whitespace and newlines
|
| 31 |
+
if line: # Skip empty lines
|
| 32 |
+
try:
|
| 33 |
+
yield json.loads(line)
|
| 34 |
+
except json.JSONDecodeError as e:
|
| 35 |
+
continue
|
| 36 |
+
|
| 37 |
+
def get_computed_answers(file_path:str):
|
| 38 |
+
answers = dict()
|
| 39 |
+
for item in read_jsonl_file(file_path):
|
| 40 |
+
answers[item["task_id"]] = {"model_answer": item["model_answer"], "reasoning_trace": item["reasoning_trace"]}
|
| 41 |
+
return answers
|
| 42 |
+
|
| 43 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 44 |
"""
|
| 45 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 47 |
"""
|
| 48 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 49 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 50 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 51 |
if profile:
|
| 52 |
username= f"{profile.username}"
|
| 53 |
print(f"User logged in: {username}")
|
|
|
|
| 59 |
questions_url = f"{api_url}/questions"
|
| 60 |
submit_url = f"{api_url}/submit"
|
| 61 |
|
| 62 |
+
# 1. Fetch Questions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
print(f"Fetching questions from: {questions_url}")
|
| 64 |
try:
|
| 65 |
response = requests.get(questions_url, timeout=15)
|
|
|
|
| 83 |
# 3. Run your Agent
|
| 84 |
results_log = []
|
| 85 |
answers_payload = []
|
| 86 |
+
agent_answers = get_computed_answers("gaia_evaluation_responses.jsonl")
|
| 87 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 88 |
for item in questions_data:
|
| 89 |
task_id = item.get("task_id")
|
|
|
|
| 92 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 93 |
continue
|
| 94 |
try:
|
| 95 |
+
submitted_answer = agent_answers[task_id]["model_answer"]
|
| 96 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 97 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 98 |
except Exception as e:
|
|
|
|
| 165 |
**Disclaimers:**
|
| 166 |
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
| 167 |
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
|
|
|
| 168 |
"""
|
| 169 |
)
|
| 170 |
|