kamorou commited on
Commit
c44ffd5
·
verified ·
1 Parent(s): 5223ff5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -24
app.py CHANGED
@@ -246,6 +246,9 @@
246
  #
247
  # =================================================================================================
248
 
 
 
 
249
  import os
250
  import io
251
  import requests
@@ -258,42 +261,38 @@ import operator
258
  # --- LangChain & LangGraph Imports ---
259
  from langchain_core.messages import BaseMessage, HumanMessage, ToolMessage, AIMessage, SystemMessage
260
  from langchain_core.tools import tool
261
- from langchain_openai import ChatOpenAI # <-- Import OpenAI
 
262
  from langgraph.graph import StateGraph, END
263
  from langgraph.prebuilt import ToolNode
264
  from tavily import TavilyClient
265
- import pypdf # <-- Import PDF library
266
 
267
- # (Keep Constants as is)
268
  # --- Constants ---
269
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
270
  FILES_DIR = "./files"
271
  os.makedirs(FILES_DIR, exist_ok=True)
272
 
273
- # --- System Prompt (Unchanged, it's already strong) ---
274
  AGENT_SYSTEM_PROMPT = """You are a world-class AI agent, specialized in solving complex problems from the GAIA benchmark.
275
-
276
  Your task is to analyze the user's question, think step-by-step, and use the provided tools to find the correct answer.
277
-
278
  CRITICAL INSTRUCTIONS:
279
  1. **Analyze the Goal:** First, understand what the user is asking for.
280
  2. **Plan & Execute:** Formulate a plan and use the available tools (`tavily_search`, `read_file`, `python_interpreter`) to gather information.
281
  3. **Final Answer Format:** Once you are absolutely certain of the answer, you MUST provide it directly and concisely.
282
  - DO NOT include your reasoning, thoughts, or any conversational text like 'The answer is...', 'Here is the result:', or 'Based on my search...'.
283
  - Your final response must ONLY be the answer itself.
284
-
285
  EXAMPLES OF CORRECT FINAL ANSWERS:
286
  - If the question asks for a year: `2023`
287
  - If it asks for a name: `John Doe`
288
  - If it asks for a number: `42`
289
  - If it asks for a comma-separated list: `item1, item2, item3`
290
-
291
  Think, use your tools, and then provide ONLY the final, precise answer.
292
  """
293
 
294
  #
295
  # ================================================================================================
296
- # ✅ 1. DEFINE THE AGENT'S UPGRADED TOOLS
297
  # ================================================================================================
298
  #
299
  tavily = TavilyClient(api_key=os.getenv("TAVILY_API_KEY"))
@@ -326,7 +325,6 @@ def read_file(url: str) -> str:
326
  with open(filename, 'wb') as f:
327
  f.write(response.content)
328
 
329
- # Check if the file is a PDF
330
  if url.lower().endswith('.pdf'):
331
  print(f"--- File identified as PDF. Reading with pypdf. ---")
332
  try:
@@ -338,7 +336,6 @@ def read_file(url: str) -> str:
338
  except Exception as e:
339
  return f"Error reading PDF file: {e}"
340
  else:
341
- # Assume it's a text file
342
  print(f"--- File identified as text. Reading normally. ---")
343
  try:
344
  with open(filename, 'r', encoding='utf-8') as f:
@@ -366,7 +363,7 @@ def python_interpreter(code: str) -> str:
366
 
367
  #
368
  # ================================================================================================
369
- # ✅ 2. CONFIGURE AND BUILD THE AGENT GRAPH (NOW WITH GPT-4o)
370
  # ================================================================================================
371
  #
372
  class AgentState(TypedDict):
@@ -376,9 +373,15 @@ def build_agent_graph():
376
  """Builds the LangGraph agent."""
377
  tools = [tavily_search, read_file, python_interpreter]
378
 
379
- # --->>> THE BRAIN UPGRADE: Using GPT-4o <<<---
380
- # It will use the OPENAI_API_KEY from your secrets.
381
- llm = ChatOpenAI(model="gpt-4o", temperature=0)
 
 
 
 
 
 
382
 
383
  llm_with_tools = llm.bind_tools(tools)
384
 
@@ -406,7 +409,8 @@ def build_agent_graph():
406
  #
407
  class GaiaAgent:
408
  def __init__(self):
409
- print("GaiaAgent initialized. Building fresh GPT-4o graph...")
 
410
  self.agent_app = build_agent_graph()
411
 
412
  def __call__(self, question: str) -> str:
@@ -418,7 +422,8 @@ class GaiaAgent:
418
  ]
419
  }
420
  final_state = None
421
- for i, step in enumerate(self.agent_app.stream(initial_input, {"recursion_limit": 15})):
 
422
  if i == 0: print("--- Starting Agentic Loop ---")
423
  final_state = step
424
 
@@ -427,6 +432,7 @@ class GaiaAgent:
427
  print(f"\n--- Agent finished. Final Answer: {final_answer} ---\n")
428
  return final_answer
429
 
 
430
  def run_and_submit_all( profile: gr.OAuthProfile | None):
431
  space_id = os.getenv("SPACE_ID")
432
  if not profile: return "Please Login to Hugging Face with the button.", None
@@ -470,14 +476,14 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
470
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
471
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
472
  try:
473
- response = requests.post(submit_url, json=submission_data, timeout=90) # Increased timeout for OpenAI
474
  response.raise_for_status()
475
  result_data = response.json()
476
  final_status = (
477
  f"Submission Successful!\n"
478
  f"User: {result_data.get('username')}\n"
479
  f"Overall Score: {result_data.get('score', 'N/A')}% "
480
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
481
  f"Message: {result_data.get('message', 'No message received.')}"
482
  )
483
  results_df = pd.DataFrame(results_log)
@@ -489,13 +495,14 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
489
 
490
  # --- Gradio Interface ---
491
  with gr.Blocks() as demo:
492
- gr.Markdown("# GAIA Agent Final Assessment (V5 - GPT-4o & PDF)")
 
493
  gr.Markdown(
494
  """
495
- **Instructor's Note:** This is the final version. It uses GPT-4o for SOTA reasoning and can now read PDFs.
496
- 1. Ensure `OPENAI_API_KEY` and `TAVILY_API_KEY` are set.
497
- 2. Ensure `requirements.txt` is updated.
498
- 3. Good luck! Let's get that certificate.
499
  """
500
  )
501
  gr.LoginButton()
 
246
  #
247
  # =================================================================================================
248
 
249
+ #
250
+
251
+ ##################
252
  import os
253
  import io
254
  import requests
 
261
  # --- LangChain & LangGraph Imports ---
262
  from langchain_core.messages import BaseMessage, HumanMessage, ToolMessage, AIMessage, SystemMessage
263
  from langchain_core.tools import tool
264
+ # <<<--- CHANGE 1: Import Google Gemini instead of OpenAI --->>>
265
+ from langchain_google_genai import ChatGoogleGenerativeAI
266
  from langgraph.graph import StateGraph, END
267
  from langgraph.prebuilt import ToolNode
268
  from tavily import TavilyClient
269
+ import pypdf
270
 
 
271
  # --- Constants ---
272
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
273
  FILES_DIR = "./files"
274
  os.makedirs(FILES_DIR, exist_ok=True)
275
 
276
+ # --- System Prompt (Unchanged, it's strong) ---
277
  AGENT_SYSTEM_PROMPT = """You are a world-class AI agent, specialized in solving complex problems from the GAIA benchmark.
 
278
  Your task is to analyze the user's question, think step-by-step, and use the provided tools to find the correct answer.
 
279
  CRITICAL INSTRUCTIONS:
280
  1. **Analyze the Goal:** First, understand what the user is asking for.
281
  2. **Plan & Execute:** Formulate a plan and use the available tools (`tavily_search`, `read_file`, `python_interpreter`) to gather information.
282
  3. **Final Answer Format:** Once you are absolutely certain of the answer, you MUST provide it directly and concisely.
283
  - DO NOT include your reasoning, thoughts, or any conversational text like 'The answer is...', 'Here is the result:', or 'Based on my search...'.
284
  - Your final response must ONLY be the answer itself.
 
285
  EXAMPLES OF CORRECT FINAL ANSWERS:
286
  - If the question asks for a year: `2023`
287
  - If it asks for a name: `John Doe`
288
  - If it asks for a number: `42`
289
  - If it asks for a comma-separated list: `item1, item2, item3`
 
290
  Think, use your tools, and then provide ONLY the final, precise answer.
291
  """
292
 
293
  #
294
  # ================================================================================================
295
+ # ✅ 1. AGENT'S TOOLS (Unchanged)
296
  # ================================================================================================
297
  #
298
  tavily = TavilyClient(api_key=os.getenv("TAVILY_API_KEY"))
 
325
  with open(filename, 'wb') as f:
326
  f.write(response.content)
327
 
 
328
  if url.lower().endswith('.pdf'):
329
  print(f"--- File identified as PDF. Reading with pypdf. ---")
330
  try:
 
336
  except Exception as e:
337
  return f"Error reading PDF file: {e}"
338
  else:
 
339
  print(f"--- File identified as text. Reading normally. ---")
340
  try:
341
  with open(filename, 'r', encoding='utf-8') as f:
 
363
 
364
  #
365
  # ================================================================================================
366
+ # ✅ 2. CONFIGURE AND BUILD THE AGENT GRAPH (NOW WITH GEMINI 1.5 PRO)
367
  # ================================================================================================
368
  #
369
  class AgentState(TypedDict):
 
373
  """Builds the LangGraph agent."""
374
  tools = [tavily_search, read_file, python_interpreter]
375
 
376
+ # <<<--- CHANGE 2: Instantiate the Gemini Model --->>>
377
+ # It will use the GOOGLE_API_KEY from your secrets.
378
+ # Note: `convert_system_message_to_human=True` is a useful flag for compatibility,
379
+ # ensuring our powerful system prompt is always understood correctly by the Gemini model.
380
+ llm = ChatGoogleGenerativeAI(
381
+ model="gemini-1.5-pro-latest",
382
+ temperature=0,
383
+ convert_system_message_to_human=True
384
+ )
385
 
386
  llm_with_tools = llm.bind_tools(tools)
387
 
 
409
  #
410
  class GaiaAgent:
411
  def __init__(self):
412
+ # <<<--- CHANGE 3: Update the print statement for clarity --->>>
413
+ print("GaiaAgent initialized. Building fresh Gemini 1.5 Pro graph...")
414
  self.agent_app = build_agent_graph()
415
 
416
  def __call__(self, question: str) -> str:
 
422
  ]
423
  }
424
  final_state = None
425
+ # We increase the recursion limit slightly, as some models might take an extra step for reasoning.
426
+ for i, step in enumerate(self.agent_app.stream(initial_input, {"recursion_limit": 20})):
427
  if i == 0: print("--- Starting Agentic Loop ---")
428
  final_state = step
429
 
 
432
  print(f"\n--- Agent finished. Final Answer: {final_answer} ---\n")
433
  return final_answer
434
 
435
+ # (The rest of the file remains exactly the same)
436
  def run_and_submit_all( profile: gr.OAuthProfile | None):
437
  space_id = os.getenv("SPACE_ID")
438
  if not profile: return "Please Login to Hugging Face with the button.", None
 
476
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
477
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
478
  try:
479
+ response = requests.post(submit_url, json=submission_data, timeout=90)
480
  response.raise_for_status()
481
  result_data = response.json()
482
  final_status = (
483
  f"Submission Successful!\n"
484
  f"User: {result_data.get('username')}\n"
485
  f"Overall Score: {result_data.get('score', 'N/A')}% "
486
+ f"({result_data.get('correct_count', '?')}/{result_a.get('total_attempted', '?')} correct)\n"
487
  f"Message: {result_data.get('message', 'No message received.')}"
488
  )
489
  results_df = pd.DataFrame(results_log)
 
495
 
496
  # --- Gradio Interface ---
497
  with gr.Blocks() as demo:
498
+ # <<<--- CHANGE 4: Update the title in the UI --->>>
499
+ gr.Markdown("# GAIA Agent Final Assessment (V6 - Gemini 1.5 Pro)")
500
  gr.Markdown(
501
  """
502
+ **Instructor's Note:** This version has been upgraded to use Google's `gemini-1.5-pro-latest` model.
503
+ 1. Ensure `GOOGLE_API_KEY` and `TAVILY_API_KEY` are set in your Space secrets.
504
+ 2. Ensure `requirements.txt` is updated with `langchain-google-genai`.
505
+ 3. Let's see how Gemini performs! Good luck.
506
  """
507
  )
508
  gr.LoginButton()