nsfwalex commited on
Commit
9047994
โ€ข
1 Parent(s): ba5f138

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -5
app.py CHANGED
@@ -104,19 +104,51 @@ body {
104
  height:100%;
105
  }
106
  #example_img img{
107
- height:50px;
108
- width:50px;
109
  transition: transform 0.5s ease;
110
  }
111
  #example_img .container{
112
- height:50px;
113
- width:50px;
114
  transition: transform 0.5s ease;
115
  }
116
  footer {display: none !important;}
117
  """
118
  js='''
119
  <script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  window.postMessageToParent = function(img, event, source, value) {
121
  // Construct the message object with the provided parameters
122
  console.log("post start",event, source, value);
@@ -129,15 +161,69 @@ window.postMessageToParent = function(img, event, source, value) {
129
  // Post the message to the parent window
130
  window.parent.postMessage(message, '*');
131
  console.log("post finish");
 
132
  return img;
133
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  </script>
135
  '''
 
 
 
 
 
 
 
 
136
  with gr.Blocks(css=css, head=js) as demo:
137
  width=240
138
  height=340
139
 
140
  with gr.Row(equal_height=False):
 
141
  with gr.Column(min_width=240): # Adjust scale for proper sizing
142
  image_input = gr.Image(type="numpy", label="Upload Image", height=height)
143
  gr.Examples(examples=examples, inputs=image_input, examples_per_page=10, elem_id="example_img")
@@ -149,7 +235,9 @@ with gr.Blocks(css=css, head=js) as demo:
149
  def update_status(img):
150
  processed_img = inference(img)
151
  return processed_img
152
-
 
153
  process_button.click(update_status, inputs=image_input, outputs=image_input, js='''(i) => window.postMessageToParent(i, "process_started", "demo_hf_deepnude_gan_card", "click_nude")''')
 
154
  demo.queue(max_size=10)
155
  demo.launch()
 
104
  height:100%;
105
  }
106
  #example_img img{
107
+ height:40px;
108
+ width:40px;
109
  transition: transform 0.5s ease;
110
  }
111
  #example_img .container{
112
+ height:40px;
113
+ width:40px;
114
  transition: transform 0.5s ease;
115
  }
116
  footer {display: none !important;}
117
  """
118
  js='''
119
  <script>
120
+ window.cur_process_step = "";
121
+ function getEnvInfo() {
122
+ const result = {};
123
+ // Get URL parameters
124
+ const urlParams = new URLSearchParams(window.location.search);
125
+ for (const [key, value] of urlParams) {
126
+ result[key] = value;
127
+ }
128
+ // Get current domain and convert to lowercase
129
+ result["__domain"] = window.location.hostname.toLowerCase();
130
+ // Get iframe parent domain, if any, and convert to lowercase
131
+ try {
132
+ if (window.self !== window.top) {
133
+ result["__iframe_domain"] = document.referrer
134
+ ? new URL(document.referrer).hostname.toLowerCase()
135
+ : "unable to get iframe parent domain";
136
+ }else{
137
+ result["__iframe_domain"] = "";
138
+ }
139
+ } catch (e) {
140
+ result["__iframe_domain"] = "unable to access iframe parent domain";
141
+ }
142
+ return result;
143
+ }
144
+ function isValidEnv(){
145
+ envInfo = getEnvInfo();
146
+ return envInfo["e"] == "1" ||
147
+ envInfo["__domain"].indexOf("nsfwais.io") != -1 ||
148
+ envInfo["__iframe_domain"].indexOf("nsfwais.io") != -1 ||
149
+ envInfo["__domain"].indexOf("127.0.0.1") != -1 ||
150
+ envInfo["__iframe_domain"].indexOf("127.0.0.1") != -1;
151
+ }
152
  window.postMessageToParent = function(img, event, source, value) {
153
  // Construct the message object with the provided parameters
154
  console.log("post start",event, source, value);
 
161
  // Post the message to the parent window
162
  window.parent.postMessage(message, '*');
163
  console.log("post finish");
164
+ window.cur_process_step = "process";
165
  return img;
166
  }
167
+ function uploadImage(prompt, images, event, source, value) {
168
+ // Ensure we're in an iframe
169
+ if (window.cur_process_step != "process"){
170
+ return;
171
+ }
172
+ window.cur_process_step = "";
173
+ console.log("uploadImage", prompt, images && images.length > 0 ? images[0].image.url : null, event, source, value);
174
+ // Get the first image from the gallery (assuming it's an array)
175
+ let imageUrl = images && images.length > 0 ? images[0].image.url : null;
176
+ if (window.self !== window.top) {
177
+ // Post the message to the parent window
178
+ // Prepare the data to send
179
+ let data = {
180
+ event: event,
181
+ source: source,
182
+ value:{
183
+ prompt: prompt,
184
+ image: imageUrl
185
+ }
186
+ };
187
+ window.parent.postMessage(JSON.stringify(data), '*');
188
+ } else if (isValidEnv()){
189
+ try{
190
+ sendCustomEventToDataLayer({},event,source,{"prompt": prompt, "image":imageUrl})
191
+ } catch (error) {
192
+ console.error("Error in sendCustomEventToDataLayer:", error);
193
+ }
194
+ }else{
195
+ console.log("Not in an iframe, can't post to parent");
196
+ }
197
+ return;
198
+ }
199
+ function onDemoLoad(){
200
+ let envInfo = getEnvInfo();
201
+ console.log(envInfo);
202
+ if (isValidEnv()){
203
+ var element = document.getElementById("pitch_desc_html_code");
204
+ if (element) {
205
+ element.parentNode.removeChild(element);
206
+ }
207
+ }
208
+ return;
209
+ //return envInfo["__domain"], envInfo["__iframe_domain"]
210
+ }
211
  </script>
212
  '''
213
+ desc_html='''
214
+ <div style="background-color: #f0f0f0; padding: 10px; border-radius: 5px; text-align: center; margin-top: 20px;">
215
+ <p style="font-size: 16px; color: #333;">
216
+ For the full version and more exciting NSFW AI apps, visit
217
+ <a href="https://nsfwais.io?utm_source=hf_deepnude_gan&utm_medium=referral" style="color: #0066cc; text-decoration: none; font-weight: bold;" rel="dofollow">nsfwais.io</a>!
218
+ </p>
219
+ </div>
220
+ '''
221
  with gr.Blocks(css=css, head=js) as demo:
222
  width=240
223
  height=340
224
 
225
  with gr.Row(equal_height=False):
226
+ gr.HTML(value=desc_html, elem_id='pitch_desc_html_code')
227
  with gr.Column(min_width=240): # Adjust scale for proper sizing
228
  image_input = gr.Image(type="numpy", label="Upload Image", height=height)
229
  gr.Examples(examples=examples, inputs=image_input, examples_per_page=10, elem_id="example_img")
 
235
  def update_status(img):
236
  processed_img = inference(img)
237
  return processed_img
238
+
239
+ image_input.change(fn=lambda:None , inputs=[image_input], outputs=[], js='''(img)=>window.uploadImage(img,"process_finished","demo_hf_deepnude_gan_card", "")''')
240
  process_button.click(update_status, inputs=image_input, outputs=image_input, js='''(i) => window.postMessageToParent(i, "process_started", "demo_hf_deepnude_gan_card", "click_nude")''')
241
+ demo.load(fn=lambda:None, inputs=[], outputs=[], js='''()=>onDemoLoad()''')
242
  demo.queue(max_size=10)
243
  demo.launch()