Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,10 @@ from Gradio_UI import GradioUI
|
|
12 |
search_tool = DuckDuckGoSearchTool()
|
13 |
|
14 |
# Load the MusicGen model
|
15 |
-
|
|
|
|
|
|
|
16 |
|
17 |
# Custom tool for AI-assisted music generation
|
18 |
@tool
|
@@ -22,16 +25,23 @@ def generate_music(theme: str) -> str:
|
|
22 |
theme: The theme or mood of the music (e.g., 'moody jazz on a rainy night').
|
23 |
"""
|
24 |
try:
|
|
|
|
|
|
|
25 |
# Generate music using the pipeline
|
26 |
-
output = pipe(theme)
|
27 |
|
|
|
|
|
|
|
28 |
# Extract the generated audio file
|
29 |
-
audio_url = output[0]
|
30 |
|
31 |
if audio_url:
|
32 |
-
return f"Here’s your AI-generated music for '{theme}': {audio_url}"
|
33 |
else:
|
34 |
-
return "Music generation failed
|
|
|
35 |
except Exception as e:
|
36 |
return f"Error generating music: {str(e)}"
|
37 |
|
@@ -39,8 +49,12 @@ def generate_music(theme: str) -> str:
|
|
39 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
40 |
|
41 |
# Load prompts
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
44 |
|
45 |
# Define model
|
46 |
model = HfApiModel(
|
|
|
12 |
search_tool = DuckDuckGoSearchTool()
|
13 |
|
14 |
# Load the MusicGen model
|
15 |
+
try:
|
16 |
+
pipe = pipeline("text-to-audio", model="facebook/musicgen-small")
|
17 |
+
except Exception as e:
|
18 |
+
print(f"Error loading MusicGen model: {str(e)}")
|
19 |
|
20 |
# Custom tool for AI-assisted music generation
|
21 |
@tool
|
|
|
25 |
theme: The theme or mood of the music (e.g., 'moody jazz on a rainy night').
|
26 |
"""
|
27 |
try:
|
28 |
+
if not theme:
|
29 |
+
return "Error: Theme cannot be empty!"
|
30 |
+
|
31 |
# Generate music using the pipeline
|
32 |
+
output = pipe(theme, guidance_scale=1.0) # Set guidance_scale=1.0 to prevent errors
|
33 |
|
34 |
+
if not output or not isinstance(output, list) or len(output) == 0:
|
35 |
+
return "Music generation failed: No output received from the model."
|
36 |
+
|
37 |
# Extract the generated audio file
|
38 |
+
audio_url = output[0].get("audio", None) # Safer way to access audio data
|
39 |
|
40 |
if audio_url:
|
41 |
+
return f"Here’s your AI-generated music for '{theme}': [Click to listen]({audio_url})"
|
42 |
else:
|
43 |
+
return "Music generation failed: No audio URL found."
|
44 |
+
|
45 |
except Exception as e:
|
46 |
return f"Error generating music: {str(e)}"
|
47 |
|
|
|
49 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
50 |
|
51 |
# Load prompts
|
52 |
+
try:
|
53 |
+
with open("prompts.yaml", 'r') as stream:
|
54 |
+
prompt_templates = yaml.safe_load(stream)
|
55 |
+
except FileNotFoundError:
|
56 |
+
print("Warning: prompts.yaml not found. Using default prompts.")
|
57 |
+
prompt_templates = {}
|
58 |
|
59 |
# Define model
|
60 |
model = HfApiModel(
|