Add error handling to AI text generation

This commit is contained in:
Sam Becker 2024-03-19 23:43:00 -05:00
parent dc7b0694ab
commit f39fa417b8
2 changed files with 17 additions and 10 deletions

View File

@ -8,19 +8,25 @@ export default function useImageQuery(
query: ImageQuery, query: ImageQuery,
) { ) {
const [text, setText] = useState(''); const [text, setText] = useState('');
const [error, setError] = useState<any>();
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const request = useCallback(async () => { const request = useCallback(async () => {
if (imageBase64) { if (imageBase64) {
setIsLoading(true); setIsLoading(true);
const textStream = await streamImageQueryAction( try {
imageBase64 ?? '', const textStream = await streamImageQueryAction(
query, imageBase64 ?? '',
); query,
for await (const text of readStreamableValue(textStream)) { );
setText(text ?? ''); for await (const text of readStreamableValue(textStream)) {
setText(text ?? '');
}
setIsLoading(false);
} catch (e) {
setError(e);
setIsLoading(false);
} }
setIsLoading(false);
} }
}, [imageBase64, query]); }, [imageBase64, query]);
@ -28,5 +34,6 @@ export default function useImageQuery(
request, request,
text, text,
isLoading, isLoading,
error,
] as const; ] as const;
}; };

View File

@ -181,21 +181,21 @@ export default function PhotoForm({
/>} />}
</div> </div>
<p> <p>
AI RESPONSE 01: {title} {isLoadingTitle && <> TITLE: {title} {isLoadingTitle && <>
<span className="inline-flex translate-y-[1.5px]"> <span className="inline-flex translate-y-[1.5px]">
<Spinner /> <Spinner />
</span> </span>
</>} </>}
</p> </p>
<p> <p>
AI RESPONSE 02: {tags} {isLoadingTags && <> TAGS: {tags} {isLoadingTags && <>
<span className="inline-flex translate-y-[1.5px]"> <span className="inline-flex translate-y-[1.5px]">
<Spinner /> <Spinner />
</span> </span>
</>} </>}
</p> </p>
<p> <p>
AI RESPONSE 01: {description} {isLoadingDescription && <> DESCRIPTION: {description} {isLoadingDescription && <>
<span className="inline-flex translate-y-[1.5px]"> <span className="inline-flex translate-y-[1.5px]">
<Spinner /> <Spinner />
</span> </span>