Yongkang ZOU commited on
Commit
a5a7826
·
1 Parent(s): b7d2d3d

update answer format

Browse files
Files changed (1) hide show
  1. app.py +15 -5
app.py CHANGED
@@ -6,6 +6,16 @@ import pandas as pd
6
  from agent import build_graph
7
  from langchain_core.messages import HumanMessage
8
 
 
 
 
 
 
 
 
 
 
 
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
@@ -23,13 +33,13 @@ class BasicAgent:
23
  outputs = result["messages"]
24
  for m in reversed(outputs):
25
  if m.type == "ai":
26
- print(f"Agent output: {m.content[:100]}")
27
- return m.content
28
- return "⚠️ No AI message found in the result."
 
29
  except Exception as e:
30
  print(f"LangGraph Agent error: {e}")
31
- return f"Error: {str(e)}"
32
-
33
 
34
  def run_and_submit_all(username: str):
35
  if not username:
 
6
  from agent import build_graph
7
  from langchain_core.messages import HumanMessage
8
 
9
+ import re
10
+
11
+ def extract_answer(text: str) -> str:
12
+ # Try extracting after "FINAL ANSWER:" or similar patterns
13
+ match = re.search(r"FINAL ANSWER:\s*(.+)", text, re.IGNORECASE)
14
+ if match:
15
+ return match.group(1).strip()
16
+ # Otherwise, fall back to the whole text (assuming it's already clean)
17
+ return text.strip()
18
+
19
  # --- Constants ---
20
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
21
 
 
33
  outputs = result["messages"]
34
  for m in reversed(outputs):
35
  if m.type == "ai":
36
+ clean_answer = extract_answer(m.content)
37
+ print(f"Extracted clean answer: {clean_answer}")
38
+ return clean_answer
39
+ return "No answer found."
40
  except Exception as e:
41
  print(f"LangGraph Agent error: {e}")
42
+ return f"Error: {str(e)}"
 
43
 
44
  def run_and_submit_all(username: str):
45
  if not username: