Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -49,94 +49,94 @@ except Exception as e:
|
|
| 49 |
|
| 50 |
# --- Helper Functions ---
|
| 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 |
} else {
|
| 91 |
-
options
|
| 92 |
-
|
| 93 |
-
});
|
| 94 |
-
return options;
|
| 95 |
-
};
|
| 96 |
-
|
| 97 |
-
if (headers) {
|
| 98 |
-
console.log("ZeroGPU Fix: Received initial headers", headers);
|
| 99 |
-
|
| 100 |
-
const originalFetch = window.fetch;
|
| 101 |
-
window.fetch = async function(url, options) {
|
| 102 |
-
// Inject headers for ALL requests to be safe, especially queue/join
|
| 103 |
-
// We filter out only obviously external or static asset requests if needed,
|
| 104 |
-
// but for debugging, let's be aggressive.
|
| 105 |
-
if (typeof url === 'string' && (url.includes('/queue/') || url.includes('/api/') || url.includes('run/predict'))) {
|
| 106 |
-
console.log("ZeroGPU Fix: Intercepting request to", url);
|
| 107 |
-
|
| 108 |
-
try {
|
| 109 |
-
// Attempt to refresh headers
|
| 110 |
-
const currentHeaders = await post_message("zerogpu-headers");
|
| 111 |
-
if (currentHeaders) {
|
| 112 |
-
options = updateHeaders(options, currentHeaders);
|
| 113 |
-
console.log("ZeroGPU Fix: Injected FRESH headers");
|
| 114 |
-
} else {
|
| 115 |
-
options = updateHeaders(options, headers);
|
| 116 |
-
console.log("ZeroGPU Fix: Injected CACHED headers (refresh failed)");
|
| 117 |
-
}
|
| 118 |
-
} catch (e) {
|
| 119 |
-
options = updateHeaders(options, headers);
|
| 120 |
-
console.log("ZeroGPU Fix: Injected CACHED headers (error)");
|
| 121 |
-
}
|
| 122 |
}
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
} else {
|
| 128 |
-
console.warn("ZeroGPU Fix: No headers received from parent.");
|
| 129 |
}
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
}
|
|
|
|
|
|
|
|
|
|
| 133 |
}
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
def get_zerogpu_headers(request_headers):
|
| 142 |
"""
|
|
|
|
| 49 |
|
| 50 |
# --- Helper Functions ---
|
| 51 |
|
| 52 |
+
// JS script to fix ZeroGPU headers propagation
|
| 53 |
+
zerogpu_fix_js = """
|
| 54 |
+
<script>
|
| 55 |
+
(function() {
|
| 56 |
+
const is_zerogpu = typeof window !== "undefined" && window.parent !== window;
|
| 57 |
+
if (!is_zerogpu) {
|
| 58 |
+
console.log("ZeroGPU Fix: Not in iframe, skipping.");
|
| 59 |
+
return;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
console.log("ZeroGPU Fix: Detected iframe, initializing...");
|
| 63 |
+
|
| 64 |
+
function post_message(message) {
|
| 65 |
+
return new Promise((resolve, reject) => {
|
| 66 |
+
const channel = new MessageChannel();
|
| 67 |
+
channel.port1.onmessage = (event) => {
|
| 68 |
+
channel.port1.close();
|
| 69 |
+
resolve(event.data);
|
| 70 |
+
};
|
| 71 |
+
window.parent.postMessage(message, "*", [channel.port2]);
|
| 72 |
+
});
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
async function initZeroGPU() {
|
| 76 |
+
try {
|
| 77 |
+
console.log("ZeroGPU Fix: Requesting initial headers...");
|
| 78 |
+
const headers = await post_message("zerogpu-headers");
|
| 79 |
+
|
| 80 |
+
// Helper to update headers object
|
| 81 |
+
const updateHeaders = (options, headers) => {
|
| 82 |
+
if (!options) options = {};
|
| 83 |
+
if (!options.headers) options.headers = {};
|
| 84 |
+
|
| 85 |
+
const headerMap = headers instanceof Map ? headers : new Map(Object.entries(headers));
|
| 86 |
+
|
| 87 |
+
headerMap.forEach((value, key) => {
|
| 88 |
+
if (options.headers instanceof Headers) {
|
| 89 |
+
options.headers.append(key, value);
|
| 90 |
+
} else {
|
| 91 |
+
options.headers[key] = value;
|
| 92 |
+
}
|
| 93 |
});
|
| 94 |
+
return options;
|
| 95 |
+
};
|
| 96 |
+
|
| 97 |
+
if (headers) {
|
| 98 |
+
console.log("ZeroGPU Fix: Received initial headers", headers);
|
| 99 |
+
|
| 100 |
+
const originalFetch = window.fetch;
|
| 101 |
+
window.fetch = async function(url, options) {
|
| 102 |
+
// Inject headers for ALL requests to be safe, especially queue/join
|
| 103 |
+
// We filter out only obviously external or static asset requests if needed,
|
| 104 |
+
// but for debugging, let's be aggressive.
|
| 105 |
+
if (typeof url === 'string' && (url.includes('/queue/') || url.includes('/api/') || url.includes('run/predict'))) {
|
| 106 |
+
console.log("ZeroGPU Fix: Intercepting request to", url);
|
| 107 |
|
| 108 |
+
try {
|
| 109 |
+
// Attempt to refresh headers
|
| 110 |
+
const currentHeaders = await post_message("zerogpu-headers");
|
| 111 |
+
if (currentHeaders) {
|
| 112 |
+
options = updateHeaders(options, currentHeaders);
|
| 113 |
+
console.log("ZeroGPU Fix: Injected FRESH headers");
|
| 114 |
} else {
|
| 115 |
+
options = updateHeaders(options, headers);
|
| 116 |
+
console.log("ZeroGPU Fix: Injected CACHED headers (refresh failed)");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
}
|
| 118 |
+
} catch (e) {
|
| 119 |
+
options = updateHeaders(options, headers);
|
| 120 |
+
console.log("ZeroGPU Fix: Injected CACHED headers (error)");
|
| 121 |
+
}
|
|
|
|
|
|
|
| 122 |
}
|
| 123 |
+
|
| 124 |
+
return originalFetch(url, options);
|
| 125 |
+
};
|
| 126 |
+
console.log("ZeroGPU Fix: Fetch interceptor installed.");
|
| 127 |
+
} else {
|
| 128 |
+
console.warn("ZeroGPU Fix: No headers received from parent.");
|
| 129 |
}
|
| 130 |
+
} catch (e) {
|
| 131 |
+
console.error("ZeroGPU Fix: Error initializing", e);
|
| 132 |
+
}
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
// Run immediately
|
| 136 |
+
initZeroGPU();
|
| 137 |
+
})();
|
| 138 |
+
</script>
|
| 139 |
+
"""
|
| 140 |
|
| 141 |
def get_zerogpu_headers(request_headers):
|
| 142 |
"""
|