Spaces:
Runtime error
Runtime error
feat: add image augmentation tool and update demo interface to include new functionality
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from gradio_client import Client, handle_file
|
| 3 |
-
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
import os
|
| 6 |
import time
|
|
@@ -172,37 +172,37 @@ register_model_with_metadata(
|
|
| 172 |
architecture="VIT", dataset="TBA"
|
| 173 |
)
|
| 174 |
|
| 175 |
-
def postprocess_simple_prediction(result, class_names):
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
def simple_prediction(img):
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
register_model_with_metadata(
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
)
|
| 206 |
def infer(image: Image.Image, model_id: str, confidence_threshold: float = 0.75) -> dict:
|
| 207 |
"""Predict using a specific model.
|
| 208 |
|
|
@@ -453,20 +453,20 @@ detection_model_eval_playground = gr.Interface(
|
|
| 453 |
|
| 454 |
community_forensics_preview = gr.Interface(
|
| 455 |
fn=lambda: gr.load("aiwithoutborders-xyz/OpenSight-Community-Forensics-Preview", src="spaces"),
|
| 456 |
-
inputs=
|
| 457 |
outputs=gr.HTML(), # or gr.Markdown() if it's just text
|
| 458 |
title="Quick and simple prediction by our strongest model.",
|
| 459 |
description="No ensemble, no context, no agents, just a quick and simple prediction by our strongest model.",
|
| 460 |
api_name="quick_predict"
|
| 461 |
)
|
| 462 |
|
| 463 |
-
leaderboard = gr.Interface(
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
)
|
| 470 |
|
| 471 |
# simple_predict_interface = gr.Interface(
|
| 472 |
# fn=simple_prediction,
|
|
@@ -544,6 +544,37 @@ minmax_processing_interface = gr.Interface(
|
|
| 544 |
api_name="tool_minmax_processing"
|
| 545 |
)
|
| 546 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 547 |
demo = gr.TabbedInterface(
|
| 548 |
[
|
| 549 |
detection_model_eval_playground,
|
|
@@ -552,7 +583,8 @@ demo = gr.TabbedInterface(
|
|
| 552 |
bit_plane_interface,
|
| 553 |
ela_interface,
|
| 554 |
gradient_processing_interface,
|
| 555 |
-
minmax_processing_interface
|
|
|
|
| 556 |
],
|
| 557 |
[
|
| 558 |
"Run Ensemble Prediction",
|
|
@@ -561,7 +593,8 @@ demo = gr.TabbedInterface(
|
|
| 561 |
"Bit Plane Values",
|
| 562 |
"Error Level Analysis (ELA)",
|
| 563 |
"Gradient Processing",
|
| 564 |
-
"MinMax Processing"
|
|
|
|
| 565 |
],
|
| 566 |
title="Deepfake Detection & Forensics Tools",
|
| 567 |
theme=None,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from gradio_client import Client, handle_file
|
| 3 |
+
from PIL import Image, ImageFilter
|
| 4 |
import numpy as np
|
| 5 |
import os
|
| 6 |
import time
|
|
|
|
| 172 |
architecture="VIT", dataset="TBA"
|
| 173 |
)
|
| 174 |
|
| 175 |
+
# def postprocess_simple_prediction(result, class_names):
|
| 176 |
+
# scores = {name: 0.0 for name in class_names}
|
| 177 |
+
# fake_prob = result.get("Fake Probability")
|
| 178 |
+
# if fake_prob is not None:
|
| 179 |
+
# # Assume class_names = ["AI", "REAL"]
|
| 180 |
+
# scores["AI"] = float(fake_prob)
|
| 181 |
+
# scores["REAL"] = 1.0 - float(fake_prob)
|
| 182 |
+
# return scores
|
| 183 |
+
|
| 184 |
+
# def simple_prediction(img):
|
| 185 |
+
# client = Client("aiwithoutborders-xyz/OpenSight-Community-Forensics-Preview")
|
| 186 |
+
# client.view_api()
|
| 187 |
+
# print(type(img))
|
| 188 |
+
# result = client.predict(
|
| 189 |
+
# handle_file(img),
|
| 190 |
+
# api_name="simple_predict"
|
| 191 |
+
# )
|
| 192 |
+
# return result
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
# register_model_with_metadata(
|
| 196 |
+
# model_id="simple_prediction",
|
| 197 |
+
# model=simple_prediction,
|
| 198 |
+
# preprocess=None,
|
| 199 |
+
# postprocess=postprocess_simple_prediction,
|
| 200 |
+
# class_names=["AI", "REAL"],
|
| 201 |
+
# display_name="Community Forensics",
|
| 202 |
+
# contributor="Jeongsoo Park",
|
| 203 |
+
# model_path="aiwithoutborders-xyz/CommunityForensics-DeepfakeDet-ViT",
|
| 204 |
+
# architecture="ViT", dataset="GOAT"
|
| 205 |
+
# )
|
| 206 |
def infer(image: Image.Image, model_id: str, confidence_threshold: float = 0.75) -> dict:
|
| 207 |
"""Predict using a specific model.
|
| 208 |
|
|
|
|
| 453 |
|
| 454 |
community_forensics_preview = gr.Interface(
|
| 455 |
fn=lambda: gr.load("aiwithoutborders-xyz/OpenSight-Community-Forensics-Preview", src="spaces"),
|
| 456 |
+
inputs=gr.Image(type="filepath"),
|
| 457 |
outputs=gr.HTML(), # or gr.Markdown() if it's just text
|
| 458 |
title="Quick and simple prediction by our strongest model.",
|
| 459 |
description="No ensemble, no context, no agents, just a quick and simple prediction by our strongest model.",
|
| 460 |
api_name="quick_predict"
|
| 461 |
)
|
| 462 |
|
| 463 |
+
# leaderboard = gr.Interface(
|
| 464 |
+
# fn=lambda: "# AI Generated / Deepfake Detection Models Leaderboard: Soon™",
|
| 465 |
+
# inputs=None,
|
| 466 |
+
# outputs=gr.Markdown(),
|
| 467 |
+
# title="Leaderboard",
|
| 468 |
+
# api_name="leaderboard"
|
| 469 |
+
# )
|
| 470 |
|
| 471 |
# simple_predict_interface = gr.Interface(
|
| 472 |
# fn=simple_prediction,
|
|
|
|
| 544 |
api_name="tool_minmax_processing"
|
| 545 |
)
|
| 546 |
|
| 547 |
+
def augment_image_interface(img, augment_methods, rotate_degrees, noise_level, sharpen_strength):
|
| 548 |
+
if img is None:
|
| 549 |
+
raise gr.Error("No image provided for augmentation. Please upload an image.")
|
| 550 |
+
|
| 551 |
+
# Ensure image is PIL Image and in RGB format
|
| 552 |
+
if not isinstance(img, Image.Image):
|
| 553 |
+
try:
|
| 554 |
+
img = Image.fromarray(img)
|
| 555 |
+
except Exception as e:
|
| 556 |
+
raise gr.Error(f"Could not convert input to PIL Image: {e}")
|
| 557 |
+
if img.mode != 'RGB':
|
| 558 |
+
img = img.convert('RGB')
|
| 559 |
+
|
| 560 |
+
augmented_img, _ = augment_image(img, augment_methods, rotate_degrees, noise_level, sharpen_strength)
|
| 561 |
+
return augmented_img
|
| 562 |
+
|
| 563 |
+
augmentation_tool_interface = gr.Interface(
|
| 564 |
+
fn=augment_image_interface,
|
| 565 |
+
inputs=[
|
| 566 |
+
gr.Image(label="Upload Image to Augment", sources=['upload', 'webcam'], type='pil'),
|
| 567 |
+
gr.CheckboxGroup(["rotate", "add_noise", "sharpen"], label="Augmentation Methods"),
|
| 568 |
+
gr.Slider(0, 360, value=0, step=1, label="Rotate Degrees", visible=True),
|
| 569 |
+
gr.Slider(0, 100, value=0, step=1, label="Noise Level", visible=True),
|
| 570 |
+
gr.Slider(0, 200, value=1, step=1, label="Sharpen Strength", visible=True)
|
| 571 |
+
],
|
| 572 |
+
outputs=gr.Image(label="Augmented Image", type='pil'),
|
| 573 |
+
title="Image Augmentation Tool",
|
| 574 |
+
description="Apply various augmentation techniques to your image.",
|
| 575 |
+
api_name="augment_image"
|
| 576 |
+
)
|
| 577 |
+
|
| 578 |
demo = gr.TabbedInterface(
|
| 579 |
[
|
| 580 |
detection_model_eval_playground,
|
|
|
|
| 583 |
bit_plane_interface,
|
| 584 |
ela_interface,
|
| 585 |
gradient_processing_interface,
|
| 586 |
+
minmax_processing_interface,
|
| 587 |
+
augmentation_tool_interface
|
| 588 |
],
|
| 589 |
[
|
| 590 |
"Run Ensemble Prediction",
|
|
|
|
| 593 |
"Bit Plane Values",
|
| 594 |
"Error Level Analysis (ELA)",
|
| 595 |
"Gradient Processing",
|
| 596 |
+
"MinMax Processing",
|
| 597 |
+
"Image Augmentation"
|
| 598 |
],
|
| 599 |
title="Deepfake Detection & Forensics Tools",
|
| 600 |
theme=None,
|