Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
-
from Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
-
from typing import Dict,
|
| 10 |
import random
|
| 11 |
|
| 12 |
class AudiobookRecommendation(TypedDict):
|
|
@@ -16,19 +16,28 @@ class AudiobookRecommendation(TypedDict):
|
|
| 16 |
length: str
|
| 17 |
description: str
|
| 18 |
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
],
|
| 31 |
-
#
|
| 32 |
}
|
| 33 |
|
| 34 |
try:
|
|
@@ -38,48 +47,53 @@ def get_audiobook_recommendation(category: Optional[str] = None) -> AudiobookRec
|
|
| 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 |
@tool
|
| 46 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 47 |
-
"""
|
|
|
|
|
|
|
| 48 |
Args:
|
| 49 |
-
timezone: A
|
|
|
|
|
|
|
|
|
|
| 50 |
"""
|
| 51 |
try:
|
| 52 |
-
# Create timezone object
|
| 53 |
tz = pytz.timezone(timezone)
|
| 54 |
-
# Get current time in that timezone
|
| 55 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 56 |
return f"The current local time in {timezone} is: {local_time}"
|
| 57 |
except Exception as e:
|
| 58 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 59 |
|
| 60 |
-
|
| 61 |
final_answer = FinalAnswerTool()
|
| 62 |
|
| 63 |
-
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 64 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 65 |
-
|
| 66 |
model = HfApiModel(
|
| 67 |
-
max_tokens=2096,
|
| 68 |
-
temperature=0.5,
|
| 69 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 70 |
-
custom_role_conversions=None,
|
| 71 |
)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
# Import tool from Hub
|
| 75 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 76 |
|
|
|
|
| 77 |
with open("prompts.yaml", 'r') as stream:
|
| 78 |
prompt_templates = yaml.safe_load(stream)
|
| 79 |
-
|
|
|
|
| 80 |
agent = CodeAgent(
|
| 81 |
model=model,
|
| 82 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
max_steps=6,
|
| 84 |
verbosity_level=1,
|
| 85 |
grammar=None,
|
|
@@ -89,5 +103,5 @@ agent = CodeAgent(
|
|
| 89 |
prompt_templates=prompt_templates
|
| 90 |
)
|
| 91 |
|
| 92 |
-
|
| 93 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
+
from Gradio_UI import GradioUI # Assuming this is your custom module
|
| 8 |
|
| 9 |
+
from typing import Dict, Optional, TypedDict
|
| 10 |
import random
|
| 11 |
|
| 12 |
class AudiobookRecommendation(TypedDict):
|
|
|
|
| 16 |
length: str
|
| 17 |
description: str
|
| 18 |
|
|
|
|
|
|
|
| 19 |
@tool
|
| 20 |
def get_audiobook_recommendation(category: Optional[str] = None) -> AudiobookRecommendation:
|
| 21 |
"""
|
| 22 |
+
Returns a single audiobook recommendation based on a category.
|
| 23 |
+
|
| 24 |
+
Args:
|
| 25 |
+
category (Optional[str]): The genre or topic of interest for the audiobook recommendation.
|
| 26 |
+
If None, a random category is chosen.
|
| 27 |
+
|
| 28 |
+
Returns:
|
| 29 |
+
AudiobookRecommendation: A dictionary containing the audiobook's title, author, narrator,
|
| 30 |
+
length, and description.
|
| 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 about politics, religion, and ecology on a desert planet."},
|
| 35 |
+
{"title": "Project Hail Mary", "author": "Andy Weir", "narrator": "Ray Porter", "length": "16h 10m", "description": "A lone astronaut must save humanity..."}
|
| 36 |
+
],
|
| 37 |
+
"fantasy": [
|
| 38 |
+
{"title": "The Hobbit", "author": "J.R.R. Tolkien", "narrator": "Andy Serkis", "length": "10h 25m", "description": "Bilbo Baggins' unexpected adventure..."}
|
| 39 |
],
|
| 40 |
+
# Add more categories as needed
|
| 41 |
}
|
| 42 |
|
| 43 |
try:
|
|
|
|
| 47 |
return random.choice(audiobook_catalog[category])
|
| 48 |
random_category = random.choice(list(audiobook_catalog.keys()))
|
| 49 |
return random.choice(audiobook_catalog[random_category])
|
| 50 |
+
except Exception as e:
|
| 51 |
all_books = [book for books in audiobook_catalog.values() for book in books]
|
| 52 |
return random.choice(all_books)
|
| 53 |
|
| 54 |
@tool
|
| 55 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 56 |
+
"""
|
| 57 |
+
Fetches the current local time in a specified timezone.
|
| 58 |
+
|
| 59 |
Args:
|
| 60 |
+
timezone (str): A valid timezone string (e.g., 'America/New_York').
|
| 61 |
+
|
| 62 |
+
Returns:
|
| 63 |
+
str: The current local time in the specified timezone or an error message.
|
| 64 |
"""
|
| 65 |
try:
|
|
|
|
| 66 |
tz = pytz.timezone(timezone)
|
|
|
|
| 67 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 68 |
return f"The current local time in {timezone} is: {local_time}"
|
| 69 |
except Exception as e:
|
| 70 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 71 |
|
|
|
|
| 72 |
final_answer = FinalAnswerTool()
|
| 73 |
|
|
|
|
|
|
|
|
|
|
| 74 |
model = HfApiModel(
|
| 75 |
+
max_tokens=2096,
|
| 76 |
+
temperature=0.5,
|
| 77 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 78 |
+
custom_role_conversions=None,
|
| 79 |
)
|
| 80 |
|
| 81 |
+
# Load external tool
|
|
|
|
| 82 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 83 |
|
| 84 |
+
# Load prompt templates
|
| 85 |
with open("prompts.yaml", 'r') as stream:
|
| 86 |
prompt_templates = yaml.safe_load(stream)
|
| 87 |
+
|
| 88 |
+
# Initialize agent with all tools
|
| 89 |
agent = CodeAgent(
|
| 90 |
model=model,
|
| 91 |
+
tools=[
|
| 92 |
+
final_answer,
|
| 93 |
+
get_audiobook_recommendation,
|
| 94 |
+
get_current_time_in_timezone,
|
| 95 |
+
image_generation_tool
|
| 96 |
+
],
|
| 97 |
max_steps=6,
|
| 98 |
verbosity_level=1,
|
| 99 |
grammar=None,
|
|
|
|
| 103 |
prompt_templates=prompt_templates
|
| 104 |
)
|
| 105 |
|
| 106 |
+
# Launch Gradio UI
|
| 107 |
GradioUI(agent).launch()
|