|
| 1 | +import { ref, computed, onMounted, inject, watch, nextTick } from "vue" |
| 2 | +import { useClient, useFiles } from "@servicestack/vue" |
| 3 | +import { createErrorStatus } from "@servicestack/client" |
| 4 | +import { SpeechToText } from "dtos" |
| 5 | +import { UiLayout, ThreadStorage, HistoryTitle, HistoryGroups, useUiLayout, icons, acceptedAudios } from "../utils.mjs" |
| 6 | +import FileUpload from "./FileUpload.mjs" |
| 7 | + |
1 | 8 | export default {
|
| 9 | + components: { |
| 10 | + UiLayout, |
| 11 | + HistoryTitle, |
| 12 | + HistoryGroups, |
| 13 | + FileUpload, |
| 14 | + }, |
2 | 15 | template: `
|
3 |
| - <h3 class="p-2 sm:block text-xl md:text-2xl font-semibold">Speech to Text</h3> |
| 16 | + <UiLayout> |
| 17 | + <template #main> |
| 18 | + <div class="flex flex-1 gap-4 text-base md:gap-5 lg:gap-6"> |
| 19 | + <form ref="refForm" :disabled="client.loading.value" class="w-full mb-0" @submit.prevent="send"> |
| 20 | + <div class="relative flex h-full max-w-full flex-1 flex-col"> |
| 21 | + <div class="flex flex-col w-full items-center"> |
| 22 | + <fieldset class="w-full"> |
| 23 | + <ErrorSummary :except="visibleFields" class="mb-4" /> |
| 24 | + <div class="grid grid-cols-6 gap-4"> |
| 25 | + <div class="col-span-6"> |
| 26 | + <FileUpload ref="refImage" id="speech" v-model="request.speech" required |
| 27 | + accept=".mp3,.m4a,.aac,.flac,.wav,.wma" :acceptLabel="acceptedAudios" @change="renderKey++"> |
| 28 | + <template #title> |
| 29 | + <span class="font-semibold text-green-600">Click to upload</span> or drag and drop |
| 30 | + </template> |
| 31 | + <template #icon> |
| 32 | + <svg class="mb-2 h-12 w-12 text-green-500 inline" viewBox="0 0 24 24"> |
| 33 | + <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1" d="M22 12c-.237 5.082-4.622 9.133-9.995 9.133q-.976.001-1.936-.178c-.459-.087-.689-.13-.849-.105c-.16.024-.387.145-.842.386a6.5 6.5 0 0 1-4.226.657a5.3 5.3 0 0 0 1.087-2.348c.1-.53-.147-1.045-.519-1.422C3.034 16.411 2 14.105 2 11.567C2 6.284 6.48 2 12.005 2q.762 0 1.495.106M16 4.5c.491-.506 1.8-2.5 2.5-2.5M21 4.5c-.491-.506-1.8-2.5-2.5-2.5m0 0v8m-6.504 2h.008m3.987 0H16m-8 0h.009" color="currentColor"/> |
| 34 | + </svg> |
| 35 | + </template> |
| 36 | + </FileUpload> |
| 37 | + </div> |
| 38 | + </div> |
| 39 | + </fieldset> |
| 40 | + </div> |
| 41 | + <div class="mt-4 mb-8 flex justify-center"> |
| 42 | + <PrimaryButton :key="renderKey" type="submit" :disabled="!validPrompt()"> |
| 43 | + <svg class="-ml-0.5 h-6 w-6 mr-1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="currentColor" d="M11 16V7.85l-2.6 2.6L7 9l5-5l5 5l-1.4 1.45l-2.6-2.6V16zm-5 4q-.825 0-1.412-.587T4 18v-3h2v3h12v-3h2v3q0 .825-.587 1.413T18 20z"/></svg> |
| 44 | + <span class="text-base font-semibold">Upload</span> |
| 45 | + </PrimaryButton> |
| 46 | + </div> |
| 47 | + </div> |
| 48 | + </form> |
| 49 | + </div> |
| 50 | + |
| 51 | + <div class="pb-20"> |
| 52 | + |
| 53 | + <div v-if="client.loading.value" class="mt-8 mb-20 flex justify-center items-center"> |
| 54 | + <Loading class="text-gray-300 font-normal" imageClass="w-7 h-7 mt-1.5">processing audio...</Loading> |
| 55 | + </div> |
| 56 | +
|
| 57 | + <div v-for="result in getThreadResults()" class="w-full "> |
| 58 | + <div class="flex items-center justify-between"> |
| 59 | + <span class="my-4 flex justify-center items-center text-xl underline-offset-4"> |
| 60 | + <span>{{ result.request.speech }}</span> |
| 61 | + </span> |
| 62 | + <div class="group flex cursor-pointer" @click="discardResult(result)"> |
| 63 | + <div class="ml-1 invisible group-hover:visible">discard</div> |
| 64 | + <svg class="w-6 h-6" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><path fill="currentColor" d="M12 12h2v12h-2zm6 0h2v12h-2z"></path><path fill="currentColor" d="M4 6v2h2v20a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V8h2V6zm4 22V8h16v20zm4-26h8v2h-8z"></path></svg> |
| 65 | + </div> |
| 66 | + </div> |
| 67 | + <div> |
| 68 | + <div v-for="output in result.response.textOutputs.filter(x => !x.text.startsWith('['))" class="relative border border-indigo-600/25 rounded-lg p-2 mb-4 overflow-hidden"> |
| 69 | + <div class="prose">{{output.text}}</div> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + </div> |
| 73 | + </div> |
| 74 | + </template> |
| 75 | + |
| 76 | + <template #sidebar> |
| 77 | + <HistoryTitle :prefix="storage.prefix" /> |
| 78 | + <HistoryGroups :history="history" v-slot="{ item }" @save="saveHistoryItem($event)" @remove="removeHistoryItem($event)"> |
| 79 | + <Icon class="h-5 w-5 rounded-full flex-shrink-0 mr-1" :src="icons.audio" loading="lazy" :alt="item.model" /> |
| 80 | + <span :title="item.title">{{item.title}}</span> |
| 81 | + </HistoryGroups> |
| 82 | + </template> |
| 83 | + </UiLayout> |
4 | 84 | `,
|
5 | 85 | setup() {
|
6 |
| - return {} |
| 86 | + const client = useClient() |
| 87 | + const routes = inject('routes') |
| 88 | + const refUi = ref() |
| 89 | + const refForm = ref() |
| 90 | + const refImage = ref() |
| 91 | + const ui = useUiLayout(refUi) |
| 92 | + const renderKey = ref(0) |
| 93 | + const { filePathUri, getExt, extSrc, svgToDataUri } = useFiles() |
| 94 | + |
| 95 | + const storage = new ThreadStorage(`spch2txt`, { |
| 96 | + tag: '', |
| 97 | + }) |
| 98 | + const error = ref() |
| 99 | + |
| 100 | + const prefs = ref(storage.getPrefs()) |
| 101 | + const history = ref(storage.getHistory()) |
| 102 | + const thread = ref() |
| 103 | + const threadRef = ref() |
| 104 | + |
| 105 | + const validPrompt = () => refForm.value?.speech?.files?.length |
| 106 | + const refMessage = ref() |
| 107 | + const visibleFields = 'speech'.split(',') |
| 108 | + const request = ref(new SpeechToText()) |
| 109 | + |
| 110 | + function savePrefs() { |
| 111 | + storage.savePrefs(Object.assign({}, request.value, { tag:'' })) |
| 112 | + } |
| 113 | + function loadHistory() { |
| 114 | + history.value = storage.getHistory() |
| 115 | + } |
| 116 | + function saveHistory() { |
| 117 | + storage.saveHistory(history.value) |
| 118 | + } |
| 119 | + function saveThread() { |
| 120 | + if (thread.value) { |
| 121 | + storage.saveThread(thread.value) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + async function send() { |
| 126 | + console.log('send', validPrompt(), client.loading.value) |
| 127 | + if (!validPrompt() || client.loading.value) return |
| 128 | + |
| 129 | + savePrefs() |
| 130 | + console.debug(`${storage.prefix}.request`, Object.assign({}, request.value)) |
| 131 | + |
| 132 | + error.value = null |
| 133 | + let formData = new FormData(refForm.value) |
| 134 | + const speech = formData.get('speech').name |
| 135 | + |
| 136 | + const api = await client.apiForm(request.value, formData, { jsconfig: 'eccn' }) |
| 137 | + /** @type {GenerationResponse} */ |
| 138 | + const r = api.response |
| 139 | + if (r) { |
| 140 | + console.debug(`${storage.prefix}.response`, r) |
| 141 | + |
| 142 | + if (!r.textOutputs?.length) { |
| 143 | + error.value = createErrorStatus("no results were returned") |
| 144 | + } else { |
| 145 | + const id = parseInt(routes.id) || storage.createId() |
| 146 | + thread.value = thread.value ?? storage.createThread(Object.assign({ |
| 147 | + id: storage.getThreadId(id), |
| 148 | + title: speech, |
| 149 | + }, request.value)) |
| 150 | + |
| 151 | + const result = { |
| 152 | + id: storage.createId(), |
| 153 | + request: Object.assign({}, request.value, { speech }), |
| 154 | + response: r, |
| 155 | + } |
| 156 | + thread.value.results.push(result) |
| 157 | + saveThread() |
| 158 | + |
| 159 | + if (!history.value.find(x => x.id === id)) { |
| 160 | + history.value.push({ |
| 161 | + id, |
| 162 | + title: thread.value.title, |
| 163 | + ext: getExt(speech), |
| 164 | + }) |
| 165 | + } |
| 166 | + saveHistory() |
| 167 | + |
| 168 | + if (routes.id !== id) { |
| 169 | + routes.to({ id }) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + } else { |
| 174 | + console.error('send.error', api.error) |
| 175 | + error.value = api.error |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + function getThreadResults() { |
| 180 | + const ret = Array.from(thread.value?.results ?? []) |
| 181 | + ret.reverse() |
| 182 | + return ret |
| 183 | + } |
| 184 | + |
| 185 | + function selectRequest(req) { |
| 186 | + Object.assign(request.value, req) |
| 187 | + ui.scrollTop() |
| 188 | + } |
| 189 | + |
| 190 | + function discardResult(result) { |
| 191 | + thread.value.results = thread.value.results.filter(x => x.id != result.id) |
| 192 | + saveThread() |
| 193 | + } |
| 194 | + |
| 195 | + function toggleIcon(item) { |
| 196 | + threadRef.value.icon = item.url |
| 197 | + saveHistory() |
| 198 | + } |
| 199 | + |
| 200 | + function onRouteChange() { |
| 201 | + console.log('onRouteChange', routes.id) |
| 202 | + refImage.value?.clear() |
| 203 | + loadHistory() |
| 204 | + if (routes.id) { |
| 205 | + const id = parseInt(routes.id) |
| 206 | + thread.value = storage.getThread(storage.getThreadId(id)) |
| 207 | + threadRef.value = history.value.find(x => x.id === parseInt(routes.id)) |
| 208 | + console.debug('thread', thread.value, threadRef.value) |
| 209 | + if (thread.value) { |
| 210 | + Object.keys(storage.defaults).forEach(k => |
| 211 | + request.value[k] = thread.value[k] ?? storage.defaults[k]) |
| 212 | + } |
| 213 | + } else { |
| 214 | + thread.value = null |
| 215 | + Object.keys(storage.defaults).forEach(k => request.value[k] = storage.defaults[k]) |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + function updated() { |
| 220 | + onRouteChange() |
| 221 | + } |
| 222 | + |
| 223 | + function saveHistoryItem(item) { |
| 224 | + storage.saveHistory(history.value) |
| 225 | + if (thread.value && item.title) { |
| 226 | + thread.value.title = item.title |
| 227 | + saveThread() |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + function removeHistoryItem(item) { |
| 232 | + const idx = history.value.findIndex(x => x.id === item.id) |
| 233 | + if (idx >= 0) { |
| 234 | + history.value.splice(idx, 1) |
| 235 | + storage.saveHistory(history.value) |
| 236 | + storage.deleteThread(storage.getThreadId(item.id)) |
| 237 | + if (routes.id == item.id) { |
| 238 | + routes.to({ id:undefined }) |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + |
| 244 | + watch(() => routes.id, updated) |
| 245 | + |
| 246 | + onMounted(async () => { |
| 247 | + updated() |
| 248 | + }) |
| 249 | + |
| 250 | + return { |
| 251 | + refForm, |
| 252 | + refImage, |
| 253 | + storage, |
| 254 | + routes, |
| 255 | + client, |
| 256 | + history, |
| 257 | + request, |
| 258 | + visibleFields, |
| 259 | + validPrompt, |
| 260 | + refMessage, |
| 261 | + thread, |
| 262 | + threadRef, |
| 263 | + icons, |
| 264 | + send, |
| 265 | + saveHistory, |
| 266 | + saveThread, |
| 267 | + toggleIcon, |
| 268 | + selectRequest, |
| 269 | + discardResult, |
| 270 | + getThreadResults, |
| 271 | + saveHistoryItem, |
| 272 | + removeHistoryItem, |
| 273 | + acceptedAudios, |
| 274 | + renderKey, |
| 275 | + } |
7 | 276 | }
|
8 | 277 | }
|
0 commit comments