temp-9384289 commited on
Commit
d0864a2
·
0 Parent(s):
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: ModelProblems
3
+ emoji: 🧠
4
+ colorFrom: pink
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 4.28.3
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,400 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # notes https://huggingface.co/spaces/Joeythemonster/Text-To-image-AllModels/blob/main/app.py
2
+ from diffusers import StableDiffusionPipeline
3
+ from diffusers import DiffusionPipeline
4
+ import torch
5
+ import time
6
+ import matplotlib.pyplot as plt
7
+ import tensorflow as tf
8
+ import os
9
+ import sys
10
+ import requests
11
+ from image_similarity_measures.evaluate import evaluation
12
+ from PIL import Image
13
+ from huggingface_hub import from_pretrained_keras
14
+ from math import sqrt, ceil
15
+ import numpy as np
16
+
17
+ modelieo=[
18
+ 'nathanReitinger/MNIST-diffusion',
19
+ 'nathanReitinger/MNIST-diffusion-oneImage',
20
+ 'nathanReitinger/MNIST-GAN',
21
+ 'nathanReitinger/MNIST-GAN-noDropout'
22
+ ]
23
+
24
+ def get_sims(gen_filepath, gen_label, file_path, hunting_time_limit):
25
+ (train_images, train_labels), (_, _) = tf.keras.datasets.mnist.load_data()
26
+ train_images = train_images.reshape(train_images.shape[0], 28, 28, 1).astype('float32')
27
+ train_images = (train_images - 127.5) / 127.5 # Normalize the images to [-1, 1]
28
+
29
+ print("how long to hunt", hunting_time_limit)
30
+ if hunting_time_limit == None:
31
+ hunting_time_limit = 2
32
+
33
+ lowest_score = 10000
34
+ lowest_image = None
35
+ lowest_image_path = ''
36
+
37
+ start = time.time()
38
+
39
+ for i in range(len(train_labels)):
40
+ # print(i)
41
+ if train_labels[i] == gen_label:
42
+
43
+ ###
44
+ # get a real image (of correct number)
45
+ ###
46
+
47
+ # print(i)
48
+ to_check = train_images[i]
49
+ fig = plt.figure(figsize=(1, 1))
50
+ plt.subplot(1, 1, 0+1)
51
+ plt.imshow(to_check, cmap='gray')
52
+ plt.axis('off')
53
+ plt.savefig(file_path + 'real_deal.png')
54
+ plt.close()
55
+
56
+ # baseline = evaluation(org_img_path='results/real_deal.png', pred_img_path='results/real_deal.png', metrics=["rmse", "psnr"])
57
+ # print("---")
58
+
59
+ ###
60
+ # check how close that real training data is to generated number
61
+ ###
62
+ results = evaluation(org_img_path=file_path + 'real_deal.png', pred_img_path=file_path+'generated_image.png', metrics=["rmse", "psnr"])
63
+ if results['rmse'] < lowest_score:
64
+ lowest_score = results['rmse']
65
+ lowest_image = to_check
66
+
67
+ to_save = train_images[i]
68
+ fig = plt.figure(figsize=(1, 1))
69
+ plt.subplot(1, 1, 0+1)
70
+ plt.imshow(to_save, cmap='gray')
71
+ plt.axis('off')
72
+ plt.savefig(file_path + 'keeper.png')
73
+ plt.close()
74
+ lowest_image_path = file_path + 'keeper.png'
75
+
76
+ print(lowest_score, str(round( ((i/len(train_labels)) * 100),2 )) + '%')
77
+ now = time.time()
78
+ if now-start > hunting_time_limit:
79
+ print(str(now-start) + "s")
80
+ return lowest_image_path
81
+
82
+ return lowest_image_path
83
+
84
+
85
+ def digit_recognition(filename):
86
+
87
+ API_URL = "https://api-inference.huggingface.co/models/farleyknight/mnist-digit-classification-2022-09-04"
88
+ special_string = '-h-f-_-RT-U-J-E-M-Pb-GC-c-i-v-sji-bMsQmxuh-x-h-C-W-B-F-W-z-Gv-'
89
+ is_escaped = special_string.replace("-", '')
90
+ bear = "Bearer " + is_escaped
91
+ headers = {"Authorization": bear}
92
+ # get a prediction on what number this is
93
+ def query(filename):
94
+ with open(filename, "rb") as f:
95
+ data = f.read()
96
+ response = requests.post(API_URL, headers=headers, data=data)
97
+ return response.json()
98
+
99
+ # use latest model to generate a new image, return path
100
+ ret = False
101
+ output = None
102
+ while ret == False:
103
+ output = query(filename + 'generated_image.png')
104
+ if 'error' in output:
105
+ time.sleep(10)
106
+ ret = False
107
+ else:
108
+ ret = True
109
+ print(output)
110
+
111
+ low_score_log = ''
112
+ this_label_for_this_image = int(output[0]['label'])
113
+ return {'full': output, 'number': this_label_for_this_image}
114
+
115
+
116
+ def get_other(original_image, hunting_time_limit):
117
+ RANDO = str(time.time())
118
+ file_path = 'tester/' + 'generation' + "/" + RANDO + '/'
119
+ os.makedirs(file_path)
120
+ fig = plt.figure(figsize=(1, 1))
121
+ plt.subplot(1, 1, 0+1)
122
+ plt.imshow(original_image, cmap='gray')
123
+ plt.axis('off')
124
+ plt.savefig(file_path + 'generated_image.png')
125
+ plt.close()
126
+ print('[+] done saving generation')
127
+ print("[-] what digit is this")
128
+ ret = digit_recognition(file_path)
129
+ print(ret['full'])
130
+ print(ret['number'])
131
+ print("[+]", ret['number'])
132
+ print("[-] show some most similar numbers")
133
+ if ret["full"][0]['score'] <= 0.90:
134
+ print("[!] error in image digit recognition, likely to not find a similar score")
135
+ sys.exit()
136
+ gen_filepath = file_path + 'generated_image.png'
137
+ gen_label = ret['number']
138
+ ret_sims = get_sims(gen_filepath, gen_label, file_path, hunting_time_limit)
139
+ print("[+] done sims")
140
+ # get the file-Path
141
+ return (file_path + 'generated_image.png', ret_sims)
142
+
143
+ def generate_and_save_images(model):
144
+ noise_dim = 100
145
+ num_examples_to_generate = 1
146
+ seed = tf.random.normal([num_examples_to_generate, noise_dim])
147
+
148
+ # print(seed)
149
+
150
+ n_samples = 1
151
+ # Notice `training` is set to False.
152
+ # This is so all layers run in inference mode (batchnorm).
153
+ examples = model(seed, training=False)
154
+ examples = examples * 255.0
155
+ size = ceil(sqrt(n_samples))
156
+ digit_images = np.zeros((28*size, 28*size), dtype=float)
157
+ n = 0
158
+ for i in range(size):
159
+ for j in range(size):
160
+ if n == n_samples:
161
+ break
162
+ digit_images[i* 28 : (i+1)*28, j*28 : (j+1)*28] = examples[n, :, :, 0]
163
+ n += 1
164
+ digit_images = (digit_images/127.5) -1
165
+ return digit_images
166
+
167
+ def TextToImage(Prompt,inference_steps, model):
168
+ model_id = model
169
+ if 'GAN' in model_id:
170
+ print("do something else")
171
+ model = from_pretrained_keras(model)
172
+ image = generate_and_save_images(model)
173
+ else:
174
+ pipe = DiffusionPipeline.from_pretrained(model_id)
175
+ the_randomness = int(str(time.time())[-1])
176
+ print('seed', the_randomness)
177
+ image = pipe(generator= torch.manual_seed(the_randomness), num_inference_steps=inference_steps).images[0]
178
+
179
+ # pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
180
+ # pipe = pipe.to("cpu")
181
+
182
+ prompt = Prompt
183
+ print(prompt)
184
+ hunting_time_limit = None
185
+ if prompt.isnumeric():
186
+ hunting_time_limit = abs(int(prompt))
187
+
188
+ original_image, other_images = get_other(image, hunting_time_limit)
189
+ ai_gen = Image.open(open(original_image, 'rb'))
190
+ training_data = Image.open(open(other_images, 'rb'))
191
+ return [ai_gen, training_data]
192
+
193
+
194
+ import gradio as gr
195
+ interface = gr.Interface(fn=TextToImage,
196
+ inputs=[gr.Textbox(show_label=True, label='How many seconds to hunt for copies?',), gr.Slider(1, 1000, label='Inference Steps', value=100, step=1), gr.Dropdown(modelieo)],
197
+ outputs=gr.Gallery(label="Generated image", show_label=True, elem_id="gallery", columns=[2], rows=[1], object_fit="contain", height="auto"),
198
+ # css="#output_image{width: 256px !important; height: 256px !important;}",
199
+ title='Unconditional Image Generation')
200
+
201
+ interface.launch()
202
+
203
+
204
+
205
+
206
+
207
+ # import tensorflow as tf
208
+ # from diffusers import DiffusionPipeline
209
+ # import spaces
210
+ # # import torch
211
+ # import PIL.Image
212
+ # from PIL import Image
213
+ # from torch.autograd import Variable
214
+ # import gradio as gr
215
+ # import gradio.components as grc
216
+ # import numpy as np
217
+ # from huggingface_hub import from_pretrained_keras
218
+ # from image_similarity_measures.evaluate import evaluation
219
+ # import keras
220
+ # import time
221
+ # import requests
222
+ # import matplotlib.pyplot as plt
223
+ # import os
224
+ # from pytorch_msssim import ssim, ms_ssim, SSIM, MS_SSIM
225
+ # from gradio_imageslider import ImageSlider
226
+
227
+ # # os.environ['KMP_DUPLICATE_LIB_OK']='TRUE'
228
+
229
+ # # options = ['Placeholder A', 'Placeholder B', 'Placeholder C']
230
+
231
+
232
+ # # pipeline = DiffusionPipeline.from_pretrained("nathanReitinger/MNIST-diffusion-oneImage")
233
+ # # device = "cuda" if torch.cuda.is_available() else "cpu"
234
+ # # pipeline = pipeline.to(device=device)
235
+
236
+ # # @spaces.GPU
237
+ # # def predict(steps, seed):
238
+ # # print("HI")
239
+ # # generator = torch.manual_seed(seed)
240
+ # # for i in range(1,steps):
241
+ # # yield pipeline(generator=generator, num_inference_steps=i).images[0]
242
+
243
+ # # gr.Interface(
244
+ # # predict,
245
+ # # inputs=[
246
+ # # grc.Slider(0, 1000, label='Inference Steps', value=42, step=1),
247
+ # # grc.Slider(0, 2147483647, label='Seed', value=42, step=1),
248
+ # # ],
249
+ # # outputs=gr.Image(height=28, width=28, type="pil", elem_id="output_image"),
250
+ # # css="#output_image{width: 256px !important; height: 256px !important;}",
251
+ # # title="Model Problems: Infringing on MNIST!",
252
+ # # description="Opening the black box.",
253
+ # # ).queue().launch()
254
+
255
+
256
+ # from diffusers import StableDiffusionPipeline
257
+ # import torch
258
+
259
+
260
+ # modellist=['nathanReitinger/MNIST-diffusion-oneImage',
261
+ # 'nathanReitinger/MNIST-diffusion',
262
+ # # 'nathanReitinger/MNIST-GAN',
263
+ # # 'nathanReitinger/MNIST-GAN-noDropout'
264
+ # ]
265
+
266
+ # # pipeline = DiffusionPipeline.from_pretrained("nathanReitinger/MNIST-diffusion-oneImage")
267
+ # # device = "cuda" if torch.cuda.is_available() else "cpu"
268
+ # # pipeline = pipeline.to(device=device)
269
+
270
+
271
+ # def getModel(model):
272
+ # model_id = model
273
+
274
+ # (train_images, train_labels), (_, _) = tf.keras.datasets.mnist.load_data()
275
+ # RANDO = str(time.time())
276
+ # file_path = 'tester/' + model_id.replace("/", "-") + "/" + RANDO + '/'
277
+ # os.makedirs(file_path)
278
+ # train_images = train_images.reshape(train_images.shape[0], 28, 28, 1).astype('float32')
279
+ # train_images = (train_images - 127.5) / 127.5 # Normalize the images to [-1, 1]
280
+
281
+ # print(model_id)
282
+ # image = None
283
+ # if 'diffusion' in model_id:
284
+ # pipe = DiffusionPipeline.from_pretrained(model_id)
285
+ # pipe = pipe.to("cpu")
286
+ # image = pipe(generator= torch.manual_seed(42), num_inference_steps=1).images[0]
287
+ # else:
288
+ # pipe = DiffusionPipeline.from_pretrained('nathanReitinger/MNIST-diffusion')
289
+ # pipe = pipe.to("cpu")
290
+ # test = from_pretrained_keras('nathanReitinger/MNIST-GAN')
291
+ # image = pipe(generator= torch.manual_seed(42), num_inference_steps=40).images[0]
292
+
293
+ # ########################################### let's save this image for comparison to others
294
+ # fig = plt.figure(figsize=(1, 1))
295
+ # plt.subplot(1, 1, 0+1)
296
+ # plt.imshow(image, cmap='gray')
297
+ # plt.axis('off')
298
+ # plt.savefig(file_path + 'generated_image.png')
299
+ # plt.close()
300
+
301
+ # API_URL = "https://api-inference.huggingface.co/models/farleyknight/mnist-digit-classification-2022-09-04"
302
+
303
+ # # get a prediction on what number this is
304
+ # def query(filename):
305
+ # with open(filename, "rb") as f:
306
+ # data = f.read()
307
+ # response = requests.post(API_URL, data=data)
308
+ # return response.json()
309
+
310
+ # # use latest model to generate a new image, return path
311
+ # ret = False
312
+ # output = None
313
+ # while ret == False:
314
+ # output = query(file_path + 'generated_image.png')
315
+ # if 'error' in output:
316
+ # time.sleep(10)
317
+ # ret = False
318
+ # else:
319
+ # ret = True
320
+ # print(output)
321
+
322
+ # low_score_log = ''
323
+ # this_label_for_this_image = int(output[0]['label'])
324
+ # low_score_log += "this image has been identified as a:" + str(this_label_for_this_image) + "\n" + str(output) + "\n"
325
+ # print("===================")
326
+
327
+ # lowest_score = 10000
328
+ # lowest_image = None
329
+
330
+ # for i in range(len(train_labels)):
331
+ # # print(i)
332
+ # if train_labels[i] == this_label_for_this_image:
333
+
334
+ # ###
335
+ # # get a real image (of correct number)
336
+ # ###
337
+
338
+ # # print(i)
339
+ # to_check = train_images[i]
340
+ # fig = plt.figure(figsize=(1, 1))
341
+ # plt.subplot(1, 1, 0+1)
342
+ # plt.imshow(to_check, cmap='gray')
343
+ # plt.axis('off')
344
+ # plt.savefig(file_path + 'real_deal.png')
345
+ # plt.close()
346
+
347
+ # # baseline = evaluation(org_img_path='results/real_deal.png', pred_img_path='results/real_deal.png', metrics=["rmse", "psnr"])
348
+ # # print("---")
349
+
350
+ # ###
351
+ # # check how close that real training data is to generated number
352
+ # ###
353
+ # results = evaluation(org_img_path=file_path + 'real_deal.png', pred_img_path=file_path+'generated_image.png', metrics=["rmse", "psnr"])
354
+ # if results['rmse'] < lowest_score:
355
+
356
+ # lowest_score = results['rmse']
357
+ # lowest_image = to_check
358
+
359
+ # # image1 = np.array(Image.open(file_path + 'real_deal.png'))
360
+ # # image2 = np.array(Image.open(file_path + 'generated_image.png'))
361
+ # # img1 = torch.from_numpy(image1).float().unsqueeze(0).unsqueeze(0)/255.0
362
+ # # img2 = torch.from_numpy(image2).float().unsqueeze(0).unsqueeze(0)/255.0
363
+ # # img1 = Variable( img1, requires_grad=False)
364
+ # # img2 = Variable( img2, requires_grad=True)
365
+ # # ssim_score = ssim(img1, img2).item()
366
+
367
+ # # # sys.exit()
368
+ # # # l2 = distance.euclidean(image1, image2)
369
+
370
+ # # low_score_log += 'rmse score:' + str(lowest_score) + "\n"
371
+ # # low_score_log += 'ssim score:' + str(ssim_score) + "\n"
372
+ # # low_score_log += 'found when:' + str(round( ((i/len(train_labels)) * 100),2 )) + '%' + "\n"
373
+
374
+ # # low_score_log += "---------\n"
375
+
376
+ # # print(lowest_score, ssim_score, str(round( ((i/len(train_labels)) * 100),2 )) + '%')
377
+
378
+ # # fig = plt.figure(figsize=(1, 1))
379
+ # # plt.subplot(1, 1, 0+1)
380
+ # # plt.imshow(to_check, cmap='gray')
381
+ # # plt.axis('off')
382
+ # # plt.savefig(file_path+str(i) + "--" + str(lowest_score) + '---most_close.png')
383
+ # # plt.close()
384
+
385
+
386
+ # # f = open(file_path + "score_log.txt", "w+")
387
+ # # f.write(low_score_log)
388
+ # # f.close()
389
+
390
+ # print("Done!")
391
+
392
+
393
+ # ############################################ return image that you just generated
394
+ # return [image, lowest_image]
395
+
396
+
397
+ # import gradio as gr
398
+ # output = "image"
399
+ # interface = gr.Interface(fn=getModel, inputs=[gr.Dropdown(modellist)], css="#output_image{width: 256px !important; height: 256px !important;}", outputs=output, title='Model Problems (infringement)') # outputs="image",
400
+ # interface.launch(debug=True)
flagged/log.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Inference Steps,Seed,output,flag,username,timestamp
2
+ 48,42,flagged/output/a7d3e3ed399e14f7629e/image.webp,,,2024-04-28 21:23:26.366305
flagged/output/a7d3e3ed399e14f7629e/image.webp ADDED
requirements.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Automatically generated by https://github.com/damnever/pigar.
2
+
3
+ diffusers==0.27.2
4
+ gradio==4.28.3
5
+ gradio_imageslider==0.0.20
6
+ huggingface-hub==0.22.2
7
+ image-similarity-measures==0.3.6
8
+ keras==2.11.0
9
+ matplotlib==3.8.4
10
+ numpy==1.25.2
11
+ pillow==10.3.0
12
+ pytorch-msssim==1.0.0
13
+ requests==2.31.0
14
+ spaces==0.26.2
15
+ tensorflow==2.11.0
16
+ torch==2.2.2
tester/generation/1714450388.794121/generated_image.png ADDED
tester/generation/1714450388.794121/keeper.png ADDED
tester/generation/1714450388.794121/real_deal.png ADDED