Siddharth Ravikumar commited on
Commit
30d0de3
·
1 Parent(s): 3f631e6

Implement robust Gradio API path resolution with fallback and logging

Browse files
Files changed (1) hide show
  1. frontend/js/alt_app.js +43 -18
frontend/js/alt_app.js CHANGED
@@ -20,10 +20,10 @@ let additionalFiles = []; // For add photos modal
20
  // Uses fetch-based SSE instead of EventSource (which can't set headers
21
  // required by HuggingFace's ZeroGPU proxy).
22
  async function callGradioApi(apiName, dataArr) {
23
- const GRADIO_API = '/gradio_api/call/';
24
-
25
- // Fetch config to get Hugging Face Token for ZeroGPU authentication
26
  let hfToken = '';
 
 
27
  try {
28
  const confRes = await fetch(`${API_BASE}/config`);
29
  if (confRes.ok) {
@@ -35,27 +35,52 @@ async function callGradioApi(apiName, dataArr) {
35
  }
36
 
37
  const headers = { 'Content-Type': 'application/json' };
38
- if (hfToken) {
39
- headers['Authorization'] = `Bearer ${hfToken}`;
40
- }
41
 
42
- // Step 1: POST to queue the job
43
- const res = await fetch(GRADIO_API + apiName, {
44
- method: 'POST',
45
- headers: headers,
46
- credentials: 'include',
47
- body: JSON.stringify({ data: dataArr })
48
- });
49
 
50
- if (!res.ok) {
51
- throw new Error('Gradio API Request Failed (' + res.status + ')');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  }
53
 
54
- const eventObj = await res.json();
55
- const eventId = eventObj.event_id;
 
56
 
57
  // Step 2: Stream the result via fetch (not EventSource)
58
- const streamUrl = GRADIO_API + apiName + '/' + eventId;
 
59
 
60
  const streamHeaders = { 'Accept': 'text/event-stream' };
61
  if (hfToken) {
 
20
  // Uses fetch-based SSE instead of EventSource (which can't set headers
21
  // required by HuggingFace's ZeroGPU proxy).
22
  async function callGradioApi(apiName, dataArr) {
23
+ const API_PATHS = ['/gradio_api/call/', '/call/'];
 
 
24
  let hfToken = '';
25
+
26
+ // Fetch config for ZeroGPU token
27
  try {
28
  const confRes = await fetch(`${API_BASE}/config`);
29
  if (confRes.ok) {
 
35
  }
36
 
37
  const headers = { 'Content-Type': 'application/json' };
38
+ if (hfToken) headers['Authorization'] = `Bearer ${hfToken}`;
 
 
39
 
40
+ let lastError = null;
41
+ let selectedApiBase = '';
42
+ let eventId = '';
 
 
 
 
43
 
44
+ // Step 1: Find working endpoint
45
+ for (const apiBase of API_PATHS) {
46
+ try {
47
+ const queueUrl = apiBase + apiName;
48
+ console.log(`[Gradio] Attempting POST: ${queueUrl}`);
49
+
50
+ const res = await fetch(queueUrl, {
51
+ method: 'POST',
52
+ headers: headers,
53
+ credentials: 'include',
54
+ body: JSON.stringify({ data: dataArr })
55
+ });
56
+
57
+ if (res.status === 404) {
58
+ console.warn(`[Gradio] 404 at ${queueUrl}, trying next fallback...`);
59
+ continue;
60
+ }
61
+
62
+ if (!res.ok) {
63
+ throw new Error(`Gradio API Request Failed (${res.status})`);
64
+ }
65
+
66
+ const eventObj = await res.json();
67
+ eventId = eventObj.event_id;
68
+ selectedApiBase = apiBase;
69
+ console.log(`[Gradio] Success at ${queueUrl}. Event ID: ${eventId}`);
70
+ break;
71
+ } catch (e) {
72
+ lastError = e;
73
+ console.error(`[Gradio] Error at ${apiBase}:`, e);
74
+ }
75
  }
76
 
77
+ if (!selectedApiBase || !eventId) {
78
+ throw lastError || new Error('Gradio API could not be reached (All paths failed)');
79
+ }
80
 
81
  // Step 2: Stream the result via fetch (not EventSource)
82
+ const streamUrl = selectedApiBase + apiName + '/' + eventId;
83
+ console.log(`[Gradio] Streaming from: ${streamUrl}`);
84
 
85
  const streamHeaders = { 'Accept': 'text/event-stream' };
86
  if (hfToken) {