Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,6 +41,19 @@ def extract_answer(text: str, original_question: str) -> str:
|
|
| 41 |
# If it's a "how many" question, try to extract just the number
|
| 42 |
if 'how many' in original_question.lower():
|
| 43 |
# Look for numbers in the response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
numbers = re.findall(r'\d+', cleaned)
|
| 45 |
if numbers:
|
| 46 |
return numbers[0] # Return the first number found
|
|
@@ -153,6 +166,24 @@ class FileAttachmentQueryTool(Tool):
|
|
| 153 |
except Exception as e:
|
| 154 |
return f"File download error: {e}"
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
# --- Agent Implementation ---
|
| 157 |
def select_model(provider="groq"):
|
| 158 |
"""Select and return a model based on the provider."""
|
|
@@ -187,6 +218,7 @@ class BasicAgent:
|
|
| 187 |
tools = [
|
| 188 |
MathSolver(),
|
| 189 |
FileAttachmentQueryTool(),
|
|
|
|
| 190 |
]
|
| 191 |
self.agent = CodeAgent(
|
| 192 |
model=model,
|
|
|
|
| 41 |
# If it's a "how many" question, try to extract just the number
|
| 42 |
if 'how many' in original_question.lower():
|
| 43 |
# Look for numbers in the response
|
| 44 |
+
|
| 45 |
+
number_patterns = [
|
| 46 |
+
r'(?:is|are)\s*(\d+)',
|
| 47 |
+
r'^\s*(\d+)',
|
| 48 |
+
r'\D(\d+)\D*$',
|
| 49 |
+
r'(\d+)'
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
for pattern in number_patterns:
|
| 53 |
+
numbers = re.findall(pattern, cleaned)
|
| 54 |
+
if numbers:
|
| 55 |
+
return numbers[0]
|
| 56 |
+
|
| 57 |
numbers = re.findall(r'\d+', cleaned)
|
| 58 |
if numbers:
|
| 59 |
return numbers[0] # Return the first number found
|
|
|
|
| 166 |
except Exception as e:
|
| 167 |
return f"File download error: {e}"
|
| 168 |
|
| 169 |
+
class WikipediaSearchTool(Tool):
|
| 170 |
+
name = "wikipedia_search"
|
| 171 |
+
description = "Search Wikipedia for information relevant to the user's query."
|
| 172 |
+
inputs = {"query": {"type": "string", "description": "The search query."}}
|
| 173 |
+
output_type = "string"
|
| 174 |
+
|
| 175 |
+
def forward(self, query: str) -> str:
|
| 176 |
+
try:
|
| 177 |
+
search_url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{requests.utils.quote(query)}"
|
| 178 |
+
response = requests.get(search_url)
|
| 179 |
+
if response.status_code == 200:
|
| 180 |
+
data = response.json()
|
| 181 |
+
return data.get("extract", "No summary available.")
|
| 182 |
+
else:
|
| 183 |
+
return f"Wikipedia search error: {response.status_code}"
|
| 184 |
+
except Exception as e:
|
| 185 |
+
return f"Wikipedia search exception: {e}"
|
| 186 |
+
|
| 187 |
# --- Agent Implementation ---
|
| 188 |
def select_model(provider="groq"):
|
| 189 |
"""Select and return a model based on the provider."""
|
|
|
|
| 218 |
tools = [
|
| 219 |
MathSolver(),
|
| 220 |
FileAttachmentQueryTool(),
|
| 221 |
+
WikipediaSearchTool()
|
| 222 |
]
|
| 223 |
self.agent = CodeAgent(
|
| 224 |
model=model,
|