Spaces:
Sleeping
Sleeping
Update app.py
Browse files41 unmodified lines424344454647484945464748495051525354555652535455565758596061626364655966676869105 unmodified lines17517617717117817918018118218318418518618718818118919019119219319419519619719819920020120220320420520620720820921021121221321421521641 unmodified lines if 'how many' in original_question.lower(): # Look for numbers in the response
number_patterns = [ r'(?:is|are)\s*(\d+)', r'^\s*(\d+)', r'\D(\d+)\D*$', r'(\d+)' album_patterns = [ r'(\d+)\s+album', r'(\d+)\s+studio\s+album', r'is\s+(\d+)', r'are\s+(\d+)' ] for pattern in number_patterns: numbers = re.findall(pattern, cleaned) if numbers: return numbers[0] for pattern in album_patterns: match = re.search(pattern, cleaned, re.IGNORECASE) if match: return match.group(1) # If no specific album pattern found, look for any number that makes sense as a count numbers = re.findall(r'\d+', cleaned) for num in numbers: # Album counts are typically small numbers if int(num) < 20: return num # Last resort: return the first number found if numbers: return numbers[0] # Return the first number found return numbers[0] # If it's asking for a year, try to extract just the year if re.search(r'\b(19|20)\d{2}\b', original_question):105 unmodified lines
class WikipediaSearchTool(Tool): name = "wikipedia_search" description = "Search Wikipedia for information relevant to the user's query." description = "Search Wikipedia for detailed information, including discographies when relevant." inputs = {"query": {"type": "string", "description": "The search query."}} output_type = "string"
def forward(self, query: str) -> str: try: # First try to get the page summary search_url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{requests.utils.quote(query)}" response = requests.get(search_url) if response.status_code == 200: data = response.json() return data.get("extract", "No summary available.") summary = data.get("extract", "") # If this is a discography-related query, try to get more specific information if "album" in query.lower() or "discography" in query.lower(): # Try to search for discography section specifically search_url = f"https://en.wikipedia.org/api/rest_v1/page/html/{requests.utils.quote(query)}" response = requests.get(search_url) if response.status_code == 200: # Simple parsing to find discography section (in a real implementation, # you would want to use a proper HTML parser) content = response.text if "discography" in content.lower(): # Return indication that detailed discography info is available return summary + " Detailed discography information is available for analysis." return summary else: # Try the search API as fallback search_url = f"https://en.wikipedia.org/api/rest_v1/page/search/{requests.utils.quote(query)}" response = requests.get(search_url) if response.status_code == 200: data = response.json() if data.get("pages"): page = data["pages"][0] return page.get("excerpt", "No excerpt available.") return f"Wikipedia search error: {response.status_code}" except Exception as e: return f"Wikipedia search exception: {e}"app.py+44-13