Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -35,6 +35,115 @@ def softmax(vector):
|
|
35 |
e = exp(vector - vector.max()) # for numerical stability
|
36 |
return e / e.sum()
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
# Function to extract images from zip
|
39 |
def extract_zip(zip_file):
|
40 |
temp_dir = tempfile.mkdtemp() # Temporary directory
|
@@ -91,7 +200,7 @@ def evaluate_model(labels, preds):
|
|
91 |
|
92 |
return accuracy, roc_score, report, fig, fig_roc
|
93 |
|
94 |
-
#
|
95 |
def process_zip(zip_file):
|
96 |
extracted_dir = extract_zip(zip_file.name)
|
97 |
|
@@ -118,34 +227,7 @@ def process_zip(zip_file):
|
|
118 |
results['Model_2_accuracy'], results['Model_2_roc_score'], results['Model_2_report'],
|
119 |
results['Model_2_cm_fig'], results['Model_2_roc_fig'])
|
120 |
|
121 |
-
# Single image
|
122 |
-
def image_classifier0(image):
|
123 |
-
labels = ["AI", "Real"]
|
124 |
-
outputs = pipe0(image)
|
125 |
-
results = {}
|
126 |
-
for idx, result in enumerate(outputs):
|
127 |
-
results[labels[idx]] = float(outputs[idx]['score']) # Convert to float
|
128 |
-
fin_sum.append(results)
|
129 |
-
return results
|
130 |
-
|
131 |
-
def image_classifier1(image):
|
132 |
-
labels = ["AI", "Real"]
|
133 |
-
outputs = pipe1(image)
|
134 |
-
results = {}
|
135 |
-
for idx, result in enumerate(outputs):
|
136 |
-
results[labels[idx]] = float(outputs[idx]['score']) # Convert to float
|
137 |
-
fin_sum.append(results)
|
138 |
-
return results
|
139 |
-
|
140 |
-
def image_classifier2(image):
|
141 |
-
labels = ["AI", "Real"]
|
142 |
-
outputs = pipe2(image)
|
143 |
-
results = {}
|
144 |
-
for idx, result in enumerate(outputs):
|
145 |
-
results[labels[idx]] = float(outputs[idx]['score']) # Convert to float
|
146 |
-
fin_sum.append(results)
|
147 |
-
return results
|
148 |
-
|
149 |
def load_url(url):
|
150 |
try:
|
151 |
urllib.request.urlretrieve(f'{url}', f"{uid}tmp_im.png")
|
@@ -201,29 +283,29 @@ with gr.Blocks() as app:
|
|
201 |
btn.click(fin_clear, None, fin, show_progress=False)
|
202 |
load_btn.click(load_url, in_url, [inp, mes])
|
203 |
|
204 |
-
btn.click(
|
205 |
-
|
206 |
-
|
207 |
tot_prob, None, fin, show_progress=False)
|
208 |
|
209 |
# Tab for batch processing
|
210 |
with gr.Tab("Batch Image Processing"):
|
211 |
zip_file = gr.File(label="Upload Zip (two folders: real, ai)")
|
212 |
-
|
213 |
-
# Outputs for all three models
|
214 |
-
for i in range(3):
|
215 |
-
with gr.Group():
|
216 |
-
gr.Markdown(f"### Results for Model {i}")
|
217 |
-
output_acc = gr.Label(label=f"Model {i} Accuracy")
|
218 |
-
output_roc = gr.Label(label=f"Model {i} ROC Score")
|
219 |
-
output_report = gr.Textbox(label=f"Model {i} Classification Report", lines=10)
|
220 |
-
output_cm = gr.Plot(label=f"Model {i} Confusion Matrix")
|
221 |
-
output_roc_plot = gr.Plot(label=f"Model {i} ROC Curve")
|
222 |
-
|
223 |
batch_btn = gr.Button("Process Batch")
|
224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
# Connect batch processing
|
226 |
batch_btn.click(process_zip, zip_file,
|
227 |
-
[
|
|
|
|
|
228 |
|
229 |
app.launch(show_api=False, max_threads=24)
|
|
|
35 |
e = exp(vector - vector.max()) # for numerical stability
|
36 |
return e / e.sum()
|
37 |
|
38 |
+
# Single image classification functions
|
39 |
+
def image_classifier0(image):
|
40 |
+
labels = ["AI", "Real"]
|
41 |
+
outputs = pipe0(image)
|
42 |
+
results = {}
|
43 |
+
for idx, result in enumerate(outputs):
|
44 |
+
results[labels[idx]] = float(outputs[idx]['score']) # Convert to float
|
45 |
+
fin_sum.append(results)
|
46 |
+
return results
|
47 |
+
|
48 |
+
def image_classifier1(image):
|
49 |
+
labels = ["AI", "Real"]
|
50 |
+
outputs = pipe1(image)
|
51 |
+
results = {}
|
52 |
+
for idx, result in enumerate(outputs):
|
53 |
+
results[labels[idx]] = float(outputs[idx]['score']) # Convert to float
|
54 |
+
fin_sum.append(results)
|
55 |
+
return results
|
56 |
+
|
57 |
+
def image_classifier2(image):
|
58 |
+
labels = ["AI", "Real"]
|
59 |
+
outputs = pipe2(image)
|
60 |
+
results = {}
|
61 |
+
for idx, result in enumerate(outputs):
|
62 |
+
results[labels[idx]] = float(outputs[idx]['score']) # Convert to float
|
63 |
+
fin_sum.append(results)
|
64 |
+
return results
|
65 |
+
|
66 |
+
def aiornot0(image):
|
67 |
+
labels = ["AI", "Real"]
|
68 |
+
mod = models[0]
|
69 |
+
feature_extractor0 = AutoFeatureExtractor.from_pretrained(mod)
|
70 |
+
model0 = AutoModelForImageClassification.from_pretrained(mod)
|
71 |
+
input = feature_extractor0(image, return_tensors="pt")
|
72 |
+
with torch.no_grad():
|
73 |
+
outputs = model0(**input)
|
74 |
+
logits = outputs.logits
|
75 |
+
probability = softmax(logits) # Apply softmax on logits
|
76 |
+
px = pd.DataFrame(probability.numpy())
|
77 |
+
prediction = logits.argmax(-1).item()
|
78 |
+
label = labels[prediction]
|
79 |
+
|
80 |
+
html_out = f"""
|
81 |
+
<h1>This image is likely: {label}</h1><br><h3>
|
82 |
+
Probabilities:<br>
|
83 |
+
Real: {float(px[1][0]):.4f}<br>
|
84 |
+
AI: {float(px[0][0]):.4f}"""
|
85 |
+
|
86 |
+
results = {
|
87 |
+
"Real": float(px[1][0]),
|
88 |
+
"AI": float(px[0][0])
|
89 |
+
}
|
90 |
+
fin_sum.append(results)
|
91 |
+
return gr.HTML.update(html_out), results
|
92 |
+
|
93 |
+
def aiornot1(image):
|
94 |
+
labels = ["AI", "Real"]
|
95 |
+
mod = models[1]
|
96 |
+
feature_extractor1 = AutoFeatureExtractor.from_pretrained(mod)
|
97 |
+
model1 = AutoModelForImageClassification.from_pretrained(mod)
|
98 |
+
input = feature_extractor1(image, return_tensors="pt")
|
99 |
+
with torch.no_grad():
|
100 |
+
outputs = model1(**input)
|
101 |
+
logits = outputs.logits
|
102 |
+
probability = softmax(logits) # Apply softmax on logits
|
103 |
+
px = pd.DataFrame(probability.numpy())
|
104 |
+
prediction = logits.argmax(-1).item()
|
105 |
+
label = labels[prediction]
|
106 |
+
|
107 |
+
html_out = f"""
|
108 |
+
<h1>This image is likely: {label}</h1><br><h3>
|
109 |
+
Probabilities:<br>
|
110 |
+
Real: {float(px[1][0]):.4f}<br>
|
111 |
+
AI: {float(px[0][0]):.4f}"""
|
112 |
+
|
113 |
+
results = {
|
114 |
+
"Real": float(px[1][0]),
|
115 |
+
"AI": float(px[0][0])
|
116 |
+
}
|
117 |
+
fin_sum.append(results)
|
118 |
+
return gr.HTML.update(html_out), results
|
119 |
+
|
120 |
+
def aiornot2(image):
|
121 |
+
labels = ["AI", "Real"]
|
122 |
+
mod = models[2]
|
123 |
+
feature_extractor2 = AutoFeatureExtractor.from_pretrained(mod)
|
124 |
+
model2 = AutoModelForImageClassification.from_pretrained(mod)
|
125 |
+
input = feature_extractor2(image, return_tensors="pt")
|
126 |
+
with torch.no_grad():
|
127 |
+
outputs = model2(**input)
|
128 |
+
logits = outputs.logits
|
129 |
+
probability = softmax(logits) # Apply softmax on logits
|
130 |
+
px = pd.DataFrame(probability.numpy())
|
131 |
+
prediction = logits.argmax(-1).item()
|
132 |
+
label = labels[prediction]
|
133 |
+
|
134 |
+
html_out = f"""
|
135 |
+
<h1>This image is likely: {label}</h1><br><h3>
|
136 |
+
Probabilities:<br>
|
137 |
+
Real: {float(px[1][0]):.4f}<br>
|
138 |
+
AI: {float(px[0][0]):.4f}"""
|
139 |
+
|
140 |
+
results = {
|
141 |
+
"Real": float(px[1][0]),
|
142 |
+
"AI": float(px[0][0])
|
143 |
+
}
|
144 |
+
fin_sum.append(results)
|
145 |
+
return gr.HTML.update(html_out), results
|
146 |
+
|
147 |
# Function to extract images from zip
|
148 |
def extract_zip(zip_file):
|
149 |
temp_dir = tempfile.mkdtemp() # Temporary directory
|
|
|
200 |
|
201 |
return accuracy, roc_score, report, fig, fig_roc
|
202 |
|
203 |
+
# Batch processing for all models
|
204 |
def process_zip(zip_file):
|
205 |
extracted_dir = extract_zip(zip_file.name)
|
206 |
|
|
|
227 |
results['Model_2_accuracy'], results['Model_2_roc_score'], results['Model_2_report'],
|
228 |
results['Model_2_cm_fig'], results['Model_2_roc_fig'])
|
229 |
|
230 |
+
# Single image section
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
def load_url(url):
|
232 |
try:
|
233 |
urllib.request.urlretrieve(f'{url}', f"{uid}tmp_im.png")
|
|
|
283 |
btn.click(fin_clear, None, fin, show_progress=False)
|
284 |
load_btn.click(load_url, in_url, [inp, mes])
|
285 |
|
286 |
+
btn.click(aiornot0, [inp], [outp0, n_out0]).then(
|
287 |
+
aiornot1, [inp], [outp1, n_out1]).then(
|
288 |
+
aiornot2, [inp], [outp2, n_out2]).then(
|
289 |
tot_prob, None, fin, show_progress=False)
|
290 |
|
291 |
# Tab for batch processing
|
292 |
with gr.Tab("Batch Image Processing"):
|
293 |
zip_file = gr.File(label="Upload Zip (two folders: real, ai)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
294 |
batch_btn = gr.Button("Process Batch")
|
295 |
|
296 |
+
for i, model in enumerate(models):
|
297 |
+
with gr.Group():
|
298 |
+
gr.Markdown(f"### Results for {model}")
|
299 |
+
globals()[f'output_acc{i}'] = gr.Label(label=f"Model {i} Accuracy")
|
300 |
+
globals()[f'output_roc{i}'] = gr.Label(label=f"Model {i} ROC Score")
|
301 |
+
globals()[f'output_report{i}'] = gr.Textbox(label=f"Model {i} Classification Report", lines=10)
|
302 |
+
globals()[f'output_cm{i}'] = gr.Plot(label=f"Model {i} Confusion Matrix")
|
303 |
+
globals()[f'output_roc_plot{i}'] = gr.Plot(label=f"Model {i} ROC Curve")
|
304 |
+
|
305 |
# Connect batch processing
|
306 |
batch_btn.click(process_zip, zip_file,
|
307 |
+
[output_acc0, output_roc0, output_report0, output_cm0, output_roc_plot0,
|
308 |
+
output_acc1, output_roc1, output_report1, output_cm1, output_roc_plot1,
|
309 |
+
output_acc2, output_roc2, output_report2, output_cm2, output_roc_plot2])
|
310 |
|
311 |
app.launch(show_api=False, max_threads=24)
|