File size: 10,185 Bytes
2b7fd6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
140
141
142
143
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<template>
  <div class="svc-container">
    <div class="svc-wrapper">
      <div class="form-panel">
        <h1 class="form-title">基本设置</h1>
        <el-form class="svc-form" :model="initForm" label-width="150px">
          <el-alert type="info" show-icon :closable="false">
            单个文件处理需要提供音频文件,输出会是单个音频文件; <br>
            批量处理支持上传多个音频文件,输出会是一个zip压缩包。
          </el-alert>
          <el-form-item label="运行模式" required>
            <el-select v-model="initForm.mode" placeholder="选择运行模式" default-first-option=default-first-option>
              <el-option label="单个处理" value="single"></el-option>
              <el-option label="批量处理" value="batch"></el-option>
            </el-select>
          </el-form-item>

          <el-alert type="info" show-icon :closable="false">
            运行设备,默认选择cuda,即GPU计算;如果没有支持的GPU设备可以选择cpu。
          </el-alert>
          <el-form-item label="计算设备" required>
            <el-select v-model="initForm.device" placeholder="选择计算设备" default-first-option="default-first-option">
              <el-option label="GPU(cuda)" value="cuda"></el-option>
              <el-option label="CPU" value="cpu"></el-option>
            </el-select>
          </el-form-item>

          <el-alert type="info" show-icon :closable="false">
            选择一个语音模型
          </el-alert>
          <el-form-item label="语音模型" required>
            <el-select v-model="initForm.model" placeholder="选择语音模型">
              <el-option
                  v-for="modelName in this.models"
                  :key="modelName"
                  :label="modelName"
                  :value="modelName"
              />
            </el-select>
          </el-form-item>

          <el-button type="primary" @click="loadModel" v-loading="isLoadingModel">加载模型</el-button>
        </el-form>
      </div>

      <el-divider v-if="displaySVCForm" />

      <div class="form-panel single-run" v-if="displaySingleSVCForm">
        <h1 class="form-title">制作一个歌曲。</h1>
        <h3 class="model-tips">你使用的语音模型是 {{initForm.model}}</h3>
        <el-form class="svc-form single-run-form" :model="singleForm" label-width="150px">
          <el-upload
              ref="audioSingleUpload"
              class="audio-upload"
              action=""
              accept=".wav,.mp3"
              :http-request="handleSingleUpload"
          >
            <template #trigger>
              <el-button type="primary" v-loading="isRuningSingle">选择音频文件并开始制作!</el-button>
            </template>

            <template #tip>
              <div class="el-upload__tip">
                需要是wav文件哦
              </div>
            </template>
          </el-upload>
        </el-form>
      </div>

      <div class="form-panel batch-run" v-if="displayBatchSVCForm">
        <h1 class="form-title">制作多个歌曲。</h1>
        <h3 class="model-tips">你使用的语音模型是 {{initForm.model}}</h3>
        <el-form class="svc-form batch-run-form" :model="batchForm" label-width="150px">
          <el-upload
              ref="audioBatchUpload"
              class="audio-upload"
              action=""
              accept=".wav,.mp3"
              :http-request="mergeBatchFiles"
              multiple
          >
            <template #trigger>
              <el-button type="primary">选择音频文件</el-button>
            </template>

            <el-button type="warning" v-loading="isRunningBatch" @click="handleBatchUpload">开始制作!</el-button>

            <template #tip>
              <div class="el-upload__tip">
                需要是wav文件哦
              </div>
            </template>
          </el-upload>
        </el-form>
      </div>

    </div>

    <GreetingAudio :title="audioTitle" :src="audioSrc" v-if="audioLoaded"></GreetingAudio>
  </div>
</template>

<script>
import axios from "axios";
import JSZip from "jszip";
import GreetingAudio from "@/components/GreetingAudio.vue";

export default {
  name: 'SVCView',
  components: {GreetingAudio},
  data() {
    return {
      models: [],
      initForm: {
        mode: '',
        model: '',
        device: '',
      },
      singleForm: {
        dsid: '',
        srcaudio: null,
      },
      batchForm: {
        dsid: '',
        srcaudio: [],
      },
      displaySVCForm: false,
      displaySingleSVCForm: false,
      displayBatchSVCForm: false,
      isLoadingModel: false,
      isRuningSingle: false,
      isRunningBatch: false,
      audioLoaded: false,
      audioTitle: '',
      audioSrc: '',
      selectedFiles: []
    }
  },
  mounted() {
    this.getModelLists()
  },
  methods: {
    axios,
    async getModelLists() {
      try {
        let resp = (await axios.get('/api/svc/model')).data
        if (resp.code === 200) {
          this.models = resp.data
          console.log(this.models)
        } else {
          this.$message.error('Failed to fetch model information: ' + resp.msg)
        }
      } catch (e) {
        this.$message.error('Failed to fetch model information: ' + e)
      }
    },
    async loadModel() {
      try {
        this.audioLoaded = false
        this.selectedFiles = []
        this.isLoadingModel = true
        let initFormData = new FormData()
        initFormData.append("mode", this.initForm.mode)
        initFormData.append("device", this.initForm.device)
        initFormData.append("model", this.initForm.model)
        if (this.initForm.mode === "" || this.initForm.model === "" || this.initForm.device === "") {
          this.$message.error('都选一下。')
          return
        }
        let resp = (await axios.post('/api/svc/switch', initFormData)).data
        if (resp.code === 200) {
          this.displaySVCForm = true
          console.log(resp.data.mode)
          if (resp.data.mode === "single") {
            this.displaySingleSVCForm = true
            this.displayBatchSVCForm = false
          } else if (resp.data.mode === "batch") {
            this.displaySingleSVCForm = false
            this.displayBatchSVCForm = true
          }
        } else {
          this.$message.error('Failed to load model: ' + resp.msg)
        }
      } catch (e) {
        this.$message.error('Failed to load model: ' + e)
      } finally {
        this.isLoadingModel = false
      }
    },
    async handleSingleUpload(file) {
      try {
        this.isRuningSingle = true
        this.isLoadingModel = true
        console.log("HANDLE UPLOAD")
        console.log(file)
        let srcaudio = file.file
        if (srcaudio) {
          console.log(srcaudio)
          let audioFileName = srcaudio.name
          let runData = new FormData()
          runData.append("srcaudio", srcaudio)
          runData.append("dsid", this.initForm.model)

          let resp = await axios.post('/api/svc/run', runData, {
            responseType: 'arraybuffer'
          })
          if (resp.status === 200) {
            const audioData = resp.data
            if (audioData) {
              const audioBlob = new Blob([audioData], { type: 'audio/wav' });
              const audioUrl = URL.createObjectURL(audioBlob);

              this.audioLoaded = true
              this.audioSrc = audioUrl
              this.audioTitle = audioFileName
            }
          } else {
            this.$message.error('Failed to load model: ' + resp.data.msg)
            this.isRuningSingle = false
            this.isLoadingModel = false
          }
        } else {
          this.$message.error('Error unable to get audio file')
          this.isRuningSingle = false
          this.isLoadingModel = false
        }
      } catch (e) {
        this.$message.error('Failed to run: ' + e)
      } finally {
        this.isRuningSingle = false
        this.isLoadingModel = false
      }
    },
    mergeBatchFiles(file) {
      this.selectedFiles.push(file.file)
    },
    async handleBatchUpload() {
      try {
        this.isRunningBatch = true
        this.isLoadingModel = true

        const audioFiles = this.selectedFiles
        if (audioFiles.length > 0) {
          let runData = new FormData()
          // runData.append('srcaudio', audioFiles)
          for (let i = 0; i < audioFiles.length; i++) {
            runData.append('srcaudio', audioFiles[i])
          }
          runData.append("dsid", this.initForm.model)

          let resp = await axios.post('/api/svc/batch', runData, {
            responseType: 'arraybuffer'
          })
          if (resp.status === 200) {
            const zipBlob = new Blob([resp.data], { type: 'application/zip' });

            // Create a temporary link element to trigger the download
            const link = document.createElement('a');
            link.href = URL.createObjectURL(zipBlob);
            link.download = 'output.zip';

            // Trigger the download
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);

          } else {
            this.$message.error('Failed to load model: ' + resp.data.msg)
            this.isRunningBatch = false
            this.isLoadingModel = false
          }
        } else {
          this.$message.error('Error unable to get audio file')
          this.isRunningBatch = false
          this.isLoadingModel = false
        }
      } catch (e) {
        this.$message.error('Failed to run: ' + e)
      } finally {
        this.isRunningBatch = false
        this.isLoadingModel = false
      }
    },
  }
}
</script>

<style scoped>
.svc-container {
  height: 100%;
  width: 100%;
}

.svc-wrapper {
  top: 60px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  /*background-color: #ccc;*/
}

.svc-form {
  max-width: 460px;
}

.svc-form el-alert {
  margin: 10px auto;
}

.svc-form el-form-item {
  margin-top: 10px;
}

.form-title {
  margin: 20px auto;
}
</style>