dunktra's picture
Update app.py
c290034 verified
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from transformers import pipeline
from Gradio_UI import GradioUI
# Web search tool to find trending music
search_tool = DuckDuckGoSearchTool()
# Load the MusicGen model
try:
pipe = pipeline("text-to-audio", model="facebook/musicgen-small")
except Exception as e:
print(f"Error loading MusicGen model: {str(e)}")
# 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').
"""
try:
if not theme:
return "Error: Theme cannot be empty!"
# Generate music using the pipeline
output = pipe(theme, guidance_scale=1.0) # Set guidance_scale=1.0 to prevent errors
if not output or not isinstance(output, list) or len(output) == 0:
return "Music generation failed: No output received from the model."
# Extract the generated audio file
audio_url = output[0].get("audio", None) # Safer way to access audio data
if audio_url:
return f"Here’s your AI-generated music for '{theme}': [Click to listen]({audio_url})"
else:
return "Music generation failed: No audio URL found."
except Exception as e:
return f"Error generating music: {str(e)}"
# Tool to generate an album cover
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
# Load prompts
try:
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
except FileNotFoundError:
print("Warning: prompts.yaml not found. Using default prompts.")
prompt_templates = {}
# 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()