Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import requests
|
|
| 4 |
import pandas as pd
|
| 5 |
import re
|
| 6 |
import sympy as sp
|
| 7 |
-
|
| 8 |
from bs4 import BeautifulSoup
|
| 9 |
|
| 10 |
# --- Constants ---
|
|
@@ -20,9 +20,7 @@ class BasicAgent:
|
|
| 20 |
if not self.api_token:
|
| 21 |
raise ValueError("HF_TOKEN environment variable not set.")
|
| 22 |
self.headers = {"Authorization": f"Bearer {self.api_token}"}
|
| 23 |
-
|
| 24 |
-
self.wiki = Wikipedia(language='en', user_agent='Mozilla/5.0')
|
| 25 |
-
print("BasicAgent initialized with Flan-T5-Base, SymPy, Wikipedia API, and DuckDuckGo search.")
|
| 26 |
|
| 27 |
def __call__(self, question: str) -> tuple[str, str]:
|
| 28 |
print(f"Question: {question[:50]}...")
|
|
@@ -51,30 +49,29 @@ class BasicAgent:
|
|
| 51 |
|
| 52 |
# Step 2: Try Wikipedia for factual questions
|
| 53 |
try:
|
|
|
|
| 54 |
key_terms = " ".join(question.split()[:5]).strip("?")
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
if
|
| 66 |
-
concise_answer = answer_match.group(1).strip()
|
| 67 |
-
wiki_reasoning = wiki_answer if wiki_answer else "No reasoning provided."
|
| 68 |
-
else:
|
| 69 |
-
concise_answer = self._extract_concise_answer(wiki_answer)
|
| 70 |
-
wiki_reasoning = wiki_answer
|
| 71 |
-
reasoning.append(f"Wikipedia API: Searched '{key_terms}'. Summary: {wiki_summary[:100]}... Answer: {concise_answer}")
|
| 72 |
-
print(f"Returning concise answer (Wikipedia): {concise_answer}")
|
| 73 |
-
return concise_answer, "\n".join(reasoning)
|
| 74 |
else:
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
except Exception as e:
|
| 77 |
-
reasoning.append(f"Wikipedia
|
| 78 |
|
| 79 |
# Step 3: Try web search with DuckDuckGo
|
| 80 |
try:
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
import re
|
| 6 |
import sympy as sp
|
| 7 |
+
import wikipedia
|
| 8 |
from bs4 import BeautifulSoup
|
| 9 |
|
| 10 |
# --- Constants ---
|
|
|
|
| 20 |
if not self.api_token:
|
| 21 |
raise ValueError("HF_TOKEN environment variable not set.")
|
| 22 |
self.headers = {"Authorization": f"Bearer {self.api_token}"}
|
| 23 |
+
print("BasicAgent initialized with Flan-T5-Base, SymPy, Wikipedia, and DuckDuckGo search.")
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def __call__(self, question: str) -> tuple[str, str]:
|
| 26 |
print(f"Question: {question[:50]}...")
|
|
|
|
| 49 |
|
| 50 |
# Step 2: Try Wikipedia for factual questions
|
| 51 |
try:
|
| 52 |
+
wikipedia.set_lang("en")
|
| 53 |
key_terms = " ".join(question.split()[:5]).strip("?")
|
| 54 |
+
wiki_summary = wikipedia.summary(key_terms, sentences=2, auto_suggest=True, parser_features="html.parser")
|
| 55 |
+
prompt = (
|
| 56 |
+
f"Question: {question}\n"
|
| 57 |
+
f"Context: {wiki_summary}\n"
|
| 58 |
+
"Provide a concise answer (e.g., a number or short phrase) based on the context."
|
| 59 |
+
)
|
| 60 |
+
wiki_answer = self._query_llm(prompt)
|
| 61 |
+
answer_match = re.search(r"Answer: (.*?)(?:\n|$)", wiki_answer, re.DOTALL)
|
| 62 |
+
if answer_match:
|
| 63 |
+
concise_answer = answer_match.group(1).strip()
|
| 64 |
+
wiki_reasoning = wiki_answer if wiki_answer else "No reasoning provided."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
else:
|
| 66 |
+
concise_answer = self._extract_concise_answer(wiki_answer)
|
| 67 |
+
wiki_reasoning = wiki_answer
|
| 68 |
+
reasoning.append(f"Wikipedia: Searched '{key_terms}'. Summary: {wiki_summary[:100]}... Answer: {concise_answer}")
|
| 69 |
+
print(f"Returning concise answer (Wikipedia): {concise_answer}")
|
| 70 |
+
return concise_answer, "\n".join(reasoning)
|
| 71 |
+
except wikipedia.exceptions.DisambiguationError:
|
| 72 |
+
reasoning.append("Wikipedia: Disambiguation error, multiple results found.")
|
| 73 |
except Exception as e:
|
| 74 |
+
reasoning.append(f"Wikipedia failed: {e}")
|
| 75 |
|
| 76 |
# Step 3: Try web search with DuckDuckGo
|
| 77 |
try:
|