CHRISDANIEL145 commited on
Commit ·
4aa7201
1
Parent(s): 62c54f8
Fix runtime error: Pin transformers, replace scraping with DDGS
Browse files- models/evidence_retriever.py +22 -108
- requirements.txt +2 -1
models/evidence_retriever.py
CHANGED
|
@@ -1,10 +1,7 @@
|
|
| 1 |
-
# models/evidence_retriever.py
|
| 2 |
import wikipedia
|
| 3 |
import requests
|
| 4 |
-
from
|
| 5 |
import time
|
| 6 |
-
from urllib.parse import quote_plus
|
| 7 |
-
|
| 8 |
|
| 9 |
class EvidenceRetriever:
|
| 10 |
def __init__(self):
|
|
@@ -24,7 +21,7 @@ class EvidenceRetriever:
|
|
| 24 |
wiki_evidence = self._get_wikipedia_evidence(keywords)
|
| 25 |
evidence.extend(wiki_evidence)
|
| 26 |
|
| 27 |
-
# Get evidence from web search (using
|
| 28 |
web_evidence = self._get_web_search_evidence(keywords)
|
| 29 |
evidence.extend(web_evidence)
|
| 30 |
|
|
@@ -89,118 +86,35 @@ class EvidenceRetriever:
|
|
| 89 |
return evidence
|
| 90 |
|
| 91 |
def _get_web_search_evidence(self, keywords):
|
| 92 |
-
"""Retrieve evidence from web search using
|
| 93 |
evidence = []
|
| 94 |
|
| 95 |
try:
|
| 96 |
query = ' '.join(keywords[:5])
|
| 97 |
|
| 98 |
-
#
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
# Method 2: If DuckDuckGo fails, use direct scraping with Google
|
| 103 |
-
if len(evidence) < 3:
|
| 104 |
-
google_results = self._search_google_scrape(query)
|
| 105 |
-
evidence.extend(google_results)
|
| 106 |
-
|
| 107 |
-
except Exception as e:
|
| 108 |
-
print(f"Web search error: {e}")
|
| 109 |
-
|
| 110 |
-
return evidence[:5]
|
| 111 |
-
|
| 112 |
-
def _search_duckduckgo_lite(self, query):
|
| 113 |
-
"""Search using DuckDuckGo Lite (HTML version, more stable)"""
|
| 114 |
-
results = []
|
| 115 |
-
|
| 116 |
-
try:
|
| 117 |
-
url = "https://lite.duckduckgo.com/lite/"
|
| 118 |
-
data = {"q": query}
|
| 119 |
-
headers = {
|
| 120 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
| 121 |
-
}
|
| 122 |
-
|
| 123 |
-
response = requests.post(url, data=data, headers=headers, timeout=10)
|
| 124 |
-
|
| 125 |
-
if response.status_code == 200:
|
| 126 |
-
soup = BeautifulSoup(response.text, 'html.parser')
|
| 127 |
-
|
| 128 |
-
# Find all result rows
|
| 129 |
-
result_table = soup.find_all('tr')
|
| 130 |
|
| 131 |
-
for
|
| 132 |
try:
|
| 133 |
-
|
| 134 |
-
|
|
|
|
| 135 |
|
| 136 |
-
if
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
'url': result_url,
|
| 146 |
-
'credibility_score': self._assign_credibility_score('web', result_url),
|
| 147 |
-
'source_type': 'web'
|
| 148 |
-
})
|
| 149 |
-
|
| 150 |
-
if len(results) >= 3:
|
| 151 |
-
break
|
| 152 |
-
except:
|
| 153 |
continue
|
| 154 |
-
|
| 155 |
-
except Exception as e:
|
| 156 |
-
print(f"DuckDuckGo Lite error: {e}")
|
| 157 |
-
|
| 158 |
-
return results
|
| 159 |
-
|
| 160 |
-
def _search_google_scrape(self, query):
|
| 161 |
-
"""Search using Google scraping (fallback method)"""
|
| 162 |
-
results = []
|
| 163 |
-
|
| 164 |
-
try:
|
| 165 |
-
url = f"https://www.google.com/search?q={quote_plus(query)}"
|
| 166 |
-
headers = {
|
| 167 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
| 168 |
-
}
|
| 169 |
-
|
| 170 |
-
response = requests.get(url, headers=headers, timeout=10)
|
| 171 |
|
| 172 |
-
if response.status_code == 200:
|
| 173 |
-
soup = BeautifulSoup(response.text, 'html.parser')
|
| 174 |
-
|
| 175 |
-
# Find search results
|
| 176 |
-
search_results = soup.find_all('div', class_='g')
|
| 177 |
-
|
| 178 |
-
for result in search_results[:5]:
|
| 179 |
-
try:
|
| 180 |
-
link = result.find('a')
|
| 181 |
-
snippet_div = result.find('div', class_=['VwiC3b', 'lEBKkf'])
|
| 182 |
-
|
| 183 |
-
if link and snippet_div:
|
| 184 |
-
result_url = link.get('href', '')
|
| 185 |
-
title = result.find('h3')
|
| 186 |
-
title_text = title.get_text(strip=True) if title else 'Unknown'
|
| 187 |
-
snippet = snippet_div.get_text(strip=True)
|
| 188 |
-
|
| 189 |
-
if result_url.startswith('http') and snippet:
|
| 190 |
-
results.append({
|
| 191 |
-
'content': snippet,
|
| 192 |
-
'source': f'Web - {title_text}',
|
| 193 |
-
'url': result_url,
|
| 194 |
-
'credibility_score': self._assign_credibility_score('web', result_url),
|
| 195 |
-
'source_type': 'web'
|
| 196 |
-
})
|
| 197 |
-
|
| 198 |
-
if len(results) >= 3:
|
| 199 |
-
break
|
| 200 |
-
except:
|
| 201 |
-
continue
|
| 202 |
-
|
| 203 |
except Exception as e:
|
| 204 |
-
print(f"
|
| 205 |
|
| 206 |
-
return
|
|
|
|
|
|
|
| 1 |
import wikipedia
|
| 2 |
import requests
|
| 3 |
+
from duckduckgo_search import DDGS
|
| 4 |
import time
|
|
|
|
|
|
|
| 5 |
|
| 6 |
class EvidenceRetriever:
|
| 7 |
def __init__(self):
|
|
|
|
| 21 |
wiki_evidence = self._get_wikipedia_evidence(keywords)
|
| 22 |
evidence.extend(wiki_evidence)
|
| 23 |
|
| 24 |
+
# Get evidence from web search (using DuckDuckGo API)
|
| 25 |
web_evidence = self._get_web_search_evidence(keywords)
|
| 26 |
evidence.extend(web_evidence)
|
| 27 |
|
|
|
|
| 86 |
return evidence
|
| 87 |
|
| 88 |
def _get_web_search_evidence(self, keywords):
|
| 89 |
+
"""Retrieve evidence from web search using DuckDuckGo library (Robust)"""
|
| 90 |
evidence = []
|
| 91 |
|
| 92 |
try:
|
| 93 |
query = ' '.join(keywords[:5])
|
| 94 |
|
| 95 |
+
# Use DuckDuckGo Search library
|
| 96 |
+
# max_results=5 to get a good mix
|
| 97 |
+
with DDGS() as ddgs:
|
| 98 |
+
results = list(ddgs.text(query, max_results=5))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
for result in results:
|
| 101 |
try:
|
| 102 |
+
title = result.get('title', 'Unknown')
|
| 103 |
+
snippet = result.get('body', '')
|
| 104 |
+
url = result.get('href', '')
|
| 105 |
|
| 106 |
+
if url and snippet:
|
| 107 |
+
evidence.append({
|
| 108 |
+
'content': snippet,
|
| 109 |
+
'source': f'Web - {title}',
|
| 110 |
+
'url': url,
|
| 111 |
+
'credibility_score': self._assign_credibility_score('web', url),
|
| 112 |
+
'source_type': 'web'
|
| 113 |
+
})
|
| 114 |
+
except Exception as loop_e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
except Exception as e:
|
| 118 |
+
print(f"DuckDuckGo Search error: {e}")
|
| 119 |
|
| 120 |
+
return evidence[:5]
|
requirements.txt
CHANGED
|
@@ -7,7 +7,8 @@ wikipedia
|
|
| 7 |
requests
|
| 8 |
beautifulsoup4
|
| 9 |
sentence-transformers
|
| 10 |
-
transformers
|
| 11 |
scikit-learn
|
| 12 |
numpy
|
|
|
|
| 13 |
https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.7.1/en_core_web_sm-3.7.1-py3-none-any.whl
|
|
|
|
| 7 |
requests
|
| 8 |
beautifulsoup4
|
| 9 |
sentence-transformers
|
| 10 |
+
transformers==4.36.2
|
| 11 |
scikit-learn
|
| 12 |
numpy
|
| 13 |
+
duckduckgo-search
|
| 14 |
https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.7.1/en_core_web_sm-3.7.1-py3-none-any.whl
|