psistolar commited on
Commit
544159d
1 Parent(s): 1637cee

Add sentiment scoring to complete sonification.

Browse files
Files changed (1) hide show
  1. app.py +11 -2
app.py CHANGED
@@ -33,7 +33,9 @@ from huggingface_hub import hf_hub_download
33
  print('Downloading model.')
34
  model_cache_path = hf_hub_download(repo_id="psistolar/musicautobot-fine1", filename="model.pth")
35
 
 
36
 
 
37
 
38
  # Default config options
39
  config = default_config()
@@ -62,6 +64,8 @@ def sonify_text(text, sentiment):
62
 
63
  note_names = [f"{letter.upper()}4" for letter in text.lower() if letter in musical_letters]
64
 
 
 
65
  p = music21.stream.Part()
66
  if sentiment == 'NEGATIVE':
67
  # If negative, use TODO
@@ -99,13 +103,18 @@ def process_midi(MIDI_File, Text_to_Sonify, Randomness, Amount_of_Music_to_Add):
99
 
100
  # create the model input object
101
  if sonification:
102
- sentiment = 'NEGATIVE'
 
 
103
  item = sonify_text(Text_to_Sonify, sentiment)
 
 
 
104
  else:
105
  item = MusicItem.from_file(name, data.vocab)
 
106
 
107
  # full is the prediction appended to the input
108
- temp = Randomness / 100
109
  pred, full = learner.predict(
110
  item,
111
  n_words=Amount_of_Music_to_Add,
33
  print('Downloading model.')
34
  model_cache_path = hf_hub_download(repo_id="psistolar/musicautobot-fine1", filename="model.pth")
35
 
36
+ from transformers import pipeline
37
 
38
+ classifier = pipeline("sentiment-analysis")
39
 
40
  # Default config options
41
  config = default_config()
64
 
65
  note_names = [f"{letter.upper()}4" for letter in text.lower() if letter in musical_letters]
66
 
67
+
68
+
69
  p = music21.stream.Part()
70
  if sentiment == 'NEGATIVE':
71
  # If negative, use TODO
103
 
104
  # create the model input object
105
  if sonification:
106
+ sentiment_analysis = classifier(Text_to_Sonify)[0]
107
+ sentiment = sentiment_analysis['label']
108
+ score = sentiment_analysis['score']
109
  item = sonify_text(Text_to_Sonify, sentiment)
110
+ # the lower our confidence in the sentiment, the more randomness we inject
111
+ score = max(0.25, score)
112
+ temp = Randomness / (100 * score)
113
  else:
114
  item = MusicItem.from_file(name, data.vocab)
115
+ temp = Randomness / 100
116
 
117
  # full is the prediction appended to the input
 
118
  pred, full = learner.predict(
119
  item,
120
  n_words=Amount_of_Music_to_Add,