File size: 2,393 Bytes
a3953cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { StepSummary } from "./client";

export const OBJ_API_BASE = import.meta.env.VITE_API_BASE ?? "http://localhost:8000";

// ---------------------------------------------------------------------------
// Upload
// ---------------------------------------------------------------------------

export async function uploadForObjectRemoval(
  file: File,
): Promise<{ job_id: string; width: number; height: number }> {
  const formData = new FormData();
  formData.append("file", file);

  const response = await fetch(`${OBJ_API_BASE}/object-removal/upload`, {
    method: "POST",
    body:   formData,
  });

  if (!response.ok) {
    let detail = `HTTP ${response.status}`;
    try { const j = await response.json(); if (j?.detail) detail = j.detail; } catch { /* ignore */ }
    throw new Error(detail);
  }

  return response.json();
}

// ---------------------------------------------------------------------------
// Generate
// ---------------------------------------------------------------------------

export async function generateObjectRemoval(
  jobId:    string,
  maskJson: object,
): Promise<{ job_id: string; step_number: number; status: string }> {
  const response = await fetch(`${OBJ_API_BASE}/object-removal/generate`, {
    method:  "POST",
    headers: { "Content-Type": "application/json" },
    body:    JSON.stringify({ job_id: jobId, mask_json: maskJson }),
  });

  if (!response.ok) {
    let detail = `HTTP ${response.status}`;
    try { const j = await response.json(); if (j?.detail) detail = j.detail; } catch { /* ignore */ }
    throw new Error(detail);
  }

  return response.json();
}

// ---------------------------------------------------------------------------
// Result polling — same response shape as getJobResult in client.ts
// ---------------------------------------------------------------------------

export async function getObjectRemovalResult(jobId: string): Promise<{
  job_id:             string;
  current_step:       number;
  status:             "pending" | "processing" | "completed" | "failed";
  result_image_url:   string | null;
  original_image_url: string | null;
  error_message:      string | null;
  steps:              StepSummary[];
}> {
  const response = await fetch(`${OBJ_API_BASE}/object-removal/result/${jobId}`);
  if (!response.ok) throw new Error(`HTTP ${response.status}`);
  return response.json();
}