Spaces:
Sleeping
Sleeping
Update app.py (#9)
Browse files- Update app.py (585eaf460cc7814227fbb88665a71263cb99110a)
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from huggingface_hub import InferenceClient # add to requirements.txt
|
|
| 6 |
|
| 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",
|
|
@@ -194,10 +195,50 @@ class BasicAgent:
|
|
| 194 |
species.add("giant petrel")
|
| 195 |
return len(species)
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
# change the template call to pass task_id as second arg
|
| 198 |
def __call__(self, question: str, task_id: str | None = None) -> str:
|
| 199 |
ql = question.lower()
|
| 200 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
# 0) YouTube special-case: count distinct bird species from description
|
| 202 |
m = YOUTUBE_RE.search(question)
|
| 203 |
if m:
|
|
|
|
| 6 |
|
| 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 |
+
REV_INSTR_RX = re.compile(r'opposite of the word ["“]?([A-Za-z]+)["”]?', re.I)
|
| 10 |
|
| 11 |
NUM_WORDS = {
|
| 12 |
"zero":"0","one":"1","two":"2","three":"3","four":"4","five":"5",
|
|
|
|
| 195 |
species.add("giant petrel")
|
| 196 |
return len(species)
|
| 197 |
|
| 198 |
+
def _opposite_word(self, w: str) -> str | None:
|
| 199 |
+
pairs = {
|
| 200 |
+
"left": "right", "right": "left",
|
| 201 |
+
"up": "down", "down": "up",
|
| 202 |
+
"true": "false", "false": "true",
|
| 203 |
+
"open": "closed", "closed": "open",
|
| 204 |
+
"on": "off", "off": "on",
|
| 205 |
+
"start": "stop", "stop": "start",
|
| 206 |
+
"yes": "no", "no": "yes",
|
| 207 |
+
"north": "south", "south": "north",
|
| 208 |
+
"east": "west", "west": "east",
|
| 209 |
+
}
|
| 210 |
+
return pairs.get(w.lower())
|
| 211 |
+
|
| 212 |
+
def _answer_from_reversed_instruction(self, q: str) -> str | None:
|
| 213 |
+
# 1) reverse the whole prompt
|
| 214 |
+
rev = q[::-1]
|
| 215 |
+
# 2) normalize quotes
|
| 216 |
+
norm = rev.replace("’", "'").replace("“", '"').replace("”", '"')
|
| 217 |
+
|
| 218 |
+
# Case A: "opposite of the word "<X>""
|
| 219 |
+
m = REV_INSTR_RX.search(norm)
|
| 220 |
+
if m:
|
| 221 |
+
target = m.group(1)
|
| 222 |
+
opp = self._opposite_word(target)
|
| 223 |
+
if opp:
|
| 224 |
+
return opp # bare string, e.g., "right"
|
| 225 |
+
|
| 226 |
+
# Case B: simple "write <X>" pattern after reversing
|
| 227 |
+
m2 = re.search(r'^\s*write\s+["\']?([A-Za-z0-9\-]+)["\']?\s*$', norm.strip(), re.I)
|
| 228 |
+
if m2:
|
| 229 |
+
return m2.group(1)
|
| 230 |
+
|
| 231 |
+
return None
|
| 232 |
+
|
| 233 |
# change the template call to pass task_id as second arg
|
| 234 |
def __call__(self, question: str, task_id: str | None = None) -> str:
|
| 235 |
ql = question.lower()
|
| 236 |
|
| 237 |
+
# NEW: reversed-instruction puzzle handler
|
| 238 |
+
rev_ans = self._answer_from_reversed_instruction(question)
|
| 239 |
+
if rev_ans is not None:
|
| 240 |
+
return rev_ans
|
| 241 |
+
|
| 242 |
# 0) YouTube special-case: count distinct bird species from description
|
| 243 |
m = YOUTUBE_RE.search(question)
|
| 244 |
if m:
|