Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,12 @@
|
|
1 |
from run import process
|
2 |
import time
|
|
|
3 |
import os
|
|
|
4 |
import cv2
|
|
|
5 |
from PIL import Image
|
|
|
6 |
import gradio as gr
|
7 |
|
8 |
TESTdevice = "cpu"
|
@@ -36,12 +40,27 @@ def inference(img):
|
|
36 |
return output
|
37 |
|
38 |
def load_image_from_file(file_path, new_height=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
try:
|
40 |
img = Image.open(file_path)
|
41 |
-
|
|
|
|
|
42 |
aspect_ratio = img.width / img.height
|
43 |
new_width = int(new_height * aspect_ratio)
|
|
|
|
|
44 |
img = img.resize((new_width, new_height), Image.LANCZOS)
|
|
|
45 |
return img
|
46 |
except FileNotFoundError:
|
47 |
print(f"File not found: {file_path}")
|
@@ -65,12 +84,104 @@ examples = [
|
|
65 |
[load_image_from_file('example8.webp')],
|
66 |
]
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
with gr.Row(equal_height=False):
|
73 |
-
with gr.Column(min_width=240):
|
74 |
image_input = gr.Image(type="numpy", label="", height=height)
|
75 |
gr.Examples(examples=examples, inputs=image_input, examples_per_page=10, elem_id="example_img")
|
76 |
process_button = gr.Button("Nude!", size="sm")
|
@@ -79,8 +190,8 @@ with gr.Blocks(theme="Nymbo/Alyx_Theme") as demo:
|
|
79 |
processed_img = inference(img)
|
80 |
return processed_img
|
81 |
|
82 |
-
image_input.change(fn=lambda x: x, inputs=[image_input], outputs=[gr.State([])])
|
83 |
-
process_button.click(update_status, inputs=image_input, outputs=image_input)
|
84 |
-
|
85 |
demo.queue(max_size=10)
|
86 |
demo.launch()
|
|
|
1 |
from run import process
|
2 |
import time
|
3 |
+
import subprocess
|
4 |
import os
|
5 |
+
import argparse
|
6 |
import cv2
|
7 |
+
import sys
|
8 |
from PIL import Image
|
9 |
+
import torch
|
10 |
import gradio as gr
|
11 |
|
12 |
TESTdevice = "cpu"
|
|
|
40 |
return output
|
41 |
|
42 |
def load_image_from_file(file_path, new_height=None):
|
43 |
+
"""
|
44 |
+
Load an image from a file and optionally resize it while maintaining the aspect ratio.
|
45 |
+
|
46 |
+
Args:
|
47 |
+
file_path (str): The path to the image file.
|
48 |
+
new_height (int, optional): The new height for the image. If None, the image is not resized.
|
49 |
+
|
50 |
+
Returns:
|
51 |
+
Image: The loaded (and optionally resized) image.
|
52 |
+
"""
|
53 |
try:
|
54 |
img = Image.open(file_path)
|
55 |
+
|
56 |
+
if (new_height is not None):
|
57 |
+
# Calculate new width to maintain aspect ratio
|
58 |
aspect_ratio = img.width / img.height
|
59 |
new_width = int(new_height * aspect_ratio)
|
60 |
+
|
61 |
+
# Resize the image
|
62 |
img = img.resize((new_width, new_height), Image.LANCZOS)
|
63 |
+
|
64 |
return img
|
65 |
except FileNotFoundError:
|
66 |
print(f"File not found: {file_path}")
|
|
|
84 |
[load_image_from_file('example8.webp')],
|
85 |
]
|
86 |
|
87 |
+
js='''
|
88 |
+
<script>
|
89 |
+
window.cur_process_step = "";
|
90 |
+
function getEnvInfo() {
|
91 |
+
const result = {};
|
92 |
+
// Get URL parameters
|
93 |
+
const urlParams = new URLSearchParams(window.location.search);
|
94 |
+
for (const [key, value] of urlParams) {
|
95 |
+
result[key] = value;
|
96 |
+
}
|
97 |
+
// Get current domain and convert to lowercase
|
98 |
+
result["__domain"] = window.location.hostname.toLowerCase();
|
99 |
+
// Get iframe parent domain, if any, and convert to lowercase
|
100 |
+
try {
|
101 |
+
if (window.self !== window.top) {
|
102 |
+
result["__iframe_domain"] = document.referrer
|
103 |
+
? new URL(document.referrer).hostname.toLowerCase()
|
104 |
+
: "unable to get iframe parent domain";
|
105 |
+
}else{
|
106 |
+
result["__iframe_domain"] = "";
|
107 |
+
}
|
108 |
+
} catch (e) {
|
109 |
+
result["__iframe_domain"] = "unable to access iframe parent domain";
|
110 |
+
}
|
111 |
+
return result;
|
112 |
+
}
|
113 |
+
function isValidEnv(){
|
114 |
+
envInfo = getEnvInfo();
|
115 |
+
return envInfo["e"] == "1" ||
|
116 |
+
envInfo["__domain"].indexOf("nsfwais.io") != -1 ||
|
117 |
+
envInfo["__iframe_domain"].indexOf("nsfwais.io") != -1 ||
|
118 |
+
envInfo["__domain"].indexOf("127.0.0.1") != -1 ||
|
119 |
+
envInfo["__iframe_domain"].indexOf("127.0.0.1") != -1;
|
120 |
+
}
|
121 |
+
window.postMessageToParent = function(img, event, source, value) {
|
122 |
+
// Construct the message object with the provided parameters
|
123 |
+
console.log("post start",event, source, value);
|
124 |
+
const message = {
|
125 |
+
event: event,
|
126 |
+
source: source,
|
127 |
+
value: value
|
128 |
+
};
|
129 |
+
|
130 |
+
// Post the message to the parent window
|
131 |
+
window.parent.postMessage(message, '*');
|
132 |
+
console.log("post finish");
|
133 |
+
window.cur_process_step = "process";
|
134 |
+
return img;
|
135 |
+
}
|
136 |
+
function uploadImage(image, event, source, value) {
|
137 |
+
// Ensure we're in an iframe
|
138 |
+
if (window.cur_process_step != "process"){
|
139 |
+
return;
|
140 |
+
}
|
141 |
+
window.cur_process_step = "";
|
142 |
+
console.log("uploadImage", image ? image.url : null, event, source, value);
|
143 |
+
// Get the first image from the gallery (assuming it's an array)
|
144 |
+
let imageUrl = image ? image.url : null;
|
145 |
+
if (window.self !== window.top) {
|
146 |
+
// Post the message to the parent window
|
147 |
+
// Prepare the data to send
|
148 |
+
let data = {
|
149 |
+
event: event,
|
150 |
+
source: source,
|
151 |
+
value: imageUrl
|
152 |
+
};
|
153 |
+
window.parent.postMessage(data, '*');
|
154 |
+
} else if (isValidEnv()){
|
155 |
+
try{
|
156 |
+
sendCustomEventToDataLayer({},event,source,{"image":imageUrl})
|
157 |
+
} catch (error) {
|
158 |
+
console.error("Error in sendCustomEventToDataLayer:", error);
|
159 |
+
}
|
160 |
+
}else{
|
161 |
+
console.log("Not in an iframe, can't post to parent");
|
162 |
+
}
|
163 |
+
return;
|
164 |
+
}
|
165 |
+
window.onDemoLoad = function(x){
|
166 |
+
let envInfo = getEnvInfo();
|
167 |
+
console.log(envInfo);
|
168 |
+
if (isValidEnv()){
|
169 |
+
var element = document.getElementById("pitch_desc_html_code");
|
170 |
+
if (element) {
|
171 |
+
element.parentNode.removeChild(element);
|
172 |
+
}
|
173 |
+
}
|
174 |
+
return "";
|
175 |
+
}
|
176 |
+
</script>
|
177 |
+
'''
|
178 |
+
|
179 |
+
with gr.Blocks(head=js, theme="Nymbo/Alyx_Theme") as demo:
|
180 |
+
width=240
|
181 |
+
height=340
|
182 |
|
183 |
with gr.Row(equal_height=False):
|
184 |
+
with gr.Column(min_width=240): # Adjust scale for proper sizing
|
185 |
image_input = gr.Image(type="numpy", label="", height=height)
|
186 |
gr.Examples(examples=examples, inputs=image_input, examples_per_page=10, elem_id="example_img")
|
187 |
process_button = gr.Button("Nude!", size="sm")
|
|
|
190 |
processed_img = inference(img)
|
191 |
return processed_img
|
192 |
|
193 |
+
image_input.change(fn=lambda x: x, inputs=[image_input], outputs=[gr.State([])], js='''(img) => window.uploadImage(img, "process_finished", "demo_hf_deepnude_gan_card", "")''')
|
194 |
+
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")''')
|
195 |
+
demo.load(fn=lambda x: x, inputs=[gr.State([])], outputs=[gr.State([])], js='''(x) => window.onDemoLoad(x)''')
|
196 |
demo.queue(max_size=10)
|
197 |
demo.launch()
|