export function represent_value( value: string, type: string | undefined, lang: "js" | "py" | "bash" | null = null ): string | null | number | boolean | Record { if (type === undefined) { return lang === "py" ? "None" : null; } if (value === null && lang === "py") { return "None"; } if (type === "string" || type === "str") { return lang === null ? value : '"' + value + '"'; } else if (type === "number") { return lang === null ? parseFloat(value) : value; } else if (type === "boolean" || type == "bool") { if (lang === "py") { value = String(value); return value === "true" ? "True" : "False"; } else if (lang === "js" || lang === "bash") { return value; } return value === "true"; } else if (type === "List[str]") { value = JSON.stringify(value); return value; } else if (type.startsWith("Literal['")) { // a literal of strings return '"' + value + '"'; } // assume object type if (lang === null) { return value === "" ? null : JSON.parse(value); } else if (typeof value === "string") { if (value === "") { return lang === "py" ? "None" : "null"; } return value; } if (lang === "bash") { value = simplify_file_data(value); } if (lang === "py") { value = replace_file_data_with_file_function(value); } return stringify_except_file_function(value); } // These helpers run inside template expressions, which are deriveds in runes // mode, so they must not mutate their input: `api_calls` payloads are `$state` // proxies and writing to one from a derived throws `state_unsafe_mutation` // (it also used to quietly corrupt the recorded payload). function simplify_file_data(obj: any): any { if (typeof obj === "object" && obj !== null && !Array.isArray(obj)) { if ( "url" in obj && obj.url && "meta" in obj && obj.meta?._type === "gradio.FileData" ) { return { path: obj.url, meta: { _type: "gradio.FileData" } }; } } if (Array.isArray(obj)) { return obj.map((item) => typeof item === "object" && item !== null ? simplify_file_data(item) : item ); } if (typeof obj === "object" && obj !== null) { const copy: Record = {}; for (const key of Object.keys(obj)) { copy[key] = simplify_file_data(obj[key]); } return copy; } return obj; } function replace_file_data_with_file_function(obj: any): any { if (typeof obj === "object" && obj !== null && !Array.isArray(obj)) { if ( "url" in obj && obj.url && "meta" in obj && obj.meta?._type === "gradio.FileData" ) { return `handle_file('${obj.url}')`; } } if (Array.isArray(obj)) { return obj.map((item) => typeof item === "object" && item !== null ? replace_file_data_with_file_function(item) : item ); } if (typeof obj === "object" && obj !== null) { const copy: Record = {}; for (const key of Object.keys(obj)) { copy[key] = replace_file_data_with_file_function(obj[key]); } return copy; } return obj; } function stringify_except_file_function(obj: any): string { let jsonString = JSON.stringify(obj, (key, value) => { if (value === null) { return "UNQUOTEDNone"; } if ( typeof value === "string" && value.startsWith("handle_file(") && value.endsWith(")") ) { return `UNQUOTED${value}`; // Flag the special strings } return value; }); const regex = /"UNQUOTEDhandle_file\(([^)]*)\)"/g; jsonString = jsonString.replace(regex, (match, p1) => `handle_file(${p1})`); const regexNone = /"UNQUOTEDNone"/g; return jsonString.replace(regexNone, "None"); } export function format_latency(val: number): string { if (val < 1) return `${Math.round(val * 1000)} ms`; return `${val.toFixed(2)} s`; } export function get_color_from_success_rate(success_rate: number): string { if (success_rate > 0.9) { return "color: green;"; } else if (success_rate > 0.1) { return "color: orange;"; } return "color: red;"; }