Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -26,33 +26,40 @@ def softmax(vector):
|
|
26 |
e = exp(vector - vector.max()) # for numerical stability
|
27 |
return e / e.sum()
|
28 |
|
|
|
29 |
def image_classifier0(image):
|
|
|
30 |
labels = ["AI", "Real"]
|
31 |
outputs = pipe0(image)
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
fin_sum.append(results)
|
36 |
return results
|
37 |
|
|
|
38 |
def image_classifier1(image):
|
|
|
39 |
labels = ["AI", "Real"]
|
40 |
outputs = pipe1(image)
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
fin_sum.append(results)
|
45 |
return results
|
46 |
|
|
|
47 |
def image_classifier2(image):
|
|
|
48 |
labels = ["AI", "Real"]
|
49 |
outputs = pipe2(image)
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
fin_sum.append(results)
|
54 |
return results
|
55 |
|
|
|
56 |
def aiornot0(image):
|
57 |
labels = ["AI", "Real"]
|
58 |
mod = models[0]
|
@@ -80,6 +87,7 @@ def aiornot0(image):
|
|
80 |
fin_sum.append(results)
|
81 |
return gr.HTML.update(html_out), results
|
82 |
|
|
|
83 |
def aiornot1(image):
|
84 |
labels = ["AI", "Real"]
|
85 |
mod = models[1]
|
@@ -107,6 +115,7 @@ def aiornot1(image):
|
|
107 |
fin_sum.append(results)
|
108 |
return gr.HTML.update(html_out), results
|
109 |
|
|
|
110 |
def aiornot2(image):
|
111 |
labels = ["AI", "Real"]
|
112 |
mod = models[2]
|
@@ -134,6 +143,7 @@ def aiornot2(image):
|
|
134 |
fin_sum.append(results)
|
135 |
return gr.HTML.update(html_out), results
|
136 |
|
|
|
137 |
def load_url(url):
|
138 |
try:
|
139 |
urllib.request.urlretrieve(f'{url}', f"{uid}tmp_im.png")
|
@@ -144,6 +154,7 @@ def load_url(url):
|
|
144 |
mes = f"Image not Found<br>Error: {e}"
|
145 |
return image, mes
|
146 |
|
|
|
147 |
def tot_prob():
|
148 |
try:
|
149 |
fin_out = sum([result["Real"] for result in fin_sum]) / len(fin_sum)
|
@@ -157,10 +168,12 @@ def tot_prob():
|
|
157 |
print(e)
|
158 |
return None
|
159 |
|
|
|
160 |
def fin_clear():
|
161 |
fin_sum.clear()
|
162 |
return None
|
163 |
|
|
|
164 |
def upd(image):
|
165 |
rand_im = uuid.uuid4()
|
166 |
image.save(f"{rand_im}-vid_tmp_proc.png")
|
@@ -181,7 +194,6 @@ with gr.Blocks() as app:
|
|
181 |
with gr.Row():
|
182 |
fin = gr.Label(label="Final Probability", visible=False)
|
183 |
with gr.Row():
|
184 |
-
# Updated model names
|
185 |
with gr.Box():
|
186 |
lab0 = gr.HTML(f"""<b>Testing on Original Model: <a href='https://huggingface.co/{models[0]}'>{models[0]}</a></b>""")
|
187 |
nun0 = gr.HTML("""""")
|
@@ -205,12 +217,9 @@ with gr.Blocks() as app:
|
|
205 |
btn.click(fin_clear, None, fin, show_progress=False)
|
206 |
load_btn.click(load_url, in_url, [inp, mes])
|
207 |
|
|
|
208 |
btn.click(aiornot0, [inp], [outp0, n_out0]).then(tot_prob, None, fin, show_progress=False)
|
209 |
btn.click(aiornot1, [inp], [outp1, n_out1]).then(tot_prob, None, fin, show_progress=False)
|
210 |
btn.click(aiornot2, [inp], [outp2, n_out2]).then(tot_prob, None, fin, show_progress=False)
|
211 |
|
212 |
-
btn.click(image_classifier0, [inp], [n_out0]).then(tot_prob, None, fin, show_progress=False)
|
213 |
-
btn.click(image_classifier1, [inp], [n_out1]).then(tot_prob, None, fin, show_progress=False)
|
214 |
-
btn.click(image_classifier2, [inp], [n_out2]).then(tot_prob, None, fin, show_progress=False)
|
215 |
-
|
216 |
app.launch(show_api=False, max_threads=24)
|
|
|
26 |
e = exp(vector - vector.max()) # for numerical stability
|
27 |
return e / e.sum()
|
28 |
|
29 |
+
# Image classification function for Model 0
|
30 |
def image_classifier0(image):
|
31 |
+
fin_sum.clear() # Clear previous results
|
32 |
labels = ["AI", "Real"]
|
33 |
outputs = pipe0(image)
|
34 |
+
scores = [output['score'] for output in outputs]
|
35 |
+
soft_scores = softmax(scores) # Ensure consistency with softmax
|
36 |
+
results = {labels[i]: float(soft_scores[i]) for i in range(len(labels))}
|
37 |
fin_sum.append(results)
|
38 |
return results
|
39 |
|
40 |
+
# Image classification function for Model 1
|
41 |
def image_classifier1(image):
|
42 |
+
fin_sum.clear() # Clear previous results
|
43 |
labels = ["AI", "Real"]
|
44 |
outputs = pipe1(image)
|
45 |
+
scores = [output['score'] for output in outputs]
|
46 |
+
soft_scores = softmax(scores) # Ensure consistency with softmax
|
47 |
+
results = {labels[i]: float(soft_scores[i]) for i in range(len(labels))}
|
48 |
fin_sum.append(results)
|
49 |
return results
|
50 |
|
51 |
+
# Image classification function for Model 2
|
52 |
def image_classifier2(image):
|
53 |
+
fin_sum.clear() # Clear previous results
|
54 |
labels = ["AI", "Real"]
|
55 |
outputs = pipe2(image)
|
56 |
+
scores = [output['score'] for output in outputs]
|
57 |
+
soft_scores = softmax(scores) # Ensure consistency with softmax
|
58 |
+
results = {labels[i]: float(soft_scores[i]) for i in range(len(labels))}
|
59 |
fin_sum.append(results)
|
60 |
return results
|
61 |
|
62 |
+
# AI or Not function for Model 0
|
63 |
def aiornot0(image):
|
64 |
labels = ["AI", "Real"]
|
65 |
mod = models[0]
|
|
|
87 |
fin_sum.append(results)
|
88 |
return gr.HTML.update(html_out), results
|
89 |
|
90 |
+
# AI or Not function for Model 1
|
91 |
def aiornot1(image):
|
92 |
labels = ["AI", "Real"]
|
93 |
mod = models[1]
|
|
|
115 |
fin_sum.append(results)
|
116 |
return gr.HTML.update(html_out), results
|
117 |
|
118 |
+
# AI or Not function for Model 2
|
119 |
def aiornot2(image):
|
120 |
labels = ["AI", "Real"]
|
121 |
mod = models[2]
|
|
|
143 |
fin_sum.append(results)
|
144 |
return gr.HTML.update(html_out), results
|
145 |
|
146 |
+
# Load URL and return image
|
147 |
def load_url(url):
|
148 |
try:
|
149 |
urllib.request.urlretrieve(f'{url}', f"{uid}tmp_im.png")
|
|
|
154 |
mes = f"Image not Found<br>Error: {e}"
|
155 |
return image, mes
|
156 |
|
157 |
+
# Calculate final probabilities
|
158 |
def tot_prob():
|
159 |
try:
|
160 |
fin_out = sum([result["Real"] for result in fin_sum]) / len(fin_sum)
|
|
|
168 |
print(e)
|
169 |
return None
|
170 |
|
171 |
+
# Clear the fin_sum list
|
172 |
def fin_clear():
|
173 |
fin_sum.clear()
|
174 |
return None
|
175 |
|
176 |
+
# Update image
|
177 |
def upd(image):
|
178 |
rand_im = uuid.uuid4()
|
179 |
image.save(f"{rand_im}-vid_tmp_proc.png")
|
|
|
194 |
with gr.Row():
|
195 |
fin = gr.Label(label="Final Probability", visible=False)
|
196 |
with gr.Row():
|
|
|
197 |
with gr.Box():
|
198 |
lab0 = gr.HTML(f"""<b>Testing on Original Model: <a href='https://huggingface.co/{models[0]}'>{models[0]}</a></b>""")
|
199 |
nun0 = gr.HTML("""""")
|
|
|
217 |
btn.click(fin_clear, None, fin, show_progress=False)
|
218 |
load_btn.click(load_url, in_url, [inp, mes])
|
219 |
|
220 |
+
# Use either the aiornot functions or image_classifier consistently
|
221 |
btn.click(aiornot0, [inp], [outp0, n_out0]).then(tot_prob, None, fin, show_progress=False)
|
222 |
btn.click(aiornot1, [inp], [outp1, n_out1]).then(tot_prob, None, fin, show_progress=False)
|
223 |
btn.click(aiornot2, [inp], [outp2, n_out2]).then(tot_prob, None, fin, show_progress=False)
|
224 |
|
|
|
|
|
|
|
|
|
225 |
app.launch(show_api=False, max_threads=24)
|