fffiloni commited on
Commit
a3d51f8
1 Parent(s): 0e36c9d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+
3
+ import gradio as gr
4
+ from sentence_transformers import SentenceTransformer
5
+
6
+ import httpx
7
+ import json
8
+
9
+ from utils import get_tags_for_prompts, get_mubert_tags_embeddings, get_pat
10
+
11
+ minilm = SentenceTransformer('all-MiniLM-L6-v2')
12
+ mubert_tags_embeddings = get_mubert_tags_embeddings(minilm)
13
+
14
+
15
+ def get_track_by_tags(tags, pat, duration, maxit=20, loop=False):
16
+ if loop:
17
+ mode = "loop"
18
+ else:
19
+ mode = "track"
20
+ r = httpx.post('https://api-b2b.mubert.com/v2/RecordTrackTTM',
21
+ json={
22
+ "method": "RecordTrackTTM",
23
+ "params": {
24
+ "pat": pat,
25
+ "duration": duration,
26
+ "tags": tags,
27
+ "mode": mode
28
+ }
29
+ })
30
+
31
+ rdata = json.loads(r.text)
32
+ assert rdata['status'] == 1, rdata['error']['text']
33
+ trackurl = rdata['data']['tasks'][0]['download_link']
34
+
35
+ print('Generating track ', end='')
36
+ for i in range(maxit):
37
+ r = httpx.get(trackurl)
38
+ if r.status_code == 200:
39
+ return trackurl
40
+ time.sleep(1)
41
+
42
+
43
+ def generate_track_by_prompt(prompt):
44
+ try:
45
+ pat = get_pat([email protected])
46
+ _, tags = get_tags_for_prompts(minilm, mubert_tags_embeddings, [prompt, ])[0]
47
+ return get_track_by_tags(tags, pat, int(duration), loop=False)
48
+ except Exception as e:
49
+ return None
50
+
51
+
52
+ iface = gr.Interface(fn=generate_track_by_prompt, inputs=["text", "text", gr.Slider(label="duration (seconds)", value=30)], outputs=[gr.Audio(label="Result")])
53
+ iface.launch()