Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
@@ -1,220 +1,152 @@
|
|
1 |
-
import
|
2 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import os
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
from
|
14 |
-
from
|
15 |
-
from
|
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 |
-
query_engine = vector_index.as_chat_engine(chat_memory=chat_summary_memory,storage_context=storage_context,use_async=True,similarity_top_k=2)
|
64 |
-
|
65 |
-
current_refs = ""
|
66 |
-
|
67 |
-
def metadata_from_doc(vec_index: VectorStoreIndex) -> dict:
|
68 |
-
qe = vec_index.as_chat_engine()
|
69 |
-
# f_prompt = """
|
70 |
-
# Given the text excerpts, analyze and provide the document's title and creation date in a structured JSON format. Here are a few examples:
|
71 |
-
|
72 |
-
# In this format:
|
73 |
-
|
74 |
-
# {
|
75 |
-
# "creation_date": "YYYY-MM-DD",
|
76 |
-
# "title": "Title of the Document"
|
77 |
-
# }
|
78 |
-
|
79 |
-
# Text: 'An analysis of historical events. Written by Alex Johnson on 5 March 2019.'
|
80 |
-
# Output: { "title": "An analysis of historical events", "creation_date": "2019-03-05" }
|
81 |
-
|
82 |
-
# Text: 'Exploring the depths of the ocean. This comprehensive guide was authored by Dr. Emily White, published on 10-July 2021.'
|
83 |
-
# Output: { "title": "Exploring the depths of the ocean", "creation_date": "2021-07-10" }
|
84 |
-
|
85 |
-
# Text: 'The history of the Roman Empire.'
|
86 |
-
# Output: { "title": "The history of the Roman Empire", "creation_date": "Unknown" }
|
87 |
-
|
88 |
-
|
89 |
-
# Now, analyze the context from the provided document and generate json object.
|
90 |
-
# """
|
91 |
-
f_prompt ="""give me a only the data when this document was written and title of this document? in json format parameter (created_date,title),
|
92 |
-
example context: 'An analysis of historical events. Written by Alex Johnson on 5 March 2019.'
|
93 |
-
example output: { "title": "An analysis of historical events", "creation_date": "2019-03-05" }
|
94 |
-
now analyse the context make sure to return output only in json format object only.
|
95 |
-
"""
|
96 |
-
res = qe.query(f_prompt)
|
97 |
-
parsed = json.loads(res.response)
|
98 |
-
return parsed
|
99 |
-
|
100 |
-
def filter_unsaved(file_paths:list):
|
101 |
-
for i in file_paths:
|
102 |
-
if os.path.isfile(os.path.join(doc_store_path,os.path.basename(i))):
|
103 |
-
file_paths.remove(i)
|
104 |
-
print("File already exist : {}".format(i))
|
105 |
-
else:
|
106 |
-
shutil.copy2(i,doc_store_path)
|
107 |
-
return file_paths
|
108 |
-
|
109 |
-
def add_doc(file_paths:list):
|
110 |
-
print(file_paths)
|
111 |
-
file_paths = filter_unsaved(file_paths)
|
112 |
-
print(file_paths)
|
113 |
-
if len(file_paths) == 0:
|
114 |
-
return
|
115 |
-
docs = SimpleDirectoryReader(input_files=file_paths).load_data()
|
116 |
-
splitter = SemanticSplitterNodeParser(buffer_size=1, breakpoint_percentile_threshold=95, embed_model=Settings.embed_model,chunk_size=256)
|
117 |
-
nodes = splitter.get_nodes_from_documents(docs)
|
118 |
-
vector_index2 = VectorStoreIndex(nodes)
|
119 |
-
for i in range (5):
|
120 |
-
try:
|
121 |
-
meta = metadata_from_doc(vector_index2)
|
122 |
-
break
|
123 |
-
except:
|
124 |
-
meta = {
|
125 |
-
"title": "Unknown",
|
126 |
-
"creation_date": "Unknown"
|
127 |
-
}
|
128 |
-
continue
|
129 |
-
|
130 |
-
print(meta)
|
131 |
-
for i in range(len(nodes)):
|
132 |
-
nodes[i].metadata.update(meta)
|
133 |
-
vector_index.insert_nodes(nodes)
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
CSS ="""
|
140 |
-
.contain { display: flex; flex-direction: column; }
|
141 |
-
.gradio-container { height: 100vh !important; }
|
142 |
-
#component-0 { height: 100%; }
|
143 |
-
#chatbot { flex-grow: 1; overflow: auto;}
|
144 |
"""
|
145 |
|
146 |
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
with gr.Row():
|
189 |
-
with gr.Column(
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
elem_id="chatbot",
|
206 |
-
bubble_full_width=False,
|
207 |
-
label="ChatDoc",
|
208 |
-
avatar_images=["https://www.freeiconspng.com/thumbs/person-icon-blue/person-icon-blue-25.png","https://cdn-icons-png.flaticon.com/512/8943/8943377.png"],
|
209 |
-
)
|
210 |
-
with gr.Row():
|
211 |
-
textbox = gr.Textbox(label="Type your message", scale=10)
|
212 |
-
clear = gr.Button(value="New Chat", size="sm", scale=1)
|
213 |
-
clear.click(new_chat,[],[textbox, chatbot,references,file_down1,file_down2])
|
214 |
-
textbox.submit(chat, [chatbot, textbox], [textbox, chatbot,references,file_down1,file_down2])
|
215 |
-
|
216 |
-
|
217 |
-
files.upload(file_upload,[files,chatbot],[title,chatbot])
|
218 |
-
|
219 |
-
|
220 |
-
demo.launch(share=True)
|
|
|
1 |
+
import subprocess
|
2 |
+
import sys
|
3 |
+
|
4 |
+
subprocess.check_call([sys.executable,"-m","pip","install",'causal-conv1d'])
|
5 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", 'torch', 'numpy', 'miditok','mamba-ssm','gradio'])
|
6 |
+
subprocess.check_call(["apt-get", "install", "timidity", "-y"])
|
7 |
+
|
8 |
+
# !pip install pretty_midi midi2audio
|
9 |
+
# !pip install miditok
|
10 |
+
# !apt-get install fluidsynth
|
11 |
+
# !apt install timidity -y
|
12 |
+
# !pip install causal-conv1d>=1.1.0
|
13 |
+
# !pip install mamba-ssm
|
14 |
+
# !pip install gradio
|
15 |
+
|
16 |
+
|
17 |
+
|
18 |
+
# !export LC_ALL="en_US.UTF-8"
|
19 |
+
# !export LD_LIBRARY_PATH="/usr/lib64-nvidia"
|
20 |
+
# !export LIBRARY_PATH="/usr/local/cuda/lib64/stubs"
|
21 |
+
|
22 |
+
# subprocess.check_call(['export', 'LC_ALL="en_US.UTF-8"'])
|
23 |
+
# subprocess.check_call(['export', 'LD_LIBRARY_PATH="/usr/lib64-nvidia"'])
|
24 |
+
# subprocess.check_call(['export', 'LIBRARY_PATH="/usr/local/cuda/lib64/stubs"'])
|
25 |
import os
|
26 |
+
|
27 |
+
os.environ['LC_ALL'] = "en_US.UTF-8"
|
28 |
+
os.environ['LD_LIBRARY_PATH'] = "/usr/lib64-nvidia"
|
29 |
+
os.environ['LIBRARY_PATH'] = "/usr/local/cuda/lib64/stubs"
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
import gradio as gr
|
34 |
+
import torch
|
35 |
+
from mamba_ssm import Mamba
|
36 |
+
from mamba_ssm.models.mixer_seq_simple import MambaLMHeadModel
|
37 |
+
from mamba_ssm.models.config_mamba import MambaConfig
|
38 |
+
import numpy as np
|
39 |
+
|
40 |
+
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
41 |
+
if torch.cuda.is_available():
|
42 |
+
subprocess.check_call(['ldconfig', '/usr/lib64-nvidia'])
|
43 |
+
# !ldconfig /usr/lib64-nvidia
|
44 |
+
|
45 |
+
# !wget "https://huggingface.co/krystv/MIDI_Mamba-159M/resolve/main/MIDI_Mamba-159M_1536VS.pt"
|
46 |
+
# !wget "https://huggingface.co/krystv/MIDI_Mamba-159M/resolve/main/tokenizer_1536mix_BPE.json"
|
47 |
+
if os.path.isfile("MIDI_Mamba-159M_1536VS.pt") == False:
|
48 |
+
subprocess.check_call(['wget', 'https://huggingface.co/krystv/MIDI_Mamba-159M/resolve/main/MIDI_Mamba-159M_1536VS.pt'])
|
49 |
+
|
50 |
+
if os.path.isfile("tokenizer_1536mix_BPE.json") == False:
|
51 |
+
subprocess.check_call(['wget', 'https://huggingface.co/krystv/MIDI_Mamba-159M/resolve/main/tokenizer_1536mix_BPE.json'])
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
mc = MambaConfig()
|
56 |
+
mc.d_model = 768
|
57 |
+
mc.n_layer = 42
|
58 |
+
mc.vocab_size = 1536
|
59 |
+
|
60 |
+
from miditok import MIDILike,REMI,TokenizerConfig
|
61 |
+
from pathlib import Path
|
62 |
+
import torch
|
63 |
+
|
64 |
+
tokenizer = REMI(params='tokenizer_1536mix_BPE.json')
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
mf = MambaLMHeadModel(config=mc,device=device)
|
69 |
+
mf.load_state_dict(torch.load("/content/MIDI_Mamba-159M_1536VS.pt",map_location=device))
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
twitter_follow_link = "https://twitter.com/iamhemantindia"
|
74 |
+
instagram_follow_link = "https://instagram.com/iamhemantindia"
|
75 |
+
|
76 |
+
custom_html = f"""
|
77 |
+
<div style='text-align: center;'>
|
78 |
+
<a href="{twitter_follow_link}" target="_blank" style="margin-right: 5px;">
|
79 |
+
<img src="https://img.icons8.com/fluent/24/000000/twitter.png" alt="Follow on Twitter"/>
|
80 |
+
</a>
|
81 |
+
<a href="{instagram_follow_link}" target="_blank">
|
82 |
+
<img src="https://img.icons8.com/fluent/24/000000/instagram-new.png" alt="Follow on Instagram"/>
|
83 |
+
</a>
|
84 |
+
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
"""
|
86 |
|
87 |
|
88 |
+
@spaces.GPU(duration=120)
|
89 |
+
def generate(number,top_k_selector,top_p_selector, temperature_selector):
|
90 |
+
input_ids = torch.tensor([[1,]]).to(device)
|
91 |
+
out = mf.generate(
|
92 |
+
input_ids=input_ids,
|
93 |
+
max_length=int(number),
|
94 |
+
temperature=temperature_selector,
|
95 |
+
top_p=top_p_selector,
|
96 |
+
top_k=top_k_selector,
|
97 |
+
|
98 |
+
eos_token_id=2,)
|
99 |
+
m = tokenizer.decode(np.array(out[0].to('cpu')))
|
100 |
+
np.array(out.to('cpu')).shape
|
101 |
+
m.dump_midi('output.mid')
|
102 |
+
# !timidity output.mid -Ow -o - | ffmpeg -y -f wav -i - output.mp3
|
103 |
+
timidity_cmd = ['timidity', 'output.mid', '-Ow', '-o', 'output.wav']
|
104 |
+
subprocess.check_call(timidity_cmd)
|
105 |
+
|
106 |
+
# Then convert the WAV to MP3 using ffmpeg
|
107 |
+
ffmpeg_cmd = ['ffmpeg', '-y', '-f', 'wav', '-i', 'output.wav', 'output.mp3']
|
108 |
+
subprocess.check_call(ffmpeg_cmd)
|
109 |
+
|
110 |
+
return "output.mp3"
|
111 |
+
|
112 |
+
|
113 |
+
# text_box = gr.Textbox(label="Enter Text")
|
114 |
+
|
115 |
+
|
116 |
+
def generate_and_save(number,top_k_selector,top_p_selector, temperature_selector,generate_button,custom_html_wid):
|
117 |
+
output_audio = generate(number,top_k_selector,top_p_selector, temperature_selector)
|
118 |
+
return gr.Audio(output_audio,autoplay=True),gr.File(label="Download MIDI",value="output.mid"),generate_button
|
119 |
+
|
120 |
+
|
121 |
+
|
122 |
+
|
123 |
+
|
124 |
+
|
125 |
+
# iface = gr.Interface(fn=generate_and_save,
|
126 |
+
# inputs=[number_selector,top_k_selector,top_p_selector, temperature_selector,generate_button,custom_html_wid],
|
127 |
+
# outputs=[output_box,download_midi_button],
|
128 |
+
# title="MIDI Mamba-159M",submit_btn=False,
|
129 |
+
# clear_btn=False,
|
130 |
+
# description="MIDI Mamba is a Mamba based model trained on MIDI data collected from open internet to train music model.",
|
131 |
+
# allow_flagging=False,)
|
132 |
+
|
133 |
+
with gr.Blocks() as b1:
|
134 |
+
gr.Markdown("<h1 style='text-align: center;'>MIDI Mamba-159M <h1/> ")
|
135 |
+
gr.Markdown("<h3 style='text-align: center;'>MIDI Mamba is a Mamba based model trained on MIDI data collected from open internet to train music model. <br> by Hemant Kumar<h3/>")
|
136 |
with gr.Row():
|
137 |
+
with gr.Column():
|
138 |
+
number_selector = gr.Number(label="Select Length of output",value=512)
|
139 |
+
top_p_selector = gr.Slider(label="Select Top P", minimum=0, maximum=1.0, step=0.05, value=0.9)
|
140 |
+
temperature_selector = gr.Slider(label="Select Temperature", minimum=0, maximum=1.0, step=0.1, value=0.9)
|
141 |
+
top_k_selector = gr.Slider(label="Select Top K", minimum=1, maximum=1536, step=1, value=30)
|
142 |
+
generate_button = gr.Button(value="Generate",variant="primary")
|
143 |
+
custom_html_wid = gr.HTML(custom_html)
|
144 |
+
with gr.Column():
|
145 |
+
output_box = gr.Audio("output.mp3",autoplay=True,)
|
146 |
+
download_midi_button = gr.File(label="Download MIDI")
|
147 |
+
generate_button.click(generate_and_save,inputs=[number_selector,top_k_selector,top_p_selector, temperature_selector,generate_button,custom_html_wid],outputs=[output_box,download_midi_button,generate_button])
|
148 |
+
|
149 |
+
|
150 |
+
|
151 |
+
|
152 |
+
b1.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|