summaryrefslogtreecommitdiff
path: root/app/client/modules/samplernn/samplernn.actions.js
blob: 4b8d00b4a845fc390253417a84d5de23815597ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import socket from '../../socket'
import types from '../../types'

import actions from '../../actions'

export const load_directories = () => (dispatch) => {
  // console.log(actions)
  Promise.all([
    actions.folder.index({ module: 'samplernn' }),
    actions.file.index({ module: 'samplernn' }),
    actions.task.index({ module: 'samplernn' }),
    actions.socket.list_directory({ module: 'samplernn', dir: 'results' }),
    actions.socket.list_directory({ module: 'samplernn', dir: 'datasets' }),
  ]).then(res => {
    // console.log(res)
    const [folders, files, tasks, results, datasets] = res

    const folderLookup = folders.reduce((folderLookup, folder) => {
      folderLookup[folder.id] = folder
      folder.files = []
      folder.results = []
      return folderLookup
    }, {
      'unsorted': {
        id: 0,
        name: 'unsorted',
        files: [],
        results: [],
      }
    })

    const processedFiles = files.filter(file => file.processed)
    const unprocessedFiles = files.filter(file => !file.processed)
    const fileLookup = unprocessedFiles.reduce((fileLookup, file) => {
      file.checkpoints = []
      if (! file.name) {
        file.name = (file.opt || {}).token || file.url
        // fileLookup[(file.name || 'unsorted').split('.')[0]] = file
      } else {
        fileLookup[(file.name).split('.')[0]] = file
      }
      folderLookup[file.folder_id] && folderLookup[file.folder_id].files.push(file)
      return fileLookup
    }, {
      unsorted: {
        checkpoints: [],
        results: [],
      }
    })
    processedFiles.map(result => {
      const pair = result.name.split('.')[0].split('-')
      const file = fileLookup[pair[0]]
      if (file) {
        file.results.push(result)
      } else {
        folderLookup[file.folder_id] && folderLookup[file.folder_id].results.push(file)
      }
      result.epoch = result.epoch || pair[1]
    })
    folderLookup.unsorted.files.push(fileLookup.unsorted)

    // console.log(datasets)
    // const flatDatasets = datasets.filter(s => s.name.match(/(wav|aiff?|flac|mp3)$/) && !s.dir)
    const builtDatasets = datasets.filter(s => s.dir)
    builtDatasets.forEach(dataset => {
      const file = fileLookup[dataset.name] || fileLookup.unsorted
      file.hasDataset = true
    })

    // exp:coccokit_3-frame_sizes:8,2-n_rnn:2-dataset:coccokit_3
    const checkpoints = results.filter(s => s.dir).map(s => {
      const checkpoint = s.name
        .split('-')
        .map(s => s.split(':'))
        .filter(b => b.length && b[1])
        .reduce((a,b) => (a[b[0]] = b[1]) && a, {})
      // console.log(checkpoint.dataset)
      checkpoint.name = checkpoint.dataset
      checkpoint.dir = s
      const file = fileLookup[checkpoint.dataset] || fileLookup.unsorted
      file.checkpoints.push(checkpoint)
      return checkpoint
    })

    dispatch({
      type: types.samplernn.init,
      data: {
        folderLookup,
        folders, files,
        checkpoints,
        builtDatasets,
      },
    })
    if (folders.length) {
      dispatch({
        type: types.samplernn.set_folder,
        folder: folders[0],
      })
    }
  }).catch(e => {
    console.error(e)
  })
}

export const load_loss = () => dispatch => {
  actions.socket.run_script({ module: 'samplernn', activity: 'report' })
  .then(report => {
    const lossReport = {}
    report.stdout.split('\n\n').filter(a=>!!a).forEach(data => {
      const [ name, ...lines ] = data.split('\n')
      lossReport[name] = lines
        .map(s =>
          s.split('\t').reduce((a,s) => {
            const b = s.split(': ')
            a[b[0]] = b[1]
            return a
          }, {})
        )
      // console.log(loss[name])
    })
    dispatch({
      type: types.samplernn.load_loss,
      lossReport
    })
  })
}

export const set_folder = (folder) => { types.samplernn.set_folder, folder }

export const fetch_url = (url) => (dispatch) => {
  console.log(url)
  actions.task.start_task({
    activity: 'fetch',
    module: 'samplernn',
    dataset: 'test',
    epochs: 1,
    opt: { url }
  }, { preempt: true, watch: true })
}