Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -22,6 +22,12 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
22 |
|
23 |
# --- Basic Agent Definition ---
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
class BasicAgent:
|
26 |
def __init__(self):
|
27 |
print("BasicAgent initialized.")
|
@@ -29,53 +35,93 @@ class BasicAgent:
|
|
29 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
30 |
'Accept-Language': 'en-US,en;q=0.9'
|
31 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
def __call__(self, question: str) -> str:
|
34 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
35 |
|
36 |
-
#
|
37 |
-
if self.
|
38 |
-
answer = self.
|
39 |
-
|
40 |
-
|
41 |
|
42 |
-
|
|
|
|
|
43 |
return answer
|
44 |
|
45 |
-
def
|
46 |
-
"""Check if
|
47 |
question_lower = question.lower()
|
48 |
-
return (
|
49 |
-
|
50 |
-
'november 2016' in question_lower)
|
51 |
|
52 |
-
def
|
53 |
-
"""
|
54 |
try:
|
55 |
-
#
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
soup = BeautifulSoup(response.text, 'html.parser')
|
61 |
|
62 |
-
#
|
63 |
-
|
|
|
|
|
|
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
if nominator:
|
70 |
-
article_title = title.get_text()
|
71 |
-
return f"{nominator.get_text()} nominated {article_title}, the only dinosaur Featured Article promoted in November 2016."
|
72 |
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
|
76 |
-
|
|
|
|
|
77 |
|
78 |
-
|
79 |
|
80 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
81 |
"""
|
|
|
22 |
|
23 |
# --- Basic Agent Definition ---
|
24 |
|
25 |
+
import requests
|
26 |
+
from bs4 import BeautifulSoup
|
27 |
+
import urllib.parse
|
28 |
+
import re
|
29 |
+
from typing import Optional
|
30 |
+
|
31 |
class BasicAgent:
|
32 |
def __init__(self):
|
33 |
print("BasicAgent initialized.")
|
|
|
35 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
36 |
'Accept-Language': 'en-US,en;q=0.9'
|
37 |
}
|
38 |
+
self.answer_patterns = {
|
39 |
+
'definition': r'(?:is|are|was|were) (?:an?|the)? (.+?)(?:\.|,)',
|
40 |
+
'quantity': r'(?:is|are|was|were) (?:about|approximately)? (\d+[\d,\.]*\s*\w+)',
|
41 |
+
'person': r'(?:by|named) (.+?)(?:\.|,)',
|
42 |
+
'date': r'(?:on|in) (.+? \d{4}|\d{1,2} [A-Za-z]+ \d{4})'
|
43 |
+
}
|
44 |
|
45 |
def __call__(self, question: str) -> str:
|
46 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
47 |
|
48 |
+
# Try Wikipedia first for factual questions
|
49 |
+
if self._is_wikipedia_question(question):
|
50 |
+
answer = self._search_wikipedia(question)
|
51 |
+
if answer and answer != "No answer found":
|
52 |
+
return answer
|
53 |
|
54 |
+
# Fall back to Google search
|
55 |
+
answer = self._search_google(question)
|
56 |
+
print(f"Agent returning answer: {answer[:50]}...")
|
57 |
return answer
|
58 |
|
59 |
+
def _is_wikipedia_question(self, question: str) -> bool:
|
60 |
+
"""Check if question is suitable for Wikipedia"""
|
61 |
question_lower = question.lower()
|
62 |
+
return any(keyword in question_lower
|
63 |
+
for keyword in ['who', 'what', 'when', 'where', 'why', 'how', 'define'])
|
|
|
64 |
|
65 |
+
def _search_wikipedia(self, question: str) -> str:
|
66 |
+
"""Search Wikipedia directly for answers"""
|
67 |
try:
|
68 |
+
# Extract main topic from question
|
69 |
+
topic = re.sub(r'(who|what|when|where|why|how|is|are|was|were|did|does|do)\s+', '', question, flags=re.IGNORECASE)
|
70 |
+
topic = re.sub(r'\?.*', '', topic).strip()
|
71 |
+
|
72 |
+
url = f"https://en.wikipedia.org/wiki/{urllib.parse.quote(topic.replace(' ', '_'))}"
|
73 |
+
response = requests.get(url, headers=self.headers, timeout=5)
|
74 |
|
75 |
+
if response.status_code == 200:
|
76 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
77 |
+
first_paragraph = soup.select_one('div.mw-parser-output > p:not(.mw-empty-elt)')
|
78 |
+
|
79 |
+
if first_paragraph:
|
80 |
+
text = first_paragraph.get_text()
|
81 |
+
# Try to extract most relevant sentence
|
82 |
+
for pattern_type, pattern in self.answer_patterns.items():
|
83 |
+
match = re.search(pattern, text, re.IGNORECASE)
|
84 |
+
if match:
|
85 |
+
return f"{match.group(1).strip()} (Source: Wikipedia)"
|
86 |
+
|
87 |
+
return text.split('.')[0] + " (Source: Wikipedia)"
|
88 |
+
|
89 |
+
return "No answer found"
|
90 |
+
|
91 |
+
except Exception:
|
92 |
+
return "No answer found"
|
93 |
+
|
94 |
+
def _search_google(self, question: str) -> str:
|
95 |
+
"""Search Google for answers"""
|
96 |
+
try:
|
97 |
+
url = f"https://www.google.com/search?q={urllib.parse.quote(question)}"
|
98 |
+
response = requests.get(url, headers=self.headers, timeout=5)
|
99 |
soup = BeautifulSoup(response.text, 'html.parser')
|
100 |
|
101 |
+
# Check Google's answer boxes
|
102 |
+
for selector in ['.Z0LcW', '.LGOjhe', '.hgKElc', '.kno-rdesc span']:
|
103 |
+
element = soup.select_one(selector)
|
104 |
+
if element:
|
105 |
+
return element.get_text() + " (Source: Google)"
|
106 |
|
107 |
+
# Try featured snippet
|
108 |
+
snippet = soup.select_one('.xpdopen .kno-rdesc span, .ifM9O')
|
109 |
+
if snippet:
|
110 |
+
return snippet.get_text() + " (Source: Google)"
|
|
|
|
|
|
|
111 |
|
112 |
+
# Fallback to first result summary
|
113 |
+
first_result = soup.select_one('.tF2Cxc')
|
114 |
+
if first_result:
|
115 |
+
summary = first_result.select_one('.IsZvec, .VwiC3b')
|
116 |
+
if summary:
|
117 |
+
return summary.get_text()[:150] + "... (Source: Google)"
|
118 |
|
119 |
+
return "No concise answer found"
|
120 |
+
|
121 |
+
except Exception:
|
122 |
+
return "Search failed"
|
123 |
|
124 |
+
|
125 |
|
126 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
127 |
"""
|