-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathMusicGeneration.py
99 lines (88 loc) · 2.43 KB
/
MusicGeneration.py
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
"""$
{
"name": "MusicGeneration",
"displayName": "",
"description": "Generate music from prompt",
"inputPattern": {
"type": "object",
"required": [
"music_prompt"
],
"properties": {
"seed": {
"type": "integer",
"description": ""
},
"duration": {
"type": "number",
"description": ""
},
"temperature": {
"type": "number",
"description": ""
},
"music_prompt": {
"type": "string",
"description": ""
}
}
},
"outputPattern": {
"type": "object",
"required": [
"music_url"
],
"properties": {
"music_url": {
"type": "string",
"description": ""
}
}
},
"tag": "VideoGeneration",
"testCases": [
{
"seed": -1,
"duration": 4.9,
"temperature": 1,
"music_prompt": "Create a classical music piece"
}
],
"aiPrompt": "",
"greeting": ""
}
$"""
import json
from os import path
import math
'''import subprocess
command = 'pip install replicate'
process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, universal_newlines=True)'''
import replicate
# Function to create a short music
def create_music(prompt: str, duration: int=15, temperature: float=1, seed: int=-1) -> str:
output = replicate.run(
"meta/musicgen:7be0f12c54a8d033a0fbd14418c9af98962da9a86f5ff7811f9b3423a1f0b7d7",
input={"model_version": "large",
"prompt": prompt,
"duration": duration,
"temperature": temperature,
"seed": seed}
)
return output
def mindsflow_function(event, context) -> dict:
# get the prompt from the event
prompt = event.get("music_prompt")
duration = event.get("duration", 15)
duration = min(duration, 28)
temperature = event.get("temperature", 1)
seed = event.get("seed", -1)
if isinstance(duration, float):
duration = math.ceil(duration) # Convert to int and approximate by excess
# get the music URL
music_url = create_music(prompt, duration, temperature, seed)
# define result
result = {
'music_url': music_url
}
return result