asigalov61 commited on
Commit
c5824b7
·
verified ·
1 Parent(s): 31464ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -43
app.py CHANGED
@@ -16,21 +16,25 @@ import statistics
16
 
17
  import gradio as gr
18
 
 
 
19
  import TMIDIX
20
 
21
  #==========================================================================================================
22
 
23
- def ID_MIDI(input_midi,
24
- render_type,
25
- soundfont_bank,
26
- render_sample_rate,
27
- custom_render_patch,
28
- render_align,
29
- render_transpose_value,
30
- render_transpose_to_C4,
31
- render_output_as_solo_piano,
32
- render_remove_drums
33
- ):
 
 
34
 
35
  print('*' * 70)
36
  print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
@@ -51,16 +55,6 @@ def ID_MIDI(input_midi,
51
  print('=' * 70)
52
  print('Input MIDI file name:', fn)
53
  print('Input MIDI md5 hash', input_midi_md5hash)
54
- print('-' * 70)
55
- print('Render type:', render_type)
56
- print('Soudnfont bank', soundfont_bank)
57
- print('Audio render sample rate', render_sample_rate)
58
- print('Custom MIDI render patch', custom_render_patch)
59
- print('Align to bars:', render_align)
60
- print('Transpose value:', render_transpose_value)
61
- print('Transpose to C4', render_transpose_to_C4)
62
- print('Output as Solo Piano', render_output_as_solo_piano)
63
- print('Remove drums:', render_remove_drums)
64
  print('=' * 70)
65
  print('Processing MIDI...Please wait...')
66
 
@@ -85,31 +79,41 @@ def ID_MIDI(input_midi,
85
  print('=' * 70)
86
  print('Processing...Please wait...')
87
 
 
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
- print('-' * 70)
91
 
92
- new_md5_hash = hashlib.md5(open(new_fn,'rb').read()).hexdigest()
93
 
94
  print('Done!')
95
  print('=' * 70)
96
 
97
  #========================================================
98
 
99
- output_midi_md5 = str(new_md5_hash)
100
- output_midi_title = str(fn1)
101
- output_midi_summary = str(meta_data)
102
- output_midi = str(new_fn)
103
- output_audio = (srate, audio)
104
-
105
- output_plot = None
106
-
107
- print('Output MIDI file name:', output_midi)
108
- print('Output MIDI title:', output_midi_title)
109
- print('Output MIDI hash:', output_midi_md5)
110
- print('Output MIDI summary:', output_midi_summary[:5])
111
- print('=' * 70)
112
-
113
  #========================================================
114
 
115
  print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
@@ -119,7 +123,7 @@ def ID_MIDI(input_midi,
119
 
120
  #========================================================
121
 
122
- return output_midi_md5, output_midi_title, output_midi_summary, output_midi, output_audio, output_plot
123
 
124
  #==========================================================================================================
125
 
@@ -131,6 +135,11 @@ if __name__ == "__main__":
131
  print('App start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
132
  print('=' * 70)
133
 
 
 
 
 
 
134
  app = gr.Blocks()
135
 
136
  with app:
@@ -150,15 +159,13 @@ if __name__ == "__main__":
150
 
151
  gr.Markdown("## MIDI identification results")
152
 
153
- output_midi_md5 = gr.Textbox(label="Output MIDI md5 hash")
154
- output_midi_title = gr.Textbox(label="Output MIDI title")
155
- output_midi_summary = gr.Textbox(label="Output MIDI summary")
156
 
157
  run_event = submit.click(ID_MIDI, [input_midi,
158
  ],
159
  [output_midi_md5,
160
- output_midi_title,
161
- output_midi_summary,
162
  ])
163
 
164
  app.queue().launch()
 
16
 
17
  import gradio as gr
18
 
19
+ from huggingface_hub import InferenceClient
20
+
21
  import TMIDIX
22
 
23
  #==========================================================================================================
24
 
25
+ def format_table_data(data_string):
26
+ # Split the string into rows based on newlines
27
+ rows = data_string.strip().split("\n")
28
+
29
+ # Split each row into columns based on the separator '|' and strip extra spaces
30
+ data_list = [row.split("|")[1:-1] for row in rows]
31
+ formatted_data = [[cell.strip() for cell in row] for row in data_list]
32
+
33
+ return formatted_data
34
+
35
+ #==========================================================================================================
36
+
37
+ def ID_MIDI(input_midi):
38
 
39
  print('*' * 70)
40
  print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
 
55
  print('=' * 70)
56
  print('Input MIDI file name:', fn)
57
  print('Input MIDI md5 hash', input_midi_md5hash)
 
 
 
 
 
 
 
 
 
 
58
  print('=' * 70)
59
  print('Processing MIDI...Please wait...')
60
 
 
79
  print('=' * 70)
80
  print('Processing...Please wait...')
81
 
82
+ if input_midi_md5hash in MIDID_database:
83
 
84
+ client = InferenceClient(api_key=HF_TOKEN)
85
+
86
+ prompt = "Please create a summary table for a MIDI file based on the following keywords strings. Please add best possible description and best possible summary fields. Please respond with the table only. Do not say anything else. Thank you."
87
+
88
+ messages = [
89
+ {
90
+ "role": "user",
91
+ "content": prompt + "\n\n" + data
92
+ }
93
+ ]
94
+
95
+ completion = client.chat.completions.create(
96
+ #model="Qwen/Qwen2.5-72B-Instruct",
97
+ model="mistralai/Mistral-Nemo-Instruct-2407",
98
+ messages=messages,
99
+ max_tokens=500
100
+ )
101
+
102
+ output_str = completion.choices[0].message['content']
103
+
104
+ output_table_data = format_table_data(output_str)
105
 
106
+ else:
107
 
108
+ output_table_data = None
109
 
110
  print('Done!')
111
  print('=' * 70)
112
 
113
  #========================================================
114
 
115
+ output_midi_md5 = str(input_midi_md5hash)
116
+
 
 
 
 
 
 
 
 
 
 
 
 
117
  #========================================================
118
 
119
  print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
 
123
 
124
  #========================================================
125
 
126
+ return output_midi_md5, output_table_data
127
 
128
  #==========================================================================================================
129
 
 
135
  print('App start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
136
  print('=' * 70)
137
 
138
+ print('Loading MIDID database...')
139
+ MIDID_database = TMIDIX.Tegridy_Any_Pickle_File_Reader('MIDID_Basic_Database_CC_BY_NC_SA.pickle')
140
+
141
+ print('=' * 70)
142
+
143
  app = gr.Blocks()
144
 
145
  with app:
 
159
 
160
  gr.Markdown("## MIDI identification results")
161
 
162
+ output_midi_md5 = gr.Textbox(label="Input MIDI md5 hash")
163
+ output_MIDID_results_table = gr.Table(label="MIDID results table")
 
164
 
165
  run_event = submit.click(ID_MIDI, [input_midi,
166
  ],
167
  [output_midi_md5,
168
+ output_MIDID_results_table
 
169
  ])
170
 
171
  app.queue().launch()