Spaces:
Running
on
Zero
Running
on
Zero
File size: 4,817 Bytes
009c9f3 |
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
"""
Example code for calling ACE-Music-Generator from another Hugging Face Space
This shows how to use the ACE-Music-Generator API from your podcast space
or any other Python application.
"""
from gradio_client import Client
import tempfile
import requests
# Method 1: Using Gradio Client (Recommended for Spaces)
def generate_music_from_space(
duration=20,
tags="edm, synth, bass, 128 bpm, energetic",
lyrics="[instrumental]",
space_name="ACloudCenter/ACE-Music-Generator"
):
"""
Generate music using the ACE-Music-Generator space API
Args:
duration: Duration in seconds
tags: Music style tags
lyrics: Lyrics or [instrumental]
space_name: Your Hugging Face space name
Returns:
audio_file_path: Path to downloaded audio file
"""
try:
# Connect to your space
client = Client(space_name)
# Call the generate function
result = client.predict(
duration,
tags,
lyrics,
60, # infer_steps
15.0, # guidance_scale
api_name="/generate"
)
# Result is the path to the audio file
return result
except Exception as e:
print(f"Error generating music: {e}")
return None
# Method 2: Direct HTTP API call
def generate_music_http(
duration=20,
tags="edm, synth, bass, 128 bpm, energetic",
lyrics="[instrumental]",
space_url="https://acloudcenter-ace-music-generator.hf.space"
):
"""
Generate music using direct HTTP API call
Args:
duration: Duration in seconds
tags: Music style tags
lyrics: Lyrics or [instrumental]
space_url: Your space URL
Returns:
audio_file_path: Path to downloaded audio file
"""
import json
api_url = f"{space_url}/run/generate"
payload = {
"data": [
duration,
tags,
lyrics,
60, # infer_steps
15.0, # guidance_scale
]
}
try:
# Make the API call
response = requests.post(api_url, json=payload)
if response.status_code == 200:
result = response.json()
# Download the audio file
audio_url = result["data"][0]["url"]
# Save to temp file
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as f:
audio_response = requests.get(audio_url)
f.write(audio_response.content)
return f.name
else:
print(f"Error: {response.status_code}")
return None
except Exception as e:
print(f"Error generating music: {e}")
return None
# Example usage in your podcast generator
def add_background_music_to_podcast():
"""
Example of how to use in your podcast space
"""
# Generate a 20-second EDM track
music_path = generate_music_from_space(
duration=20,
tags="edm, ambient, soft, background, 100 bpm, calm",
lyrics="[instrumental]"
)
if music_path:
print(f"Generated music saved to: {music_path}")
# Now you can use this in your podcast generation
# For example, mix it with your podcast audio
return music_path
else:
print("Failed to generate music")
return None
# Different music styles you can generate
MUSIC_STYLES = {
"podcast_intro": "upbeat, electronic, professional, 120 bpm, energetic, modern",
"podcast_outro": "calm, ambient, soft, 80 bpm, relaxing, fade out",
"news_background": "minimal, electronic, subtle, 90 bpm, serious, professional",
"commercial": "pop, upbeat, catchy, 128 bpm, happy, commercial",
"dramatic": "orchestral, dramatic, cinematic, 100 bpm, intense, emotional",
"tech": "electronic, futuristic, synth, 110 bpm, innovative, modern",
"chill": "lofi, relaxed, warm, 75 bpm, cozy, background",
}
def generate_podcast_music(style="podcast_intro", duration=15):
"""
Generate music for different podcast segments
Args:
style: One of the predefined styles
duration: Duration in seconds
Returns:
audio_file_path: Path to generated audio
"""
tags = MUSIC_STYLES.get(style, MUSIC_STYLES["podcast_intro"])
return generate_music_from_space(
duration=duration,
tags=tags,
lyrics="[instrumental]"
)
if __name__ == "__main__":
# Test the API
print("Generating test music...")
audio_file = generate_podcast_music(style="podcast_intro", duration=10)
if audio_file:
print(f"Success! Audio saved to: {audio_file}")
else:
print("Failed to generate audio") |