LiuZichen commited on
Commit
03420c4
·
verified ·
1 Parent(s): 5c0f2ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -15
app.py CHANGED
@@ -71,27 +71,48 @@ zerogpu_fix_js = """
71
 
72
  async function initZeroGPU() {
73
  try {
 
74
  const headers = await post_message("zerogpu-headers");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  if (headers) {
76
- console.log("ZeroGPU Fix: Received headers", headers);
77
 
78
  const originalFetch = window.fetch;
79
  window.fetch = async function(url, options) {
80
- // Only inject headers for prediction requests to avoid rate limiting on heartbeats/static assets
81
- // Gradio requests typically go to /queue/join, /api/predict, etc.
82
  if (typeof url === 'string' && (url.includes('/queue/join') || url.includes('/call/') || url.includes('/api/'))) {
83
- if (!options) options = {};
84
- if (!options.headers) options.headers = {};
85
-
86
- const headerMap = headers instanceof Map ? headers : new Map(Object.entries(headers));
87
-
88
- headerMap.forEach((value, key) => {
89
- if (options.headers instanceof Headers) {
90
- options.headers.append(key, value);
91
- } else {
92
- options.headers[key] = value;
93
- }
94
- });
 
 
 
95
  }
96
 
97
  return originalFetch(url, options);
@@ -145,6 +166,13 @@ def generate_image_handler(x, negative_prompt, fine_edge, fix_perspective, grow_
145
 
146
  # 1. Extract headers from the current user's request
147
  forward_headers = get_zerogpu_headers(request.headers)
 
 
 
 
 
 
 
148
 
149
  try:
150
  # 2. Instantiate a client specifically for this request with the forwarded headers.
 
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);
 
166
 
167
  # 1. Extract headers from the current user's request
168
  forward_headers = get_zerogpu_headers(request.headers)
169
+
170
+ # 打印调试信息,确认是否收到了 x-zerogpu-token
171
+ print(f"Debug: Received headers keys: {list(request.headers.keys())}")
172
+ if "x-zerogpu-token" in forward_headers:
173
+ print("Debug: x-zerogpu-token found in request headers.")
174
+ else:
175
+ print("Debug: x-zerogpu-token NOT found in request headers.")
176
 
177
  try:
178
  # 2. Instantiate a client specifically for this request with the forwarded headers.