Spaces:
Runtime error
Runtime error
Commit
·
3ecf8b3
1
Parent(s):
ca59589
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json, urllib
|
| 3 |
+
from model import GPT, GPTConfig
|
| 4 |
+
from utils import sample
|
| 5 |
+
import torch
|
| 6 |
+
import pickle
|
| 7 |
+
|
| 8 |
+
device = torch.device('cpu')
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# Create the model
|
| 12 |
+
vocab_size=147
|
| 13 |
+
block_size=128
|
| 14 |
+
mconf = GPTConfig(vocab_size, block_size,
|
| 15 |
+
n_layer=6, n_head=8, n_embd=256)
|
| 16 |
+
model = GPT(mconf)
|
| 17 |
+
|
| 18 |
+
# Load checkpoint
|
| 19 |
+
model.load_state_dict(torch.load('another_epoch_1.75total.ckpt', map_location=device))
|
| 20 |
+
|
| 21 |
+
# Vocab
|
| 22 |
+
stoi = pickle.load(open('stoi.pkl', 'rb'))
|
| 23 |
+
itos = pickle.load(open('itos.pkl', 'rb'))
|
| 24 |
+
|
| 25 |
+
# Generate function
|
| 26 |
+
def generate_song(randomize, title, nu, ks, key):
|
| 27 |
+
# Start sequence
|
| 28 |
+
context = b"""T:"""
|
| 29 |
+
|
| 30 |
+
if not randomize:
|
| 31 |
+
context += bytes(title+'\n', 'utf-8')
|
| 32 |
+
context += bytes('M:'+ks+'\n', 'utf-8')
|
| 33 |
+
context += bytes('K:'+key+'\n', 'utf-8')
|
| 34 |
+
context += bytes('L:'+nu+'\n', 'utf-8')
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# Model inputs
|
| 38 |
+
x = torch.tensor([stoi[s] for s in context], dtype=torch.long)[None,...].to(device)
|
| 39 |
+
|
| 40 |
+
# Completion
|
| 41 |
+
y = sample(model, x, 400, temperature=1.0, sample=True, top_k=10)[0]
|
| 42 |
+
completion = ''.join([chr(itos[int(i)]) for i in y])
|
| 43 |
+
|
| 44 |
+
# Return the first song
|
| 45 |
+
song = completion_to_song(completion)
|
| 46 |
+
|
| 47 |
+
html_song = song.replace('\n', '<br>')
|
| 48 |
+
url_song = urllib.parse.quote(song, safe='~@#$&()*!+=:;,?/\'')
|
| 49 |
+
|
| 50 |
+
html_text = '<p><a href="https://editor.drawthedots.com?t='+url_song+'" target="_blank"><b>EDIT LINK - click to open abcjs editor (allows download and playback)</b></a></p>'+"<p>"+html_song+'</p>'
|
| 51 |
+
return html_text
|
| 52 |
+
|
| 53 |
+
# Gradio demo
|
| 54 |
+
demo = gr.Blocks()
|
| 55 |
+
|
| 56 |
+
with demo:
|
| 57 |
+
gr.Markdown("Quick demo for [WhistleGen v2](https://wandb.ai/johnowhitaker/whistlegen_v2/reports/WhistleGen-v2--VmlldzoyMTAwNjAz) which lets you generate folk music using a transformer model. I can't get the javascript needed for rendering and playback working with gradio, so this shows the raw ABC notation from the model and a link to view it properly in an external editor.")
|
| 58 |
+
with gr.Row():
|
| 59 |
+
title = gr.Text(label='Title', value='The Song of AI')
|
| 60 |
+
with gr.Column():
|
| 61 |
+
nu = gr.Text(label='Note unit', value='1/8')
|
| 62 |
+
with gr.Row():
|
| 63 |
+
key_signature = gr.Dropdown(['3/4', '4/4', '6/8', 'Random'], value='4/4', label='Time Signature')
|
| 64 |
+
with gr.Column():
|
| 65 |
+
key = gr.Text(label='Key', value='D')
|
| 66 |
+
with gr.Row():
|
| 67 |
+
randomize = gr.Checkbox(label='Randomize (ignores settings above)', value=True)
|
| 68 |
+
with gr.Row():
|
| 69 |
+
out = gr.HTML(label="Output", value='Output...')
|
| 70 |
+
btn = gr.Button("Run")
|
| 71 |
+
btn.click(fn=generate_song, inputs=[randomize, title, nu, key_signature, key], outputs=out)
|
| 72 |
+
|
| 73 |
+
with gr.Row():
|
| 74 |
+
gr.Markdown("")
|
| 75 |
+
gr.Markdown("This is currently using an early model. See the [report](https://wandb.ai/johnowhitaker/whistlegen_v2/reports/WhistleGen-v2--VmlldzoyMTAwNjAz) for training info and updates.")
|
| 76 |
+
|
| 77 |
+
demo.launch(enable_queue=True)
|