Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,17 +7,50 @@ from huggingface_hub import InferenceClient # add to requirements.txt
|
|
| 7 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 8 |
YOUTUBE_RE = re.compile(r"https?://(?:www\.)?youtube\.com/watch\?v=[\w-]+")
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def format_final_answer(q: str, raw: str) -> str:
|
| 11 |
-
text = raw.strip()
|
| 12 |
-
if "how many" in q.lower():
|
| 13 |
-
m = re.search(r"\d+", text)
|
| 14 |
-
if m:
|
| 15 |
-
return m.group(0)
|
| 16 |
-
# As a safeguard, also strip common wrappers:
|
| 17 |
for pre in ("final answer:", "answer:", "final:", "prediction:"):
|
| 18 |
if text.lower().startswith(pre):
|
| 19 |
text = text[len(pre):].strip()
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# --- provider selection (HF serverless text-generation by default; optional Groq) ---
|
| 23 |
def select_model():
|
|
|
|
| 7 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 8 |
YOUTUBE_RE = re.compile(r"https?://(?:www\.)?youtube\.com/watch\?v=[\w-]+")
|
| 9 |
|
| 10 |
+
NUM_WORDS = {
|
| 11 |
+
"zero":"0","one":"1","two":"2","three":"3","four":"4","five":"5",
|
| 12 |
+
"six":"6","seven":"7","eight":"8","nine":"9","ten":"10","eleven":"11",
|
| 13 |
+
"twelve":"12","thirteen":"13","fourteen":"14","fifteen":"15","sixteen":"16",
|
| 14 |
+
"seventeen":"17","eighteen":"18","nineteen":"19","twenty":"20"
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
def _extract_bare_number(text: str) -> str | None:
|
| 18 |
+
"""Return the first number found as a string (prefers integers, falls back to decimals or number-words)."""
|
| 19 |
+
line = text.strip().splitlines()[0]
|
| 20 |
+
|
| 21 |
+
# 1) integer
|
| 22 |
+
m = re.search(r"(?<![\d.])[-+]?\d+(?![\d.])", line)
|
| 23 |
+
if m:
|
| 24 |
+
return m.group(0).lstrip("+")
|
| 25 |
+
|
| 26 |
+
# 2) decimal (if ever needed)
|
| 27 |
+
m = re.search(r"[-+]?\d+\.\d+", line)
|
| 28 |
+
if m:
|
| 29 |
+
return m.group(0).lstrip("+")
|
| 30 |
+
|
| 31 |
+
# 3) number words → digits
|
| 32 |
+
mw = re.search(r"\b(" + "|".join(NUM_WORDS.keys()) + r")\b", line.lower())
|
| 33 |
+
if mw:
|
| 34 |
+
return NUM_WORDS[mw.group(1)]
|
| 35 |
+
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
def format_final_answer(q: str, raw: str) -> str:
|
| 39 |
+
text = raw.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
for pre in ("final answer:", "answer:", "final:", "prediction:"):
|
| 41 |
if text.lower().startswith(pre):
|
| 42 |
text = text[len(pre):].strip()
|
| 43 |
+
break
|
| 44 |
+
|
| 45 |
+
# If the question implies a numeric answer, force a bare number
|
| 46 |
+
ql = question.lower()
|
| 47 |
+
if any(k in ql for k in ["how many", "number", "highest number", "count", "total", "included"]):
|
| 48 |
+
n = _extract_bare_number(text)
|
| 49 |
+
if n is not None:
|
| 50 |
+
return n # <-- always a string, e.g. "3"
|
| 51 |
+
|
| 52 |
+
# otherwise, keep first line as-is (already stripped)
|
| 53 |
+
return text.splitlines()[0]
|
| 54 |
|
| 55 |
# --- provider selection (HF serverless text-generation by default; optional Groq) ---
|
| 56 |
def select_model():
|