Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download packages/inference/src/tasks/nlp/textGeneration.ts from huggingfacejs/inference-widgets: direct link, hf CLI and curl.
- Browser
- Download file 929 Bytes
-
https://huggingface.co/spaces/huggingfacejs/inference-widgets/resolve/main/packages/inference/src/tasks/nlp/textGeneration.ts
- Command line
-
hf download hf://spaces/huggingfacejs/inference-widgets/packages/inference/src/tasks/nlp/textGeneration.ts
-
curl -L -o textGeneration.ts https://huggingface.co/spaces/huggingfacejs/inference-widgets/resolve/main/packages/inference/src/tasks/nlp/textGeneration.ts
929 Bytes
| import type { TextGenerationInput, TextGenerationOutput } from "@huggingface/tasks"; | |
| import { InferenceOutputError } from "../../lib/InferenceOutputError"; | |
| import type { BaseArgs, Options } from "../../types"; | |
| import { request } from "../custom/request"; | |
| export type { TextGenerationInput, TextGenerationOutput }; | |
| /** | |
| * Use to continue text from a prompt. This is a very generic task. Recommended model: gpt2 (it’s a simple model, but fun to play with). | |
| */ | |
| export async function textGeneration( | |
| args: BaseArgs & TextGenerationInput, | |
| options?: Options | |
| ): Promise<TextGenerationOutput> { | |
| const res = await request<TextGenerationOutput[]>(args, { | |
| ...options, | |
| taskHint: "text-generation", | |
| }); | |
| const isValidOutput = Array.isArray(res) && res.every((x) => typeof x?.generated_text === "string"); | |
| if (!isValidOutput) { | |
| throw new InferenceOutputError("Expected Array<{generated_text: string}>"); | |
| } | |
| return res?.[0]; | |
| } | |