GilbertoEwaldFilho commited on
Commit
8564855
·
verified ·
1 Parent(s): 18d75cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -21
app.py CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
 
6
 
7
  # 🔹 NOVO: imports do smolagents
8
  from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel
@@ -17,35 +18,42 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
17
  # =========================================================
18
  def clean_answer(text: str) -> str:
19
  """
20
- Limpa a saída do modelo para ficar mais adequada ao EXACT MATCH.
21
- Remove prefixos como 'Answer:', 'Final answer:' etc.,
22
- aspas externas e ponto final solto.
 
 
 
23
  """
24
- if text is None:
 
25
  return ""
26
 
27
- ans = str(text).strip()
28
 
29
- # remove prefixos comuns
30
- prefixes = [
31
- "answer:", "resposta:", "final answer:", "final:", "ans:", "a:",
32
- "the answer is", "the final answer is",
 
 
33
  ]
34
- lower = ans.lower()
35
- for p in prefixes:
36
- if lower.startswith(p):
37
- ans = ans[len(p):].strip()
38
- break
39
 
40
- # remove ponto final se não parecer número decimal
41
- if ans.endswith(".") and not ans.replace(".", "", 1).isdigit():
42
- ans = ans[:-1].strip()
 
 
43
 
44
- # remove aspas externas
45
- if len(ans) > 1 and ans[0] == ans[-1] and ans[0] in ["'", '"']:
46
- ans = ans[1:-1].strip()
47
 
48
- return ans
49
 
50
 
51
  # =========================================================
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ import re
7
 
8
  # 🔹 NOVO: imports do smolagents
9
  from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel
 
18
  # =========================================================
19
  def clean_answer(text: str) -> str:
20
  """
21
+ Cleans the answer returned by the model:
22
+ - removes newlines
23
+ - removes phrases like 'final answer', 'answer:'
24
+ - removes quotes
25
+ - trims spaces
26
+ But DOES NOT delete useful content.
27
  """
28
+
29
+ if not text:
30
  return ""
31
 
32
+ text = str(text).strip()
33
 
34
+ # Remover frases proibidas
35
+ patterns_to_remove = [
36
+ r"(?i)final answer[:\- ]*",
37
+ r"(?i)answer[:\- ]*",
38
+ r"(?i)the answer is[:\- ]*",
39
+ r"(?i)my answer is[:\- ]*",
40
  ]
41
+ for p in patterns_to_remove:
42
+ text = re.sub(p, "", text).strip()
43
+
44
+ # Remover quebras de linha
45
+ text = text.replace("\n", " ").strip()
46
 
47
+ # Remover aspas externas (se existirem)
48
+ if len(text) >= 2 and text.startswith('"') and text.endswith('"'):
49
+ text = text[1:-1].strip()
50
+ if len(text) >= 2 and text.startswith("'") and text.endswith("'"):
51
+ text = text[1:-1].strip()
52
 
53
+ # Remover espaços múltiplos
54
+ text = re.sub(r"\s+", " ", text)
 
55
 
56
+ return text.strip()
57
 
58
 
59
  # =========================================================