Spaces:
Sleeping
Sleeping
debugging
Browse files- app.py +16 -11
- langgraph_agents.py +21 -18
app.py
CHANGED
|
@@ -3,28 +3,29 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
import asyncio
|
| 6 |
-
from langgraph_agents import graph # Use your agent
|
| 7 |
from typing import Optional
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
# Constants
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
user_answers_cache = {} # session-based cache
|
| 12 |
|
|
|
|
| 13 |
class GaiaAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("Graph-based agent initialized.")
|
| 16 |
|
| 17 |
def __call__(self, question: str) -> str:
|
| 18 |
print("Received question:", question)
|
| 19 |
-
state = {"question": question, "answer": ""}
|
| 20 |
try:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
if
|
| 25 |
-
return
|
| 26 |
-
|
| 27 |
-
return f"Unexpected output from graph: {result}"
|
| 28 |
except Exception as e:
|
| 29 |
return f"ERROR invoking graph: {e}"
|
| 30 |
|
|
@@ -62,6 +63,7 @@ async def run_agent(profile: gr.OAuthProfile | None):
|
|
| 62 |
return f"Answered {len(results)} questions. Ready to submit.", df
|
| 63 |
|
| 64 |
|
|
|
|
| 65 |
def submit_answers(profile: gr.OAuthProfile | None):
|
| 66 |
if not profile:
|
| 67 |
return "Please login to Hugging Face.", None
|
|
@@ -77,9 +79,12 @@ def submit_answers(profile: gr.OAuthProfile | None):
|
|
| 77 |
|
| 78 |
space_id = os.getenv("SPACE_ID", "")
|
| 79 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
|
| 80 |
-
submission_data = {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
# 3. Submit to scoring API
|
| 83 |
try:
|
| 84 |
response = requests.post(f"{DEFAULT_API_URL}/submit", json=submission_data, timeout=60)
|
| 85 |
response.raise_for_status()
|
|
|
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
import asyncio
|
|
|
|
| 6 |
from typing import Optional
|
| 7 |
|
| 8 |
+
from langchain_core.messages import HumanMessage
|
| 9 |
+
from langgraph_agents import graph # Your graph agent
|
| 10 |
+
|
| 11 |
# Constants
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 13 |
user_answers_cache = {} # session-based cache
|
| 14 |
|
| 15 |
+
|
| 16 |
class GaiaAgent:
|
| 17 |
def __init__(self):
|
| 18 |
print("Graph-based agent initialized.")
|
| 19 |
|
| 20 |
def __call__(self, question: str) -> str:
|
| 21 |
print("Received question:", question)
|
|
|
|
| 22 |
try:
|
| 23 |
+
# FIXED: Correct input for LangGraph
|
| 24 |
+
result = graph.invoke({"messages": [HumanMessage(content=question)]})
|
| 25 |
+
messages = result.get("messages", [])
|
| 26 |
+
if messages:
|
| 27 |
+
return messages[-1].content.strip()
|
| 28 |
+
return "No messages returned."
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
return f"ERROR invoking graph: {e}"
|
| 31 |
|
|
|
|
| 63 |
return f"Answered {len(results)} questions. Ready to submit.", df
|
| 64 |
|
| 65 |
|
| 66 |
+
# Submission
|
| 67 |
def submit_answers(profile: gr.OAuthProfile | None):
|
| 68 |
if not profile:
|
| 69 |
return "Please login to Hugging Face.", None
|
|
|
|
| 79 |
|
| 80 |
space_id = os.getenv("SPACE_ID", "")
|
| 81 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
|
| 82 |
+
submission_data = {
|
| 83 |
+
"username": username,
|
| 84 |
+
"agent_code": agent_code,
|
| 85 |
+
"answers": answers_payload,
|
| 86 |
+
}
|
| 87 |
|
|
|
|
| 88 |
try:
|
| 89 |
response = requests.post(f"{DEFAULT_API_URL}/submit", json=submission_data, timeout=60)
|
| 90 |
response.raise_for_status()
|
langgraph_agents.py
CHANGED
|
@@ -132,21 +132,24 @@ qa_pairs = [
|
|
| 132 |
}
|
| 133 |
]
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
}
|
| 133 |
]
|
| 134 |
|
| 135 |
+
|
| 136 |
+
if __name__ == "__main__":
|
| 137 |
+
# Run only if script is executed directly, NOT imported
|
| 138 |
+
|
| 139 |
+
print("\n๐ Evaluating QA Agent\n")
|
| 140 |
+
|
| 141 |
+
for idx, qa in enumerate(qa_pairs, 1):
|
| 142 |
+
question = qa["q"]
|
| 143 |
+
ground_truth = qa["gt"]
|
| 144 |
+
|
| 145 |
+
print(f"๐น Q{idx}: {question}")
|
| 146 |
+
|
| 147 |
+
try:
|
| 148 |
+
result = graph.invoke({"messages": [HumanMessage(content=question)]})
|
| 149 |
+
raw_answer = result["messages"][-1].content.strip()
|
| 150 |
+
reflected = reflect_answer(question, raw_answer)
|
| 151 |
+
score = similarity_score(reflected, ground_truth)
|
| 152 |
+
verdict = "โ
" if score == 1.0 else "โ"
|
| 153 |
+
print(f"{verdict} A{idx}: {reflected} | GT: {ground_truth} | Similarity: {score}\n")
|
| 154 |
+
except Exception as e:
|
| 155 |
+
print(f"โ A{idx} ERROR: {e}\n")
|