Spaces:
Sleeping
Sleeping
File size: 2,556 Bytes
9b5b26a c19d193 6aae614 8257c70 8fe992b 9b5b26a fe11f80 9b5b26a 8257c70 c290034 8257c70 fe11f80 9b5b26a fe11f80 9b5b26a fe11f80 9b5b26a 8257c70 c290034 8257c70 c290034 8257c70 c290034 8257c70 c290034 8257c70 c290034 8257c70 c290034 8257c70 fe11f80 ae7a494 fe11f80 c290034 ae7a494 fe11f80 e121372 fe11f80 13d500a 8c01ffb fe11f80 8c01ffb 8fe992b fe11f80 8c01ffb fe11f80 861422e 8fe992b 9b5b26a fe11f80 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
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()
|