nsfwalex commited on
Commit
1a72e70
1 Parent(s): 7c4ee69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -10
app.py CHANGED
@@ -109,9 +109,16 @@ function getEnvInfo() {
109
 
110
  return result;
111
  }
 
 
 
 
 
 
 
112
  window.g=function(p){
113
  params = getEnvInfo();
114
- if (params["e"] != "1"){
115
  return "";
116
  }
117
  const conditions = {
@@ -132,7 +139,9 @@ window.postMessageToParent = function(prompt, event, source, value) {
132
  source: source,
133
  value: value
134
  };
135
-
 
 
136
  // Post the message to the parent window
137
  window.parent.postMessage(message, '*');
138
  console.log("post finish");
@@ -163,11 +172,7 @@ function uploadImage(prompt, images, event, source, value) {
163
  function onDemoLoad(){
164
  let envInfo = getEnvInfo();
165
  console.log(envInfo);
166
- if (envInfo["e"] == "1" ||
167
- envInfo["__domain"].indexOf("nsfwais.io") != -1 ||
168
- envInfo["__iframe_domain"].indexOf("nsfwais.io") != -1 ||
169
- envInfo["__domain"].indexOf("127.0.0.1") != -1 ||
170
- envInfo["__iframe_domain"].indexOf("127.0.0.1") != -1){
171
  var element = document.getElementById("desc_html_code");
172
  if (element) {
173
  element.parentNode.removeChild(element);
@@ -237,7 +242,7 @@ def generate(prompt, progress=gr.Progress(track_tqdm=True)):
237
  images = [save_image(img) for img in images]
238
  image_paths = [i[1] for i in images]
239
  print(prompt_str, image_paths)
240
- return [i[0] for i in images]
241
 
242
  with gr.Blocks(css=css,head=js,fill_height=True) as demo:
243
  with gr.Row(equal_height=False):
@@ -299,8 +304,8 @@ with gr.Blocks(css=css,head=js,fill_height=True) as demo:
299
  return []
300
 
301
 
302
- result.change(fn=lambda x,y:x, inputs=[prompt,result], outputs=[], js=f'''(p,img)=>window.uploadImage(p, img,"process_finished","demo_hf_{cfg.get("name")}_card", "finish")''')
303
- run_button.click(generate, inputs=[prompt], outputs=[result], js=f'''(p)=>window.postMessageToParent(p,"process_started","demo_hf_{cfg.get("name")}_card", "click_go")''')
304
  random_button.click(fn=lambda x:x, inputs=[prompt], outputs=[prompt], js='''(p)=>window.g(p)''')
305
  demo.load(fn=on_demo_load, inputs=[], outputs=[result], js='''()=>onDemoLoad()''')
306
  if __name__ == "__main__":
 
109
 
110
  return result;
111
  }
112
+ function isValidEnv(){
113
+ return envInfo["e"] == "1" ||
114
+ envInfo["__domain"].indexOf("nsfwais.io") != -1 ||
115
+ envInfo["__iframe_domain"].indexOf("nsfwais.io") != -1 ||
116
+ envInfo["__domain"].indexOf("127.0.0.1") != -1 ||
117
+ envInfo["__iframe_domain"].indexOf("127.0.0.1") != -1;
118
+ }
119
  window.g=function(p){
120
  params = getEnvInfo();
121
+ if (!isValidEnv()){
122
  return "";
123
  }
124
  const conditions = {
 
139
  source: source,
140
  value: value
141
  };
142
+ if (!prompt){
143
+ prompt = window.g();
144
+ }
145
  // Post the message to the parent window
146
  window.parent.postMessage(message, '*');
147
  console.log("post finish");
 
172
  function onDemoLoad(){
173
  let envInfo = getEnvInfo();
174
  console.log(envInfo);
175
+ if (isValidEnv()){
 
 
 
 
176
  var element = document.getElementById("desc_html_code");
177
  if (element) {
178
  element.parentNode.removeChild(element);
 
242
  images = [save_image(img) for img in images]
243
  image_paths = [i[1] for i in images]
244
  print(prompt_str, image_paths)
245
+ return [i[0] for i in images], prompt
246
 
247
  with gr.Blocks(css=css,head=js,fill_height=True) as demo:
248
  with gr.Row(equal_height=False):
 
304
  return []
305
 
306
 
307
+ result.change(fn=lambda x,y:x, inputs=[prompt,result], outputs=[], js=f'''(p,img)=>window.uploadImage(p, img,"process_finished","demo_hf_{cfg.get("name")}_card", "{cfg["model_id"]}")''')
308
+ run_button.click(generate, inputs=[prompt], outputs=[result, prompt], js=f'''(p)=>window.postMessageToParent(p,"process_started","demo_hf_{cfg.get("name")}_card", "click_go")''')
309
  random_button.click(fn=lambda x:x, inputs=[prompt], outputs=[prompt], js='''(p)=>window.g(p)''')
310
  demo.load(fn=on_demo_load, inputs=[], outputs=[result], js='''()=>onDemoLoad()''')
311
  if __name__ == "__main__":