File size: 6,496 Bytes
a03b3ba |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
<script>
import { client } from "@gradio/client";
import EndpointInputs from "../../lib/EndpointInputs.svelte";
import ResponsePreview from "../../lib/ResponsePreview.svelte";
let api = "gradio/cancel_events";
let hf_token = "";
/**
* @type Awaited<ReturnType<typeof client>>
*/
let app;
/**
* @type any
*/
let app_info;
/**
* @type string[]
*/
let named = [];
/**
* @type string[]
*/
let unnamed = [];
/**
* @type string | number
*/
let active_endpoint = "";
/**
* @type {{data: any[]; fn_index: number; endpoint: string|number}}
*/
let response_data = { data: [], fn_index: 0, endpoint: "" };
/**
* @type any[]
*/
let request_data = [];
async function connect() {
named = [];
unnamed = [];
app_info = undefined;
active_endpoint = "";
response_data = { data: [], fn_index: 0, endpoint: "" };
if (!api || (hf_token && !hf_token.startsWith("_hf"))) return;
app = await client(api, {
//@ts-ignore
hf_token: hf_token
});
const { named_endpoints, unnamed_endpoints } = await app.view_api();
named = Object.keys(named_endpoints);
unnamed = Object.keys(unnamed_endpoints);
}
/**
* @param type {"named" | "unnamed"}
* @param _endpoint {string}
*/
async function select_endpoint(type, _endpoint) {
response_data = { data: [], fn_index: 0, endpoint: "" };
const _endpoint_info = (await app.view_api())?.[`${type}_endpoints`]?.[
type === "unnamed" ? parseInt(_endpoint) : _endpoint
];
if (!_endpoint_info) return;
app_info = _endpoint_info;
active_endpoint = type === "unnamed" ? parseInt(_endpoint) : _endpoint;
}
/**
* @type ReturnType<Awaited<ReturnType<typeof client>>["submit"]>
*/
let job;
/**
* @type {"pending" | "error" | "complete" | "generating" | 'idle'}
*/
let status = "idle";
async function submit() {
response_data = { data: [], fn_index: 0, endpoint: "" };
job = app
.submit(active_endpoint, request_data)
.on("data", (data) => {
response_data = data;
})
.on("status", (_status) => {
status = _status.stage;
});
}
function cancel() {
job.cancel();
}
let endpoint_type_text = "";
$: {
if (!app_info) {
endpoint_type_text = "";
} else if (!app_info.type.continuos && app_info.type.generator) {
endpoint_type_text =
"This endpoint generates values over time and can be cancelled.";
} else if (app_info.type.continuos && app_info.type.generator) {
endpoint_type_text =
"This endpoint generates values over time and will continue to yield values until cancelled.";
} else {
endpoint_type_text = "This endpoint runs once and cannot be cancelled.";
}
}
</script>
<h2>Client Browser</h2>
<p>
Enter a space <code>user-space/name</code> to test the client in a browser environment
with any space.
</p>
<p>
You may optionally provide a <code>hf_token</code> to test a private space
</p>
<div class="input-wrap">
<label for="">
<span>Space Name</span>
<input type="text" placeholder="user/space-name" bind:value={api} />
</label>
<label for="">
<span>Hugging Face Token <i>(optional)</i></span>
<input type="text" placeholder="hf_..." bind:value={hf_token} />
</label>
</div>
<button on:click={connect}>Connect</button>
{#if named.length || unnamed.length}
<div class="endpoints">
<div class="endpoint">
<h3>Named endpoints</h3>
{#if named.length}
{#each named as endpoint}
<button
class="endpoint-button"
class:selected={endpoint === active_endpoint}
on:click={() => select_endpoint("named", endpoint)}
>{endpoint}</button
>
{/each}
{:else}
<p>There are no named endpoints</p>
{/if}
</div>
<div class="endpoint">
<h3>Unnamed endpoints</h3>
{#if unnamed.length}
{#each unnamed as endpoint}
<button
class="endpoint-button"
class:selected={parseInt(endpoint) === active_endpoint}
on:click={() => select_endpoint("unnamed", endpoint)}
>{endpoint}</button
>
{/each}
{:else}
<p>There are no unnamed endpoints</p>
{/if}
</div>
</div>
{/if}
{#if app_info}
<hr />
<p>
This endpoint accepts {app_info.parameters.length
? app_info.parameters.length
: "no"} piece{app_info.parameters.length < 1 ||
app_info.parameters.length > 1
? "s"
: ""} of data and returns {app_info.returns.length
? app_info.returns.length
: "no"} piece{app_info.returns.length < 1 || app_info.returns.length > 1
? "s"
: ""} of data. {endpoint_type_text}
</p>
<hr />
<div class="app_info">
<div>
<EndpointInputs app_info={app_info.parameters} bind:request_data />
<button class="submit" on:click={submit}>Submit Request</button>
{#if app_info.type.generator || app_info.type.continuous}
<button
class="cancel"
on:click={cancel}
disabled={status !== "generating" && status !== "pending"}
>Cancel Request</button
>
{/if}
</div>
<div>
<ResponsePreview {status} app_info={app_info.returns} {response_data} />
</div>
</div>
{/if}
<style>
label {
display: flex;
flex-direction: column;
gap: var(--size-1);
width: 100%;
}
label:first-child input {
margin-right: -1px;
border-top-left-radius: 2px;
}
label:last-child input {
border-top-right-radius: 2px;
}
input {
outline: none;
border-bottom: none;
border-radius: 0px;
}
input:focus-visible {
z-index: 1;
border-color: var(--color-accent);
}
.input-wrap {
display: flex;
margin-top: var(--size-6);
}
button {
border-bottom-right-radius: 2px;
border-bottom-left-radius: 2px;
background: var(--color-accent);
padding: var(--size-2) var(--size-2-5);
width: 100%;
color: var(--background-fill-primary);
font-weight: bold;
font-size: var(--scale-0);
/* margin-top: var(--size-3); */
}
.endpoints button {
background: var(--color-orange-200);
width: auto;
color: var(--body-text-color);
}
button.selected {
background: var(--color-accent);
color: white;
}
.endpoints {
display: flex;
gap: var(--size-10);
margin-top: var(--size-10);
}
.endpoint {
width: 100%;
}
.app_info {
display: flex;
gap: var(--size-10);
margin-top: var(--size-10);
}
.app_info > div {
width: 100%;
}
.submit {
margin-top: var(--size-5);
}
hr {
margin: var(--size-6) 0;
}
.cancel {
margin-top: var(--size-2);
background-color: var(--color-red-600);
}
.endpoint-button {
margin-right: var(--size-1);
border-radius: 2px;
}
button[disabled] {
opacity: 0.2;
}
</style>
|