Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,38 +5,57 @@ import gradio as gr
|
|
| 5 |
import requests
|
| 6 |
import pandas as pd
|
| 7 |
from langchain_core.messages import HumanMessage
|
| 8 |
-
from
|
| 9 |
-
|
| 10 |
|
| 11 |
# --- Constants ---
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 13 |
|
|
|
|
| 14 |
class BasicAgent:
|
| 15 |
"""A langgraph agent."""
|
| 16 |
def __init__(self):
|
| 17 |
print("BasicAgent initialized.")
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def __call__(self, question: str) -> str:
|
| 21 |
-
print(f"Agent received question
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
"
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
"agno_resp": ""
|
| 29 |
-
}
|
| 30 |
config = {"configurable": {"thread_id": f"eval_{hash(question)}"}}
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 42 |
"""
|
|
@@ -60,12 +79,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 60 |
# 1. Instantiate Agent
|
| 61 |
try:
|
| 62 |
agent = BasicAgent()
|
|
|
|
|
|
|
| 63 |
except Exception as e:
|
| 64 |
print(f"Error instantiating agent: {e}")
|
| 65 |
return f"Error initializing agent: {e}", None
|
| 66 |
|
| 67 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 68 |
-
print(agent_code)
|
| 69 |
|
| 70 |
# 2. Fetch Questions
|
| 71 |
print(f"Fetching questions from: {questions_url}")
|
|
@@ -88,19 +109,34 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 88 |
results_log = []
|
| 89 |
answers_payload = []
|
| 90 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 91 |
-
|
|
|
|
| 92 |
task_id = item.get("task_id")
|
| 93 |
question_text = item.get("question")
|
|
|
|
| 94 |
if not task_id or question_text is None:
|
| 95 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 96 |
continue
|
|
|
|
|
|
|
|
|
|
| 97 |
try:
|
| 98 |
submitted_answer = agent(question_text)
|
| 99 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 100 |
-
results_log.append({
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
except Exception as e:
|
|
|
|
| 102 |
print(f"Error running agent on task {task_id}: {e}")
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
if not answers_payload:
|
| 106 |
print("Agent did not produce any answers to submit.")
|
|
|
|
| 5 |
import requests
|
| 6 |
import pandas as pd
|
| 7 |
from langchain_core.messages import HumanMessage
|
| 8 |
+
from veryfinal import build_graph
|
|
|
|
| 9 |
|
| 10 |
# --- Constants ---
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
| 13 |
+
# --- Basic Agent Definition ---
|
| 14 |
class BasicAgent:
|
| 15 |
"""A langgraph agent."""
|
| 16 |
def __init__(self):
|
| 17 |
print("BasicAgent initialized.")
|
| 18 |
+
try:
|
| 19 |
+
self.graph = build_graph(provider="groq") # Using Groq as default
|
| 20 |
+
print("Graph built successfully.")
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"Error building graph: {e}")
|
| 23 |
+
self.graph = None
|
| 24 |
|
| 25 |
def __call__(self, question: str) -> str:
|
| 26 |
+
print(f"Agent received question: {question}")
|
| 27 |
+
|
| 28 |
+
if self.graph is None:
|
| 29 |
+
return "Error: Agent not properly initialized"
|
| 30 |
+
|
| 31 |
+
# Wrap the question in a HumanMessage from langchain_core
|
| 32 |
+
messages = [HumanMessage(content=question)]
|
|
|
|
|
|
|
| 33 |
config = {"configurable": {"thread_id": f"eval_{hash(question)}"}}
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
result = self.graph.invoke({"messages": messages}, config)
|
| 37 |
+
|
| 38 |
+
# Handle different response formats
|
| 39 |
+
if isinstance(result, dict):
|
| 40 |
+
if 'messages' in result and result['messages']:
|
| 41 |
+
answer = result['messages'][-1].content
|
| 42 |
+
elif 'final_answer' in result:
|
| 43 |
+
answer = result['final_answer']
|
| 44 |
+
else:
|
| 45 |
+
answer = str(result)
|
| 46 |
+
else:
|
| 47 |
+
answer = str(result)
|
| 48 |
+
|
| 49 |
+
# Extract final answer if present
|
| 50 |
+
if "FINAL ANSWER:" in answer:
|
| 51 |
+
return answer.split("FINAL ANSWER:")[-1].strip()
|
| 52 |
+
else:
|
| 53 |
+
return answer.strip()
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
error_msg = f"Error: {str(e)}"
|
| 57 |
+
print(error_msg)
|
| 58 |
+
return error_msg
|
| 59 |
|
| 60 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 61 |
"""
|
|
|
|
| 79 |
# 1. Instantiate Agent
|
| 80 |
try:
|
| 81 |
agent = BasicAgent()
|
| 82 |
+
if agent.graph is None:
|
| 83 |
+
return "Error: Failed to initialize agent properly", None
|
| 84 |
except Exception as e:
|
| 85 |
print(f"Error instantiating agent: {e}")
|
| 86 |
return f"Error initializing agent: {e}", None
|
| 87 |
|
| 88 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "No space ID available"
|
| 89 |
+
print(f"Agent code URL: {agent_code}")
|
| 90 |
|
| 91 |
# 2. Fetch Questions
|
| 92 |
print(f"Fetching questions from: {questions_url}")
|
|
|
|
| 109 |
results_log = []
|
| 110 |
answers_payload = []
|
| 111 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 112 |
+
|
| 113 |
+
for i, item in enumerate(questions_data):
|
| 114 |
task_id = item.get("task_id")
|
| 115 |
question_text = item.get("question")
|
| 116 |
+
|
| 117 |
if not task_id or question_text is None:
|
| 118 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 119 |
continue
|
| 120 |
+
|
| 121 |
+
print(f"Processing question {i+1}/{len(questions_data)}: {task_id}")
|
| 122 |
+
|
| 123 |
try:
|
| 124 |
submitted_answer = agent(question_text)
|
| 125 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 126 |
+
results_log.append({
|
| 127 |
+
"Task ID": task_id,
|
| 128 |
+
"Question": question_text[:100] + "..." if len(question_text) > 100 else question_text,
|
| 129 |
+
"Submitted Answer": submitted_answer[:200] + "..." if len(submitted_answer) > 200 else submitted_answer
|
| 130 |
+
})
|
| 131 |
except Exception as e:
|
| 132 |
+
error_msg = f"AGENT ERROR: {e}"
|
| 133 |
print(f"Error running agent on task {task_id}: {e}")
|
| 134 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": error_msg})
|
| 135 |
+
results_log.append({
|
| 136 |
+
"Task ID": task_id,
|
| 137 |
+
"Question": question_text[:100] + "..." if len(question_text) > 100 else question_text,
|
| 138 |
+
"Submitted Answer": error_msg
|
| 139 |
+
})
|
| 140 |
|
| 141 |
if not answers_payload:
|
| 142 |
print("Agent did not produce any answers to submit.")
|