gradio/.config/playwright-setup.js
Dawood Khan bf26b5f66d
Chatbot Tests (#4662)
* playwright tests for chatbot

* more tests

* changelog

* Update CHANGELOG.md

* fix upload file delay (#4661)

* fix

* changes

* changes

---------

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>

* Forward `tqdm` constructor arguments to `Progress` component (#4475)

* Forward tqdm constructor arguments to Progress component

Signed-off-by: Ilya Trushkin <ilya.trushkin@intel.com>

* lint

* reorder args

* added tests

---------

Signed-off-by: Ilya Trushkin <ilya.trushkin@intel.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>

* Remove cleared_value (#4685)

* Remove cleared_value

* CHANGELOG

* Add requirements.txt to dialogpt demo (#4686)

* Add requirements.txt

* Update demo notebook

* Add torch

* remove streaming demo + more tests

* Fix blocks_kitchen_sink and streaming_stt demos (#4699)

* Add code

* Add json file

* Remove streaming_stt demo

* Undo generate_notebooks

* Add blocks_kitchen_sink

* fix tests

* Update js/app/test/chatbot_multimodal.spec.ts

Co-authored-by: pngwn <hello@pngwn.io>

* Update js/app/test/chatbot_multimodal.spec.ts

Co-authored-by: pngwn <hello@pngwn.io>

* Update js/app/test/chatbot_multimodal.spec.ts

Co-authored-by: pngwn <hello@pngwn.io>

* Update js/app/test/chatbot_multimodal.spec.ts

Co-authored-by: pngwn <hello@pngwn.io>

* Update js/app/test/chatbot_multimodal.spec.ts

Co-authored-by: pngwn <hello@pngwn.io>

* Update js/app/test/chatbot_multimodal.spec.ts

Co-authored-by: pngwn <hello@pngwn.io>

* Update js/app/test/chatbot_multimodal.spec.ts

Co-authored-by: pngwn <hello@pngwn.io>

* Update js/app/test/chatbot_multimodal.spec.ts

Co-authored-by: pngwn <hello@pngwn.io>

* Update js/app/test/chatbot_multimodal.spec.ts

Co-authored-by: pngwn <hello@pngwn.io>

* update notebook

* remove debug

* remove debug

---------

Signed-off-by: Ilya Trushkin <ilya.trushkin@intel.com>
Co-authored-by: aliabid94 <aabid94@gmail.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
Co-authored-by: Ilya Trushkin <ilya.trushkin@intel.com>
Co-authored-by: Freddy Boulton <alfonsoboulton@gmail.com>
Co-authored-by: pngwn <hello@pngwn.io>
2023-06-28 17:11:46 -04:00

172 lines
4.0 KiB
JavaScript

import { spawn } from "node:child_process";
import { join, basename } from "path";
import { fileURLToPath } from "url";
import { readdirSync, writeFileSync } from "fs";
import net from "net";
import kl from "kleur";
const __dirname = fileURLToPath(new URL(".", import.meta.url));
const TEST_APP_PATH = join(__dirname, "./test.py");
const TEST_FILES_PATH = join(__dirname, "..", "js", "app", "test");
const ROOT = join(__dirname, "..");
const test_files = readdirSync(TEST_FILES_PATH)
.filter(
(f) =>
f.endsWith("spec.ts") &&
!f.endsWith(".skip.spec.ts") &&
!f.endsWith(".component.spec.ts")
)
.map((f) => basename(f, ".spec.ts"));
export default async function global_setup() {
const verbose = process.env.GRADIO_TEST_VERBOSE;
const port = await find_free_port(7860, 8860);
process.env.GRADIO_E2E_TEST_PORT = port;
process.stdout.write(kl.yellow("\nCreating test gradio app.\n\n"));
const test_app = make_app(test_files, port);
process.stdout.write(kl.yellow("App created. Starting test server.\n\n"));
process.stdout.write(kl.bgBlue(" =========================== \n"));
process.stdout.write(kl.bgBlue(" === PYTHON STARTUP LOGS === \n"));
process.stdout.write(kl.bgBlue(" =========================== \n\n"));
writeFileSync(TEST_APP_PATH, test_app);
const app = await spawn_gradio_app(TEST_APP_PATH, port, verbose);
process.stdout.write(
kl.green(`\n\nServer started. Running tests on port ${port}.\n`)
);
return () => {
process.stdout.write(kl.green(`\nTests complete, cleaning up!\n`));
kill_process(app);
};
}
const INFO_RE = /^INFO:/;
function spawn_gradio_app(app, port, verbose) {
const PORT_RE = new RegExp(`:${port}`);
return new Promise((res, rej) => {
const _process = spawn(`python`, [app], {
shell: true,
stdio: "pipe",
cwd: ROOT,
env: {
...process.env,
GRADIO_SERVER_PORT: `7879`,
PYTHONUNBUFFERED: "true"
}
});
_process.stdout.setEncoding("utf8");
_process.stdout.on("data", (data) => {
const _data = data.toString();
const is_info = INFO_RE.test(_data);
if (is_info) {
process.stdout.write(kl.yellow(_data));
}
if (!is_info) {
process.stdout.write(`${_data}\n`);
}
if (PORT_RE.test(_data)) {
process.stdout.write(kl.bgBlue("\n =========== END =========== "));
res(_process);
if (!verbose) {
_process.stdout.destroy();
_process.stderr.destroy();
}
}
});
_process.stderr.on("data", (data) => {
const _data = data.toString();
const is_info = INFO_RE.test(_data);
if (is_info) {
process.stdout.write(kl.yellow(_data));
}
if (!is_info) {
process.stdout.write(`${_data}\n`);
}
if (PORT_RE.test(_data)) {
process.stderr.write(kl.bgBlue("\n =========== END =========== "));
res(_process);
if (!verbose) {
_process.stdout.destroy();
_process.stderr.destroy();
}
}
});
});
}
function kill_process(process) {
return new Promise((res, rej) => {
process.on("close", res);
process.kill("SIGTERM");
});
}
function make_app(demos, port) {
return `import gradio as gr
import uvicorn
from fastapi import FastAPI
import gradio as gr
${demos.map((d) => `from demo.${d}.run import demo as ${d}`).join("\n")}
app = FastAPI()
${demos
.map((d) => `app = gr.mount_gradio_app(app, ${d}, path="/${d}")`)
.join("\n")}
config = uvicorn.Config(app, port=${port}, log_level="info")
server = uvicorn.Server(config=config)
server.run()`;
}
export async function find_free_port(start_port, end_port) {
for (let port = start_port; port < end_port; port++) {
if (await is_free_port(port)) {
return port;
}
}
throw new Error(
`Could not find free ports: there were not enough ports available.`
);
}
export function is_free_port(port) {
return new Promise((accept, reject) => {
const sock = net.createConnection(port, "127.0.0.1");
sock.once("connect", () => {
sock.end();
accept(false);
});
sock.once("error", (e) => {
sock.destroy();
if (e.code === "ECONNREFUSED") {
accept(true);
} else {
reject(e);
}
});
});
}