File size: 13,362 Bytes
10e9b7d
2ac3a83
10e9b7d
eccf8e4
3c4371f
4489283
2ac3a83
 
 
 
 
 
 
 
 
 
 
85d8289
e80aab9
3db6293
2ac3a83
 
 
 
 
 
 
 
 
b795696
2ac3a83
 
 
 
ec8845c
2ac3a83
 
 
 
 
 
 
 
 
ec8845c
 
2ac3a83
 
 
 
 
 
 
 
 
 
 
 
85d8289
2ac3a83
 
 
85d8289
2ac3a83
85d8289
 
2ac3a83
 
 
 
ec8845c
2ac3a83
 
 
ec8845c
2ac3a83
 
ec8845c
 
 
 
2ac3a83
 
 
 
97a46b7
2ac3a83
 
 
 
 
97a46b7
2ac3a83
97a46b7
 
2ac3a83
 
 
 
 
 
 
 
 
31243f4
2ac3a83
ec8845c
2ac3a83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b795696
2ac3a83
 
 
 
 
 
 
 
 
85d8289
2ac3a83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95afeec
31243f4
2ac3a83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bb9df1
2ac3a83
 
 
4021bf3
b795696
 
2ac3a83
 
 
 
 
 
7d65c66
2ac3a83
 
 
 
 
 
 
 
7e4a06b
31243f4
 
2ac3a83
 
31243f4
2ac3a83
31243f4
2ac3a83
31243f4
2ac3a83
36ed51a
2ac3a83
 
 
 
eccf8e4
2ac3a83
7d65c66
31243f4
2ac3a83
 
 
 
85d8289
2ac3a83
 
7d65c66
 
3c4371f
31243f4
 
 
 
 
2ac3a83
31243f4
7d65c66
b795696
ec8845c
b795696
 
 
 
 
ec8845c
b795696
 
31243f4
2ac3a83
b795696
 
 
 
 
 
 
2ac3a83
31243f4
2ac3a83
 
 
b795696
 
 
 
 
2ac3a83
e80aab9
2ac3a83
e80aab9
 
31243f4
e80aab9
 
3c4371f
 
 
e80aab9
2ac3a83
85d8289
2ac3a83
 
 
7d65c66
85d8289
 
 
e80aab9
 
2ac3a83
e80aab9
2ac3a83
0ee0419
e514fd7
97a46b7
2ac3a83
 
 
97a46b7
2ac3a83
 
e514fd7
e80aab9
2ac3a83
7e4a06b
2ac3a83
31243f4
b795696
 
 
7d65c66
2ac3a83
 
 
 
 
 
e80aab9
 
b795696
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import os
import re
import gradio as gr
import requests
import pandas as pd
import torch
from transformers import pipeline
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_core.prompts import ChatPromptTemplate
from langchain.prompts import PromptTemplate
from langchain_huggingface import HuggingFacePipeline
from langchain_core.output_parsers import StrOutputParser
from langchain_core.tools import tool
from langgraph.graph import StateGraph, END
from typing import TypedDict, Annotated, List
from langchain_community.document_loaders.youtube import YoutubeLoader
import numexpr

# --- Constants ---
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
SYSTEM_PROMPT = """You are a helpful assistant tasked with answering questions.

You have access to a set of tools to help you. The question you receive may require you to use these tools.
When you receive a question, you should first think about what steps you need to take.
Based on your plan, you can then call the necessary tools.
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.

When you have the final answer, you must output it in the following format:
FINAL ANSWER: [YOUR FINAL ANSWER]

YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma-separated list of numbers and/or strings.
- If you are asked for a number, do not use commas for thousands separators or units like '$' or '%' unless specified.
- If you are asked for a string, do not use articles or abbreviations (e.g., for cities).
- If you are asked for a comma-separated list, apply the above rules to each element.

Example:
Question: What is the capital of France?
Your thought process: I need to find the capital of France. I will use the web search tool.
Tool call: web_search("capital of France")
Tool output: Paris is the capital of France.
Your final answer: FINAL ANSWER: Paris
"""

# --- Tool Definitions ---


@tool
def web_search(query: str):
    """Searches the web using DuckDuckGo."""
    print(f"--- Calling Web Search Tool with query: {query} ---")
    search = DuckDuckGoSearchRun()
    return search.run(query)


@tool
def math_calculator(expression: str):
    """Calculates the result of a mathematical expression."""
    print(f"--- Calling Math Calculator Tool with expression: {expression} ---")
    try:
        # Use numexpr for safe evaluation
        result = numexpr.evaluate(expression).item()
        return result
    except Exception as e:
        return f"Error evaluating expression: {e}"


@tool
def image_analyzer(image_url: str):
    """Analyzes an image and returns a description."""
    print(f"--- Calling Image Analyzer Tool with URL: {image_url} ---")
    try:
        # Using a CPU-friendly image-to-text model
        image_to_text = pipeline(
            "image-to-text", model="Salesforce/blip-image-captioning-base"
        )
        description = image_to_text(image_url)[0]["generated_text"]
        return description
    except Exception as e:
        return f"Error analyzing image: {e}"


@tool
def youtube_transcript_reader(youtube_url: str):
    """Reads the transcript of a YouTube video."""
    print(f"--- Calling YouTube Transcript Reader Tool with URL: {youtube_url} ---")
    try:
        loader = YoutubeLoader.from_youtube_url(youtube_url, add_video_info=False)
        docs = loader.load()
        transcript = " ".join([doc.page_content for doc in docs])
        # Return a manageable chunk of the transcript
        return transcript[:4000]
    except Exception as e:
        return f"Error reading YouTube transcript: {e}"


# --- Agent State Definition ---
class AgentState(TypedDict):
    question: str
    messages: Annotated[list, lambda x, y: x + y]
    sender: str


# --- LangGraph Agent Definition ---
class GaiaAgent:
    def __init__(self):
        print("Initializing GaiaAgent...")
        self.tools = [
            web_search,
            math_calculator,
            image_analyzer,
            youtube_transcript_reader,
        ]

        # Initialize the LLM
        print("Loading LLM...")
        llm = HuggingFacePipeline.from_model_id(
            model_id="HuggingFaceH4/zephyr-7b-beta",
            task="text-generation",
            pipeline_kwargs={
                "max_new_tokens": 512,
                "top_k": 50,
                "temperature": 0.1,
                "do_sample": False,
                "torch_dtype": torch.bfloat16,
                "device_map": "auto",
            },
        )
        print("LLM loaded.")

        # Create the agent graph
        prompt = PromptTemplate(
            template=SYSTEM_PROMPT
            + """
Here is the current conversation:
{messages}

Question: {question}
""",
            input_variables=["messages", "question"],
        )

        self.agent = prompt | llm | StrOutputParser()
        self.graph = self._create_graph()
        print("GaiaAgent initialized.")

    def _create_graph(self):
        graph = StateGraph(AgentState)
        graph.add_node("agent", self._call_agent)
        graph.add_node("tools", self._call_tools)
        graph.add_conditional_edges(
            "agent", self._decide_action, {"tools": "tools", END: END}
        )
        graph.add_edge("tools", "agent")
        graph.set_entry_point("agent")
        return graph.compile()

    def _call_agent(self, state: AgentState):
        print("--- Calling Agent ---")
        message_history = "\n".join(state["messages"])
        response = self.agent.invoke(
            {"messages": message_history, "question": state["question"]}
        )
        return {"messages": [response], "sender": "agent"}

    def _decide_action(self, state: AgentState):
        print("--- Deciding Action ---")
        response = state["messages"][-1]
        if "FINAL ANSWER:" in response:
            return END
        else:
            return "tools"

    def _call_tools(self, state: AgentState):
        print("--- Calling Tools ---")
        raw_tool_call = state["messages"][-1]

        # Simple regex to find tool calls like tool_name("argument")
        tool_call_match = re.search(r"(\w+)\((.*?)\)", raw_tool_call)
        if not tool_call_match:
            return {"messages": ["No valid tool call found."], "sender": "tools"}

        tool_name = tool_call_match.group(1).strip()
        tool_input_str = tool_call_match.group(2).strip()

        # Remove quotes from the input string if they exist
        if tool_input_str.startswith('"') and tool_input_str.endswith('"'):
            tool_input = tool_input_str[1:-1]
        else:
            tool_input = tool_input_str

        tool_to_call = next((t for t in self.tools if t.name == tool_name), None)

        if tool_to_call:
            try:
                result = tool_to_call.run(tool_input)
                return {"messages": [str(result)], "sender": "tools"}
            except Exception as e:
                return {
                    "messages": [f"Error executing tool {tool_name}: {e}"],
                    "sender": "tools",
                }
        else:
            return {"messages": [f"Tool '{tool_name}' not found."], "sender": "tools"}

    def __call__(self, question: str) -> str:
        print(f"Agent received question: {question[:100]}...")

        initial_state = {"question": question, "messages": [], "sender": "user"}

        final_state = self.graph.invoke(initial_state, {"recursion_limit": 10})

        final_answer = final_state["messages"][-1]

        # Extract the answer after "FINAL ANSWER:"
        match = re.search(
            r"FINAL ANSWER:\s*(.*)", final_answer, re.IGNORECASE | re.DOTALL
        )
        if match:
            extracted_answer = match.group(1).strip()
            print(f"Agent returning final answer: {extracted_answer}")
            return extracted_answer
        else:
            print("Agent could not find a final answer in the required format.")
            # Return a fallback answer if parsing fails
            return "Could not determine the final answer."


def run_and_submit_all(profile: gr.OAuthProfile | None):
    """
    Fetches all questions, runs the GaiaAgent on them, submits all answers,
    and displays the results.
    """
    if not profile:
        print("User not logged in.")
        return "Please Login to Hugging Face with the button.", None

    username = profile.username
    print(f"User logged in: {username}")

    space_id = os.getenv("SPACE_ID")
    if not space_id:
        return "SPACE_ID environment variable is not set. Cannot proceed.", None

    api_url = DEFAULT_API_URL
    questions_url = f"{api_url}/questions"
    submit_url = f"{api_url}/submit"

    # 1. Instantiate Agent
    try:
        agent = GaiaAgent()
    except Exception as e:
        print(f"Error instantiating agent: {e}")
        return f"Error initializing agent: {e}", None

    agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
    print(f"Agent code URL: {agent_code}")

    # 2. Fetch Questions
    print(f"Fetching questions from: {questions_url}")
    try:
        response = requests.get(questions_url, timeout=20)
        response.raise_for_status()
        questions_data = response.json()
        if not questions_data:
            return "Fetched questions list is empty.", None
        print(f"Fetched {len(questions_data)} questions.")
    except requests.exceptions.RequestException as e:
        return f"Error fetching questions: {e}", None

    # 3. Run your Agent
    results_log = []
    answers_payload = []
    print(f"Running agent on {len(questions_data)} questions...")
    for item in questions_data:
        task_id = item.get("task_id")
        question_text = item.get("question")
        if not task_id or question_text is None:
            continue

        try:
            submitted_answer = agent(question_text)
            answers_payload.append(
                {"task_id": task_id, "submitted_answer": submitted_answer}
            )
            results_log.append(
                {
                    "Task ID": task_id,
                    "Question": question_text,
                    "Submitted Answer": submitted_answer,
                }
            )
        except Exception as e:
            print(f"Error running agent on task {task_id}: {e}")
            results_log.append(
                {
                    "Task ID": task_id,
                    "Question": question_text,
                    "Submitted Answer": f"AGENT ERROR: {e}",
                }
            )

    if not answers_payload:
        return "Agent did not produce any answers.", pd.DataFrame(results_log)

    # 4. Prepare and Submit
    submission_data = {
        "username": username.strip(),
        "agent_code": agent_code,
        "answers": answers_payload,
    }
    print(f"Submitting {len(answers_payload)} answers for user '{username}'...")
    try:
        response = requests.post(submit_url, json=submission_data, timeout=60)
        response.raise_for_status()
        result_data = response.json()
        final_status = (
            f"Submission Successful!\n"
            f"User: {result_data.get('username')}\n"
            f"Overall Score: {result_data.get('score', 'N/A')}% "
            f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
            f"Message: {result_data.get('message', 'No message received.')}"
        )
        print("Submission successful.")
        return final_status, pd.DataFrame(results_log)
    except requests.exceptions.HTTPError as e:
        error_detail = f"Server responded with status {e.response.status_code}. Detail: {e.response.text}"
        return f"Submission Failed: {error_detail}", pd.DataFrame(results_log)
    except Exception as e:
        return f"An unexpected error occurred during submission: {e}", pd.DataFrame(
            results_log
        )


# --- Build Gradio Interface ---
with gr.Blocks() as demo:
    gr.Markdown("# GAIA Agent Evaluation Runner")
    gr.Markdown(
        """
        **Instructions:**
        1.  This Space contains a `langgraph`-based agent equipped with tools for web search, math, image analysis, and YouTube transcript reading.
        2.  Log in to your Hugging Face account using the button below. Your HF username is used for the submission.
        3.  Click 'Run Evaluation & Submit All Answers' to fetch the questions, run the agent, submit the answers, and see your score.
        ---
        **Disclaimer:**
        -   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.
        """
    )

    gr.LoginButton()

    run_button = gr.Button("Run Evaluation & Submit All Answers")
    status_output = gr.Textbox(
        label="Run Status / Submission Result", lines=5, interactive=False
    )
    results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)

    run_button.click(
        fn=run_and_submit_all,
        outputs=[status_output, results_table],
        api_name="run_evaluation",
    )

if __name__ == "__main__":
    print("\n" + "-" * 30 + " App Starting " + "-" * 30)
    demo.launch(debug=True, share=False)