RafaelAgreda commited on
Commit
591cce9
·
verified ·
1 Parent(s): 82bd6be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -30
app.py CHANGED
@@ -10,7 +10,7 @@ from Gradio_UI import GradioUI
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: str) -> str:
14
  """
15
  Tool function that recommends audiobooks based on specified category.
16
 
@@ -122,36 +122,48 @@ def get_audiobook_recommendation(category: str) -> str:
122
  ]
123
  }
124
 
125
- # Normalize category input if provided
126
- if category:
127
- category = category.lower().strip()
128
-
129
- # Check if category exists in catalog
130
- if category not in audiobook_catalog:
131
- # Try partial matching
132
- matching_categories = [cat for cat in audiobook_catalog.keys() if category in cat]
133
 
134
- if matching_categories:
135
- category = matching_categories[0]
136
- else:
137
- # Return message if no matching category
138
- all_books = [book for booklist in audiobook_catalog.values() for book in booklist]
139
- recommendation = random.choice(all_books)
140
- return {
141
- "result": recommendation,
142
- "message": f"No exact match for '{category}'. Here's a random recommendation instead."
143
- }
144
- else:
145
- # If no category specified, choose random category
146
- category = random.choice(list(audiobook_catalog.keys()))
147
-
148
- # Select a random book from the category
149
- recommendation = random.choice(audiobook_catalog[category])
150
-
151
- return {
152
- "result": recommendation,
153
- "message": f"Here's a {category} audiobook recommendation for you!"
154
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
 
156
  @tool
157
  def get_current_time_in_timezone(timezone: str) -> str:
 
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
 
 
122
  ]
123
  }
124
 
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."
147
+ }
148
+ else:
149
+ # If no category specified, choose random category
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,
157
+ "message": f"Here's a {category} audiobook recommendation for you!"
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: