Spaces:
Runtime error
Runtime error
Update tools/book_tool.py
Browse files5 unmodified lines6789101112131410 unmodified lines25262728293031323334353637383940414243444546474849505152535455565 unmodified linesname = "search_books"description = "Search for books using the Open Library API"inputs = {'query': {'type': 'string', 'description': 'The search query to find books.'}}output_type = List[Dict]def forward(self, query: str) -> List[Dict]:try:url = f"https://openlibrary.org/search.json?q={query}&limit=5"response = requests.get(url)10 unmodified lines'key': book.get('key', '')}results.append(result)return resultsexcept Exception as e:return [{"error": f"Error searching books: {str(e)}"}]class BookDetailsTool(Tool):name = "get_book_details"description = "Get detailed information about a specific book from Open Library"inputs = {'book_key': {'type': 'string', 'description': 'The Open Library key for the book.'}}output_type = Dictdef forward(self, book_key: str) -> Dict:try:url = f"https://openlibrary.org{book_key}.json"response = requests.get(url)response.raise_for_status()data = response.json()return {'title': data.get('title', 'Unknown Title'),'description': data.get('description', {}).get('value', 'No description available')if isinstance(data.get('description'), dict)else data.get('description', 'No description available'),'subjects': data.get('subjects', [])[:5],'first_sentence': data.get('first_sentence', {}).get('value', 'Not available')if isinstance(data.get('first_sentence'), dict)else data.get('first_sentence', 'Not available')}except Exception as e:return {"error": f"Error fetching book details: {str(e)}"}5 unmodified lines6789101112131410 unmodified lines252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475765 unmodified linesname = "search_books"description = "Search for books using the Open Library API"inputs = {'query': {'type': 'string', 'description': 'The search query to find books.'}}output_type = "string"def forward(self, query: str) -> str:try:url = f"https://openlibrary.org/search.json?q={query}&limit=5"response = requests.get(url)10 unmodified lines'key': book.get('key', '')}results.append(result)# Format results as a stringformatted_results = "## Book Search Results\n\n"for book in results:formatted_results += f"### {book['title']}\n"formatted_results += f"- Author: {book['author']}\n"formatted_results += f"- Year: {book['first_publish_year']}\n"formatted_results += f"- Subjects: {', '.join(book['subject'])}\n\n"return formatted_resultsexcept Exception as e:return f"Error searching books: {str(e)}"class BookDetailsTool(Tool):name = "get_book_details"description = "Get detailed information about a specific book from Open Library"inputs = {'book_key': {'type': 'string', 'description': 'The Open Library key for the book.'}}output_type = "string"def forward(self, book_key: str) -> str:try:url = f"https://openlibrary.org{book_key}.json"response = requests.get(url)response.raise_for_status()data = response.json()description = data.get('description', {}).get('value', 'No description available') \if isinstance(data.get('description'), dict) \else data.get('description', 'No description available')subjects = data.get('subjects', [])[:5]first_sentence = data.get('first_sentence', {}).get('value', 'Not available') \if isinstance(data.get('first_sentence'), dict) \else data.get('first_sentence', 'Not available')# Format as a stringdetails = f"## {data.get('title', 'Unknown Title')}\n\n"details += "### Description\n"details += f"{description}\n\n"details += "### Subjects\n"details += ', '.join(subjects) + "\n\n"details += "### First Sentence\n"details += f"{first_sentence}\n"return detailsexcept Exception as e:return f"Error fetching book details: {str(e)}"
tools/book_tool.py+37-17