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

update answer format

Browse files
Files changed (1) hide show
  1. app.py +29 -10
app.py CHANGED
@@ -8,13 +8,30 @@ 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"
@@ -33,14 +50,16 @@ class BasicAgent:
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:
46
  return "❌ Please enter your Hugging Face username.", None
 
8
 
9
  import re
10
 
11
+ import re
12
+
13
  def extract_answer(text: str) -> str:
14
+ """
15
+ Clean and extract the final answer from agent output.
16
+ - Removes 'FINAL ANSWER:' if present.
17
+ - Strips trailing punctuation and whitespace.
18
+ - Normalizes comma/semicolon-separated lists.
19
+ """
20
+ # 提取 FINAL ANSWER 后的内容(如果存在)
21
+ match = re.search(r"final answer[::]?\s*(.+)", text, re.IGNORECASE)
22
+ answer = match.group(1) if match else text
23
+
24
+ # 去除结尾句号和首尾空格
25
+ answer = answer.strip().rstrip('.').strip()
26
+
27
+ # 多项答案的格式标准化(如 a , b -> a,b)
28
+ if ',' in answer:
29
+ answer = ",".join(part.strip() for part in answer.split(','))
30
+ if ';' in answer:
31
+ answer = "; ".join(part.strip() for part in answer.split(';'))
32
+
33
+ return answer
34
+
35
 
36
  # --- Constants ---
37
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
50
  outputs = result["messages"]
51
  for m in reversed(outputs):
52
  if m.type == "ai":
53
+ raw_answer = m.content
54
+ clean = extract_answer(raw_answer)
55
+ print(f"Extracted clean answer: {clean}")
56
+ return clean
57
+ return ""
58
  except Exception as e:
59
  print(f"LangGraph Agent error: {e}")
60
  return f"Error: {str(e)}"
61
 
62
+
63
  def run_and_submit_all(username: str):
64
  if not username:
65
  return "❌ Please enter your Hugging Face username.", None