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

✨ react/components: Add ImageGenerated Component #115

Merged
merged 3 commits into from
Dec 7, 2023
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
33 changes: 33 additions & 0 deletions lib/components/ImageGenerated.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React, { useState, useEffect } from "react";
import { usePolyfire } from "../hooks";

export interface ImageGeneratedProps extends React.HTMLAttributes<HTMLDivElement> {
prompt: string;
model: string;
loadingElement?: React.JSX.Element | string;
}

export function ImageGenerated({
prompt,
loadingElement,
model,
...props
}: ImageGeneratedProps): React.ReactElement {
const {
auth: { status },
models: { generateImage },
} = usePolyfire();

const [imageUrl, setImageUrl] = useState<string>();

useEffect(() => {
if (status === "authenticated" && prompt) {
generateImage(prompt, { model }).then(({ url }) => setImageUrl(url));
}
}, [status, generateImage, prompt]);

if (imageUrl) {
return <img {...props} src={imageUrl} alt={prompt} />;
}
return <>{loadingElement}</>;
}
11 changes: 10 additions & 1 deletion lib/components/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { TextGenerated, TextGeneratedProps } from "./TextGenerated";
import { TextSummary, TextSummaryProps } from "./TextSummary";
import { AutoCompleteInput } from "./AutoCompleteInput";
import { ImageGenerated, ImageGeneratedProps } from "./ImageGenerated";

export { TextGenerated, TextGeneratedProps, TextSummary, TextSummaryProps, AutoCompleteInput };
export {
TextGenerated,
TextGeneratedProps,
TextSummary,
TextSummaryProps,
AutoCompleteInput,
ImageGenerated,
ImageGeneratedProps,
};
29 changes: 16 additions & 13 deletions lib/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { InputClientOptions, defaultOptions } from "./clientOpts";
import { ApiError, ErrorData } from "./helpers/error";

export type ImageGenerationOptions = {
provider?: "openai";
model?: "dall-e-2" | "dall-e-3" | string;
};

const ImageGenerationResponseType = t.type({
Expand All @@ -13,23 +13,26 @@ const ImageGenerationResponseType = t.type({

export async function generateImage(
prompt: string,
options: ImageGenerationOptions = {},
{ model }: ImageGenerationOptions = {},
clientOptions: InputClientOptions = {},
): Promise<t.TypeOf<typeof ImageGenerationResponseType>> {
try {
const { token, endpoint } = await defaultOptions(clientOptions);

const { provider = "openai" } = options;
const image = await axios.get(
`${endpoint}/image/generate?p=${encodeURIComponent(
prompt,
)}&provider=${encodeURIComponent(provider)}&format=json`,
{
headers: {
"X-Access-Token": token,
},
const url = new URL(`${endpoint}/image/generate`);

url.searchParams.append("p", prompt);
url.searchParams.append("format", "json");

if (model) {
url.searchParams.append("model", model);
}

const image = await axios.get(url.toString(), {
headers: {
"X-Access-Token": token,
},
);
});

if (!ImageGenerationResponseType.is(image.data)) {
throw new ApiError({
Expand Down Expand Up @@ -57,7 +60,7 @@ export type ImageGenerationClient = {

export default function client(clientOptions: InputClientOptions = {}): ImageGenerationClient {
return {
generateImage: (prompt: string, options: ImageGenerationOptions) =>
generateImage: (prompt: string, options: ImageGenerationOptions = {}) =>
generateImage(prompt, options, clientOptions),
};
}
Loading