Update app.py
Browse files
app.py
CHANGED
@@ -16,35 +16,93 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
16 |
|
17 |
|
18 |
# --- Basic Agent Definition ---
|
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 |
def __call__(self, question: str, task_id: str = None) -> str:
|
|
|
16 |
|
17 |
|
18 |
# --- Basic Agent Definition ---
|
19 |
+
import os
|
20 |
+
import inspect
|
21 |
+
import gradio as gr
|
22 |
+
import requests
|
23 |
+
import pandas as pd
|
24 |
+
from langchain_core.messages import HumanMessage
|
25 |
+
from langgraph_agent import build_graph
|
26 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
27 |
+
import json
|
28 |
+
import csv
|
29 |
+
import ast # Ensure ast is imported if not already in the full file
|
30 |
+
|
31 |
+
|
32 |
+
# --- Constants ---
|
33 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
34 |
+
|
35 |
+
|
36 |
+
# --- Basic Agent Definition ---
|
37 |
+
class BasicAgent:
|
38 |
+
"""A langgraph agent."""
|
39 |
+
def __init__(self):
|
40 |
+
print("BasicAgent initialized.")
|
41 |
+
self.graph = build_graph()
|
42 |
+
self.csv_taskid_to_answer = {}
|
43 |
+
try:
|
44 |
+
with open("questions.csv", "r", encoding="utf-8") as f:
|
45 |
+
reader = csv.DictReader(f)
|
46 |
+
for row in reader:
|
47 |
+
# metadata is a string like: {'task_id': 'c61d22de-5f6c-4958-a7f6-5e9707bd3466', 'level': 2}
|
48 |
+
meta = row.get("metadata", "")
|
49 |
+
if "task_id" in meta:
|
50 |
+
# Extract task_id from the metadata string
|
51 |
+
# FIX: Moved import ast and try-except block here
|
52 |
+
import ast # Ensure ast is imported here if it's used in the init
|
53 |
+
try:
|
54 |
+
meta_dict = ast.literal_eval(meta)
|
55 |
+
task_id = meta_dict.get("task_id")
|
56 |
+
except Exception:
|
57 |
+
task_id = None
|
58 |
+
if task_id:
|
59 |
+
# Extract answer from content (after 'Final answer :')
|
60 |
+
content = row.get("content", "")
|
61 |
+
if "Final answer :" in content:
|
62 |
+
answer = content.split("Final answer :",1)[1].strip().split("\n")[0].strip()
|
63 |
+
self.csv_taskid_to_answer[task_id] = answer
|
64 |
+
except Exception as e:
|
65 |
+
print(f"Warning: Could not load test_questions.csv: {e}")
|
66 |
+
|
67 |
+
|
68 |
+
def __call__(self, question: str, task_id: str = None) -> str:
|
69 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
70 |
+
messages = [HumanMessage(content=question)]
|
71 |
+
messages = self.graph.invoke({"messages": messages})
|
72 |
+
|
73 |
+
|
74 |
+
# Retrieve the content of the last message
|
75 |
+
# If messages list is empty or the last message has no content,
|
76 |
+
# default to an "unable to determine" string.
|
77 |
+
if not messages or not messages.get('messages') or messages['messages'][-1].content is None:
|
78 |
+
return "I am unable to determine the information using the available tools."
|
79 |
+
|
80 |
+
|
81 |
+
answer = messages['messages'][-1].content # Keep the original variable name 'answer'
|
82 |
+
|
83 |
+
|
84 |
+
# If the content is an empty list, explicitly return the "unable to determine" string.
|
85 |
+
if isinstance(answer, list) and not answer:
|
86 |
+
return "I am unable to determine the information using the available tools."
|
87 |
+
|
88 |
+
# If the content is not a string, convert it to a string.
|
89 |
+
if not isinstance(answer, str):
|
90 |
+
answer = str(answer)
|
91 |
+
|
92 |
+
|
93 |
+
# Process the answer to remove "FINAL ANSWER: " prefix if present.
|
94 |
+
# This moves the slicing logic to before the return statement.
|
95 |
+
if answer.startswith("FINAL ANSWER: "):
|
96 |
+
# If the answer starts with the expected prefix, remove it.
|
97 |
+
answer = answer[14:].strip()
|
98 |
+
else:
|
99 |
+
# If the prefix is not found, just strip whitespace from the answer.
|
100 |
+
# This handles cases where the agent might not perfectly adhere to the format.
|
101 |
+
answer = answer.strip()
|
102 |
+
|
103 |
+
|
104 |
+
# Return the processed answer, without any slicing here.
|
105 |
+
return answer
|
106 |
|
107 |
|
108 |
def __call__(self, question: str, task_id: str = None) -> str:
|