Spaces:
Sleeping
Sleeping
File size: 4,337 Bytes
ee0bba4 cd39fb1 ee0bba4 cd39fb1 ee0bba4 cd39fb1 ee0bba4 016cbbb cd39fb1 0db47ae cd39fb1 ee0bba4 cd39fb1 ee0bba4 4db7d21 cd39fb1 4db7d21 cd39fb1 4db7d21 cd39fb1 4db7d21 cd39fb1 0db47ae cd39fb1 4db7d21 cd39fb1 0db47ae 016cbbb 0db47ae 016cbbb 0db47ae 016cbbb 0db47ae cd39fb1 4db7d21 cd39fb1 4db7d21 cd39fb1 4db7d21 ee0bba4 |
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 |
import axios from "axios";
import FormData from "form-data";
import dotenv from "dotenv";
dotenv.config();
const FASHION_CLIP_URL = process.env.FASHION_CLIP_URL || "https://nexusbert-fashionclip.hf.space";
export interface ClassificationResult {
label: string;
score: number;
}
export interface IdentifyResult {
label: string;
score: number;
type: "brand" | "item" | "color" | "accessory" | "combined";
}
export interface IdentifyResponse {
precise_identification: IdentifyResult[];
detected_components: {
top_colors: Array<{ color: string; score: number }>;
top_items: Array<{ item: string; score: number }>;
top_accessories: Array<{ accessory: string; score: number }>;
top_brands: Array<{ brand: string; score: number }>;
};
}
/**
* Classify a fashion image using the FashionCLIP /classify endpoint
* @param fileBuffer - The image file buffer
* @param filename - The original filename
* @param mimetype - The file MIME type
* @returns Array of classification results sorted by score (highest first)
*/
export async function classifyFashionImage(
fileBuffer: Buffer,
filename: string,
mimetype: string
): Promise<ClassificationResult[]> {
const endpoint = `${FASHION_CLIP_URL}/classify`;
// Create form data with file
const formData = new FormData();
formData.append("file", fileBuffer, {
filename: filename,
contentType: mimetype,
});
let lastError: any = null;
const maxAttempts = 2;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
const response = await axios.post<ClassificationResult[]>(endpoint, formData, {
headers: {
...formData.getHeaders(),
},
timeout: 60000, // 60 second timeout for model inference
validateStatus: (status) => status >= 200 && status < 300,
});
// Response is already an array of {label, score} objects
return response.data;
} catch (error: any) {
lastError = error;
const status = error?.response?.status;
// Retry once for transient errors
if (attempt < maxAttempts && (status === 429 || status === 503 || status === 502)) {
console.log(`FashionCLIP classify API returned ${status}. Retrying...`);
await new Promise((r) => setTimeout(r, 2000));
continue;
}
// Provide clearer error messages
if (error?.response?.data?.detail) {
throw new Error(`FashionCLIP classify API error: ${error.response.data.detail}`);
}
if (error?.code === "ECONNREFUSED" || error?.code === "ETIMEDOUT") {
throw new Error("Unable to connect to FashionCLIP service. Please try again later.");
}
throw error;
}
}
throw lastError;
}
export async function identifyFashionItem(
fileBuffer: Buffer,
filename: string,
mimetype: string,
topK: number = 5
): Promise<IdentifyResponse> {
const clampedTopK = Math.max(1, Math.min(20, topK));
const endpoint = `${FASHION_CLIP_URL}/identify?top_k=${clampedTopK}`;
const formData = new FormData();
formData.append("file", fileBuffer, {
filename: filename,
contentType: mimetype,
});
let lastError: any = null;
const maxAttempts = 2;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
const response = await axios.post<IdentifyResponse>(endpoint, formData, {
headers: {
...formData.getHeaders(),
},
timeout: 60000,
validateStatus: (status) => status >= 200 && status < 300,
});
return response.data;
} catch (error: any) {
lastError = error;
const status = error?.response?.status;
if (attempt < maxAttempts && (status === 429 || status === 503 || status === 502)) {
console.log(`FashionCLIP identify API returned ${status}. Retrying...`);
await new Promise((r) => setTimeout(r, 2000));
continue;
}
if (error?.response?.data?.detail) {
throw new Error(`FashionCLIP identify API error: ${error.response.data.detail}`);
}
if (error?.code === "ECONNREFUSED" || error?.code === "ETIMEDOUT") {
throw new Error("Unable to connect to FashionCLIP service. Please try again later.");
}
throw error;
}
}
throw lastError;
}
|