Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support multi format in batch transcribe #376

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions desktop/src-tauri/locales/en-US/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"find-here": "Find Here",
"focus-window-on-finish": "Focus window",
"format": "Format",
"formats": "Formats",
"general": "General",
"gpu-device": "GPU Device number",
"high-gpu-performance": "Set Graphics performance to high",
Expand Down
1 change: 1 addition & 0 deletions desktop/src-tauri/locales/fr-FR/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"find-here": "Trouver ici",
"focus-window-on-finish": "Mettre la fenêtre au premier plan",
"format": "Format",
"formats": "Formats",
"general": "Général",
"gpu-device": "Numéro de périphérique GPU",
"high-gpu-performance": "Définir les performances graphiques sur élevé",
Expand Down
1 change: 1 addition & 0 deletions desktop/src-tauri/locales/he-IL/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"find-here": "מצא כאן",
"focus-window-on-finish": "העבר חלון לחזית",
"format": "פורמט",
"formats": "פורמטים",
"general": "כללי",
"gpu-device": "מספר התקן GPU",
"high-gpu-performance": "הגדר את ביצועי הגרפיקה לגבוהים",
Expand Down
1 change: 1 addition & 0 deletions desktop/src-tauri/locales/hi-IN/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"find-here": "यहां खोजें",
"focus-window-on-finish": "विंडो पर ध्यान केंद्रित करें",
"format": "स्वरूप",
"formats": "प्रारूप",
"general": "सामान्य",
"gpu-device": "GPU डिवाइस नंबर",
"high-gpu-performance": "ग्राफिक्स प्रदर्शन को उच्च सेट करें",
Expand Down
1 change: 1 addition & 0 deletions desktop/src-tauri/locales/it-IT/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"find-here": "Trova qui",
"focus-window-on-finish": "Focus finestra",
"format": "Formato",
"formats": "Formati",
"general": "Generale",
"gpu-device": "Numero GPU Device",
"high-gpu-performance": "Imposta il livello delle performance grafiche su Alto",
Expand Down
1 change: 1 addition & 0 deletions desktop/src-tauri/locales/pl-PL/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"find-here": "Znajdź tutaj",
"focus-window-on-finish": "Skupienie na oknie",
"format": "format",
"formats": "Formaty",
"general": "ogólny",
"gpu-device": "Numer urządzenia GPU",
"high-gpu-performance": "Ustaw wydajność grafiki na wysoką",
Expand Down
1 change: 1 addition & 0 deletions desktop/src-tauri/locales/pt-BR/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"find-here": "Encontre aqui",
"focus-window-on-finish": "Focar janela ao terminar",
"format": "Formatar",
"formats": "Formatos",
"general": "Geral",
"gpu-device": "Número do dispositivo GPU",
"high-gpu-performance": "Defina o desempenho gráfico como alto",
Expand Down
1 change: 1 addition & 0 deletions desktop/src-tauri/locales/sv-SE/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"find-here": "Hitta här",
"focus-window-on-finish": "Fokusera Vibe när transkibering är klar",
"format": "Formatera",
"formats": "Format",
"general": "Allmän",
"gpu-device": "GPU-enhetsnummer",
"high-gpu-performance": "Ställ in grafikprestanda på hög",
Expand Down
1 change: 1 addition & 0 deletions desktop/src-tauri/locales/zh-CN/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"find-here": "在这里找到",
"focus-window-on-finish": "转录完成时切换到此应用窗口",
"format": "格式",
"formats": "格式",
"general": "通用",
"gpu-device": "GPU 设备编号",
"high-gpu-performance": "将图形性能设置为高",
Expand Down
54 changes: 54 additions & 0 deletions desktop/src/components/FormatMultiSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Dispatch, SetStateAction } from 'react'
import { useTranslation } from 'react-i18next'

export type TextFormat = 'normal' | 'srt' | 'vtt' | 'html' | 'pdf' | 'json' | 'docx'
export type FormatExtensions = {
[name in TextFormat]: string
}
export const formatExtensions: FormatExtensions = {
normal: '.txt',
srt: '.srt',
vtt: '.vtt',
html: '.html',
pdf: '.pdf',
json: '.json',
docx: '.docx',
}

interface FormatMultiSelectProps {
formats: TextFormat[]
setFormats: Dispatch<SetStateAction<TextFormat[]>>
}
export default function FormatMultiSelect({ formats, setFormats }: FormatMultiSelectProps) {
const { t } = useTranslation()

const handleFormatButtonClick = (formatOption: TextFormat) => {
// Check if the format is already selected
if (formats.includes(formatOption)) {
// If it's selected, remove it
setFormats(formats.filter((format) => format !== formatOption))
} else {
// If it's not selected, add it to the array
setFormats([...formats, formatOption])
}
}

return (
<label className="form-control w-full">
<div className="label">
<span className="label-text">{t('common.formats')}</span>
</div>

<div className="flex flex-wrap gap-2 justify-center">
{['normal', 'srt', 'docx', 'vtt', 'json'].map((formatOption) => (
<button
key={formatOption}
className={`btn btn-xs ${formats.includes(formatOption as TextFormat) ? 'btn-primary' : ''}`}
onClick={() => handleFormatButtonClick(formatOption as TextFormat)}>
{formatOption}
</button>
))}
</div>
</label>
)
}
4 changes: 2 additions & 2 deletions desktop/src/pages/batch/Page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useTranslation } from 'react-i18next'
import FormatSelect from '~/components/FormatSelect'
import LanguageInput from '~/components/LanguageInput'
import Layout from '~/components/Layout'
import ModelOptions from '~/components/Params'
import BatchPanel from './BatchPanel'
import { viewModel } from './viewModel'
import { cx } from '~/lib/utils'
import FormatMultiSelect from '~/components/FormatMultiSelect'

export default function BatchPage() {
const vm = viewModel()
Expand All @@ -16,7 +16,7 @@ export default function BatchPage() {
<div className="m-auto w-[80%] max-w-[300px]">
<LanguageInput />

<FormatSelect setFormat={vm.setFormat} format={vm.format} />
<FormatMultiSelect setFormats={vm.setFormats} formats={vm.formats} />
<ModelOptions options={vm.preference.modelOptions} setOptions={vm.preference.setModelOptions} />

<div className="mt-5">
Expand Down
29 changes: 15 additions & 14 deletions desktop/src/pages/batch/viewModel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import toast from 'react-hot-toast'
export function viewModel() {
const { files, setFiles } = useFilesContext()

const [format, setFormat] = useState<TextFormat>('normal')
const [formats, setFormats] = useState<TextFormat[]>(['normal'])
const [currentIndex, setCurrentIndex] = useState(0)
const [progress, setProgress] = useState<number | null>(null)
const [inProgress, setInProgress] = useState(false)
Expand Down Expand Up @@ -153,18 +153,19 @@ export function viewModel() {
}
}

const dst = await invoke<string>('get_path_dst', { src: file.path, suffix: formatExtensions[format] })
// Write file
if (format === 'docx') {
const fileName = await path.basename(dst)
const doc = await toDocx(fileName, res.segments, preference.textAreaDirection)
const arrayBuffer = await doc.arrayBuffer()
const buffer = new Uint8Array(arrayBuffer)
fs.writeFile(dst, buffer)
} else {
await fs.writeTextFile(dst, getText(res.segments, format))
for (const format of formats) {
const dst = await invoke<string>('get_path_dst', { src: file.path, suffix: formatExtensions[format] })
// Write file
if (format === 'docx') {
const fileName = await path.basename(dst)
const doc = await toDocx(fileName, res.segments, preference.textAreaDirection)
const arrayBuffer = await doc.arrayBuffer()
const buffer = new Uint8Array(arrayBuffer)
fs.writeFile(dst, buffer)
} else {
await fs.writeTextFile(dst, getText(res.segments, format))
}
}

localIndex += 1
await new Promise((resolve) => setTimeout(resolve, 100))
setCurrentIndex(localIndex)
Expand Down Expand Up @@ -230,8 +231,8 @@ export function viewModel() {
cancel,
start,
files,
format,
setFormat,
formats,
setFormats,
preference: preference,
}
}