hmb HF Staff commited on
Commit
bc656d2
·
verified ·
1 Parent(s): b29b5d6

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +35 -3
app.py CHANGED
@@ -1190,13 +1190,45 @@ countries.forEach(c => {{
1190
  countrySelect.value = 'Brazil';
1191
 
1192
  async function callApi(name, params) {{
1193
- const resp = await fetch('/gradio_api/run/' + name, {{
 
1194
  method: 'POST',
1195
  headers: {{'Content-Type': 'application/json'}},
1196
  body: JSON.stringify({{data: Object.values(params)}})
1197
  }});
1198
- const result = await resp.json();
1199
- return result.data ? result.data[0] : result;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1200
  }}
1201
 
1202
  async function loadData() {{
 
1190
  countrySelect.value = 'Brazil';
1191
 
1192
  async function callApi(name, params) {{
1193
+ // Try direct /run/ first (works locally)
1194
+ const directResp = await fetch('/gradio_api/run/' + name, {{
1195
  method: 'POST',
1196
  headers: {{'Content-Type': 'application/json'}},
1197
  body: JSON.stringify({{data: Object.values(params)}})
1198
  }});
1199
+ const direct = await directResp.json().catch(() => ({{}}));
1200
+ if (direct.data) return direct.data[0];
1201
+
1202
+ // Fall back to /call/ + SSE result (HF Spaces queue mode)
1203
+ const callResp = await fetch('/gradio_api/call/' + name, {{
1204
+ method: 'POST',
1205
+ headers: {{'Content-Type': 'application/json'}},
1206
+ body: JSON.stringify({{data: Object.values(params)}})
1207
+ }});
1208
+ const {{event_id}} = await callResp.json();
1209
+ if (!event_id) return {{error: 'No event id returned'}};
1210
+
1211
+ const stream = await fetch('/gradio_api/call/' + name + '/' + event_id);
1212
+ const reader = stream.body.getReader();
1213
+ const decoder = new TextDecoder();
1214
+ let buffer = '';
1215
+ while (true) {{
1216
+ const {{done, value}} = await reader.read();
1217
+ if (done) break;
1218
+ buffer += decoder.decode(value, {{stream: true}});
1219
+ const lines = buffer.split('\\n');
1220
+ buffer = lines.pop();
1221
+ for (const line of lines) {{
1222
+ if (line.startsWith('event: complete')) continue;
1223
+ if (line.startsWith('data: ')) {{
1224
+ try {{
1225
+ const parsed = JSON.parse(line.slice(6));
1226
+ return Array.isArray(parsed) ? parsed[0] : parsed;
1227
+ }} catch (e) {{}}
1228
+ }}
1229
+ }}
1230
+ }}
1231
+ return {{error: 'No data received'}};
1232
  }}
1233
 
1234
  async function loadData() {{