Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
|
@@ -11,14 +16,22 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 11 |
|
| 12 |
# --- Basic Agent Definition ---
|
| 13 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
| 14 |
class BasicAgent:
|
|
|
|
| 15 |
def __init__(self):
|
| 16 |
print("BasicAgent initialized.")
|
|
|
|
|
|
|
| 17 |
def __call__(self, question: str) -> str:
|
| 18 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 24 |
"""
|
|
@@ -41,7 +54,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 41 |
|
| 42 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 43 |
try:
|
| 44 |
-
agent =
|
| 45 |
except Exception as e:
|
| 46 |
print(f"Error instantiating agent: {e}")
|
| 47 |
return f"Error initializing agent: {e}", None
|
|
@@ -80,19 +93,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 80 |
if not task_id or question_text is None:
|
| 81 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 82 |
continue
|
|
|
|
|
|
|
|
|
|
| 83 |
try:
|
| 84 |
-
print(f"Processing task {task_id}: {question_text[:100]}...")
|
| 85 |
submitted_answer = agent(question_text)
|
| 86 |
-
print(f"Task {task_id} completed successfully")
|
| 87 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 88 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 89 |
except Exception as e:
|
| 90 |
print(f"Error running agent on task {task_id}: {e}")
|
| 91 |
-
|
| 92 |
-
traceback.print_exc()
|
| 93 |
-
error_message = f"AGENT ERROR: {str(e)}"
|
| 94 |
-
answers_payload.append({"task_id": task_id, "submitted_answer": error_message})
|
| 95 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": error_message})
|
| 96 |
|
| 97 |
if not answers_payload:
|
| 98 |
print("Agent did not produce any answers to submit.")
|
|
@@ -117,6 +127,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 117 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 118 |
)
|
| 119 |
print("Submission successful.")
|
|
|
|
| 120 |
results_df = pd.DataFrame(results_log)
|
| 121 |
return final_status, results_df
|
| 122 |
except requests.exceptions.HTTPError as e:
|
|
|
|
| 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 |
+
import time
|
| 8 |
+
from langchain_core.messages import HumanMessage
|
| 9 |
+
from agent import build_graph
|
| 10 |
+
|
| 11 |
+
|
| 12 |
|
| 13 |
# (Keep Constants as is)
|
| 14 |
# --- Constants ---
|
|
|
|
| 16 |
|
| 17 |
# --- Basic Agent Definition ---
|
| 18 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 19 |
+
|
| 20 |
+
|
| 21 |
class BasicAgent:
|
| 22 |
+
"""A langgraph agent."""
|
| 23 |
def __init__(self):
|
| 24 |
print("BasicAgent initialized.")
|
| 25 |
+
self.graph = build_graph()
|
| 26 |
+
|
| 27 |
def __call__(self, question: str) -> str:
|
| 28 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 29 |
+
# Wrap the question in a HumanMessage from langchain_core
|
| 30 |
+
messages = [HumanMessage(content=question)]
|
| 31 |
+
messages = self.graph.invoke({"messages": messages})
|
| 32 |
+
answer = messages['messages'][-1].content
|
| 33 |
+
return answer[14:]
|
| 34 |
+
|
| 35 |
|
| 36 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 37 |
"""
|
|
|
|
| 54 |
|
| 55 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 56 |
try:
|
| 57 |
+
agent = BasicAgent()
|
| 58 |
except Exception as e:
|
| 59 |
print(f"Error instantiating agent: {e}")
|
| 60 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 93 |
if not task_id or question_text is None:
|
| 94 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 95 |
continue
|
| 96 |
+
|
| 97 |
+
# time.sleep(10)
|
| 98 |
+
|
| 99 |
try:
|
|
|
|
| 100 |
submitted_answer = agent(question_text)
|
|
|
|
| 101 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 102 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 103 |
except Exception as e:
|
| 104 |
print(f"Error running agent on task {task_id}: {e}")
|
| 105 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
if not answers_payload:
|
| 108 |
print("Agent did not produce any answers to submit.")
|
|
|
|
| 127 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 128 |
)
|
| 129 |
print("Submission successful.")
|
| 130 |
+
print(result_data)
|
| 131 |
results_df = pd.DataFrame(results_log)
|
| 132 |
return final_status, results_df
|
| 133 |
except requests.exceptions.HTTPError as e:
|