Spaces:
Running on Zero
Running on Zero
Siddharth Ravikumar commited on
Commit Β·
73001cc
1
Parent(s): 4355d54
feat: implement phone-home debugging for frontend errors
Browse files- backend/app/api/routes.py +7 -0
- frontend/js/alt_app.js +142 -124
backend/app/api/routes.py
CHANGED
|
@@ -442,3 +442,10 @@ async def get_logs(lines: int = Query(100, ge=1, le=1000)):
|
|
| 442 |
except Exception as e:
|
| 443 |
return {"error": str(e)}
|
| 444 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
except Exception as e:
|
| 443 |
return {"error": str(e)}
|
| 444 |
|
| 445 |
+
|
| 446 |
+
@router.post("/debug/report_error")
|
| 447 |
+
async def report_error(error_data: dict):
|
| 448 |
+
"""Log a frontend-side error to the server log for debugging."""
|
| 449 |
+
logger.error(f"[FRONTEND-ERROR] {json.dumps(error_data)}")
|
| 450 |
+
return {"status": "ok"}
|
| 451 |
+
|
frontend/js/alt_app.js
CHANGED
|
@@ -20,159 +20,177 @@ 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 API_PATHS = ['/gradio_api/call/', '/call/'];
|
| 24 |
-
let hfToken = '';
|
| 25 |
-
|
| 26 |
-
// Fetch config for ZeroGPU token
|
| 27 |
try {
|
| 28 |
-
const
|
| 29 |
-
|
| 30 |
-
const confData = await confRes.json();
|
| 31 |
-
hfToken = confData.hf_token || '';
|
| 32 |
-
}
|
| 33 |
-
} catch (e) {
|
| 34 |
-
console.warn('Failed to fetch config', e);
|
| 35 |
-
}
|
| 36 |
-
|
| 37 |
-
const headers = { 'Content-Type': 'application/json' };
|
| 38 |
-
if (hfToken) headers['Authorization'] = `Bearer ${hfToken}`;
|
| 39 |
|
| 40 |
-
|
| 41 |
-
let selectedApiBase = '';
|
| 42 |
-
let eventId = '';
|
| 43 |
-
|
| 44 |
-
// Step 1: Find working endpoint
|
| 45 |
-
for (const apiBase of API_PATHS) {
|
| 46 |
try {
|
| 47 |
-
const
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
}
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
}
|
| 148 |
-
|
| 149 |
-
if (
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
if (!done) reader.cancel();
|
| 160 |
-
if (msg.success) {
|
| 161 |
-
return msg.output.data;
|
| 162 |
-
} else {
|
| 163 |
-
throw new Error(msg.output?.error || 'Server Error');
|
| 164 |
}
|
|
|
|
|
|
|
| 165 |
}
|
| 166 |
-
} catch {
|
| 167 |
-
// Not JSON or not the expected format, skip
|
| 168 |
}
|
| 169 |
}
|
| 170 |
-
}
|
| 171 |
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
}
|
| 174 |
-
|
| 175 |
-
throw new Error('Gradio stream ended without a result');
|
| 176 |
}
|
| 177 |
|
| 178 |
// ββ Init ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 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 |
try {
|
| 24 |
+
const API_PATHS = ['/gradio_api/call/', '/call/'];
|
| 25 |
+
let hfToken = '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
// Fetch config for ZeroGPU token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
try {
|
| 29 |
+
const confRes = await fetch(`${API_BASE}/config`);
|
| 30 |
+
if (confRes.ok) {
|
| 31 |
+
const confData = await confRes.json();
|
| 32 |
+
hfToken = confData.hf_token || '';
|
| 33 |
+
}
|
| 34 |
+
} catch (e) {
|
| 35 |
+
console.warn('Failed to fetch config', e);
|
| 36 |
+
}
|
| 37 |
|
| 38 |
+
const headers = { 'Content-Type': 'application/json' };
|
| 39 |
+
if (hfToken) headers['Authorization'] = `Bearer ${hfToken}`;
|
| 40 |
+
|
| 41 |
+
let lastError = null;
|
| 42 |
+
let selectedApiBase = '';
|
| 43 |
+
let eventId = '';
|
| 44 |
+
|
| 45 |
+
// Step 1: Find working endpoint
|
| 46 |
+
for (const apiBase of API_PATHS) {
|
| 47 |
+
try {
|
| 48 |
+
const queueUrl = apiBase + apiName;
|
| 49 |
+
console.log(`[Gradio] Attempting POST: ${queueUrl}`);
|
| 50 |
+
|
| 51 |
+
const res = await fetch(queueUrl, {
|
| 52 |
+
method: 'POST',
|
| 53 |
+
headers: headers,
|
| 54 |
+
credentials: 'include',
|
| 55 |
+
body: JSON.stringify({ data: dataArr })
|
| 56 |
+
});
|
| 57 |
+
|
| 58 |
+
if (res.status === 404) {
|
| 59 |
+
console.warn(`[Gradio] 404 at ${queueUrl}, trying next fallback...`);
|
| 60 |
+
continue;
|
| 61 |
+
}
|
| 62 |
|
| 63 |
+
if (!res.ok) {
|
| 64 |
+
throw new Error(`Gradio API Request Failed (${res.status})`);
|
| 65 |
+
}
|
|
|
|
| 66 |
|
| 67 |
+
const eventObj = await res.json();
|
| 68 |
+
eventId = eventObj.event_id;
|
| 69 |
+
selectedApiBase = apiBase;
|
| 70 |
+
console.log(`[Gradio] Success at ${queueUrl}. Event ID: ${eventId}`);
|
| 71 |
+
break;
|
| 72 |
+
} catch (e) {
|
| 73 |
+
lastError = e;
|
| 74 |
+
console.error(`[Gradio] Error at ${apiBase}:`, e);
|
| 75 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
}
|
|
|
|
| 77 |
|
| 78 |
+
if (!selectedApiBase || !eventId) {
|
| 79 |
+
throw lastError || new Error('Gradio API could not be reached (All paths failed)');
|
| 80 |
+
}
|
| 81 |
|
| 82 |
+
// Step 2: Stream the result via fetch (not EventSource)
|
| 83 |
+
const streamUrl = selectedApiBase + apiName + '/' + eventId;
|
| 84 |
+
console.log(`[Gradio] Streaming from: ${streamUrl}`);
|
| 85 |
|
| 86 |
+
const streamHeaders = { 'Accept': 'text/event-stream' };
|
| 87 |
+
if (hfToken) {
|
| 88 |
+
streamHeaders['Authorization'] = `Bearer ${hfToken}`;
|
| 89 |
+
}
|
| 90 |
|
| 91 |
+
const sseRes = await fetch(streamUrl, {
|
| 92 |
+
method: 'GET',
|
| 93 |
+
headers: streamHeaders,
|
| 94 |
+
credentials: 'include',
|
| 95 |
+
cache: 'no-cache',
|
| 96 |
+
});
|
| 97 |
|
| 98 |
+
if (!sseRes.ok) {
|
| 99 |
+
throw new Error('Gradio SSE Stream Failed (' + sseRes.status + ')');
|
| 100 |
+
}
|
| 101 |
|
| 102 |
+
const reader = sseRes.body.getReader();
|
| 103 |
+
const decoder = new TextDecoder();
|
| 104 |
+
let buffer = '';
|
| 105 |
|
| 106 |
+
while (true) {
|
| 107 |
+
const { done, value } = await reader.read();
|
| 108 |
|
| 109 |
+
if (value) {
|
| 110 |
+
buffer += decoder.decode(value, { stream: true });
|
| 111 |
+
}
|
| 112 |
+
if (done) {
|
| 113 |
+
buffer += decoder.decode();
|
| 114 |
+
if (buffer.trim()) {
|
| 115 |
+
buffer += '\n\n';
|
| 116 |
+
}
|
| 117 |
}
|
|
|
|
| 118 |
|
| 119 |
+
// Parse SSE events from buffer (events separated by double newline)
|
| 120 |
+
const parts = buffer.split('\n\n');
|
| 121 |
+
// Keep the last incomplete chunk in the buffer
|
| 122 |
+
buffer = parts.pop();
|
| 123 |
|
| 124 |
+
for (const part of parts) {
|
| 125 |
+
let eventType = '';
|
| 126 |
+
const dataLines = [];
|
| 127 |
|
| 128 |
+
for (const line of part.split('\n')) {
|
| 129 |
+
if (line.startsWith('event:')) {
|
| 130 |
+
eventType = line.substring(6).trim();
|
| 131 |
+
} else if (line.startsWith('data:')) {
|
| 132 |
+
dataLines.push(line.substring(5).replace(/^ /, ''));
|
| 133 |
+
}
|
| 134 |
}
|
|
|
|
| 135 |
|
| 136 |
+
const dataLine = dataLines.join('\n');
|
| 137 |
+
if (!dataLine && !eventType) continue;
|
| 138 |
+
|
| 139 |
+
// ZeroGPU format: event type is "complete" or "error",
|
| 140 |
+
// data is a raw JSON array of outputs
|
| 141 |
+
if (eventType === 'complete') {
|
| 142 |
+
if (!done) reader.cancel();
|
| 143 |
+
try {
|
| 144 |
+
const result = JSON.parse(dataLine);
|
| 145 |
+
return Array.isArray(result) ? result : [result];
|
| 146 |
+
} catch {
|
| 147 |
+
return [dataLine];
|
| 148 |
+
}
|
| 149 |
+
} else if (eventType === 'error') {
|
| 150 |
+
if (!done) reader.cancel();
|
| 151 |
+
console.error(`[Gradio] SSE Error event:`, part);
|
| 152 |
+
const errorDetail = dataLine === 'null' ? 'Server Error (null)' : dataLine;
|
| 153 |
+
// Phone home specifically for SSE error
|
| 154 |
+
throw new Error(`SSE_ERROR: ${errorDetail}`);
|
| 155 |
}
|
| 156 |
+
// Also handle the non-ZeroGPU format (Gradio standard)
|
| 157 |
+
if (dataLine) {
|
| 158 |
+
try {
|
| 159 |
+
const msg = JSON.parse(dataLine);
|
| 160 |
+
if (msg.msg === 'process_completed') {
|
| 161 |
+
if (!done) reader.cancel();
|
| 162 |
+
if (msg.success) {
|
| 163 |
+
return msg.output.data;
|
| 164 |
+
} else {
|
| 165 |
+
throw new Error(msg.output?.error || 'Server Error');
|
| 166 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
}
|
| 168 |
+
} catch {
|
| 169 |
+
// Not JSON or not the expected format, skip
|
| 170 |
}
|
|
|
|
|
|
|
| 171 |
}
|
| 172 |
}
|
|
|
|
| 173 |
|
| 174 |
+
if (done) break;
|
| 175 |
+
}
|
| 176 |
+
} catch (err) {
|
| 177 |
+
// Phone-home error report
|
| 178 |
+
try {
|
| 179 |
+
fetch(`${API_BASE}/debug/report_error`, {
|
| 180 |
+
method: 'POST',
|
| 181 |
+
headers: { 'Content-Type': 'application/json' },
|
| 182 |
+
body: JSON.stringify({
|
| 183 |
+
apiName,
|
| 184 |
+
error: err.message,
|
| 185 |
+
stack: err.stack,
|
| 186 |
+
userAgent: navigator.userAgent,
|
| 187 |
+
url: window.location.href,
|
| 188 |
+
timestamp: new Date().toISOString()
|
| 189 |
+
})
|
| 190 |
+
});
|
| 191 |
+
} catch (e) { /* ignore secondary error */ }
|
| 192 |
+
throw err;
|
| 193 |
}
|
|
|
|
|
|
|
| 194 |
}
|
| 195 |
|
| 196 |
// ββ Init ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|