KidIkaros commited on
Commit
87aa658
·
verified ·
1 Parent(s): 0b4544a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -8
app.py CHANGED
@@ -32,21 +32,21 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
32
  @tool
33
  def asoiaf_book(query: str) -> str:
34
  """Handle book queries by ID or name. Returns JSON with {title, authors, pages, characters}
35
-
36
  Args:
37
- query (str): The book ID (1-12) or name (e.g., 'A Game of Thrones')
38
- """
39
  try:
40
  if query.isdigit():
41
  book_id = int(query)
42
  response = requests.get(f"https://anapioficeandfire.com/api/books/{book_id}")
43
  else:
44
  response = requests.get("https://anapioficeandfire.com/api/books")
45
-
46
  if response.status_code == 200:
47
  books = response.json() if isinstance(response.json(), list) else [response.json()]
48
  matched = next((b for b in books if normalize_input(b["name"]) == normalize_input(query)), None)
49
-
50
  if matched:
51
  return json.dumps({
52
  "title": matched["name"],
@@ -54,11 +54,11 @@ def asoiaf_book(query: str) -> str:
54
  "pages": matched["numberOfPages"],
55
  "characters": len(matched["characters"])
56
  })
57
-
58
  return json.dumps({"error": f"No book matching '{query}' found"})
59
-
60
  return json.dumps({"error": "Book API unavailable"})
61
-
62
  except Exception as e:
63
  return json.dumps({"error": f"Book search failed: {str(e)}"})
64
 
 
32
  @tool
33
  def asoiaf_book(query: str) -> str:
34
  """Handle book queries by ID or name. Returns JSON with {title, authors, pages, characters}
35
+
36
  Args:
37
+ query: The book ID (1-12) or name (e.g., 'A Game of Thrones'). <-- Corrected!
38
+ """ # <--- Closing triple quotes here
39
  try:
40
  if query.isdigit():
41
  book_id = int(query)
42
  response = requests.get(f"https://anapioficeandfire.com/api/books/{book_id}")
43
  else:
44
  response = requests.get("https://anapioficeandfire.com/api/books")
45
+
46
  if response.status_code == 200:
47
  books = response.json() if isinstance(response.json(), list) else [response.json()]
48
  matched = next((b for b in books if normalize_input(b["name"]) == normalize_input(query)), None)
49
+
50
  if matched:
51
  return json.dumps({
52
  "title": matched["name"],
 
54
  "pages": matched["numberOfPages"],
55
  "characters": len(matched["characters"])
56
  })
57
+
58
  return json.dumps({"error": f"No book matching '{query}' found"})
59
+
60
  return json.dumps({"error": "Book API unavailable"})
61
+
62
  except Exception as e:
63
  return json.dumps({"error": f"Book search failed: {str(e)}"})
64