Commit ·
569e5a8
1
Parent(s): fe4bb03
Final BasicAgent improvements
Browse files
app.py
CHANGED
|
@@ -16,7 +16,13 @@ class BasicAgent:
|
|
| 16 |
|
| 17 |
def search_wikipedia(self, query):
|
| 18 |
try:
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
response = requests.get(url, timeout=10)
|
| 21 |
|
| 22 |
if response.status_code == 200:
|
|
@@ -34,16 +40,75 @@ class BasicAgent:
|
|
| 34 |
print(f"Question: {question}")
|
| 35 |
|
| 36 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
context = self.search_wikipedia(question)
|
| 38 |
|
| 39 |
if context:
|
| 40 |
-
answer = context[:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
except Exception as e:
|
| 49 |
print(f"Agent error: {e}")
|
|
|
|
| 16 |
|
| 17 |
def search_wikipedia(self, query):
|
| 18 |
try:
|
| 19 |
+
clean_query = query.split("?")[0].strip()
|
| 20 |
+
|
| 21 |
+
url = (
|
| 22 |
+
"https://en.wikipedia.org/api/rest_v1/page/summary/"
|
| 23 |
+
+ clean_query.replace(" ", "_")
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
response = requests.get(url, timeout=10)
|
| 27 |
|
| 28 |
if response.status_code == 200:
|
|
|
|
| 40 |
print(f"Question: {question}")
|
| 41 |
|
| 42 |
try:
|
| 43 |
+
|
| 44 |
+
q = question.lower().strip()
|
| 45 |
+
|
| 46 |
+
# ---------------------------------------------------
|
| 47 |
+
# Reverse text detection
|
| 48 |
+
# Example:
|
| 49 |
+
# .rewsna eht ...
|
| 50 |
+
# ---------------------------------------------------
|
| 51 |
+
if question.startswith("."):
|
| 52 |
+
reversed_text = question[::-1]
|
| 53 |
+
return reversed_text
|
| 54 |
+
|
| 55 |
+
# ---------------------------------------------------
|
| 56 |
+
# Basic arithmetic
|
| 57 |
+
# ---------------------------------------------------
|
| 58 |
+
math_symbols = ["+", "-", "*", "/"]
|
| 59 |
+
|
| 60 |
+
if any(symbol in question for symbol in math_symbols):
|
| 61 |
+
try:
|
| 62 |
+
expression = (
|
| 63 |
+
question.replace("=", "")
|
| 64 |
+
.replace("What is", "")
|
| 65 |
+
.replace("?", "")
|
| 66 |
+
.strip()
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
result = eval(expression)
|
| 70 |
+
return str(result)
|
| 71 |
+
|
| 72 |
+
except:
|
| 73 |
+
pass
|
| 74 |
+
|
| 75 |
+
# ---------------------------------------------------
|
| 76 |
+
# Keyword heuristics
|
| 77 |
+
# ---------------------------------------------------
|
| 78 |
+
if "youtube" in q:
|
| 79 |
+
return "Could not analyze YouTube video."
|
| 80 |
+
|
| 81 |
+
if "chess" in q:
|
| 82 |
+
return "Could not analyze chess image."
|
| 83 |
+
|
| 84 |
+
if "excel" in q:
|
| 85 |
+
return "Could not analyze attached Excel file."
|
| 86 |
+
|
| 87 |
+
if "image" in q:
|
| 88 |
+
return "Could not analyze image."
|
| 89 |
+
|
| 90 |
+
if "audio" in q:
|
| 91 |
+
return "Could not analyze audio."
|
| 92 |
+
|
| 93 |
+
if "video" in q:
|
| 94 |
+
return "Could not analyze video."
|
| 95 |
+
|
| 96 |
+
# ---------------------------------------------------
|
| 97 |
+
# Wikipedia fallback search
|
| 98 |
+
# ---------------------------------------------------
|
| 99 |
context = self.search_wikipedia(question)
|
| 100 |
|
| 101 |
if context:
|
| 102 |
+
answer = context[:400]
|
| 103 |
+
|
| 104 |
+
print(f"Answer: {answer}")
|
| 105 |
|
| 106 |
+
return answer
|
| 107 |
|
| 108 |
+
# ---------------------------------------------------
|
| 109 |
+
# Default fallback
|
| 110 |
+
# ---------------------------------------------------
|
| 111 |
+
return "Could not determine the answer."
|
| 112 |
|
| 113 |
except Exception as e:
|
| 114 |
print(f"Agent error: {e}")
|