File size: 2,951 Bytes
9824e69
e825ddc
 
9824e69
e825ddc
 
c4d699c
45f3287
e825ddc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45f3287
e825ddc
 
 
 
45f3287
 
e825ddc
 
 
 
 
 
 
 
 
 
 
 
 
45f3287
e825ddc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from llama_cpp import Llama
import urllib

# Initialize the Llama model
llm = Llama(model_path="ggml-model-Q4_K_M.gguf", chat_format="llama-3")


def completion_to_song(c):
    lines = c.split("\n")
    kept_lines = []
    notes = False
    for l in lines:

        # Record if we've hit music
        if "|" in l:
            notes = True

        # Stop if we then go back to the start of another song
        if "X" in l and notes:
            break
        if "T" in l and notes:
            break

        # Stop on an empty line
        if len(l.strip()) < 2 and notes:
            break

        # Otherwise keep the line
        kept_lines.append(l)

    return "\n".join(kept_lines)


def generate_html(input):
    song = completion_to_song(input)
    url_song = urllib.parse.quote(song, safe="~@#$&()*!+=:;,?/'")
    html_song = song.replace("\n", "<br>")
    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>'
    )
    return html_text


# Function to generate ABC notation
def generate_abc(input):
    messages = [
        {
            "role": "system",
            "content": "you are a helpful assistant that generates ABC notation music. Only use ABC notation music",
        },
        {"role": "user", "content": f"{input}"},
    ]
    response = llm.create_chat_completion(messages=messages, temperature=1, top_k=10)
    return response["choices"][0]["message"][
        "content"
    ]  # Assuming this returns valid ABC notation


# Gradio Blocks Interface
with gr.Blocks(
    head="""<script src="https://cdnjs.cloudflare.com/ajax/libs/abcjs/6.4.4/abcjs-basic-min.js" type="text/javascript"></script>"""
) as demo:
    gr.Markdown("# ABC Notation Generator with LLM")
    gr.Markdown(
        "Enter a prompt to generate music in ABC notation, and view the rendered sheet music."
    )

    # Input section
    with gr.Row():
        user_input = gr.Textbox(
            label="Enter your prompt",
            value="Design melodic orchestrations by following the indicated chord progression. 'A', 'D', 'E7', 'A', 'E/G#', 'A', 'Bm', 'A7/C#', 'D', 'E7', 'A', 'A', 'D', 'A', 'A', 'D', 'A', 'A', 'D', 'A', 'D', 'A/D#', 'E', 'A', 'D', 'A', 'A', 'D', 'A', 'E7'",
        )

    # Output section
    with gr.Row():
        abc_output = gr.Textbox(label="Output ABC notation")

    # Button to trigger generation
    with gr.Row():
        abc_html = gr.HTML(label="link")
    submit_btn = gr.Button("Generate Music")

    # Button click logic
    def on_submit(input_text):
        abc_notation = generate_abc(input_text)  # Generate ABC notation
        html_output = generate_html(abc_notation)
        return abc_notation, html_output

    submit_btn.click(on_submit, inputs=user_input, outputs=[abc_output, abc_html])

# Launch the Gradio app
demo.launch()