Spaces:
Sleeping
Sleeping
commit
Browse files- src/utils/hfClient.ts +28 -3
- stylegptUI +1 -1
src/utils/hfClient.ts
CHANGED
|
@@ -5,7 +5,8 @@ dotenv.config();
|
|
| 5 |
const HF_API_KEY = process.env.HF_API_KEY;
|
| 6 |
|
| 7 |
export async function classifyFashionImage(imageUrl: string) {
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
const payload = {
|
| 11 |
inputs: imageUrl,
|
|
@@ -32,7 +33,31 @@ export async function classifyFashionImage(imageUrl: string) {
|
|
| 32 |
"Content-Type": "application/json",
|
| 33 |
};
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
}
|
| 38 |
|
|
|
|
| 5 |
const HF_API_KEY = process.env.HF_API_KEY;
|
| 6 |
|
| 7 |
export async function classifyFashionImage(imageUrl: string) {
|
| 8 |
+
// Use new Router Inference API endpoint
|
| 9 |
+
const endpoint = "https://router.huggingface.co/hf-inference/models/patrickjohncyh/fashion-clip";
|
| 10 |
|
| 11 |
const payload = {
|
| 12 |
inputs: imageUrl,
|
|
|
|
| 33 |
"Content-Type": "application/json",
|
| 34 |
};
|
| 35 |
|
| 36 |
+
let lastError: any = null;
|
| 37 |
+
const maxAttempts = 2;
|
| 38 |
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
| 39 |
+
try {
|
| 40 |
+
const response = await axios.post(endpoint, payload, {
|
| 41 |
+
headers,
|
| 42 |
+
timeout: 15000,
|
| 43 |
+
validateStatus: (status) => status >= 200 && status < 300,
|
| 44 |
+
});
|
| 45 |
+
return response.data;
|
| 46 |
+
} catch (error: any) {
|
| 47 |
+
lastError = error;
|
| 48 |
+
const status = error?.response?.status;
|
| 49 |
+
// Retry once for transient/transition statuses
|
| 50 |
+
if (attempt < maxAttempts && (status === 429 || status === 503)) {
|
| 51 |
+
await new Promise((r) => setTimeout(r, 1500));
|
| 52 |
+
continue;
|
| 53 |
+
}
|
| 54 |
+
// Provide clearer message when old endpoint deprecation surfaces from proxies
|
| 55 |
+
if (status === 410 || error?.response?.data?.error?.includes("no longer supported")) {
|
| 56 |
+
throw new Error("Hugging Face API endpoint changed. Please try again in a moment.");
|
| 57 |
+
}
|
| 58 |
+
throw error;
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
throw lastError;
|
| 62 |
}
|
| 63 |
|
stylegptUI
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
Subproject commit
|
|
|
|
| 1 |
+
Subproject commit 2c2829b11d2df45586826648a426a326d0543978
|