AudioAudit.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <template>
  2. <div class="audio-asr-wrapper">
  3. <div class="content-header">
  4. <div class="title-info">
  5. <h2 class="main-title">音频 ASR 识别</h2>
  6. <p class="sub-title">支持多种格式音频上传,利用 GPU 加速提取语音文本</p>
  7. </div>
  8. </div>
  9. <el-row :gutter="20">
  10. <el-col :xs="24" :sm="24" :md="8" :lg="7">
  11. <el-card shadow="never" class="config-card">
  12. <div slot="header" class="card-header">
  13. <span><i class="el-icon-setting"></i> 任务配置</span>
  14. </div>
  15. <el-form label-position="top">
  16. <el-form-item label="音频上传">
  17. <el-upload
  18. class="audio-uploader-v2"
  19. drag
  20. action=""
  21. :auto-upload="false"
  22. :show-file-list="true"
  23. :limit="1"
  24. accept="audio/*"
  25. :on-change="handleFileChange"
  26. >
  27. <i class="el-icon-upload"></i>
  28. <div class="el-upload__text">拖拽音频到此处 或 <em>点击上传</em></div>
  29. <div class="el-upload__tip" slot="tip">建议格式:mp3, wav (不超过 50MB)</div>
  30. </el-upload>
  31. </el-form-item>
  32. <el-button
  33. type="primary"
  34. class="submit-btn"
  35. :loading="submitting"
  36. :disabled="!file"
  37. @click="submitAsrTask"
  38. >
  39. {{ submitting ? '正在上传...' : '提交识别任务' }}
  40. </el-button>
  41. </el-form>
  42. <transition name="el-zoom-in-top">
  43. <div v-if="taskId" class="task-monitor-box">
  44. <div class="monitor-header">
  45. <span class="status-dot" :class="status"></span>
  46. <span class="status-label">{{ statusText }}</span>
  47. </div>
  48. <div class="task-details">
  49. <p>任务 ID: <code>{{ taskId }}</code></p>
  50. <el-progress
  51. v-if="status !== 'completed' && status !== 'error'"
  52. :percentage="status === 'processing' ? 70 : 30"
  53. :status="status === 'error' ? 'exception' : ''"
  54. :stroke-width="8"
  55. striped
  56. striped-animated
  57. ></el-progress>
  58. </div>
  59. </div>
  60. </transition>
  61. </el-card>
  62. </el-col>
  63. <el-col :xs="24" :sm="24" :md="16" :lg="17">
  64. <el-card shadow="never" v-if="asrResult" class="result-card">
  65. <div slot="header" class="card-header-flex">
  66. <div class="header-left">
  67. <span class="result-title">识别结果</span>
  68. <el-tag size="mini" effect="plain" class="duration-tag">时长: {{ asrResult.duration.toFixed(1) }}s</el-tag>
  69. <el-button
  70. type="text"
  71. size="small"
  72. icon="el-icon-document"
  73. @click="showFullText = true"
  74. >全文预览</el-button>
  75. </div>
  76. <div class="custom-model-tag">
  77. <i class="el-icon-cpu"></i> GPU 识别加速
  78. </div>
  79. </div>
  80. <div class="audio-control-section">
  81. <audio ref="audioPlayer" controls :src="asrResult.audio_url" class="modern-audio-player"></audio>
  82. </div>
  83. <div class="srt-list-wrapper">
  84. <div class="srt-list-header">
  85. <span class="h-time">起始时间</span>
  86. <span class="h-text">识别内容</span>
  87. </div>
  88. <div class="srt-scroll-area">
  89. <div
  90. v-for="(item, index) in asrResult.srt"
  91. :key="index"
  92. class="srt-entry"
  93. @click="seekAudio(item.time)"
  94. >
  95. <div class="entry-time">
  96. <span class="time-badge">{{ formatTimeLabel(item.time) }}</span>
  97. </div>
  98. <div class="entry-content">
  99. <p class="text">{{ item.text }}</p>
  100. <el-tooltip content="复制此行" placement="top">
  101. <i class="el-icon-copy-document copy-icon" @click.stop="handleCopy(item.text)"></i>
  102. </el-tooltip>
  103. </div>
  104. </div>
  105. </div>
  106. </div>
  107. </el-card>
  108. <div v-else class="empty-placeholder">
  109. <el-empty :description="emptyDescription">
  110. <template #image>
  111. <i class="el-icon-service" style="font-size: 60px; color: #dcdfe6;"></i>
  112. </template>
  113. </el-empty>
  114. </div>
  115. </el-col>
  116. </el-row>
  117. <el-dialog
  118. title="识别全文预览"
  119. :visible.sync="showFullText"
  120. width="50%"
  121. custom-class="full-text-dialog"
  122. append-to-body
  123. >
  124. <div class="full-text-body">
  125. {{ asrResult ? asrResult.text : '' }}
  126. </div>
  127. <div slot="footer">
  128. <el-button @click="showFullText = false">关闭</el-button>
  129. <el-button type="primary" icon="el-icon-document-copy" @click="handleCopy(asrResult.text)">复制全文</el-button>
  130. </div>
  131. </el-dialog>
  132. </div>
  133. </template>
  134. <script>
  135. import axios from 'axios';
  136. export default {
  137. name: 'AudioAsrDetail',
  138. data() {
  139. return {
  140. submitting: false,
  141. file: null,
  142. taskId: '',
  143. status: '',
  144. asrResult: null,
  145. timer: null,
  146. showFullText: false
  147. };
  148. },
  149. computed: {
  150. statusText() {
  151. const maps = { 'queued': '队列中', 'processing': '正在提取语音...', 'completed': '识别成功', 'error': '识别失败' };
  152. return maps[this.status] || '等待操作';
  153. },
  154. emptyDescription() {
  155. if (this.status === 'processing' || this.status === 'queued') return '正在努力识别中,请稍后...';
  156. return '暂无数据,请在左侧上传音频文件';
  157. }
  158. },
  159. beforeDestroy() {
  160. this.stopPolling();
  161. },
  162. methods: {
  163. handleFileChange(file) {
  164. this.file = file.raw;
  165. this.taskId = '';
  166. this.status = '';
  167. this.asrResult = null;
  168. this.stopPolling();
  169. },
  170. async submitAsrTask() {
  171. this.submitting = true;
  172. try {
  173. const formData = new FormData();
  174. formData.append('file', this.file);
  175. const res = await axios.post('/api1/audio/asr', formData, {
  176. headers: { 'Content-Type': 'multipart/form-data' }
  177. });
  178. this.taskId = res.data.task_id;
  179. this.status = res.data.status;
  180. this.$message.success('任务已加入 GPU 队列');
  181. this.startPolling();
  182. } catch (error) {
  183. this.$message.error('提交失败');
  184. } finally {
  185. this.submitting = false;
  186. }
  187. },
  188. startPolling() {
  189. this.stopPolling();
  190. this.timer = setInterval(this.fetchAsrResult, 3000);
  191. },
  192. stopPolling() {
  193. if (this.timer) clearInterval(this.timer);
  194. },
  195. async fetchAsrResult() {
  196. try {
  197. const res = await axios.get('/api1/audio/result/' + this.taskId);
  198. if (res.data && (res.data.srt || res.data.text)) {
  199. this.asrResult = res.data;
  200. this.status = 'completed';
  201. this.stopPolling();
  202. }
  203. } catch (e) { /* 继续轮询 */ }
  204. },
  205. // 提取时间戳并跳转播放
  206. seekAudio(timeStr) {
  207. const player = this.$refs.audioPlayer;
  208. if (!player || !timeStr) return;
  209. // 假设格式为 "00:00:01,500 --> ..."
  210. const startPart = timeStr.split('-->')[0].trim().replace(',', '.');
  211. const parts = startPart.split(':');
  212. const seconds = (+parts[0]) * 3600 + (+parts[1]) * 60 + (+parts[2]);
  213. player.currentTime = seconds;
  214. player.play();
  215. },
  216. formatTimeLabel(timeStr) {
  217. return timeStr.split('-->')[0].trim().split(',')[0]; // 简化显示为 HH:mm:ss
  218. },
  219. async handleCopy(text) {
  220. try {
  221. await navigator.clipboard.writeText(text);
  222. this.$message.success('已复制到剪贴板');
  223. } catch (e) {
  224. const input = document.createElement('textarea');
  225. input.value = text;
  226. document.body.appendChild(input);
  227. input.select();
  228. document.execCommand('copy');
  229. document.body.removeChild(input);
  230. this.$message.success('已复制到剪贴板');
  231. }
  232. }
  233. }
  234. };
  235. </script>
  236. <style scoped>
  237. .audio-asr-wrapper {
  238. padding: 0 4px;
  239. }
  240. /* 标题区 */
  241. .content-header {
  242. margin-bottom: 24px;
  243. padding-bottom: 16px;
  244. border-bottom: 1px solid #ebeef5;
  245. }
  246. .main-title {
  247. margin: 0;
  248. font-size: 20px;
  249. color: #303133;
  250. }
  251. .sub-title {
  252. margin: 8px 0 0;
  253. font-size: 13px;
  254. color: #909399;
  255. }
  256. /* 卡片通用 */
  257. .config-card, .result-card {
  258. border-radius: 8px;
  259. border: 1px solid #ebeef5;
  260. }
  261. /* 上传区优化 */
  262. .audio-uploader-v2 {
  263. width: 100%;
  264. }
  265. ::v-deep .el-upload-dragger {
  266. width: 100%;
  267. height: 160px;
  268. }
  269. ::v-deep .el-upload-dragger .el-icon-upload {
  270. margin: 20px 0 10px;
  271. }
  272. .submit-btn {
  273. width: 100%;
  274. padding: 12px;
  275. font-weight: bold;
  276. }
  277. /* 状态监控 */
  278. .task-monitor-box {
  279. margin-top: 20px;
  280. padding: 16px;
  281. background: #f8f9fb;
  282. border-radius: 6px;
  283. }
  284. .monitor-header {
  285. display: flex;
  286. align-items: center;
  287. margin-bottom: 12px;
  288. }
  289. .status-dot {
  290. width: 8px;
  291. height: 8px;
  292. border-radius: 50%;
  293. margin-right: 8px;
  294. background: #909399;
  295. }
  296. .status-dot.processing { background: #409EFF; box-shadow: 0 0 5px #409EFF; }
  297. .status-dot.completed { background: #67C23A; }
  298. .status-label { font-size: 14px; font-weight: 500; }
  299. .task-details { font-size: 12px; color: #606266; }
  300. .task-details code { background: #eee; padding: 2px 4px; border-radius: 3px; }
  301. /* 结果区 Header */
  302. .card-header-flex {
  303. display: flex;
  304. justify-content: space-between;
  305. align-items: center;
  306. }
  307. .result-title {
  308. font-weight: bold;
  309. font-size: 16px;
  310. }
  311. .duration-tag {
  312. margin-left: 12px;
  313. }
  314. /* 播放器 */
  315. .audio-control-section {
  316. background: #f0f2f5;
  317. padding: 12px;
  318. border-radius: 4px;
  319. margin-bottom: 20px;
  320. }
  321. .modern-audio-player {
  322. width: 100%;
  323. }
  324. /* SRT 列表 */
  325. .srt-list-wrapper {
  326. border: 1px solid #f0f0f0;
  327. border-radius: 4px;
  328. }
  329. .srt-list-header {
  330. display: flex;
  331. padding: 10px 16px;
  332. background: #fafafa;
  333. border-bottom: 1px solid #f0f0f0;
  334. color: #909399;
  335. font-size: 13px;
  336. font-weight: 500;
  337. }
  338. .h-time { width: 120px; }
  339. .srt-scroll-area {
  340. max-height: 480px;
  341. overflow-y: auto;
  342. }
  343. .srt-entry {
  344. display: flex;
  345. padding: 14px 16px;
  346. border-bottom: 1px solid #f9f9f9;
  347. cursor: pointer;
  348. transition: all 0.2s;
  349. }
  350. .srt-entry:hover {
  351. background-color: #f5f7fa;
  352. }
  353. .entry-time {
  354. width: 120px;
  355. flex-shrink: 0;
  356. }
  357. .time-badge {
  358. font-family: monospace;
  359. font-size: 12px;
  360. color: #409EFF;
  361. background: #ecf5ff;
  362. padding: 2px 6px;
  363. border-radius: 4px;
  364. }
  365. .entry-content {
  366. flex: 1;
  367. display: flex;
  368. justify-content: space-between;
  369. }
  370. .text {
  371. margin: 0;
  372. font-size: 14px;
  373. line-height: 1.6;
  374. color: #303133;
  375. }
  376. .copy-icon {
  377. margin-left: 10px;
  378. color: #c0c4cc;
  379. cursor: pointer;
  380. visibility: hidden;
  381. }
  382. .srt-entry:hover .copy-icon {
  383. visibility: visible;
  384. }
  385. .copy-icon:hover {
  386. color: #409EFF;
  387. }
  388. /* 渐变标签 */
  389. .custom-model-tag {
  390. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  391. color: white !important;
  392. font-size: 12px;
  393. padding: 4px 12px;
  394. border-radius: 20px;
  395. font-weight: 500;
  396. }
  397. .full-text-body {
  398. white-space: pre-wrap;
  399. line-height: 1.8;
  400. background: #f8f9fb;
  401. padding: 20px;
  402. border-radius: 4px;
  403. max-height: 400px;
  404. overflow-y: auto;
  405. }
  406. .empty-placeholder {
  407. padding: 80px 0;
  408. background: #fff;
  409. border-radius: 8px;
  410. }
  411. </style>