Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,9 @@
|
|
1 |
-
""" Basic Agent Evaluation Runner"""
|
2 |
import os
|
3 |
-
import inspect
|
4 |
import gradio as gr
|
5 |
import requests
|
6 |
import pandas as pd
|
7 |
from langchain_core.messages import HumanMessage
|
8 |
from langgraph_agent import build_graph
|
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"
|
@@ -20,76 +14,39 @@ class BasicAgent:
|
|
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 |
-
#
|
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
|
62 |
|
63 |
-
#
|
64 |
if isinstance(answer, list) and not answer:
|
65 |
return "I am unable to determine the information using the available tools."
|
66 |
-
|
67 |
-
#
|
68 |
if not isinstance(answer, str):
|
69 |
-
answer = str(answer)
|
70 |
-
|
71 |
-
#
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
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 |
-
|
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):
|
@@ -97,7 +54,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
97 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
98 |
and displays the results.
|
99 |
"""
|
100 |
-
# Determine HF Space Runtime URL and Repo URL
|
101 |
space_id = os.getenv("SPACE_ID")
|
102 |
|
103 |
if profile:
|
@@ -154,6 +110,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
154 |
continue
|
155 |
try:
|
156 |
submitted_answer = agent(question_text, task_id=task_id)
|
|
|
157 |
answers_payload.append({
|
158 |
"task_id": task_id,
|
159 |
"submitted_answer": submitted_answer
|
@@ -280,4 +237,4 @@ if __name__ == "__main__":
|
|
280 |
print("-" * (60 + len(" App Starting ")) + "\n")
|
281 |
|
282 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
283 |
-
demo.launch(debug=True, share=False)
|
|
|
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
from langchain_core.messages import HumanMessage
|
6 |
from langgraph_agent import build_graph
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# --- Constants ---
|
9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
14 |
def __init__(self):
|
15 |
print("BasicAgent initialized.")
|
16 |
self.graph = build_graph()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
|
|
|
|
18 |
def __call__(self, question: str, task_id: str = None) -> str:
|
19 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
20 |
messages = [HumanMessage(content=question)]
|
21 |
messages = self.graph.invoke({"messages": messages})
|
22 |
|
23 |
+
# Check if messages or content is missing
|
|
|
|
|
24 |
if not messages or not messages.get('messages') or messages['messages'][-1].content is None:
|
25 |
return "I am unable to determine the information using the available tools."
|
26 |
|
27 |
+
answer = messages['messages'][-1].content
|
28 |
|
29 |
+
# Handle empty list content
|
30 |
if isinstance(answer, list) and not answer:
|
31 |
return "I am unable to determine the information using the available tools."
|
32 |
+
|
33 |
+
# Convert non-string answers to string
|
34 |
if not isinstance(answer, str):
|
35 |
+
answer = str(answer)
|
36 |
+
|
37 |
+
# Normalize and strip common answer prefixes
|
38 |
+
answer = answer.strip()
|
39 |
+
prefixes = ["FINAL ANSWER:", "Final answer :", "Final answer:", "FINAL ANSWER :"]
|
40 |
+
for prefix in prefixes:
|
41 |
+
if answer.upper().startswith(prefix.upper()):
|
42 |
+
answer = answer[len(prefix):].strip()
|
43 |
+
break
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
# Handle empty or quote-only answers explicitly
|
46 |
+
if not answer or answer.strip() in ['""', "''"]:
|
47 |
+
answer = "I am unable to determine the information using the available tools."
|
48 |
|
49 |
+
return answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
|
52 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
54 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
55 |
and displays the results.
|
56 |
"""
|
|
|
57 |
space_id = os.getenv("SPACE_ID")
|
58 |
|
59 |
if profile:
|
|
|
110 |
continue
|
111 |
try:
|
112 |
submitted_answer = agent(question_text, task_id=task_id)
|
113 |
+
print(f"Submitting answer for task {task_id}: '{submitted_answer}'")
|
114 |
answers_payload.append({
|
115 |
"task_id": task_id,
|
116 |
"submitted_answer": submitted_answer
|
|
|
237 |
print("-" * (60 + len(" App Starting ")) + "\n")
|
238 |
|
239 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
240 |
+
demo.launch(debug=True, share=False)
|