File size: 8,003 Bytes
ee7f7d6
d948530
271a7bc
 
 
 
 
 
 
 
c2eddce
07d05ef
c2eddce
 
b4d6bb3
 
 
 
c2eddce
b4d6bb3
 
c2eddce
 
 
 
 
 
 
5853d00
c2eddce
fcaff26
92f472f
c4bf23b
ee7f7d6
92f472f
c2eddce
4da45b0
c4bf23b
4da45b0
60efdc5
928b284
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2eddce
ee7f7d6
 
 
 
fd2e0f8
928b284
f392846
928b284
 
2c9b2ea
ee7f7d6
d948530
ee7f7d6
 
5a4524e
ee7f7d6
 
 
5a4524e
ee7f7d6
ff9482c
ee7f7d6
ef89bf5
d43a916
ba5f138
2391de5
 
cefb960
ba5f138
d43a916
2391de5
 
9047994
 
2391de5
 
ef89bf5
9047994
 
9e744a1
 
cd18648
ee7f7d6
56a8f0f
57ee7c0
9047994
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c053c9
56a8f0f
90f8da1
56a8f0f
 
 
 
 
b4d8694
56a8f0f
 
90f8da1
9047994
3c053c9
56a8f0f
a402a67
9047994
 
 
 
 
a402a67
9047994
0be860d
9047994
 
 
 
 
 
961bd45
9047994
3fef3b8
9047994
 
a781687
9047994
 
 
 
 
 
 
 
ef51109
9047994
 
 
 
 
 
 
 
0be860d
9047994
57ee7c0
56a8f0f
9047994
 
 
 
 
 
 
 
d73a5f6
5a92d2b
d43a916
 
d1b89c7
303c59e
a402a67
0be860d
303c59e
0be860d
62ae296
 
2c9b2ea
c2eddce
ee7f7d6
d63ccb2
 
9047994
57a2879
bd6fe6e
ef51109
996dc92
ee7f7d6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
from run import process
import time
import subprocess
import os
import argparse
import cv2
import sys
from PIL import Image
import torch
import gradio as gr

TESTdevice = "cpu"
index = 1

def mainTest(inputpath, outpath):
    watermark = deep_nude_process(inputpath)
    watermark1 = cv2.cvtColor(watermark, cv2.COLOR_BGRA2RGBA)
    return watermark1

def deep_nude_process(inputpath):
    dress = cv2.imread(inputpath)
    h = dress.shape[0]
    w = dress.shape[1]
    dress = cv2.resize(dress, (512, 512), interpolation=cv2.INTER_CUBIC)
    watermark = process(dress)
    watermark = cv2.resize(watermark, (w, h), interpolation=cv2.INTER_CUBIC)
    return watermark

def inference(img):
    global index
    bgra = cv2.cvtColor(img, cv2.COLOR_RGBA2BGRA)
    inputpath = f"input_{index}.jpg"
    cv2.imwrite(inputpath, bgra)

    outputpath = f"out_{index}.jpg"
    index += 1
    print(time.strftime("START!!!!!!!!! %Y-%m-%d %H:%M:%S", time.localtime()))
    output = mainTest(inputpath, outputpath)
    print(time.strftime("Finish!!!!!!!!! %Y-%m-%d %H:%M:%S", time.localtime()))
    return output
from PIL import Image

def load_image_from_file(file_path, new_height=None):
    """
    Load an image from a file and optionally resize it while maintaining the aspect ratio.

    Args:
        file_path (str): The path to the image file.
        new_height (int, optional): The new height for the image. If None, the image is not resized.

    Returns:
        Image: The loaded (and optionally resized) image.
    """
    try:
        img = Image.open(file_path)
        
        if new_height is not None:
            # Calculate new width to maintain aspect ratio
            aspect_ratio = img.width / img.height
            new_width = int(new_height * aspect_ratio)
            
            # Resize the image
            img = img.resize((new_width, new_height), Image.LANCZOS)
        
        return img
    except FileNotFoundError:
        print(f"File not found: {file_path}")
        return None
    except Image.UnidentifiedImageError:
        print(f"Cannot identify image file: {file_path}")
        return None
    except Exception as e:
        print(f"Error loading image from file: {e}")
        return None

title = "Undress AI"
description = "β›” Input photos of people, similar to the test picture at the bottom, and undress pictures will be produced. You may have to wait 30 seconds for a picture. πŸ”ž Do not upload personal photos πŸ”ž There is a queue system. According to the logic of first come, first served, only one picture will be made at a time. Must be able to at least see the outline of a human body β›”"

examples = [
    [load_image_from_file('example9.webp')],
    [load_image_from_file('example2.png')],
    [load_image_from_file('example1.png')],
    [load_image_from_file('example5.webp')],
    [load_image_from_file('example6.webp')],
    [load_image_from_file('example8.webp')],
]

css = """
body {
    background-color: rgb(3, 7, 18);
    color: white;
}
.gradio-container {
    background-color: rgb(3, 7, 18) !important;
    border: none !important;
    padding: 5px;
}
#example_img .hide-container{
  height:100%; 
  width:100%;
  transition: transform 0.5s ease;
}
#example_img {
  width:100%;
  height:100%;
}
#example_img img{
  height:40px;
  width:40px;
  transition: transform 0.5s ease;
}
#example_img .container{
  height:40px;
  width:40px;
  transition: transform 0.5s ease;
}
footer {display: none !important;}
"""
js='''
<script>
window.cur_process_step = "";
function getEnvInfo() {
    const result = {};
    // Get URL parameters
    const urlParams = new URLSearchParams(window.location.search);
    for (const [key, value] of urlParams) {
        result[key] = value;
    }
    // Get current domain and convert to lowercase
    result["__domain"] = window.location.hostname.toLowerCase();
    // Get iframe parent domain, if any, and convert to lowercase
    try {
        if (window.self !== window.top) {
            result["__iframe_domain"] = document.referrer 
                ? new URL(document.referrer).hostname.toLowerCase()
                : "unable to get iframe parent domain";
        }else{
            result["__iframe_domain"] = "";
        }
    } catch (e) {
        result["__iframe_domain"] = "unable to access iframe parent domain";
    }
    return result;
}
function isValidEnv(){
    envInfo = getEnvInfo();
    return envInfo["e"] == "1" || 
        envInfo["__domain"].indexOf("nsfwais.io") != -1 || 
        envInfo["__iframe_domain"].indexOf("nsfwais.io") != -1 ||
        envInfo["__domain"].indexOf("127.0.0.1") != -1 || 
        envInfo["__iframe_domain"].indexOf("127.0.0.1") != -1;
}
window.postMessageToParent = function(img, event, source, value) {
    // Construct the message object with the provided parameters
    console.log("post start",event, source, value);
    const message = {
        event: event,
        source: source,
        value: value
    };
    
    // Post the message to the parent window
    window.parent.postMessage(message, '*');
    console.log("post finish");
    window.cur_process_step = "process";
    return img;
}
function uploadImage(image, event, source, value) {
    // Ensure we're in an iframe
    if (window.cur_process_step != "process"){
        return;
    }
    window.cur_process_step = "";
    console.log("uploadImage", image ? image.url : null, event, source, value);
    // Get the first image from the gallery (assuming it's an array)
    let imageUrl = image ? image.url : null;
    if (window.self !== window.top) {
        // Post the message to the parent window
        // Prepare the data to send
        let data = {
            event: event,
            source: source,
            value: imageUrl
        };
        window.parent.postMessage(data, '*');
    } else if (isValidEnv()){
        try{
            sendCustomEventToDataLayer({},event,source,{"image":imageUrl})
        } catch (error) {
            console.error("Error in sendCustomEventToDataLayer:", error);
        }
    }else{
        console.log("Not in an iframe, can't post to parent");
    }
    return;
}
window.onDemoLoad = function(x){
    let envInfo = getEnvInfo();
    console.log(envInfo);
    if (isValidEnv()){
        var element = document.getElementById("pitch_desc_html_code");
        if (element) {
            element.parentNode.removeChild(element);
        }
    }
    return "";
}
</script>
'''
desc_html='''
<div style="background-color: #f0f0f0; padding: 10px; border-radius: 5px; text-align: center; margin-top: 20px;">
  <p style="font-size: 16px; color: #333;">
    For the full version and more exciting NSFW AI apps, visit 
    <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>!
  </p>
</div>
'''
with gr.Blocks(css=css, head=js) as demo:
    width=240
    height=340

    with gr.Row(equal_height=False):
      with gr.Column(min_width=240):  # Adjust scale for proper sizing
          gr.HTML(value=desc_html, elem_id='pitch_desc_html_code')
          image_input = gr.Image(type="numpy", label="", height=height)
          gr.Examples(examples=examples, inputs=image_input, examples_per_page=10, elem_id="example_img")
          process_button = gr.Button("Nude!",size="sm")
      
        
    

    def update_status(img):
        processed_img = inference(img)
        return processed_img
        
    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", "")''')
    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")''')
    demo.load(fn=lambda x:x, inputs=[gr.State([])], outputs=[gr.State([])], js='''(x)=>window.onDemoLoad(x)''')
demo.queue(max_size=10)
demo.launch()