Update app.py
Browse files
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 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
"""
|
| 24 |
-
|
|
|
|
| 25 |
return ""
|
| 26 |
|
| 27 |
-
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
"
|
| 32 |
-
"
|
|
|
|
|
|
|
| 33 |
]
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
if
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
ans = ans[1:-1].strip()
|
| 47 |
|
| 48 |
-
return
|
| 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 |
# =========================================================
|