bruceyyu commited on
Commit
6cdf6a8
·
verified ·
1 Parent(s): aab4ba6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -96
app.py CHANGED
@@ -47,95 +47,6 @@ except Exception as e:
47
  print(f"Failed to connect to SAM client: {e}")
48
  sam_client = None
49
 
50
- # --- Helper Functions ---
51
- zerogpu_fix_js = """
52
- <script>
53
- (function() {
54
- const is_zerogpu = typeof window !== "undefined" && window.parent !== window;
55
- if (!is_zerogpu) {
56
- console.log("ZeroGPU Fix: Not in iframe, skipping.");
57
- return;
58
- }
59
-
60
- console.log("ZeroGPU Fix: Detected iframe, initializing...");
61
-
62
- function post_message(message) {
63
- return new Promise((resolve, reject) => {
64
- const channel = new MessageChannel();
65
- channel.port1.onmessage = (event) => {
66
- channel.port1.close();
67
- resolve(event.data);
68
- };
69
- window.parent.postMessage(message, "*", [channel.port2]);
70
- });
71
- }
72
-
73
- async function initZeroGPU() {
74
- try {
75
- console.log("ZeroGPU Fix: Requesting initial headers...");
76
- const headers = await post_message("zerogpu-headers");
77
-
78
- // Helper to update headers object
79
- const updateHeaders = (options, headers) => {
80
- if (!options) options = {};
81
- if (!options.headers) options.headers = {};
82
-
83
- const headerMap = headers instanceof Map ? headers : new Map(Object.entries(headers));
84
-
85
- headerMap.forEach((value, key) => {
86
- // Force lowercase keys to avoid case mismatch
87
- key = key.toLowerCase();
88
- if (options.headers instanceof Headers) {
89
- options.headers.set(key, value); // Use set instead of append to avoid duplicates
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
- console.log("ZeroGPU Fix: Intercepting request to", url);
103
-
104
- if (!options) options = {};
105
- // Inject headers for ALL requests to be safe
106
- try {
107
- // Attempt to refresh headers
108
- const currentHeaders = await post_message("zerogpu-headers");
109
- if (currentHeaders) {
110
- options = updateHeaders(options, currentHeaders);
111
- console.log("ZeroGPU Fix: Injected FRESH headers");
112
- console.log(options.headers)
113
- } else {
114
- options = updateHeaders(options, headers);
115
- console.log("ZeroGPU Fix: Injected CACHED headers (refresh failed)");
116
- }
117
- } catch (e) {
118
- options = updateHeaders(options, headers);
119
- console.log("ZeroGPU Fix: Injected CACHED headers (error)");
120
- }
121
-
122
- return originalFetch(url, options);
123
- };
124
- console.log("ZeroGPU Fix: Fetch interceptor installed.");
125
- } else {
126
- console.warn("ZeroGPU Fix: No headers received from parent.");
127
- }
128
- } catch (e) {
129
- console.error("ZeroGPU Fix: Error initializing", e);
130
- }
131
- }
132
-
133
- // Run immediately
134
- initZeroGPU();
135
- })();
136
- </script>
137
- """
138
-
139
  def get_zerogpu_headers(request_headers):
140
  """
141
  Extracts ZeroGPU specific headers from the incoming request headers.
@@ -172,17 +83,10 @@ def generate_image_handler(x, negative_prompt, fine_edge, fix_perspective, grow_
172
  add_prop_image = x['from_frontend']['add_prop_image']
173
  positive_prompt = x['from_backend']['prompt']
174
 
175
- # 1. Extract headers from the current user's request
176
- print(request.headers)
177
  forward_headers = get_zerogpu_headers(request.headers)
178
 
179
- # 打印调试信息,确认是否收到了 x-zerogpu-token
180
  print(f"Debug: Received headers keys: {list(request.headers.keys())}")
181
  print(forward_headers)
182
- if "x-zerogpu-token" in forward_headers:
183
- print("Debug: x-zerogpu-token found in request headers.")
184
- else:
185
- print("Debug: x-zerogpu-token NOT found in request headers.")
186
 
187
  try:
188
  # 2. Instantiate a client specifically for this request with the forwarded headers.
 
47
  print(f"Failed to connect to SAM client: {e}")
48
  sam_client = None
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  def get_zerogpu_headers(request_headers):
51
  """
52
  Extracts ZeroGPU specific headers from the incoming request headers.
 
83
  add_prop_image = x['from_frontend']['add_prop_image']
84
  positive_prompt = x['from_backend']['prompt']
85
 
 
 
86
  forward_headers = get_zerogpu_headers(request.headers)
87
 
 
88
  print(f"Debug: Received headers keys: {list(request.headers.keys())}")
89
  print(forward_headers)
 
 
 
 
90
 
91
  try:
92
  # 2. Instantiate a client specifically for this request with the forwarded headers.