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