Spaces:
Sleeping
Sleeping
Commit
·
e825ddc
1
Parent(s):
cb3d5c1
updating
Browse files- app.py +88 -18
- ggml-model-Q4_K_M.gguf +3 -0
- requirements.txt +1 -2
app.py
CHANGED
@@ -1,26 +1,96 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
|
|
4 |
|
5 |
-
tokenizer = AutoTokenizer.from_pretrained("benjamintli/llama3.2_abc_fine_tune")
|
6 |
-
model = AutoModelForCausalLM.from_pretrained("benjamintli/llama3.2_abc_fine_tune")
|
7 |
|
8 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
messages = [
|
10 |
-
{
|
|
|
|
|
|
|
11 |
{"role": "user", "content": f"{input}"},
|
12 |
]
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
)
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from llama_cpp import Llama
|
3 |
+
import urllib
|
4 |
|
5 |
+
# Initialize the Llama model
|
6 |
+
llm = Llama(model_path="ggml-model-Q4_K_M.gguf", chat_format="llama-3")
|
7 |
|
|
|
|
|
8 |
|
9 |
+
def completion_to_song(c):
|
10 |
+
lines = c.split("\n")
|
11 |
+
kept_lines = []
|
12 |
+
notes = False
|
13 |
+
for l in lines:
|
14 |
+
|
15 |
+
# Record if we've hit music
|
16 |
+
if "|" in l:
|
17 |
+
notes = True
|
18 |
+
|
19 |
+
# Stop if we then go back to the start of another song
|
20 |
+
if "X" in l and notes:
|
21 |
+
break
|
22 |
+
if "T" in l and notes:
|
23 |
+
break
|
24 |
+
|
25 |
+
# Stop on an empty line
|
26 |
+
if len(l.strip()) < 2 and notes:
|
27 |
+
break
|
28 |
+
|
29 |
+
# Otherwise keep the line
|
30 |
+
kept_lines.append(l)
|
31 |
+
|
32 |
+
return "\n".join(kept_lines)
|
33 |
+
|
34 |
+
|
35 |
+
def generate_html(input):
|
36 |
+
song = completion_to_song(input)
|
37 |
+
url_song = urllib.parse.quote(song, safe="~@#$&()*!+=:;,?/'")
|
38 |
+
html_song = song.replace("\n", "<br>")
|
39 |
+
html_text = (
|
40 |
+
'<p><a href="https://editor.drawthedots.com?t='
|
41 |
+
+ url_song
|
42 |
+
+ '" target="_blank"><b>EDIT LINK - click to open abcjs editor (allows download and playback)</b></a></p>'
|
43 |
+
)
|
44 |
+
return html_text
|
45 |
+
|
46 |
+
|
47 |
+
# Function to generate ABC notation
|
48 |
+
def generate_abc(input):
|
49 |
messages = [
|
50 |
+
{
|
51 |
+
"role": "system",
|
52 |
+
"content": "you are a helpful assistant that generates ABC notation music. Only use ABC notation music",
|
53 |
+
},
|
54 |
{"role": "user", "content": f"{input}"},
|
55 |
]
|
56 |
+
response = llm.create_chat_completion(messages=messages, temperature=1, top_k=10)
|
57 |
+
return response["choices"][0]["message"][
|
58 |
+
"content"
|
59 |
+
] # Assuming this returns valid ABC notation
|
60 |
+
|
61 |
+
|
62 |
+
# Gradio Blocks Interface
|
63 |
+
with gr.Blocks(
|
64 |
+
head="""<script src="https://cdnjs.cloudflare.com/ajax/libs/abcjs/6.4.4/abcjs-basic-min.js" type="text/javascript"></script>"""
|
65 |
+
) as demo:
|
66 |
+
gr.Markdown("# ABC Notation Generator with LLM")
|
67 |
+
gr.Markdown(
|
68 |
+
"Enter a prompt to generate music in ABC notation, and view the rendered sheet music."
|
69 |
)
|
70 |
+
|
71 |
+
# Input section
|
72 |
+
with gr.Row():
|
73 |
+
user_input = gr.Textbox(
|
74 |
+
label="Enter your prompt",
|
75 |
+
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'",
|
76 |
+
)
|
77 |
+
|
78 |
+
# Output section
|
79 |
+
with gr.Row():
|
80 |
+
abc_output = gr.Textbox(label="Output ABC notation")
|
81 |
+
|
82 |
+
# Button to trigger generation
|
83 |
+
with gr.Row():
|
84 |
+
abc_html = gr.HTML(label="link")
|
85 |
+
submit_btn = gr.Button("Generate Music")
|
86 |
+
|
87 |
+
# Button click logic
|
88 |
+
def on_submit(input_text):
|
89 |
+
abc_notation = generate_abc(input_text) # Generate ABC notation
|
90 |
+
html_output = generate_html(abc_notation)
|
91 |
+
return abc_notation, html_output
|
92 |
+
|
93 |
+
submit_btn.click(on_submit, inputs=user_input, outputs=[abc_output, abc_html])
|
94 |
+
|
95 |
+
# Launch the Gradio app
|
96 |
+
demo.launch()
|
ggml-model-Q4_K_M.gguf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ec2c80fbe6a232fbaaada9065fe1ed945b185ba6de96f3153b3847c47c354c40
|
3 |
+
size 807693984
|
requirements.txt
CHANGED
@@ -1,2 +1 @@
|
|
1 |
-
|
2 |
-
https://github.com/bitsandbytes-foundation/bitsandbytes/releases/download/continuous-release_multi-backend-refactor/bitsandbytes-0.44.1.dev0-py3-none-manylinux_2_24_x86_64.whl
|
|
|
1 |
+
llama-cpp-python
|
|