RafaelAgreda commited on
Commit
da5b7b5
·
verified ·
1 Parent(s): 1c6487e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -34
app.py CHANGED
@@ -19,54 +19,38 @@ class AudiobookRecommendation(TypedDict):
19
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
20
  # The next step should be connect directly to a spotify API for example or Audible API, but let's keep it simple for now.
21
  @tool
22
- def get_audiobook_recommendation(category: Optional[str] = None) -> Dict[str, AudiobookRecommendation | str]:
23
  """
24
- Tool function that recommends audiobooks based on specified category.
25
-
26
- Args:
27
- category (Optional[str]): Genre or topic of interest. If None, returns a random recommendation.
28
-
29
- Returns:
30
- Dict[str, AudiobookRecommendation | str]: Dictionary with 'result' (audiobook details) and 'message' (string).
31
  """
32
  audiobook_catalog = {
33
  "science fiction": [
34
  {"title": "Dune", "author": "Frank Herbert", "narrator": "Simon Vance", "length": "21h 8m", "description": "An epic science fiction novel..."},
35
  # ... other books
36
  ],
37
- # ... other categories (omitted for brevity)
38
  }
39
 
40
  try:
41
  if category is not None:
42
  category = category.lower().strip()
43
  if category in audiobook_catalog:
44
- recommendation = random.choice(audiobook_catalog[category])
45
- return {
46
- "result": recommendation,
47
- "message": f"Here's a {category} audiobook recommendation for you!"
48
- }
49
- else:
50
- all_books = [book for books in audiobook_catalog.values() for book in books]
51
- recommendation = random.choice(all_books)
52
- return {
53
- "result": recommendation,
54
- "message": f"Category '{category}' not found. Here's a random recommendation."
55
- }
56
- else:
57
- random_category = random.choice(list(audiobook_catalog.keys()))
58
- recommendation = random.choice(audiobook_catalog[random_category])
59
- return {
60
- "result": recommendation,
61
- "message": f"No category specified. Here's a {random_category} recommendation!"
62
- }
63
- except Exception as e:
64
  all_books = [book for books in audiobook_catalog.values() for book in books]
65
- recommendation = random.choice(all_books)
66
- return {
67
- "result": recommendation,
68
- "message": f"Error occurred: {str(e)}. Here's a random recommendation."
69
- }
 
 
 
 
 
 
70
 
71
  @tool
72
  def get_current_time_in_timezone(timezone: str) -> str:
 
19
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
20
  # The next step should be connect directly to a spotify API for example or Audible API, but let's keep it simple for now.
21
  @tool
22
+ def get_audiobook_recommendation(category: Optional[str] = None) -> AudiobookRecommendation:
23
  """
24
+ Returns a single audiobook recommendation as a flat dictionary.
 
 
 
 
 
 
25
  """
26
  audiobook_catalog = {
27
  "science fiction": [
28
  {"title": "Dune", "author": "Frank Herbert", "narrator": "Simon Vance", "length": "21h 8m", "description": "An epic science fiction novel..."},
29
  # ... other books
30
  ],
31
+ # ... other categories
32
  }
33
 
34
  try:
35
  if category is not None:
36
  category = category.lower().strip()
37
  if category in audiobook_catalog:
38
+ return random.choice(audiobook_catalog[category])
39
+ random_category = random.choice(list(audiobook_catalog.keys()))
40
+ return random.choice(audiobook_catalog[random_category])
41
+ except Exception:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  all_books = [book for books in audiobook_catalog.values() for book in books]
43
+ return random.choice(all_books)
44
+
45
+ # Test with Gradio
46
+ import gradio as gr
47
+
48
+ interface = gr.Interface(
49
+ fn=get_audiobook_recommendation,
50
+ inputs=gr.Textbox(label="Category (optional)"),
51
+ outputs=gr.JSON(label="Recommendation"),
52
+ title="Audiobook Recommender"
53
+ )
54
 
55
  @tool
56
  def get_current_time_in_timezone(timezone: str) -> str: