Omnibus commited on
Commit
e2346d7
1 Parent(s): 2671156

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -47
app.py CHANGED
@@ -2,7 +2,8 @@ import gradio as gr
2
  import torch
3
  from transformers import AutoFeatureExtractor, AutoModelForImageClassification, pipeline
4
  from numpy import exp
5
-
 
6
  def softmax(vector):
7
  e = exp(vector)
8
  return e / e.sum()
@@ -10,68 +11,107 @@ def softmax(vector):
10
 
11
  models=[
12
  "Nahrawy/AIorNot",
13
- "arnolfokam/ai-generated-image-detector",
14
  "umm-maybe/AI-image-detector",
15
- ]
16
 
 
 
17
  def aiornot0(image):
18
  labels = ["Real", "AI"]
19
  mod=models[0]
20
- feature_extractor = AutoFeatureExtractor.from_pretrained(mod)
21
- model = AutoModelForImageClassification.from_pretrained(mod)
22
- input = feature_extractor(image, return_tensors="pt")
23
  with torch.no_grad():
24
- outputs = model(**input)
25
- print (outputs)
26
  logits = outputs.logits
27
- print (logits)
28
  probability = softmax(logits)
29
- print(probability)
30
-
31
  prediction = logits.argmax(-1).item()
32
- label = labels[prediction]
33
- return label
 
 
 
 
 
 
 
 
 
 
 
34
  def aiornot1(image):
35
  labels = ["Real", "AI"]
36
  mod=models[1]
37
- feature_extractor = AutoFeatureExtractor.from_pretrained(mod)
38
- model = AutoModelForImageClassification.from_pretrained(mod)
39
- input = feature_extractor(image, return_tensors="pt")
40
  with torch.no_grad():
41
- outputs = model(**input)
42
- print (outputs)
43
- logits = outputs.logits
44
- print (logits)
45
  prediction = logits.argmax(-1).item()
46
- label = labels[prediction]
47
- return label
 
 
 
 
 
 
 
 
 
 
 
48
  def aiornot2(image):
49
- labels = ["Real", "AI"]
50
  mod=models[2]
51
- feature_extractor = AutoFeatureExtractor.from_pretrained(mod)
52
- model = AutoModelForImageClassification.from_pretrained(mod)
53
- input = feature_extractor(image, return_tensors="pt")
54
  with torch.no_grad():
55
- outputs = model(**input)
56
- print (outputs)
57
- logits = outputs.logits
58
- print (logits)
59
  prediction = logits.argmax(-1).item()
60
- label = labels[prediction]
61
- return label
62
- with gr.Blocks() as app:
63
- with gr.Row():
64
- with gr.Column():
65
- inp = gr.Image()
66
- mod_choose=gr.Number(value=0)
67
- btn = gr.Button()
68
-
69
- with gr.Column():
70
- outp0 = gr.Textbox(label=f'{models[0]}')
71
- outp1 = gr.Textbox(label=f'{models[1]}')
72
- outp2 = gr.Textbox(label=f'{models[2]}')
73
- btn.click(aiornot0,[inp],outp0)
74
- btn.click(aiornot1,[inp],outp1)
75
- btn.click(aiornot2,[inp],outp2)
76
 
77
- app.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import torch
3
  from transformers import AutoFeatureExtractor, AutoModelForImageClassification, pipeline
4
  from numpy import exp
5
+ import pandas as pd
6
+
7
  def softmax(vector):
8
  e = exp(vector)
9
  return e / e.sum()
 
11
 
12
  models=[
13
  "Nahrawy/AIorNot",
 
14
  "umm-maybe/AI-image-detector",
15
+ "arnolfokam/ai-generated-image-detector",
16
 
17
+ ]
18
+
19
  def aiornot0(image):
20
  labels = ["Real", "AI"]
21
  mod=models[0]
22
+ feature_extractor0 = AutoFeatureExtractor.from_pretrained(mod)
23
+ model0 = AutoModelForImageClassification.from_pretrained(mod)
24
+ input = feature_extractor0(image, return_tensors="pt")
25
  with torch.no_grad():
26
+ outputs = model0(**input)
 
27
  logits = outputs.logits
 
28
  probability = softmax(logits)
29
+ px = pd.DataFrame(probability.numpy())
 
30
  prediction = logits.argmax(-1).item()
31
+ label = labels[prediction]
32
+ html_out = f"""
33
+ <h1>This image is likely: {label}</h1><br><h3>
34
+ Model used: <a href='https://huggingface.co/{mod}'>{mod}</a><br>
35
+ <br>
36
+ Probabilites:<br>
37
+ Real: {px[0][0]}<br>
38
+ AI: {px[1][0]}"""
39
+ results = {}
40
+ for idx,result in enumerate(px):
41
+ results[labels[idx]] = px[idx][0]
42
+ #results[labels['label']] = result['score']
43
+ return gr.HTML.update(html_out),results
44
  def aiornot1(image):
45
  labels = ["Real", "AI"]
46
  mod=models[1]
47
+ feature_extractor1 = AutoFeatureExtractor.from_pretrained(mod)
48
+ model1 = AutoModelForImageClassification.from_pretrained(mod)
49
+ input = feature_extractor1(image, return_tensors="pt")
50
  with torch.no_grad():
51
+ outputs = model1(**input)
52
+ logits = outputs.logits
53
+ probability = softmax(logits)
54
+ px = pd.DataFrame(probability.numpy())
55
  prediction = logits.argmax(-1).item()
56
+ label = labels[prediction]
57
+ html_out = f"""
58
+ <h1>This image is likely: {label}</h1><br><h3>
59
+ Model used: <a href='https://huggingface.co/{mod}'>{mod}</a><br>
60
+ <br>
61
+ Probabilites:<br>
62
+ Real: {px[0][0]}<br>
63
+ AI: {px[1][0]}"""
64
+ results = {}
65
+ for idx,result in enumerate(px):
66
+ results[labels[idx]] = px[idx][0]
67
+ #results[labels['label']] = result['score']
68
+ return gr.HTML.update(html_out),results
69
  def aiornot2(image):
70
+ labels = ["AI", "Real"]
71
  mod=models[2]
72
+ feature_extractor2 = AutoFeatureExtractor.from_pretrained(mod)
73
+ model2 = AutoModelForImageClassification.from_pretrained(mod)
74
+ input = feature_extractor2(image, return_tensors="pt")
75
  with torch.no_grad():
76
+ outputs = model2(**input)
77
+ logits = outputs.logits
78
+ probability = softmax(logits)
79
+ px = pd.DataFrame(probability.numpy())
80
  prediction = logits.argmax(-1).item()
81
+ label = labels[prediction]
82
+ html_out = f"""
83
+ <h1>This image is likely: {label}</h1><br><h3>
84
+ Model used: <a href='https://huggingface.co/{mod}'>{mod}</a><br>
85
+ <br>
86
+ Probabilites:<br>
87
+ Real: {px[1][0]}<br>
88
+ AI: {px[0][0]}"""
89
+
90
+ results = {}
91
+ for idx,result in enumerate(px):
92
+ results[labels[idx]] = px[idx][0]
93
+ #results[labels['label']] = result['score']
94
+ return gr.HTML.update(html_out),results
 
 
95
 
96
+ with gr.Blocks() as app:
97
+ with gr.Column():
98
+ inp = gr.Pil()
99
+ btn = gr.Button()
100
+ with gr.Group():
101
+ with gr.Row():
102
+ with gr.Box():
103
+ lab0 = gr.HTML(f"""<b>Testing on Model: {models[0]}</b>""")
104
+ outp0 = gr.HTML("""""")
105
+ n_out0=gr.Label(label="Output")
106
+ with gr.Box():
107
+ lab1 = gr.HTML(f"""<b>Testing on Model: {models[1]}</b>""")
108
+ outp1 = gr.HTML("""""")
109
+ n_out1=gr.Label(label="Output")
110
+ with gr.Box():
111
+ lab2 = gr.HTML(f"""<b>Testing on Model: {models[2]}</b>""")
112
+ outp2 = gr.HTML("""""")
113
+ n_out2=gr.Label(label="Output")
114
+ btn.click(aiornot0,[inp],[outp0,n_out0])
115
+ btn.click(aiornot1,[inp],[outp1,n_out1])
116
+ btn.click(aiornot2,[inp],[outp2,n_out2])
117
+ app.launch(enable_queue=False)