Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,28 +13,28 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 13 |
# --- Basic Agent Definition ---
|
| 14 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 15 |
class BasicAgent:
|
| 16 |
-
"""A langgraph agent."""
|
| 17 |
def __init__(self):
|
| 18 |
print("BasicAgent initialized.")
|
| 19 |
-
self.graph = build_graph()
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def __call__(self, question: str) -> str:
|
| 22 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
if cleaned.startswith("**") and cleaned.endswith("**"):
|
| 34 |
-
cleaned = cleaned.strip("*").strip()
|
| 35 |
-
return cleaned
|
| 36 |
-
|
| 37 |
-
return str(answer)
|
| 38 |
|
| 39 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 40 |
"""
|
|
|
|
| 13 |
# --- Basic Agent Definition ---
|
| 14 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 15 |
class BasicAgent:
|
|
|
|
| 16 |
def __init__(self):
|
| 17 |
print("BasicAgent initialized.")
|
|
|
|
| 18 |
|
| 19 |
+
def post_process_answer(self, answer: str):
|
| 20 |
+
if "FINAL ANSWER:" not in answer:
|
| 21 |
+
raise ValueError("The answer does not contain 'FINAL ANSWER:' keyword")
|
| 22 |
+
key_answer_start_idx = answer.find("FINAL ANSWER:") + len("FINAL ANSWER:")
|
| 23 |
+
key_answer = answer[key_answer_start_idx:].strip()
|
| 24 |
+
return key_answer
|
| 25 |
+
|
| 26 |
def __call__(self, question: str) -> str:
|
| 27 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 28 |
+
try:
|
| 29 |
+
result = agent.invoke({"messages": [HumanMessage(content=question)]})
|
| 30 |
+
answer = result["messages"][-1].content
|
| 31 |
+
key_answer = self.post_process_answer(answer)
|
| 32 |
+
print("Question:", question)
|
| 33 |
+
print("Answer:", key_answer)
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(e)
|
| 36 |
+
key_answer = str(e)
|
| 37 |
+
return key_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 40 |
"""
|