Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,6 +21,13 @@ class BasicAgent:
|
|
| 21 |
token=os.environ.get("HF_TOKEN")
|
| 22 |
)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def web_search(self, query: str, max_results: int = 5) -> str:
|
| 25 |
"""Runs a DuckDuckGo search and returns formatted snippets."""
|
| 26 |
try:
|
|
@@ -60,6 +67,30 @@ class BasicAgent:
|
|
| 60 |
def __call__(self, question: str) -> str:
|
| 61 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
# Step 1: Search the web for relevant context
|
| 64 |
search_results = self.web_search(question)
|
| 65 |
|
|
|
|
| 21 |
token=os.environ.get("HF_TOKEN")
|
| 22 |
)
|
| 23 |
|
| 24 |
+
def is_reversed_text(self, text: str) -> bool:
|
| 25 |
+
"""Heuristic: if reversing the text produces mostly recognizable English words, it's likely reversed."""
|
| 26 |
+
reversed_text = text[::-1]
|
| 27 |
+
common_words = [" the ", " is ", " you ", " and ", " of ", " a ", " to ", " write ", " answer ", " sentence "]
|
| 28 |
+
hits = sum(1 for w in common_words if w in reversed_text.lower())
|
| 29 |
+
return hits >= 2
|
| 30 |
+
|
| 31 |
def web_search(self, query: str, max_results: int = 5) -> str:
|
| 32 |
"""Runs a DuckDuckGo search and returns formatted snippets."""
|
| 33 |
try:
|
|
|
|
| 67 |
def __call__(self, question: str) -> str:
|
| 68 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 69 |
|
| 70 |
+
# Step 0: Handle reversed-text questions directly, no search/LLM needed
|
| 71 |
+
if self.is_reversed_text(question):
|
| 72 |
+
print("Detected reversed text question.")
|
| 73 |
+
decoded = question[::-1]
|
| 74 |
+
print(f"Decoded question: {decoded}")
|
| 75 |
+
answer_prompt = (
|
| 76 |
+
"The following is a normal English instruction. Follow it exactly and "
|
| 77 |
+
"return ONLY the final answer, nothing else, no explanation:\n\n"
|
| 78 |
+
f"{decoded}"
|
| 79 |
+
)
|
| 80 |
+
try:
|
| 81 |
+
answer = self.call_llm(
|
| 82 |
+
messages=[
|
| 83 |
+
{"role": "system", "content": "You follow instructions precisely and answer with only the final answer."},
|
| 84 |
+
{"role": "user", "content": answer_prompt},
|
| 85 |
+
],
|
| 86 |
+
max_tokens=100,
|
| 87 |
+
)
|
| 88 |
+
except Exception as e:
|
| 89 |
+
print(f"Error solving reversed text question: {e}")
|
| 90 |
+
answer = "Unknown"
|
| 91 |
+
print(f"Agent returning answer: {answer}")
|
| 92 |
+
return answer
|
| 93 |
+
|
| 94 |
# Step 1: Search the web for relevant context
|
| 95 |
search_results = self.web_search(question)
|
| 96 |
|