-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* AI Speech generation
- Loading branch information
Showing
19 changed files
with
339 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 9 additions & 4 deletions
13
src/lib/platform/browser/use-main-button-progress-browser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { observer } from "mobx-react-lite"; | ||
import { CardFormType } from "./store/deck-form-store.ts"; | ||
import { useBackButton } from "../../lib/platform/use-back-button.ts"; | ||
import { Screen } from "../shared/screen.tsx"; | ||
import { AudioPlayer } from "../../ui/audio-player.tsx"; | ||
import { useMainButton } from "../../lib/platform/use-main-button.ts"; | ||
import { t } from "../../translations/t.ts"; | ||
import { Button } from "../../ui/button.tsx"; | ||
import { ButtonGrid } from "../../ui/button-grid.tsx"; | ||
import { css } from "@emotion/css"; | ||
import { theme } from "../../ui/theme.tsx"; | ||
import { Flex } from "../../ui/flex.tsx"; | ||
import { Chip } from "../../ui/chip.tsx"; | ||
import { Input } from "../../ui/input.tsx"; | ||
import { useState } from "react"; | ||
import { AiSpeechGeneratorStore } from "./store/ai-speech-generator-store.ts"; | ||
|
||
type Props = { | ||
cardForm: CardFormType; | ||
onBack: () => void; | ||
}; | ||
|
||
export const CardAiSpeech = observer((props: Props) => { | ||
const { cardForm, onBack } = props; | ||
|
||
const [store] = useState(() => new AiSpeechGeneratorStore(cardForm)); | ||
const { form } = store; | ||
|
||
useBackButton(() => { | ||
onBack(); | ||
}); | ||
|
||
useMainButton(t("go_back"), () => { | ||
onBack(); | ||
}); | ||
|
||
return ( | ||
<Screen title={t("ai_speech_title")}> | ||
{cardForm.options.value?.voice ? ( | ||
<> | ||
<AudioPlayer src={cardForm.options.value.voice} /> | ||
<ButtonGrid> | ||
<Button | ||
icon={"mdi-delete"} | ||
outline | ||
onClick={() => { | ||
store.onDeleteAiVoice(); | ||
}} | ||
> | ||
{t("delete")} | ||
</Button> | ||
</ButtonGrid> | ||
</> | ||
) : ( | ||
<> | ||
<div | ||
className={css({ | ||
alignSelf: "center", | ||
textAlign: "center", | ||
display: "flex", | ||
flexDirection: "column", | ||
gap: 4, | ||
color: theme.hintColor, | ||
width: "100%", | ||
fontSize: 14, | ||
})} | ||
> | ||
<span>{t("ai_speech_empty")}</span> | ||
<Flex gap={8}> | ||
{(["front", "back"] as const).map((side) => { | ||
return ( | ||
<Chip | ||
key={side} | ||
fullWidth | ||
isSelected={side === form.sourceSide.value} | ||
onClick={() => { | ||
form.sourceSide.onChange(side); | ||
}} | ||
> | ||
{t(side)} | ||
</Chip> | ||
); | ||
})} | ||
</Flex> | ||
<span>{t("ai_speech_type")}</span> | ||
<Input field={form.sourceText} /> | ||
</div> | ||
|
||
<Button | ||
disabled={store.isLoading} | ||
icon={store.isLoading ? "mdi-loading mdi-spin" : undefined} | ||
outline | ||
onClick={() => { | ||
store.generate(); | ||
}} | ||
> | ||
{store.isLoading ? undefined : t("ai_speech_generate")} | ||
</Button> | ||
</> | ||
)} | ||
</Screen> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { RequestStore } from "../../../lib/mobx-request/request-store.ts"; | ||
import { aiSpeechGenerateRequest } from "../../../api/api.ts"; | ||
import { formTouchAll, isFormValid, TextField } from "mobx-form-lite"; | ||
import { CardFormType } from "./deck-form-store.ts"; | ||
import { makeAutoObservable } from "mobx"; | ||
import { notifyError } from "../../shared/snackbar/snackbar.tsx"; | ||
import { t } from "../../../translations/t.ts"; | ||
|
||
export class AiSpeechGeneratorStore { | ||
speechGenerateRequest = new RequestStore(aiSpeechGenerateRequest); | ||
|
||
form = { | ||
sourceText: new TextField("", { | ||
validate: (value) => { | ||
if (!value && !this.form.sourceSide.value) { | ||
return t("ai_speech_validate"); | ||
} | ||
}, | ||
afterChange: (newValue) => { | ||
if (newValue && this.form.sourceSide.value !== null) { | ||
this.form.sourceSide.onChange(null); | ||
} | ||
}, | ||
}), | ||
sourceSide: new TextField<"front" | "back" | null>(null), | ||
}; | ||
|
||
constructor(public cardForm: CardFormType) { | ||
makeAutoObservable( | ||
this, | ||
{ | ||
cardForm: false, | ||
}, | ||
{ autoBind: true }, | ||
); | ||
} | ||
|
||
get isLoading() { | ||
return this.speechGenerateRequest.isLoading; | ||
} | ||
|
||
async generate() { | ||
if (!isFormValid(this.form)) { | ||
formTouchAll(this.form); | ||
return; | ||
} | ||
|
||
const text = (() => { | ||
if (this.form.sourceText.value) { | ||
return this.form.sourceText.value; | ||
} | ||
if (this.form.sourceSide.value) { | ||
return this.cardForm[this.form.sourceSide.value].value; | ||
} | ||
throw new Error("Unexpected state"); | ||
})(); | ||
|
||
const result = await this.speechGenerateRequest.execute({ | ||
text, | ||
}); | ||
|
||
if (result.status === "error") { | ||
notifyError({ e: result.error, info: "Error generating AI voice" }); | ||
return; | ||
} | ||
if (!result.data.data) { | ||
notifyError(false, { message: result.data.error }); | ||
return; | ||
} | ||
|
||
this.cardForm.options.onChange({ | ||
...(this.cardForm.options.value || {}), | ||
voice: result.data.data.publicUrl, | ||
}); | ||
} | ||
|
||
onDeleteAiVoice() { | ||
this.cardForm.options.onChange({ | ||
...(this.cardForm.options.value || {}), | ||
voice: null, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.