LiuZichen commited on
Commit
9a41f03
·
verified ·
1 Parent(s): 4263bec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -1
app.py CHANGED
@@ -49,6 +49,60 @@ except Exception as e:
49
 
50
  # --- Helper Functions ---
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  def get_zerogpu_headers(request_headers):
53
  """
54
  Extracts ZeroGPU specific headers from the incoming request headers.
@@ -126,7 +180,7 @@ def generate_image_handler(x, negative_prompt, fine_edge, fix_perspective, grow_
126
 
127
  # --- Gradio UI ---
128
 
129
- with gr.Blocks(title="MagicQuill V2") as demo:
130
  with gr.Row(elem_classes="row"):
131
  text = gr.Markdown(
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) 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
+ 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
+ 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
+ if (options.headers instanceof Headers) {
87
+ options.headers.append(key, value);
88
+ } else {
89
+ options.headers[key] = value;
90
+ }
91
+ });
92
+
93
+ return originalFetch(url, options);
94
+ };
95
+ }
96
+ } catch (e) {
97
+ console.error("ZeroGPU Fix: Error fetching headers", e);
98
+ }
99
+ }
100
+
101
+ initZeroGPU();
102
+ })();
103
+ </script>
104
+ """
105
+
106
  def get_zerogpu_headers(request_headers):
107
  """
108
  Extracts ZeroGPU specific headers from the incoming request headers.
 
180
 
181
  # --- Gradio UI ---
182
 
183
+ with gr.Blocks(title="MagicQuill V2", head=zerogpu_fix_js) as demo:
184
  with gr.Row(elem_classes="row"):
185
  text = gr.Markdown(
186
  """