Spaces:
Build error
Build error
maybe I start to understand something
Browse files- app.py +249 -138
- requirements.txt +8 -6
app.py
CHANGED
@@ -1,168 +1,273 @@
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
import torch
|
6 |
-
import
|
7 |
-
from
|
8 |
-
|
9 |
-
|
10 |
-
from
|
11 |
-
from
|
12 |
-
from
|
13 |
-
from
|
14 |
-
from
|
15 |
-
from
|
|
|
16 |
|
17 |
# --- Constants ---
|
18 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
try:
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
|
36 |
-
transcript = " ".join([d["text"] for d in transcript_list])
|
37 |
-
return transcript
|
38 |
except Exception as e:
|
39 |
-
return f"Error
|
40 |
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
"Error: Hugging Face token is not set. Cannot use the image analysis tool."
|
47 |
-
)
|
48 |
try:
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
headers = {"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "image/png"}
|
53 |
-
response = requests.post(
|
54 |
-
IMAGE_ANALYSIS_API_URL, headers=headers, data=image_bytes
|
55 |
)
|
56 |
-
|
57 |
-
|
58 |
-
generated_text = result[0].get("generated_text", "").strip()
|
59 |
-
final_answer = generated_text.split("ASSISTANT:")[-1].strip()
|
60 |
-
return f"The image description is: {final_answer}. Now, answer the original question based on this."
|
61 |
except Exception as e:
|
62 |
return f"Error analyzing image: {e}"
|
63 |
|
64 |
|
65 |
-
|
66 |
-
def
|
67 |
-
"""
|
|
|
68 |
try:
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
72 |
except Exception as e:
|
73 |
-
return f"Error
|
74 |
|
75 |
|
76 |
-
# ---
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
description="Use this tool to analyze an image when you are given a URL. Provide both the image URL and the question about the image.",
|
86 |
-
)
|
87 |
-
math_tool = FunctionTool.from_defaults(
|
88 |
-
fn=evaluate_math_expression,
|
89 |
-
name="math_evaluator_tool",
|
90 |
-
description="Use this tool to evaluate simple mathematical expressions (e.g., '3 * (4 + 2)').",
|
91 |
-
)
|
92 |
-
|
93 |
-
|
94 |
-
# --- LlamaIndex Agent Definition ---
|
95 |
-
class LlamaIndexAgent:
|
96 |
def __init__(self):
|
97 |
-
print("Initializing
|
98 |
-
ddg_spec = DuckDuckGoSearchToolSpec()
|
99 |
self.tools = [
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
)
|
120 |
-
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
)
|
123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
def __call__(self, question: str) -> str:
|
126 |
-
print(f"Agent received question: {question[:
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
else:
|
132 |
-
print(
|
133 |
-
|
134 |
-
|
135 |
-
final_answer = answer
|
136 |
-
return final_answer
|
137 |
|
138 |
|
139 |
-
# --- Main Gradio App Logic ---
|
140 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
if profile:
|
148 |
-
username = f"{profile.username}"
|
149 |
-
else:
|
150 |
return "Please Login to Hugging Face with the button.", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
api_url = DEFAULT_API_URL
|
152 |
questions_url = f"{api_url}/questions"
|
153 |
submit_url = f"{api_url}/submit"
|
|
|
|
|
154 |
try:
|
155 |
-
|
156 |
-
agent = LlamaIndexAgent()
|
157 |
except Exception as e:
|
|
|
158 |
return f"Error initializing agent: {e}", None
|
|
|
159 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
|
|
|
|
|
|
|
|
160 |
try:
|
161 |
-
response = requests.get(questions_url, timeout=
|
162 |
response.raise_for_status()
|
163 |
questions_data = response.json()
|
164 |
-
|
|
|
|
|
|
|
165 |
return f"Error fetching questions: {e}", None
|
|
|
|
|
166 |
results_log = []
|
167 |
answers_payload = []
|
168 |
print(f"Running agent on {len(questions_data)} questions...")
|
@@ -171,6 +276,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
171 |
question_text = item.get("question")
|
172 |
if not task_id or question_text is None:
|
173 |
continue
|
|
|
174 |
try:
|
175 |
submitted_answer = agent(question_text)
|
176 |
answers_payload.append(
|
@@ -184,6 +290,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
184 |
}
|
185 |
)
|
186 |
except Exception as e:
|
|
|
187 |
results_log.append(
|
188 |
{
|
189 |
"Task ID": task_id,
|
@@ -191,15 +298,19 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
191 |
"Submitted Answer": f"AGENT ERROR: {e}",
|
192 |
}
|
193 |
)
|
|
|
194 |
if not answers_payload:
|
195 |
-
return "Agent did not produce any answers
|
|
|
|
|
196 |
submission_data = {
|
197 |
"username": username.strip(),
|
198 |
"agent_code": agent_code,
|
199 |
"answers": answers_payload,
|
200 |
}
|
|
|
201 |
try:
|
202 |
-
response = requests.post(submit_url, json=submission_data, timeout=
|
203 |
response.raise_for_status()
|
204 |
result_data = response.json()
|
205 |
final_status = (
|
@@ -209,46 +320,46 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
209 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
210 |
f"Message: {result_data.get('message', 'No message received.')}"
|
211 |
)
|
|
|
212 |
return final_status, pd.DataFrame(results_log)
|
|
|
|
|
|
|
213 |
except Exception as e:
|
214 |
return f"An unexpected error occurred during submission: {e}", pd.DataFrame(
|
215 |
results_log
|
216 |
)
|
217 |
|
218 |
|
219 |
-
# --- Build Gradio Interface
|
220 |
-
# UI HAS BEEN REVERTED TO THE INITIAL TEMPLATE AS REQUESTED
|
221 |
with gr.Blocks() as demo:
|
222 |
-
gr.Markdown("#
|
223 |
gr.Markdown(
|
224 |
"""
|
225 |
**Instructions:**
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
230 |
-
|
231 |
---
|
232 |
-
**
|
233 |
-
Once
|
234 |
-
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.
|
235 |
"""
|
236 |
)
|
|
|
237 |
gr.LoginButton()
|
|
|
238 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
239 |
status_output = gr.Textbox(
|
240 |
label="Run Status / Submission Result", lines=5, interactive=False
|
241 |
)
|
242 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
243 |
-
|
|
|
|
|
|
|
|
|
|
|
244 |
|
245 |
if __name__ == "__main__":
|
246 |
print("\n" + "-" * 30 + " App Starting " + "-" * 30)
|
247 |
-
if not HF_TOKEN:
|
248 |
-
print(
|
249 |
-
"⚠️ WARNING: The `HF_TOKEN` secret is not set. The image analysis tool will be unavailable."
|
250 |
-
)
|
251 |
-
else:
|
252 |
-
print("✅ `HF_TOKEN` secret is set.")
|
253 |
-
print("Launching Gradio Interface...")
|
254 |
demo.launch(debug=True, share=False)
|
|
|
1 |
import os
|
2 |
+
import re
|
3 |
import gradio as gr
|
4 |
import requests
|
5 |
import pandas as pd
|
6 |
import torch
|
7 |
+
from transformers import pipeline
|
8 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
9 |
+
from langchain_core.prompts import ChatPromptTemplate
|
10 |
+
from langchain.prompts import PromptTemplate
|
11 |
+
from langchain_huggingface import HuggingFacePipeline
|
12 |
+
from langchain_core.output_parsers import StrOutputParser
|
13 |
+
from langchain_core.tools import tool
|
14 |
+
from langgraph.graph import StateGraph, END
|
15 |
+
from typing import TypedDict, Annotated, List
|
16 |
+
from langchain_community.document_loaders.youtube import YoutubeLoader
|
17 |
+
import numexpr
|
18 |
|
19 |
# --- Constants ---
|
20 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
21 |
+
SYSTEM_PROMPT = """You are a helpful assistant tasked with answering questions.
|
22 |
+
|
23 |
+
You have access to a set of tools to help you. The question you receive may require you to use these tools.
|
24 |
+
When you receive a question, you should first think about what steps you need to take.
|
25 |
+
Based on your plan, you can then call the necessary tools.
|
26 |
+
After calling a tool, you will get a result. You should analyze the result and decide if you need to call another tool or if you have enough information to answer the question.
|
27 |
+
|
28 |
+
When you have the final answer, you must output it in the following format:
|
29 |
+
FINAL ANSWER: [YOUR FINAL ANSWER]
|
30 |
|
31 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma-separated list of numbers and/or strings.
|
32 |
+
- If you are asked for a number, do not use commas for thousands separators or units like '$' or '%' unless specified.
|
33 |
+
- If you are asked for a string, do not use articles or abbreviations (e.g., for cities).
|
34 |
+
- If you are asked for a comma-separated list, apply the above rules to each element.
|
35 |
|
36 |
+
Example:
|
37 |
+
Question: What is the capital of France?
|
38 |
+
Your thought process: I need to find the capital of France. I will use the web search tool.
|
39 |
+
Tool call: web_search("capital of France")
|
40 |
+
Tool output: Paris is the capital of France.
|
41 |
+
Your final answer: FINAL ANSWER: Paris
|
42 |
+
"""
|
43 |
+
|
44 |
+
# --- Tool Definitions ---
|
45 |
|
46 |
|
47 |
+
@tool
|
48 |
+
def web_search(query: str):
|
49 |
+
"""Searches the web using DuckDuckGo."""
|
50 |
+
print(f"--- Calling Web Search Tool with query: {query} ---")
|
51 |
+
search = DuckDuckGoSearchRun()
|
52 |
+
return search.run(query)
|
53 |
+
|
54 |
+
|
55 |
+
@tool
|
56 |
+
def math_calculator(expression: str):
|
57 |
+
"""Calculates the result of a mathematical expression."""
|
58 |
+
print(f"--- Calling Math Calculator Tool with expression: {expression} ---")
|
59 |
try:
|
60 |
+
# Use numexpr for safe evaluation
|
61 |
+
result = numexpr.evaluate(expression).item()
|
62 |
+
return result
|
|
|
|
|
|
|
63 |
except Exception as e:
|
64 |
+
return f"Error evaluating expression: {e}"
|
65 |
|
66 |
|
67 |
+
@tool
|
68 |
+
def image_analyzer(image_url: str):
|
69 |
+
"""Analyzes an image and returns a description."""
|
70 |
+
print(f"--- Calling Image Analyzer Tool with URL: {image_url} ---")
|
|
|
|
|
71 |
try:
|
72 |
+
# Using a CPU-friendly image-to-text model
|
73 |
+
image_to_text = pipeline(
|
74 |
+
"image-to-text", model="Salesforce/blip-image-captioning-base"
|
|
|
|
|
|
|
75 |
)
|
76 |
+
description = image_to_text(image_url)[0]["generated_text"]
|
77 |
+
return description
|
|
|
|
|
|
|
78 |
except Exception as e:
|
79 |
return f"Error analyzing image: {e}"
|
80 |
|
81 |
|
82 |
+
@tool
|
83 |
+
def youtube_transcript_reader(youtube_url: str):
|
84 |
+
"""Reads the transcript of a YouTube video."""
|
85 |
+
print(f"--- Calling YouTube Transcript Reader Tool with URL: {youtube_url} ---")
|
86 |
try:
|
87 |
+
loader = YoutubeLoader.from_youtube_url(youtube_url, add_video_info=False)
|
88 |
+
docs = loader.load()
|
89 |
+
transcript = " ".join([doc.page_content for doc in docs])
|
90 |
+
# Return a manageable chunk of the transcript
|
91 |
+
return transcript[:4000]
|
92 |
except Exception as e:
|
93 |
+
return f"Error reading YouTube transcript: {e}"
|
94 |
|
95 |
|
96 |
+
# --- Agent State Definition ---
|
97 |
+
class AgentState(TypedDict):
|
98 |
+
question: str
|
99 |
+
messages: Annotated[list, lambda x, y: x + y]
|
100 |
+
sender: str
|
101 |
+
|
102 |
+
|
103 |
+
# --- LangGraph Agent Definition ---
|
104 |
+
class GaiaAgent:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
def __init__(self):
|
106 |
+
print("Initializing GaiaAgent...")
|
|
|
107 |
self.tools = [
|
108 |
+
web_search,
|
109 |
+
math_calculator,
|
110 |
+
image_analyzer,
|
111 |
+
youtube_transcript_reader,
|
112 |
+
]
|
113 |
+
|
114 |
+
# Initialize the LLM
|
115 |
+
print("Loading LLM...")
|
116 |
+
llm = HuggingFacePipeline.from_model_id(
|
117 |
+
model_id="HuggingFaceH4/zephyr-7b-beta",
|
118 |
+
task="text-generation",
|
119 |
+
pipeline_kwargs={
|
120 |
+
"max_new_tokens": 512,
|
121 |
+
"top_k": 50,
|
122 |
+
"temperature": 0.1,
|
123 |
+
"do_sample": False,
|
124 |
+
"torch_dtype": torch.bfloat16,
|
125 |
+
"device_map": "auto",
|
126 |
+
},
|
127 |
+
)
|
128 |
+
print("LLM loaded.")
|
129 |
+
|
130 |
+
# Create the agent graph
|
131 |
+
prompt = PromptTemplate(
|
132 |
+
template=SYSTEM_PROMPT
|
133 |
+
+ """
|
134 |
+
Here is the current conversation:
|
135 |
+
{messages}
|
136 |
+
|
137 |
+
Question: {question}
|
138 |
+
""",
|
139 |
+
input_variables=["messages", "question"],
|
140 |
+
)
|
141 |
+
|
142 |
+
self.agent = prompt | llm | StrOutputParser()
|
143 |
+
self.graph = self._create_graph()
|
144 |
+
print("GaiaAgent initialized.")
|
145 |
+
|
146 |
+
def _create_graph(self):
|
147 |
+
graph = StateGraph(AgentState)
|
148 |
+
graph.add_node("agent", self._call_agent)
|
149 |
+
graph.add_node("tools", self._call_tools)
|
150 |
+
graph.add_conditional_edges(
|
151 |
+
"agent", self._decide_action, {"tools": "tools", END: END}
|
152 |
)
|
153 |
+
graph.add_edge("tools", "agent")
|
154 |
+
graph.set_entry_point("agent")
|
155 |
+
return graph.compile()
|
156 |
+
|
157 |
+
def _call_agent(self, state: AgentState):
|
158 |
+
print("--- Calling Agent ---")
|
159 |
+
message_history = "\n".join(state["messages"])
|
160 |
+
response = self.agent.invoke(
|
161 |
+
{"messages": message_history, "question": state["question"]}
|
162 |
)
|
163 |
+
return {"messages": [response], "sender": "agent"}
|
164 |
+
|
165 |
+
def _decide_action(self, state: AgentState):
|
166 |
+
print("--- Deciding Action ---")
|
167 |
+
response = state["messages"][-1]
|
168 |
+
if "FINAL ANSWER:" in response:
|
169 |
+
return END
|
170 |
+
else:
|
171 |
+
return "tools"
|
172 |
+
|
173 |
+
def _call_tools(self, state: AgentState):
|
174 |
+
print("--- Calling Tools ---")
|
175 |
+
raw_tool_call = state["messages"][-1]
|
176 |
+
|
177 |
+
# Simple regex to find tool calls like tool_name("argument")
|
178 |
+
tool_call_match = re.search(r"(\w+)\((.*?)\)", raw_tool_call)
|
179 |
+
if not tool_call_match:
|
180 |
+
return {"messages": ["No valid tool call found."], "sender": "tools"}
|
181 |
+
|
182 |
+
tool_name = tool_call_match.group(1).strip()
|
183 |
+
tool_input_str = tool_call_match.group(2).strip()
|
184 |
+
|
185 |
+
# Remove quotes from the input string if they exist
|
186 |
+
if tool_input_str.startswith('"') and tool_input_str.endswith('"'):
|
187 |
+
tool_input = tool_input_str[1:-1]
|
188 |
+
else:
|
189 |
+
tool_input = tool_input_str
|
190 |
+
|
191 |
+
tool_to_call = next((t for t in self.tools if t.name == tool_name), None)
|
192 |
+
|
193 |
+
if tool_to_call:
|
194 |
+
try:
|
195 |
+
result = tool_to_call.run(tool_input)
|
196 |
+
return {"messages": [str(result)], "sender": "tools"}
|
197 |
+
except Exception as e:
|
198 |
+
return {
|
199 |
+
"messages": [f"Error executing tool {tool_name}: {e}"],
|
200 |
+
"sender": "tools",
|
201 |
+
}
|
202 |
+
else:
|
203 |
+
return {"messages": [f"Tool '{tool_name}' not found."], "sender": "tools"}
|
204 |
|
205 |
def __call__(self, question: str) -> str:
|
206 |
+
print(f"Agent received question: {question[:100]}...")
|
207 |
+
|
208 |
+
initial_state = {"question": question, "messages": [], "sender": "user"}
|
209 |
+
|
210 |
+
final_state = self.graph.invoke(initial_state, {"recursion_limit": 10})
|
211 |
+
|
212 |
+
final_answer = final_state["messages"][-1]
|
213 |
+
|
214 |
+
# Extract the answer after "FINAL ANSWER:"
|
215 |
+
match = re.search(
|
216 |
+
r"FINAL ANSWER:\s*(.*)", final_answer, re.IGNORECASE | re.DOTALL
|
217 |
+
)
|
218 |
+
if match:
|
219 |
+
extracted_answer = match.group(1).strip()
|
220 |
+
print(f"Agent returning final answer: {extracted_answer}")
|
221 |
+
return extracted_answer
|
222 |
else:
|
223 |
+
print("Agent could not find a final answer in the required format.")
|
224 |
+
# Return a fallback answer if parsing fails
|
225 |
+
return "Could not determine the final answer."
|
|
|
|
|
226 |
|
227 |
|
|
|
228 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
229 |
+
"""
|
230 |
+
Fetches all questions, runs the GaiaAgent on them, submits all answers,
|
231 |
+
and displays the results.
|
232 |
+
"""
|
233 |
+
if not profile:
|
234 |
+
print("User not logged in.")
|
|
|
|
|
|
|
235 |
return "Please Login to Hugging Face with the button.", None
|
236 |
+
|
237 |
+
username = profile.username
|
238 |
+
print(f"User logged in: {username}")
|
239 |
+
|
240 |
+
space_id = os.getenv("SPACE_ID")
|
241 |
+
if not space_id:
|
242 |
+
return "SPACE_ID environment variable is not set. Cannot proceed.", None
|
243 |
+
|
244 |
api_url = DEFAULT_API_URL
|
245 |
questions_url = f"{api_url}/questions"
|
246 |
submit_url = f"{api_url}/submit"
|
247 |
+
|
248 |
+
# 1. Instantiate Agent
|
249 |
try:
|
250 |
+
agent = GaiaAgent()
|
|
|
251 |
except Exception as e:
|
252 |
+
print(f"Error instantiating agent: {e}")
|
253 |
return f"Error initializing agent: {e}", None
|
254 |
+
|
255 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
256 |
+
print(f"Agent code URL: {agent_code}")
|
257 |
+
|
258 |
+
# 2. Fetch Questions
|
259 |
+
print(f"Fetching questions from: {questions_url}")
|
260 |
try:
|
261 |
+
response = requests.get(questions_url, timeout=20)
|
262 |
response.raise_for_status()
|
263 |
questions_data = response.json()
|
264 |
+
if not questions_data:
|
265 |
+
return "Fetched questions list is empty.", None
|
266 |
+
print(f"Fetched {len(questions_data)} questions.")
|
267 |
+
except requests.exceptions.RequestException as e:
|
268 |
return f"Error fetching questions: {e}", None
|
269 |
+
|
270 |
+
# 3. Run your Agent
|
271 |
results_log = []
|
272 |
answers_payload = []
|
273 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
|
276 |
question_text = item.get("question")
|
277 |
if not task_id or question_text is None:
|
278 |
continue
|
279 |
+
|
280 |
try:
|
281 |
submitted_answer = agent(question_text)
|
282 |
answers_payload.append(
|
|
|
290 |
}
|
291 |
)
|
292 |
except Exception as e:
|
293 |
+
print(f"Error running agent on task {task_id}: {e}")
|
294 |
results_log.append(
|
295 |
{
|
296 |
"Task ID": task_id,
|
|
|
298 |
"Submitted Answer": f"AGENT ERROR: {e}",
|
299 |
}
|
300 |
)
|
301 |
+
|
302 |
if not answers_payload:
|
303 |
+
return "Agent did not produce any answers.", pd.DataFrame(results_log)
|
304 |
+
|
305 |
+
# 4. Prepare and Submit
|
306 |
submission_data = {
|
307 |
"username": username.strip(),
|
308 |
"agent_code": agent_code,
|
309 |
"answers": answers_payload,
|
310 |
}
|
311 |
+
print(f"Submitting {len(answers_payload)} answers for user '{username}'...")
|
312 |
try:
|
313 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
314 |
response.raise_for_status()
|
315 |
result_data = response.json()
|
316 |
final_status = (
|
|
|
320 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
321 |
f"Message: {result_data.get('message', 'No message received.')}"
|
322 |
)
|
323 |
+
print("Submission successful.")
|
324 |
return final_status, pd.DataFrame(results_log)
|
325 |
+
except requests.exceptions.HTTPError as e:
|
326 |
+
error_detail = f"Server responded with status {e.response.status_code}. Detail: {e.response.text}"
|
327 |
+
return f"Submission Failed: {error_detail}", pd.DataFrame(results_log)
|
328 |
except Exception as e:
|
329 |
return f"An unexpected error occurred during submission: {e}", pd.DataFrame(
|
330 |
results_log
|
331 |
)
|
332 |
|
333 |
|
334 |
+
# --- Build Gradio Interface ---
|
|
|
335 |
with gr.Blocks() as demo:
|
336 |
+
gr.Markdown("# GAIA Agent Evaluation Runner")
|
337 |
gr.Markdown(
|
338 |
"""
|
339 |
**Instructions:**
|
340 |
+
1. This Space contains a `langgraph`-based agent equipped with tools for web search, math, image analysis, and YouTube transcript reading.
|
341 |
+
2. Log in to your Hugging Face account using the button below. Your HF username is used for the submission.
|
342 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch the questions, run the agent, submit the answers, and see your score.
|
|
|
|
|
343 |
---
|
344 |
+
**Disclaimer:**
|
345 |
+
- Once you click the submit button, please be patient. The agent needs time to process all the questions, which can take several minutes depending on the model and hardware.
|
|
|
346 |
"""
|
347 |
)
|
348 |
+
|
349 |
gr.LoginButton()
|
350 |
+
|
351 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
352 |
status_output = gr.Textbox(
|
353 |
label="Run Status / Submission Result", lines=5, interactive=False
|
354 |
)
|
355 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
356 |
+
|
357 |
+
run_button.click(
|
358 |
+
fn=run_and_submit_all,
|
359 |
+
outputs=[status_output, results_table],
|
360 |
+
api_name="run_evaluation",
|
361 |
+
)
|
362 |
|
363 |
if __name__ == "__main__":
|
364 |
print("\n" + "-" * 30 + " App Starting " + "-" * 30)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
365 |
demo.launch(debug=True, share=False)
|
requirements.txt
CHANGED
@@ -1,15 +1,17 @@
|
|
1 |
gradio
|
2 |
requests
|
3 |
pandas
|
4 |
-
llama-index
|
5 |
torch
|
6 |
transformers
|
7 |
accelerate
|
8 |
bitsandbytes
|
|
|
|
|
|
|
|
|
|
|
9 |
youtube-transcript-api
|
10 |
-
|
11 |
-
llama-index-tools-duckduckgo
|
12 |
-
llama-index-llms-huggingface
|
13 |
-
# A reliable library for safe math evaluation
|
14 |
numexpr
|
15 |
-
|
|
|
|
1 |
gradio
|
2 |
requests
|
3 |
pandas
|
|
|
4 |
torch
|
5 |
transformers
|
6 |
accelerate
|
7 |
bitsandbytes
|
8 |
+
langchain
|
9 |
+
langgraph
|
10 |
+
langchain-community
|
11 |
+
langchain-huggingface
|
12 |
+
duckduckgo-search
|
13 |
youtube-transcript-api
|
14 |
+
pytube
|
|
|
|
|
|
|
15 |
numexpr
|
16 |
+
Pillow
|
17 |
+
sentence-transformers
|