ACE-Music-Generator / api_usage_example.py
ACloudCenter's picture
Add API endpoint and fix deprecation warnings
009c9f3
"""
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")