asigalov61 commited on
Commit
86ae8a5
1 Parent(s): 4449b73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -23
app.py CHANGED
@@ -25,39 +25,68 @@ import matplotlib.pyplot as plt
25
  #==========================================================================================================
26
 
27
  def find_midi(title, artist):
28
-
29
  print('=' * 70)
30
- print('Loading MIDI file...')
31
-
32
- #==================================================
33
-
34
- print('Searching titles...Please wait...')
35
- random.shuffle(AUX_DATA)
36
-
37
- titles_index = []
38
-
39
- for A in AUX_DATA:
40
- titles_index.append(A[0])
 
 
 
 
41
 
42
- search_string = ''
 
 
43
 
44
- if title != '' and artist != '':
45
- search_string = title + ' --- ' + artist
46
 
47
- else:
48
- search_string = title + artist
 
 
 
 
 
 
49
 
50
- search_match = process.extract(query=search_string, choices=titles_index, limit=1)
51
- search_index = titles_index.index(search_match[0][0])
52
 
53
- print('Done!')
54
  print('=' * 70)
55
- print('Selected title:', AUX_DATA[search_index][0])
56
  print('=' * 70)
57
 
58
- outy = AUX_DATA[search_index][1]
 
 
 
59
 
60
- print('Sample INTs', outy[:12])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  print('=' * 70)
62
 
63
  if len(outy) != 0:
 
25
  #==========================================================================================================
26
 
27
  def find_midi(title, artist):
28
+
29
  print('=' * 70)
30
+ print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
31
+ start_time = reqtime.time()
32
+
33
+ print('-' * 70)
34
+ print('Req title:', title)
35
+ print('Req artist:', artist)
36
+ print('-' * 70)
37
+
38
+
39
+ input_text = ''
40
+
41
+ if title != '':
42
+ input_text += title
43
+ if artist != '':
44
+ input_text += ' by ' + artist
45
 
46
+ print('Searching...')
47
+
48
+ query_embedding = model.encode([input_text])
49
 
50
+ # Compute cosine similarity between query and each sentence in the corpus
51
+ similarities = util.cos_sim(query_embedding, corpus_embeddings)
52
 
53
+ # Find the index of the most similar sentence
54
+ closest_index = np.argmax(similarities)
55
+ closest_index_match_ratio = max(similarities[0]).tolist()
56
+
57
+ best_corpus_match = all_MIDI_files_names[closest_index]
58
+
59
+ print('Done!')
60
+ print('=' * 70)
61
 
62
+ print('Match corpus index', closest_index)
63
+ print('Match corpus ratio', closest_index_match_ratio)
64
 
 
65
  print('=' * 70)
66
+ print('Done!')
67
  print('=' * 70)
68
 
69
+ song_artist = best_corpus_match[0]
70
+ zlib_file_name = best_corpus_match[1]
71
+
72
+ print('Fetching MIDI score...')
73
 
74
+ with open(zlib_file_name, 'rb') as f:
75
+ compressed_data = f.read()
76
+
77
+ # Decompress the data
78
+ decompressed_data = zlib.decompress(compressed_data)
79
+
80
+ # Convert the bytes back to a list using pickle
81
+ scores_data = pickle.loads(decompressed_data)
82
+
83
+ fnames = [f[0] for f in scores_data]
84
+
85
+ fnameidx = fnames.index(song_artist)
86
+
87
+ MIDI_score_data = scores_data[fnameidx][1]
88
+
89
+ print('Sample INTs', MIDI_score_data[:12])
90
  print('=' * 70)
91
 
92
  if len(outy) != 0: