Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
| 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 |
-
|
| 126 |
-
|
| 127 |
-
category
|
| 128 |
-
|
| 129 |
-
|
| 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 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 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:
|