LiuZichen commited on
Commit
77abe98
·
verified ·
1 Parent(s): 03420c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -72
app.py CHANGED
@@ -49,84 +49,94 @@ except Exception as e:
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) return;
58
-
59
- console.log("ZeroGPU Fix: Detected iframe, attempting to fetch headers.");
60
-
61
- function post_message(message) {
62
- return new Promise((resolve, reject) => {
63
- const channel = new MessageChannel();
64
- channel.port1.onmessage = (event) => {
65
- channel.port1.close();
66
- resolve(event.data);
67
- };
68
- window.parent.postMessage(message, "*", [channel.port2]);
69
- });
70
- }
71
-
72
- async function initZeroGPU() {
73
- try {
74
- // Initial check for headers
75
- const headers = await post_message("zerogpu-headers");
76
-
77
- // Helper to update headers object
78
- const updateHeaders = (options, headers) => {
79
- if (!options) options = {};
80
- if (!options.headers) options.headers = {};
81
-
82
- const headerMap = headers instanceof Map ? headers : new Map(Object.entries(headers));
83
-
84
- headerMap.forEach((value, key) => {
85
- if (options.headers instanceof Headers) {
86
- options.headers.append(key, value);
87
- } else {
88
- options.headers[key] = value;
89
- }
90
  });
91
- return options;
92
- };
93
-
94
- if (headers) {
95
- console.log("ZeroGPU Fix: Received initial headers", headers);
96
-
97
- const originalFetch = window.fetch;
98
- window.fetch = async function(url, options) {
99
- // Only inject headers for prediction requests
100
- if (typeof url === 'string' && (url.includes('/queue/join') || url.includes('/call/') || url.includes('/api/'))) {
 
101
 
102
- // Re-fetch headers before every important request to ensure token freshness
103
- // This is critical because tokens might expire or change
104
- try {
105
- const currentHeaders = await post_message("zerogpu-headers");
106
- if (currentHeaders) {
107
- options = updateHeaders(options, currentHeaders);
108
  } else {
109
- // Fallback to initial headers if fetch fails
110
- options = updateHeaders(options, headers);
111
  }
112
- } catch (e) {
113
- // Fallback to initial headers
114
- options = updateHeaders(options, headers);
115
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  }
117
-
118
- return originalFetch(url, options);
119
- };
120
  }
121
- } catch (e) {
122
- console.error("ZeroGPU Fix: Error fetching headers", e);
123
- }
124
- }
125
-
126
- initZeroGPU();
127
- })();
128
- </script>
129
- """
130
 
131
  def get_zerogpu_headers(request_headers):
132
  """
 
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
  """