Move requirements generation in playground to playground worker (#10126)

* move requirements genereation

* formatting

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
Ali Abdalla 2024-12-04 20:48:49 -08:00 committed by GitHub
parent c5ae60780c
commit a623fafbcf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 11 additions and 70 deletions

View File

@ -0,0 +1,5 @@
---
"website": minor
---
feat:Move requirements generation in playground to playground worker

View File

@ -13,7 +13,6 @@
import { onMount } from "svelte";
import SYSTEM_PROMPT from "$lib/json/system_prompt.json";
import WHEEL from "$lib/json/wheel.json";
import { excludeUnavailablePackages } from "./requirements-utils";
let generated = true;
@ -21,7 +20,7 @@
let compare = false;
const workerUrl = "https://playground-worker.pages.dev/api/generate";
// const workerUrl = "http://localhost:5174/api/generate";
// const workerUrl = "http://localhost:5173/api/generate";
let model_info = "";
let abortController: AbortController | null = null;
@ -87,6 +86,8 @@
// }
} else if (parsed.info) {
console.log(parsed.info);
} else if (parsed.requirements) {
yield { requirements: parsed.requirements };
} else if (parsed.choices && parsed.choices.length > 0) {
yield parsed;
}
@ -125,7 +126,9 @@
SYSTEM_PROMPT.SYSTEM,
abortController.signal
)) {
if (chunk.choices && chunk.choices.length > 0) {
if (chunk.requirements) {
demos[queried_index].requirements = chunk.requirements;
} else if (chunk.choices && chunk.choices.length > 0) {
const content = chunk.choices[0].delta.content;
if (content) {
out += content;
@ -151,28 +154,6 @@
}
}
const system_prompt_requirements_txt = `User gives Python code.
You return the required package list in the format of \`requirements.txt\` for pip.
You exclude \`gradio\` from the package list because it's already installed in the user's environment.
You only return the content of \`requirements.txt\`, without any other texts or messages.`;
const query_requirements_txt = demos[queried_index].code;
let generated_requirements_txt = "";
for await (const chunk of streamFromWorker(
query_requirements_txt,
system_prompt_requirements_txt,
abortController.signal
)) {
if (chunk.choices && chunk.choices.length > 0) {
const content = chunk.choices[0].delta.content;
if (content) {
generated_requirements_txt += content;
}
}
}
demos[queried_index].requirements = await excludeUnavailablePackages(
generated_requirements_txt.split("\n").filter((r) => r.trim() !== "")
);
generated = true;
if (selected_demo.name === demo_name) {
highlight_changes(code_to_compare, demos[queried_index].code);

View File

@ -1,45 +0,0 @@
export function extractPackageName(line: string): string | null {
const noComments = line.split("#")[0].trim();
const match = noComments.match(/^([a-zA-Z0-9_\-.]+)/);
return match ? match[1] : null;
}
export async function packageExistsOnPyPi(packageName: string) {
const url = `https://pypi.org/pypi/${packageName}/json`;
try {
const response = await fetch(url);
if (response.status === 200) {
console.log(`The package '${packageName}' exists on PyPI.`);
return true;
} else if (response.status === 404) {
console.log(`The package '${packageName}' does not exist on PyPI.`);
return false;
} else {
console.log(
`Failed to fetch information for '${packageName}'. HTTP Status Code: ${response.status}`
);
return false;
}
} catch (error) {
console.error(`Error while checking package '${packageName}':`, error);
return false;
}
}
export async function excludeUnavailablePackages(
requirements: string[]
): Promise<string[]> {
const packageChecks = requirements.map(async (req) => {
const packageName = extractPackageName(req);
if (!packageName) return null;
const exists = await packageExistsOnPyPi(packageName);
return exists === true ? req : null;
});
const results = await Promise.all(packageChecks);
return results.filter((line) => line !== null);
}