Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,22 +33,22 @@ class BasicAgent:
|
|
| 33 |
return answer.strip()
|
| 34 |
|
| 35 |
|
|
|
|
| 36 |
def __call__(self, question: str) -> str:
|
| 37 |
print(f"Question: {question[:100]}")
|
| 38 |
|
| 39 |
q = question.lower()
|
| 40 |
|
| 41 |
-
# ✅
|
| 42 |
-
|
| 43 |
-
try:
|
| 44 |
-
import re
|
| 45 |
-
nums = re.findall(r"\d+", question)
|
| 46 |
-
if nums:
|
| 47 |
-
return nums[0]
|
| 48 |
-
except:
|
| 49 |
-
pass
|
| 50 |
|
| 51 |
-
# ✅
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
if any(x in q for x in ["sum", "add", "multiply", "divide"]):
|
| 53 |
try:
|
| 54 |
expr = re.findall(r"[0-9\+\-\*\/\.\(\) ]+", question)[0]
|
|
@@ -56,35 +56,35 @@ class BasicAgent:
|
|
| 56 |
except:
|
| 57 |
pass
|
| 58 |
|
| 59 |
-
# ✅ 3.
|
| 60 |
-
|
| 61 |
-
prompt = f"""
|
| 62 |
-
Give ONLY a single short answer (one word or number).
|
| 63 |
-
|
| 64 |
-
Question: {question}
|
| 65 |
-
Answer:
|
| 66 |
-
"""
|
| 67 |
-
|
| 68 |
-
|
| 69 |
try:
|
| 70 |
response = self.client.text_generation(
|
| 71 |
model="google/flan-t5-large",
|
| 72 |
-
prompt=
|
| 73 |
max_new_tokens=20
|
| 74 |
)
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
answer = answer.split("
|
| 80 |
answer = answer.split(".")[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
print(f"HF error: {e}")
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
print(f"Final answer: {answer}")
|
| 87 |
-
return answer
|
| 88 |
|
| 89 |
|
| 90 |
|
|
|
|
| 33 |
return answer.strip()
|
| 34 |
|
| 35 |
|
| 36 |
+
|
| 37 |
def __call__(self, question: str) -> str:
|
| 38 |
print(f"Question: {question[:100]}")
|
| 39 |
|
| 40 |
q = question.lower()
|
| 41 |
|
| 42 |
+
# ✅ fallback SIEMPRE (evita blanks)
|
| 43 |
+
fallback = "unknown"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# ✅ 1. detectar números simples
|
| 46 |
+
import re
|
| 47 |
+
nums = re.findall(r"\d+", question)
|
| 48 |
+
if "how many" in q and nums:
|
| 49 |
+
return nums[-1]
|
| 50 |
+
|
| 51 |
+
# ✅ 2. matemáticas simples
|
| 52 |
if any(x in q for x in ["sum", "add", "multiply", "divide"]):
|
| 53 |
try:
|
| 54 |
expr = re.findall(r"[0-9\+\-\*\/\.\(\) ]+", question)[0]
|
|
|
|
| 56 |
except:
|
| 57 |
pass
|
| 58 |
|
| 59 |
+
# ✅ 3. llamada HF con protección
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
try:
|
| 61 |
response = self.client.text_generation(
|
| 62 |
model="google/flan-t5-large",
|
| 63 |
+
prompt=f"Answer with one word or number: {question}",
|
| 64 |
max_new_tokens=20
|
| 65 |
)
|
| 66 |
|
| 67 |
+
# ✅ controlar respuesta vacía
|
| 68 |
+
if not response or response.strip() == "":
|
| 69 |
+
print("Empty HF response → fallback")
|
| 70 |
+
return fallback
|
| 71 |
+
|
| 72 |
+
answer = response.strip()
|
| 73 |
|
| 74 |
+
# limpiar
|
| 75 |
+
answer = answer.split("\n")[0]
|
| 76 |
answer = answer.split(".")[0]
|
| 77 |
+
answer = answer.split(",")[0].strip()
|
| 78 |
+
|
| 79 |
+
if answer == "":
|
| 80 |
+
return fallback
|
| 81 |
+
|
| 82 |
+
return answer
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
print(f"HF error: {e}")
|
| 86 |
+
return fallback
|
| 87 |
+
|
|
|
|
|
|
|
| 88 |
|
| 89 |
|
| 90 |
|