Spaces:
Sleeping
Sleeping
File size: 3,621 Bytes
88aeaf5 2a9b8d1 a837fd9 88aeaf5 f005306 88aeaf5 20905e1 88aeaf5 f005306 2a9b8d1 88aeaf5 2a9b8d1 88aeaf5 f005306 2a9b8d1 88aeaf5 f005306 88aeaf5 | 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 | import { Client, handle_file } from "@gradio/client"
import type {
CompileResult,
CloudCompilerConfig,
CompilerProvider,
DetectParams,
LocalConfig,
RuleMutationResult,
RunResult,
ValidationResult,
} from "./types"
// Single shared connection to the gradio.Server backend.
let clientPromise: Promise<Client> | null = null
function getClient(): Promise<Client> {
if (!clientPromise) {
const origin =
import.meta.env.DEV && import.meta.env.VITE_API_ORIGIN
? import.meta.env.VITE_API_ORIGIN
: window.location.origin
clientPromise = Client.connect(origin)
}
return clientPromise
}
async function call<T>(endpoint: string, payload: Record<string, unknown>): Promise<T> {
const client = await getClient()
const result = await client.predict(endpoint, payload)
return (result.data as unknown[])[0] as T
}
export async function detectAndAutomate(
video: File,
rulesText: string,
params: DetectParams,
): Promise<RunResult> {
return call<RunResult>("/detect_and_automate", {
video: handle_file(video),
classes: params.classes,
rules_text: rulesText,
confidence: params.confidence,
sample_interval_sec: params.sampleIntervalSec,
max_frames: params.maxFrames,
model_name: params.modelName,
image_size: params.imageSize,
device: params.device,
max_detections: params.maxDetections,
enable_webhooks: params.enableWebhooks,
webhook_url: params.webhookUrl,
})
}
export async function compileRules(
instruction: string,
classes: string,
existingRulesText: string,
compiler: CloudCompilerConfig,
): Promise<CompileResult> {
const apiKeyByProvider: Record<CompilerProvider, string> = {
replicate: compiler.replicateApiKey,
openai: compiler.openaiApiKey,
anthropic: compiler.anthropicApiKey,
}
const modelByProvider: Record<CompilerProvider, string> = {
replicate: "",
openai: compiler.openaiModel,
anthropic: compiler.anthropicModel,
}
return call<CompileResult>("/compile_rules", {
instruction,
classes,
existing_rules_text: existingRulesText,
append: true,
provider: compiler.provider,
api_key: apiKeyByProvider[compiler.provider],
model: modelByProvider[compiler.provider],
})
}
export async function validateRules(rulesText: string): Promise<ValidationResult> {
return call<ValidationResult>("/validate_rules", { rules_text: rulesText })
}
export async function saveRules(rulesText: string): Promise<{ ok: boolean; rule_count: number }> {
return call("/save_rules", { rules_text: rulesText })
}
export async function setRuleEnabled(
rulesText: string,
ruleName: string,
enabled: boolean,
): Promise<RuleMutationResult> {
return call<RuleMutationResult>("/set_rule_enabled", {
rules_text: rulesText,
rule_name: ruleName,
enabled,
})
}
export async function deleteRule(
rulesText: string,
ruleName: string,
): Promise<RuleMutationResult> {
return call<RuleMutationResult>("/delete_rule", {
rules_text: rulesText,
rule_name: ruleName,
})
}
export async function loadRules(): Promise<{ rules_text: string | null }> {
return call("/load_rules", {})
}
export async function getConfig(): Promise<LocalConfig> {
return call<LocalConfig>("/get_config", {})
}
// Resolve a backend media path (e.g. "/media/foo.mp4") to a fully-qualified URL.
export function mediaUrl(path: string): string {
const origin =
import.meta.env.DEV && import.meta.env.VITE_API_ORIGIN
? import.meta.env.VITE_API_ORIGIN
: window.location.origin
return path.startsWith("http") ? path : `${origin}${path}`
}
|