RafaelAgreda commited on
Commit
81ffb99
·
verified ·
1 Parent(s): 591cce9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -16
app.py CHANGED
@@ -6,24 +6,25 @@ import yaml
6
  from tools.final_answer import FinalAnswerTool
7
  from Gradio_UI import GradioUI
8
 
 
 
 
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  # The next step should be connect directly to a spotify API for example or Audible API, but let's keep it simple for now.
12
  @tool
13
- def get_audiobook_recommendation(category=None):
14
  """
15
  Tool function that recommends audiobooks based on specified category.
16
 
17
  Args:
18
- category (str, optional): Genre or topic of interest. If None, returns a random recommendation.
19
 
20
  Returns:
21
- dict: Audiobook recommendation with title, author, and description
22
  """
23
- import random
24
-
25
  # Audiobook catalog organized by category
26
- audiobook_catalog = {
27
  "science fiction": [
28
  {
29
  "title": "Dune",
@@ -125,22 +126,25 @@ def get_audiobook_recommendation(category=None):
125
  try:
126
  # Normalize category input if provided
127
  if category is not None:
128
- if not isinstance(category, str):
129
- category = str(category)
130
-
131
  category = category.lower().strip()
132
 
133
  # Check if category exists in catalog
134
  if category not in audiobook_catalog:
135
  # Try partial matching
136
- matching_categories = [cat for cat in audiobook_catalog.keys() if category in cat]
 
 
 
137
 
138
  if matching_categories:
139
  category = matching_categories[0]
140
  else:
141
  # Return message if no matching category
142
- all_books = [book for booklist in audiobook_catalog.values() for book in booklist]
143
- recommendation = random.choice(all_books)
 
 
 
144
  return {
145
  "result": recommendation,
146
  "message": f"No exact match for '{category}'. Here's a random recommendation instead."
@@ -150,7 +154,7 @@ def get_audiobook_recommendation(category=None):
150
  category = random.choice(list(audiobook_catalog.keys()))
151
 
152
  # Select a random book from the category
153
- recommendation = random.choice(audiobook_catalog[category])
154
 
155
  return {
156
  "result": recommendation,
@@ -158,13 +162,16 @@ def get_audiobook_recommendation(category=None):
158
  }
159
  except Exception as e:
160
  # Provide a fallback recommendation if anything goes wrong
161
- all_books = [book for booklist in audiobook_catalog.values() for book in booklist]
162
- recommendation = random.choice(all_books)
 
 
 
163
  return {
164
  "result": recommendation,
165
  "message": f"Encountered an error: {str(e)}. Here's a random recommendation instead."
166
  }
167
-
168
  @tool
169
  def get_current_time_in_timezone(timezone: str) -> str:
170
  """A tool that fetches the current local time in a specified timezone.
 
6
  from tools.final_answer import FinalAnswerTool
7
  from Gradio_UI import GradioUI
8
 
9
+ from typing import Dict, Union, List, Optional, Any
10
+ import random
11
+
12
 
13
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
14
  # The next step should be connect directly to a spotify API for example or Audible API, but let's keep it simple for now.
15
  @tool
16
+ def get_audiobook_recommendation(category: Optional[str] = None) -> Dict[str, Any]:
17
  """
18
  Tool function that recommends audiobooks based on specified category.
19
 
20
  Args:
21
+ category (Optional[str]): Genre or topic of interest. If None, returns a random recommendation.
22
 
23
  Returns:
24
+ Dict[str, Any]: Audiobook recommendation with title, author, and description
25
  """
 
 
26
  # Audiobook catalog organized by category
27
+ audiobook_catalog: Dict[str, List[Dict[str, str]]] = {
28
  "science fiction": [
29
  {
30
  "title": "Dune",
 
126
  try:
127
  # Normalize category input if provided
128
  if category is not None:
 
 
 
129
  category = category.lower().strip()
130
 
131
  # Check if category exists in catalog
132
  if category not in audiobook_catalog:
133
  # Try partial matching
134
+ matching_categories: List[str] = [
135
+ cat for cat in audiobook_catalog.keys()
136
+ if category in cat
137
+ ]
138
 
139
  if matching_categories:
140
  category = matching_categories[0]
141
  else:
142
  # Return message if no matching category
143
+ all_books: List[Dict[str, str]] = [
144
+ book for booklist in audiobook_catalog.values()
145
+ for book in booklist
146
+ ]
147
+ recommendation: Dict[str, str] = random.choice(all_books)
148
  return {
149
  "result": recommendation,
150
  "message": f"No exact match for '{category}'. Here's a random recommendation instead."
 
154
  category = random.choice(list(audiobook_catalog.keys()))
155
 
156
  # Select a random book from the category
157
+ recommendation: Dict[str, str] = random.choice(audiobook_catalog[category])
158
 
159
  return {
160
  "result": recommendation,
 
162
  }
163
  except Exception as e:
164
  # Provide a fallback recommendation if anything goes wrong
165
+ all_books: List[Dict[str, str]] = [
166
+ book for booklist in audiobook_catalog.values()
167
+ for book in booklist
168
+ ]
169
+ recommendation: Dict[str, str] = random.choice(all_books)
170
  return {
171
  "result": recommendation,
172
  "message": f"Encountered an error: {str(e)}. Here's a random recommendation instead."
173
  }
174
+
175
  @tool
176
  def get_current_time_in_timezone(timezone: str) -> str:
177
  """A tool that fetches the current local time in a specified timezone.