dunktra's picture
Update app.py
fe11f80 verified
raw
history blame
1.75 kB
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
# Web search tool to find trending music
search_tool = DuckDuckGoSearchTool()
# Custom tool for AI-assisted music generation
@tool
def generate_music(theme: str) -> str:
"""Generates an AI-assisted music piece based on the given theme.
Args:
theme: The theme or mood of the music (e.g., 'moody jazz on a rainy night').
"""
response = requests.post(
"https://huggingface.co/api/text-to-music",
json={"prompt": theme}
)
if response.status_code == 200:
return f"Here’s your AI-generated music for '{theme}': {response.json().get('music_url', 'Music not available')}"
return "Failed to generate music. Try another theme!"
# Tool to generate an album cover
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
# Load prompts
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
# Define model
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
custom_role_conversions=None,
)
# Initialize agent
final_answer = FinalAnswerTool()
agent = CodeAgent(
model=model,
tools=[final_answer, search_tool, generate_music, image_generation_tool],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name="Music Inspiration Agent",
description="An agent that generates music and album covers based on user themes.",
prompt_templates=prompt_templates
)
# Launch UI
GradioUI(agent).launch()